Browse Source

added LuaFunction object, updated bindings and LuaObject registerFunction, added push_value_to_lua util function

v0.1.x
Brett Langdon 13 years ago
parent
commit
d03d843d0f
9 changed files with 133 additions and 38 deletions
  1. +1
    -0
      binding.gyp
  2. +3
    -1
      example/example.js
  3. +60
    -0
      src/luafunction.cc
  4. +28
    -0
      src/luafunction.h
  5. +20
    -34
      src/luaobject.cc
  6. +4
    -2
      src/luaobject.h
  7. +2
    -0
      src/nodelua.cc
  8. +14
    -0
      src/utils.cc
  9. +1
    -1
      src/utils.h

+ 1
- 0
binding.gyp View File

@ -4,6 +4,7 @@
"target_name": "nodelua",
"sources": [
"src/utils.cc",
"src/luafunction.cc",
"src/luaobject.cc",
"src/nodelua.cc"
],


+ 3
- 1
example/example.js View File

@ -6,9 +6,11 @@ console.dir(nodelua.INFO);
var lua = new nodelua.LuaObject();
lua.registerFunction('test_func', function(a,b){
var func = new nodelua.LuaFunction('test_func', function(a,b){
console.dir(a+b);
});
lua.registerFunction(func);
lua.setGlobal("test", 5);


+ 60
- 0
src/luafunction.cc View File

@ -0,0 +1,60 @@
#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_ = 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) {
}

+ 28
- 0
src/luafunction.h View File

@ -0,0 +1,28 @@
#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::Handle<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

+ 20
- 34
src/luaobject.cc View File

@ -1,14 +1,13 @@
#define BUILDING_NODELUA
#include <node.h>
#include "luaobject.h"
#include "utils.h"
using namespace v8;
LuaObject::LuaObject() {};
LuaObject::~LuaObject() {};
Local<Object> LuaObject::functions = Object::New();
Persistent<Object> functions = Persistent<Object>(Object::New());
void LuaObject::Init(Handle<Object> target) {
// Prepare constructor template
@ -41,10 +40,9 @@ Handle<Value> LuaObject::New(const Arguments& args) {
HandleScope scope;
LuaObject* obj = new LuaObject();
obj->functions = Object::New();
obj->lua_ = lua_open();
luaL_openlibs(obj->lua_);
lua_register(obj->lua_, "nodelua", obj->LuaFunction);
lua_register(obj->lua_, "nodelua", LuaObject::CallFunction);
obj->Wrap(args.This());
return args.This();
@ -160,20 +158,7 @@ Handle<Value> LuaObject::SetGlobal(const Arguments& args) {
LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This());
if(args[1]->IsString()){
String::AsciiValue value(args[1]);
char *value_str = (char *) malloc(value.length() + 1);
strcpy(value_str, *value);
lua_pushstring(obj->lua_, value_str);
}else if(args[1]->IsNumber()){
int value = args[1]->ToNumber()->Value();
lua_pushinteger(obj->lua_, value);
}else if(args[1]->IsBoolean()){
int value = (int)args[1]->ToBoolean()->Value();
lua_pushboolean(obj->lua_, value);
}else{
lua_pushnil(obj->lua_);
}
push_value_to_lua(obj->lua_, args[1]);
lua_setglobal(obj->lua_, global_name);
return scope.Close(Undefined());
@ -183,28 +168,25 @@ Handle<Value> LuaObject::SetGlobal(const Arguments& args) {
Handle<Value> LuaObject::RegisterFunction(const Arguments& args){
HandleScope scope;
if(args.Length() < 2){
ThrowException(Exception::TypeError(String::New("Must Have 2 Arguments")));
if(args.Length() < 1){
ThrowException(Exception::TypeError(String::New("Must Have 1 Argument")));
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")));
if(!args[0]->IsObject()){
ThrowException(Exception::TypeError(String::New("Argument 1 Must Be An Object")));
return scope.Close(Undefined());
}
LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This());
obj->functions->Set(args[0]->ToString(), args[1]);
Handle<Object> handle = Handle<Object>::Cast(args[0]);
class LuaFunction* func = LuaFunction::unwrap(handle);
functions->Set(String::New(func->func_name),
func->func_def_);
return scope.Close(Undefined());
}
int LuaObject::LuaFunction(lua_State *L){
int LuaObject::CallFunction(lua_State *L){
int n = lua_gettop(L);
if(n < 1){
lua_pushstring(L, "must have at least 1 argument");
@ -225,8 +207,12 @@ int LuaObject::LuaFunction(lua_State *L){
argv[i-1] = lua_to_value(L, i+1);
}
Local<Function> func = Local<Function>::Cast(LuaObject::functions->Get(func_name));
func->Call(Context::GetCurrent()->Global(), argc, argv);
return 0;
Handle<Value> ret_val = Undefined();
if(functions->Has(func_name)){
Local<Function> func = Local<Function>::Cast(functions->Get(func_name));
ret_val = func->Call(Context::GetCurrent()->Global(), argc, argv);
}
push_value_to_lua(L, ret_val);
return 1;
}

+ 4
- 2
src/luaobject.h View File

@ -3,6 +3,9 @@
#include <node.h>
#include "utils.h"
#include "luafunction.h"
extern "C"{
#include <lua.h>
#include <lauxlib.h>
@ -12,8 +15,7 @@ extern "C"{
class LuaObject : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> target);
static int LuaFunction(lua_State *L);
static v8::Local<v8::Object> functions;
static int CallFunction(lua_State *L);
private:
LuaObject();


+ 2
- 0
src/nodelua.cc View File

@ -2,6 +2,7 @@
#include <v8.h>
#include "luaobject.h"
#include "luafunction.h"
extern "C"{
#include <lua.h>
@ -43,6 +44,7 @@ void init_gc_constants(Handle<Object> target){
void init(Handle<Object> target) {
LuaObject::Init(target);
LuaFunction::Init(target);
init_gc_constants(target);
init_status_constants(target);
init_info_constants(target);


+ 14
- 0
src/utils.cc View File

@ -29,3 +29,17 @@ v8::Local<v8::Value> lua_to_value(lua_State* L, int i){
break;
}
}
void push_value_to_lua(lua_State* L, v8::Handle<v8::Value> value){
if(value->IsString()){
lua_pushstring(L, get_str(v8::Local<v8::Value>::New(value)));
}else if(value->IsNumber()){
int i_value = value->ToNumber()->Value();
lua_pushinteger(L, i_value);
}else if(value->IsBoolean()){
int b_value = (int)value->ToBoolean()->Value();
lua_pushboolean(L, b_value);
}else{
lua_pushnil(L);
}
}

+ 1
- 1
src/utils.h View File

@ -11,5 +11,5 @@ extern "C"{
char * get_str(v8::Local<v8::Value> val);
v8::Local<v8::Value> lua_to_value(lua_State* L, int);
void push_value_to_lua(lua_State* L, v8::Handle<v8::Value> value);
#endif

Loading…
Cancel
Save