Browse Source

finished porting LuaObject to LuaState

v0.2.x
Brett Langdon 13 years ago
parent
commit
1ce4878732
3 changed files with 98 additions and 2 deletions
  1. +90
    -2
      src/luastate.cc
  2. +5
    -0
      src/luastate.h
  3. +3
    -0
      src/nodelua.cc

+ 90
- 2
src/luastate.cc View File

@ -156,10 +156,20 @@ void LuaState::Init(Handle<Object> 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<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("LuaState"), constructor);
}
@ -308,7 +318,7 @@ Handle<Value> 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<Value> LuaState::RegisterFunction(const Arguments& args){
return scope.Close(Undefined());
}
Handle<Value> 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<LuaState>(args.This());
push_value_to_lua(obj->lua_, args[0]);
return scope.Close(Undefined());
}
Handle<Value> 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<LuaState>(args.This());
lua_pop(obj->lua_, pop_n);
return scope.Close(Undefined());
}
Handle<Value> LuaState::GetTop(const Arguments& args) {
HandleScope scope;
LuaState* obj = ObjectWrap::Unwrap<LuaState>(args.This());
int n = lua_gettop(obj->lua_);
return scope.Close(Number::New(n));
}
Handle<Value> 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<LuaState>(args.This());
lua_settop(obj->lua_, set_n);
return scope.Close(Undefined());
}
Handle<Value> 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<LuaState>(args.This());
lua_replace(obj->lua_, index);
return scope.Close(Undefined());
}

+ 5
- 0
src/luastate.h View File

@ -46,5 +46,10 @@ class LuaState : public node::ObjectWrap{
static v8::Handle<v8::Value> RegisterFunction(const v8::Arguments& args);
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

+ 3
- 0
src/nodelua.cc View File

@ -18,6 +18,7 @@ void init_info_constants(Handle<Object> target){
target->Set(String::NewSymbol("INFO"), constants);
}
void init_status_constants(Handle<Object> target){
Local<Object> constants = Object::New();
constants->Set(String::NewSymbol("YIELD"), Number::New(LUA_YIELD));
@ -28,6 +29,7 @@ void init_status_constants(Handle<Object> target){
target->Set(String::NewSymbol("STATUS"), constants);
}
void init_gc_constants(Handle<Object> target){
Local<Object> constants = Object::New();
constants->Set(String::NewSymbol("STOP"), Number::New(LUA_GCSTOP));
@ -41,6 +43,7 @@ void init_gc_constants(Handle<Object> target){
target->Set(String::NewSymbol("GC"), constants);
}
void init(Handle<Object> target) {
LuaState::Init(target);
init_gc_constants(target);


Loading…
Cancel
Save