commit d1e2fd3c620381238e31b4efb412e4be062fc3db Author: Brett Langdon Date: Sat Nov 24 15:57:01 2012 -0500 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9af98f1 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +NodeLua +====== + +NodeLua is a module to expose Lua bindings to Node.JS. + +This is still a work in progress, collaborators welcome. + +## Install +```bash +npm install nodelua +``` + +## Example +See test/test.js + +```javascript +var nodelua = require('nodelua'); +var lua = nodelua.LuaObject(); + +lua.doFile('some_file.lua'); +console.dir(lua.getGlobal('some_var')); +``` + +## License +The MIT License (MIT) +Copyright (c) 2012 Brett Langdon + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..095a405 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,15 @@ +{ + "targets": [ + { + "target_name": "nodelua", + "sources": [ + "src/luaobject.cc", + "src/nodelua.cc" + ], + "libraries": [ + "-llua", + "-ldl" + ] + } + ] +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..b0b63d7 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,9 @@ +var nodelua = null; + +try{ + nodelua = require('../build/Release/nodelua'); +}catch(e){ + nodelua = require('../build/Debug/nodelua'); +} + +module.exports = nodelua; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..fe6c90a --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "nodelua", + "version": "0.1.0", + "description": "Lua Bindings For Node.JS", + "keywords": [ + "lua" + ], + "main": "lib/index.js", + "scripts": { + "test": "test/test.js", + "install": "node-gyp rebuild" + }, + "homepage": "http://github.com/brettlangdon/nodelua.git", + "repository": { + "type": "git", + "url": "git://github.com/brettlangdon/nodelua.git" + }, + "author": "Brett Langdon (http://www.brett.is)", + "license": "MIT", + "gypfile": true, + "engines": "*" +} diff --git a/src/luaobject.cc b/src/luaobject.cc new file mode 100644 index 0000000..c138fd6 --- /dev/null +++ b/src/luaobject.cc @@ -0,0 +1,139 @@ +#define BUILDING_NODELUA +#include +#include "luaobject.h" + +using namespace v8; + +LuaObject::LuaObject() {}; +LuaObject::~LuaObject() {}; + +void LuaObject::Init(Handle target) { + // Prepare constructor template + Local tpl = FunctionTemplate::New(New); + tpl->SetClassName(String::NewSymbol("LuaObject")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + // Prototype + tpl->PrototypeTemplate()->Set(String::NewSymbol("doFile"), + FunctionTemplate::New(DoFile)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getGlobal"), + FunctionTemplate::New(GetGlobal)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("setGlobal"), + FunctionTemplate::New(SetGlobal)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("close"), + FunctionTemplate::New(Close)->GetFunction()); + + Persistent constructor = Persistent::New(tpl->GetFunction()); + target->Set(String::NewSymbol("LuaObject"), constructor); +} + +Handle LuaObject::New(const Arguments& args) { + HandleScope scope; + + LuaObject* obj = new LuaObject(); + obj->lua_ = lua_open(); + luaL_openlibs(obj->lua_); + obj->Wrap(args.This()); + + return args.This(); +} + +Handle LuaObject::Close(const Arguments& args){ + HandleScope scope; + LuaObject* obj = ObjectWrap::Unwrap(args.This()); + lua_close(obj->lua_); + return scope.Close(Undefined()); +} + +Handle LuaObject::DoFile(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1){ + ThrowException(Exception::TypeError(String::New("Wrong number of arguments"))); + return scope.Close(Undefined()); + } + + if (!args[0]->IsString()) { + ThrowException(Exception::TypeError(String::New("Argument 1 Must Be A String"))); + return scope.Close(Undefined()); + } + + v8::String::AsciiValue arg_name(args[0]); + char *file_name = (char *) malloc(arg_name.length() + 1); + strcpy(file_name, *arg_name); + + LuaObject* obj = ObjectWrap::Unwrap(args.This()); + if(luaL_dofile(obj->lua_, file_name)){ + char buf[1000]; + sprintf(buf, "Execution Of File %s Has Failed:\n%s\n", file_name, lua_tostring(obj->lua_, -1)); + ThrowException(Exception::TypeError(String::New(buf))); + return scope.Close(Undefined()); + } + + return scope.Close(Undefined()); +} + + +Handle LuaObject::GetGlobal(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 1){ + ThrowException(Exception::TypeError(String::New("Wrong number of arguments"))); + return scope.Close(Undefined()); + } + + if (!args[0]->IsString()) { + ThrowException(Exception::TypeError(String::New("Argument 1 Must Be A String"))); + return scope.Close(Undefined()); + } + + v8::String::AsciiValue arg_name(args[0]); + char *global_name = (char *) malloc(arg_name.length() + 1); + strcpy(global_name, *arg_name); + + LuaObject* obj = ObjectWrap::Unwrap(args.This()); + lua_getglobal(obj->lua_, global_name); + + if(lua_isnumber(obj->lua_, -1)){ + return scope.Close(Number::New((int)lua_tonumber(obj->lua_, -1))); + }else if(lua_isstring(obj->lua_, -1)){ + return scope.Close(String::New((char *)lua_tostring(obj->lua_, -1))); + } + + return scope.Close(Undefined()); +} + + +Handle LuaObject::SetGlobal(const Arguments& args) { + HandleScope scope; + + if(args.Length() < 2){ + ThrowException(Exception::TypeError(String::New("Wrong number of arguments"))); + return scope.Close(Undefined()); + } + + if (!args[0]->IsString()) { + ThrowException(Exception::TypeError(String::New("Argument 1 Must Be A String"))); + return scope.Close(Undefined()); + } + + v8::String::AsciiValue arg_name(args[0]); + char *global_name = (char *) malloc(arg_name.length() + 1); + strcpy(global_name, *arg_name); + + LuaObject* obj = ObjectWrap::Unwrap(args.This()); + + if(args[1]->IsString()){ + v8::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{ + lua_pushnil(obj->lua_); + } + lua_setglobal(obj->lua_, global_name); + + return scope.Close(Undefined()); +} diff --git a/src/luaobject.h b/src/luaobject.h new file mode 100644 index 0000000..d544f4f --- /dev/null +++ b/src/luaobject.h @@ -0,0 +1,28 @@ +#ifndef LUAOBJECT_H +#define LUAOBJECT_H + +#include + +extern "C"{ +#include +#include +#include +} + +class LuaObject : public node::ObjectWrap { + public: + static void Init(v8::Handle target); + + private: + LuaObject(); + ~LuaObject(); + + static v8::Handle New(const v8::Arguments& args); + static v8::Handle DoFile(const v8::Arguments& args); + static v8::Handle GetGlobal(const v8::Arguments& args); + static v8::Handle SetGlobal(const v8::Arguments& args); + static v8::Handle Close(const v8::Arguments& args); + lua_State *lua_; +}; + +#endif diff --git a/src/nodelua.cc b/src/nodelua.cc new file mode 100644 index 0000000..ed8f1ad --- /dev/null +++ b/src/nodelua.cc @@ -0,0 +1,12 @@ +#include +#include + +#include "luaobject.h" + +using namespace v8; + + +void init(Handle target) { + LuaObject::Init(target); +} +NODE_MODULE(nodelua, init) diff --git a/test/test.js b/test/test.js new file mode 100755 index 0000000..2b02e2c --- /dev/null +++ b/test/test.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node + var path = require('path'); +var nodelua = require('../'); + +var lua = new nodelua.LuaObject(); + +var test_file = path.resolve(__dirname, 'test.lua'); +lua.doFile(test_file); + +lua.setGlobal("test", "value"); +lua.setGlobal("one", 1); +lua.setGlobal("two", "two"); +lua.setGlobal("none", null); + +console.dir(lua.getGlobal("one")); +console.dir(lua.getGlobal("test")); +console.dir(lua.getGlobal("none")); +console.dir(lua.getGlobal("two")); + +lua.close(); \ No newline at end of file diff --git a/test/test.lua b/test/test.lua new file mode 100644 index 0000000..21bbf5a --- /dev/null +++ b/test/test.lua @@ -0,0 +1 @@ +print "Hello, Lua" \ No newline at end of file