| @ -1,60 +0,0 @@ | |||
| #define BUILDING_NODELUA | |||
| #include <node.h> | |||
| #include "luafunction.h" | |||
| #include "utils.h" | |||
| using namespace v8; | |||
| LuaFunction::LuaFunction() {}; | |||
| LuaFunction::~LuaFunction() {}; | |||
| void LuaFunction::Init(Handle<Object> target) { | |||
| // Prepare constructor template | |||
| Local<FunctionTemplate> tpl = FunctionTemplate::New(New); | |||
| tpl->SetClassName(String::NewSymbol("LuaFunction")); | |||
| tpl->InstanceTemplate()->SetInternalFieldCount(2); | |||
| tpl->InstanceTemplate()->SetAccessor(String::New("name"), GetName, SetName); | |||
| Persistent<Function> constructor = Persistent<Function>::New(tpl->GetFunction()); | |||
| target->Set(String::NewSymbol("LuaFunction"), constructor); | |||
| } | |||
| LuaFunction* LuaFunction::unwrap(Handle<Object> handle){ | |||
| return ObjectWrap::Unwrap<LuaFunction>(handle); | |||
| } | |||
| Handle<Value> LuaFunction::New(const Arguments& args) { | |||
| HandleScope scope; | |||
| if(args.Length() < 2){ | |||
| ThrowException(Exception::TypeError(String::New("Must have 2 arguments"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| if(!args[0]->IsString()){ | |||
| ThrowException(Exception::TypeError(String::New("Argument 1 must be a string"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| if(!args[1]->IsFunction()){ | |||
| ThrowException(Exception::TypeError(String::New("Argument 2 must be a function"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| LuaFunction* obj = new LuaFunction(); | |||
| obj->func_name = get_str(args[0]); | |||
| obj->func_def_ = Persistent<Function>::New(Handle<Function>::Cast(args[1])); | |||
| obj->Wrap(args.This()); | |||
| return args.This(); | |||
| } | |||
| Handle<Value> LuaFunction::GetName(Local<String> property, const AccessorInfo &info) { | |||
| HandleScope scope; | |||
| LuaFunction* obj = ObjectWrap::Unwrap<LuaFunction>(info.This()); | |||
| return scope.Close(String::New(obj->func_name)); | |||
| } | |||
| void LuaFunction::SetName(Local<String> property, Local<Value> value, const AccessorInfo& info) { | |||
| } | |||
| @ -1,28 +0,0 @@ | |||
| #ifndef LUAFUNCTION_H | |||
| #define LUAFUNCTION_H | |||
| #include <node.h> | |||
| extern "C"{ | |||
| #include <lua.h> | |||
| #include <lauxlib.h> | |||
| #include <lualib.h> | |||
| } | |||
| class LuaFunction : public node::ObjectWrap { | |||
| public: | |||
| static void Init(v8::Handle<v8::Object> target); | |||
| static LuaFunction* unwrap(v8::Handle<v8::Object> handle); | |||
| char *func_name; | |||
| v8::Persistent<v8::Function> func_def_; | |||
| private: | |||
| LuaFunction(); | |||
| ~LuaFunction(); | |||
| static v8::Handle<v8::Value> New(const v8::Arguments& args); | |||
| static v8::Handle<v8::Value> GetName(v8::Local<v8::String> property, const v8::AccessorInfo &info); | |||
| static void SetName(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::AccessorInfo& info); | |||
| }; | |||
| #endif | |||
| @ -1,91 +0,0 @@ | |||
| #define BUILDING_NODELUA | |||
| #include <node.h> | |||
| #include <map> | |||
| #include "luaobject.h" | |||
| using namespace v8; | |||
| void LuaObject::Init(Handle<Object> target) { | |||
| tpl->PrototypeTemplate()->Set(String::NewSymbol("push"), | |||
| FunctionTemplate::New(Push)->GetFunction()); | |||
| tpl->PrototypeTemplate()->Set(String::NewSymbol("pop"), | |||
| FunctionTemplate::New(Pop)->GetFunction()); | |||
| tpl->PrototypeTemplate()->Set(String::NewSymbol("getTop"), | |||
| FunctionTemplate::New(GetTop)->GetFunction()); | |||
| tpl->PrototypeTemplate()->Set(String::NewSymbol("setTop"), | |||
| FunctionTemplate::New(SetTop)->GetFunction()); | |||
| tpl->PrototypeTemplate()->Set(String::NewSymbol("replace"), | |||
| FunctionTemplate::New(Replace)->GetFunction()); | |||
| } | |||
| Handle<Value> LuaObject::Push(const Arguments& args) { | |||
| HandleScope scope; | |||
| if(args.Length() < 1){ | |||
| ThrowException(Exception::TypeError(String::New("Wrong number of arguments"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This()); | |||
| push_value_to_lua(obj->lua_, args[0]); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| Handle<Value> LuaObject::Pop(const Arguments& args) { | |||
| HandleScope scope; | |||
| int pop_n = 1; | |||
| if(args.Length() > 0 && args[0]->IsNumber()){ | |||
| pop_n = (int)args[0]->ToNumber()->Value(); | |||
| } | |||
| LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This()); | |||
| lua_pop(obj->lua_, pop_n); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| Handle<Value> LuaObject::GetTop(const Arguments& args) { | |||
| HandleScope scope; | |||
| LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This()); | |||
| int n = lua_gettop(obj->lua_); | |||
| return scope.Close(Number::New(n)); | |||
| } | |||
| Handle<Value> LuaObject::SetTop(const Arguments& args) { | |||
| HandleScope scope; | |||
| int set_n = 0; | |||
| if(args.Length() > 0 && args[0]->IsNumber()){ | |||
| set_n = (int)args[0]->ToNumber()->Value(); | |||
| } | |||
| LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This()); | |||
| lua_settop(obj->lua_, set_n); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| Handle<Value> LuaObject::Replace(const Arguments& args) { | |||
| HandleScope scope; | |||
| if(args.Length() < 1){ | |||
| ThrowException(Exception::TypeError(String::New("Must Have 1 Argument"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| if(!args[0]->IsNumber()){ | |||
| ThrowException(Exception::TypeError(String::New("Argument 1 Must Be A Number"))); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| int index = (int)args[0]->ToNumber()->Value(); | |||
| LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This()); | |||
| lua_replace(obj->lua_, index); | |||
| return scope.Close(Undefined()); | |||
| } | |||
| @ -1,23 +0,0 @@ | |||
| #ifndef LUAOBJECT_H | |||
| #define LUAOBJECT_H | |||
| #include <node.h> | |||
| #include "utils.h" | |||
| #include "luafunction.h" | |||
| extern "C"{ | |||
| #include <lua.h> | |||
| #include <lauxlib.h> | |||
| #include <lualib.h> | |||
| } | |||
| class LuaObject : public node::ObjectWrap { | |||
| static v8::Handle<v8::Value> Push(const v8::Arguments& args); | |||
| static v8::Handle<v8::Value> Pop(const v8::Arguments& args); | |||
| static v8::Handle<v8::Value> GetTop(const v8::Arguments& args); | |||
| static v8::Handle<v8::Value> SetTop(const v8::Arguments& args); | |||
| static v8::Handle<v8::Value> Replace(const v8::Arguments& args); | |||
| }; | |||
| #endif | |||