diff --git a/src/luastate.cc b/src/luastate.cc index 7a0b0f9..591733c 100644 --- a/src/luastate.cc +++ b/src/luastate.cc @@ -156,10 +156,20 @@ void LuaState::Init(Handle target){ tpl->PrototypeTemplate()->Set(String::NewSymbol("getName"), FunctionTemplate::New(GetName)->GetFunction()); - tpl->PrototypeTemplate()->Set(String::NewSymbol("registerFunction"), FunctionTemplate::New(RegisterFunction)->GetFunction()); + 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()); + Persistent constructor = Persistent::New(tpl->GetFunction()); target->Set(String::NewSymbol("LuaState"), constructor); } @@ -308,7 +318,7 @@ Handle LuaState::DoStringSync(const Arguments& args) { if(luaL_dostring(obj->lua_, lua_code)){ char buf[1000]; sprintf(buf, "Execution Of Lua Code Has Failed:\n%s\n", lua_tostring(obj->lua_, -1)); - ThrowException(Exception::TypeError(String::New(buf))); + ThrowException(Exception::Error(String::New(buf))); return scope.Close(Undefined()); } @@ -538,3 +548,81 @@ Handle LuaState::RegisterFunction(const Arguments& args){ return scope.Close(Undefined()); } + + +Handle LuaState::Push(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1){ + ThrowException(Exception::TypeError(String::New("LuaState.push Requires 1 Argument"))); + return scope.Close(Undefined()); + } + + LuaState* obj = ObjectWrap::Unwrap(args.This()); + + push_value_to_lua(obj->lua_, args[0]); + + return scope.Close(Undefined()); +} + + +Handle LuaState::Pop(const Arguments& args) { + HandleScope scope; + + int pop_n = 1; + if(args.Length() > 0 && args[0]->IsNumber()){ + pop_n = (int)args[0]->ToNumber()->Value(); + } + + LuaState* obj = ObjectWrap::Unwrap(args.This()); + lua_pop(obj->lua_, pop_n); + + return scope.Close(Undefined()); +} + + +Handle LuaState::GetTop(const Arguments& args) { + HandleScope scope; + + LuaState* obj = ObjectWrap::Unwrap(args.This()); + int n = lua_gettop(obj->lua_); + + return scope.Close(Number::New(n)); +} + + +Handle LuaState::SetTop(const Arguments& args) { + HandleScope scope; + + int set_n = 0; + if(args.Length() > 0 && args[0]->IsNumber()){ + set_n = (int)args[0]->ToNumber()->Value(); + } + + LuaState* obj = ObjectWrap::Unwrap(args.This()); + lua_settop(obj->lua_, set_n); + + return scope.Close(Undefined()); +} + + +Handle LuaState::Replace(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1){ + ThrowException(Exception::TypeError(String::New("LuaState.replace Requires 1 Argument"))); + return scope.Close(Undefined()); + } + + if(!args[0]->IsNumber()){ + ThrowException(Exception::TypeError(String::New("LuaState.replace Argument 1 Must Be A Number"))); + return scope.Close(Undefined()); + } + + int index = (int)args[0]->ToNumber()->Value(); + + LuaState* obj = ObjectWrap::Unwrap(args.This()); + lua_replace(obj->lua_, index); + + return scope.Close(Undefined()); +} diff --git a/src/luastate.h b/src/luastate.h index 255a8e5..2f8597d 100644 --- a/src/luastate.h +++ b/src/luastate.h @@ -46,5 +46,10 @@ class LuaState : public node::ObjectWrap{ static v8::Handle RegisterFunction(const v8::Arguments& args); + static v8::Handle Push(const v8::Arguments& args); + static v8::Handle Pop(const v8::Arguments& args); + static v8::Handle GetTop(const v8::Arguments& args); + static v8::Handle SetTop(const v8::Arguments& args); + static v8::Handle Replace(const v8::Arguments& args); }; #endif diff --git a/src/nodelua.cc b/src/nodelua.cc index f8ac4d2..716d24c 100644 --- a/src/nodelua.cc +++ b/src/nodelua.cc @@ -18,6 +18,7 @@ void init_info_constants(Handle target){ target->Set(String::NewSymbol("INFO"), constants); } + void init_status_constants(Handle target){ Local constants = Object::New(); constants->Set(String::NewSymbol("YIELD"), Number::New(LUA_YIELD)); @@ -28,6 +29,7 @@ void init_status_constants(Handle target){ target->Set(String::NewSymbol("STATUS"), constants); } + void init_gc_constants(Handle target){ Local constants = Object::New(); constants->Set(String::NewSymbol("STOP"), Number::New(LUA_GCSTOP)); @@ -41,6 +43,7 @@ void init_gc_constants(Handle target){ target->Set(String::NewSymbol("GC"), constants); } + void init(Handle target) { LuaState::Init(target); init_gc_constants(target);