Browse Source

initial commit

v0.1.x
Brett Langdon 13 years ago
commit
d1e2fd3c62
10 changed files with 279 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +32
    -0
      README.md
  3. +15
    -0
      binding.gyp
  4. +9
    -0
      lib/index.js
  5. +22
    -0
      package.json
  6. +139
    -0
      src/luaobject.cc
  7. +28
    -0
      src/luaobject.h
  8. +12
    -0
      src/nodelua.cc
  9. +20
    -0
      test/test.js
  10. +1
    -0
      test/test.lua

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
build

+ 32
- 0
README.md View File

@ -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 <brett@blangdon.com>
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.

+ 15
- 0
binding.gyp View File

@ -0,0 +1,15 @@
{
"targets": [
{
"target_name": "nodelua",
"sources": [
"src/luaobject.cc",
"src/nodelua.cc"
],
"libraries": [
"-llua",
"-ldl"
]
}
]
}

+ 9
- 0
lib/index.js View File

@ -0,0 +1,9 @@
var nodelua = null;
try{
nodelua = require('../build/Release/nodelua');
}catch(e){
nodelua = require('../build/Debug/nodelua');
}
module.exports = nodelua;

+ 22
- 0
package.json View File

@ -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 <brett@blangdon.com> (http://www.brett.is)",
"license": "MIT",
"gypfile": true,
"engines": "*"
}

+ 139
- 0
src/luaobject.cc View File

@ -0,0 +1,139 @@
#define BUILDING_NODELUA
#include <node.h>
#include "luaobject.h"
using namespace v8;
LuaObject::LuaObject() {};
LuaObject::~LuaObject() {};
void LuaObject::Init(Handle<Object> target) {
// Prepare constructor template
Local<FunctionTemplate> 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<Function> constructor = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("LuaObject"), constructor);
}
Handle<Value> 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<Value> LuaObject::Close(const Arguments& args){
HandleScope scope;
LuaObject* obj = ObjectWrap::Unwrap<LuaObject>(args.This());
lua_close(obj->lua_);
return scope.Close(Undefined());
}
Handle<Value> 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<LuaObject>(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<Value> 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<LuaObject>(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<Value> 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<LuaObject>(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());
}

+ 28
- 0
src/luaobject.h View File

@ -0,0 +1,28 @@
#ifndef LUAOBJECT_H
#define LUAOBJECT_H
#include <node.h>
extern "C"{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
class LuaObject : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> target);
private:
LuaObject();
~LuaObject();
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> DoFile(const v8::Arguments& args);
static v8::Handle<v8::Value> GetGlobal(const v8::Arguments& args);
static v8::Handle<v8::Value> SetGlobal(const v8::Arguments& args);
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
lua_State *lua_;
};
#endif

+ 12
- 0
src/nodelua.cc View File

@ -0,0 +1,12 @@
#include <node.h>
#include <v8.h>
#include "luaobject.h"
using namespace v8;
void init(Handle<Object> target) {
LuaObject::Init(target);
}
NODE_MODULE(nodelua, init)

+ 20
- 0
test/test.js View File

@ -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();

+ 1
- 0
test/test.lua View File

@ -0,0 +1 @@
print "Hello, Lua"

Loading…
Cancel
Save