Skip to content

Commit 7e7e80c

Browse files
committed
support napi.
1 parent 9b73663 commit 7e7e80c

File tree

11 files changed

+170
-179
lines changed

11 files changed

+170
-179
lines changed

index.js

Lines changed: 72 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,89 @@
11
'use strict';
2-
var nodelua = null;
3-
var path = require('path');
4-
try{
2+
let nodelua = null;
3+
let path = require('path');
4+
try {
55
nodelua = require('./build/Release/node-luajit');
6-
}catch(e){
7-
console.log('``` release load err:',e);
8-
nodelua = require('./build/Debug/node-luajit');
6+
} catch (e) {
7+
console.log('``` release load err:', e);
8+
try{
9+
nodelua = require('./build/Debug/node-luajit');
10+
}catch (e){
11+
nodelua = require('/Users/nuc/Library/Developer/Xcode/DerivedData/binding-ebwzqyofwzjwfzcrbpisobhcazli/Build/Products/Debug/node-luajit.node')
12+
}
913
}
1014

11-
var MyCLua = nodelua.MyLuaState;
15+
let MyCLua = nodelua['MyLuaState'];
1216

13-
if(!nodelua){
17+
if (!nodelua) {
18+
console.error('no node lua')
1419
return;
1520
}
16-
var singleIdx = 0;
17-
var MyLua = function(formatprint){
18-
if(typeof(formatprint)== 'undefined'){
19-
formatprint = true;
20-
}
21-
singleIdx++;
22-
var self = this;
23-
self.lua = new MyCLua(singleIdx);
24-
//init path
25-
var paths = ';'+__dirname+'/?.so;'+__dirname+'/build/Release/?.so;'+__dirname+'/build/Release/?.dll';
26-
var luapaths = ';'+__dirname+'/?.lua;'+__dirname+'/test/?.lua;'+process.cwd()+'/?.lua';
27-
var luastr = 'package.cpath = package.cpath .. "'+paths+'";package.path = package.path .. "'+luapaths+'";LUASINGLEIDX = '+singleIdx+';';
28-
if(formatprint){
29-
luastr += 'require("initlua.formatPrint");';
30-
}
31-
if(process.platform=='win32'){
32-
luastr = luastr.replace(new RegExp('\\\\','g'),'/');
33-
}
34-
self.lua.doString(luastr,function(err,ret){
35-
// console.log(' package path:',err,ret);
36-
if(err){
37-
console.log('!!add find pash err:',err);
38-
console.log('lua:',luastr);
39-
}
40-
});
41-
// self.lua.doString("print(package.path)")
42-
};
43-
44-
var prop = MyLua.prototype;
21+
let singleIdx = 0;
4522

23+
class MyLua {
24+
constructor(formatprint) {
25+
if (typeof (formatprint) == 'undefined') {
26+
formatprint = true;
27+
}
28+
singleIdx++;
29+
let self = this;
30+
self.lua = new MyCLua(singleIdx);
31+
//init path
32+
let paths = ';' + __dirname + '/?.so;' + __dirname + '/build/Release/?.so;' + __dirname + '/build/Release/?.dll';
33+
let luapaths = ';' + __dirname + '/?.lua;' + __dirname + '/test/?.lua;' + process.cwd() + '/?.lua';
34+
let luastr = 'package.cpath = package.cpath .. "' + paths + '";package.path = package.path .. "' + luapaths + '";LUASINGLEIDX = ' + singleIdx + ';';
35+
if (formatprint) {
36+
luastr += 'require("initlua.formatPrint");';
37+
}
38+
if (process.platform === 'win32') {
39+
luastr = luastr.replace(new RegExp('\\\\', 'g'), '/');
40+
}
41+
self.lua.doString(luastr, function (err, ret) {
42+
// console.log(' package path:',err,ret);
43+
if (err) {
44+
console.log('!!add find pash err:', err);
45+
console.log('lua:', luastr);
46+
}
47+
});
48+
}
4649

47-
prop.doFile = function(fn,cb){
48-
return this.lua.doFile(fn,cb);
49-
};
50+
doFile(fn, cb) {
51+
return this.lua.doFile(fn, cb);
52+
}
5053

54+
doString(str, cb) {
55+
return this.lua.doString(str, cb);
56+
}
5157

52-
prop.doString = function(str,cb){
53-
return this.lua.doString(str,cb);
54-
};
58+
/**
59+
* cb(null,status)
60+
* @param cb
61+
*/
62+
status(cb) {
63+
return this.lua.status(cb);
64+
}
5565

56-
/**
57-
* cb(null,status)
58-
* @param cb
59-
*/
60-
prop.status = function(cb){
61-
return this.lua.status(cb);
62-
};
66+
addPachagePath(path, isC) {
67+
let luavar = 'package.path';
68+
if (isC) {
69+
luavar = 'package.cpath';
70+
}
71+
let luastr = luavar + ' = ' + luavar + ' .. ";' + path + '";';
72+
return this.lua.doString(luastr);
73+
}
6374

64-
prop.addPachagePath = function(path,isC){
65-
var luavar = 'package.path';
66-
if(isC){
67-
luavar = 'package.cpath';
75+
/**
76+
* call(funname[,args][,cb]) cb(err,ret)
77+
* @param funName
78+
* @returns {number}
79+
*/
80+
callGlobalFunction(funName) {
81+
return this.lua.callGlobalFunction.apply(this.lua, arguments);
6882
}
69-
var luastr = luavar +' = ' + luavar+' .. ";' + path + '";';
70-
return this.lua.doString(luastr);
71-
};
7283

73-
/**
74-
* call(funname[,args][,cb]) cb(err,ret)
75-
* @param funName
76-
* @returns {number}
77-
*/
78-
prop.callGlobalFunction = function(funName){
79-
return this.lua.callGlobalFunction.apply(this.lua,arguments);
80-
};
84+
static GC = nodelua.GC;
85+
static STATUS = nodelua.STATUS;
86+
static INFO = nodelua.INFO;
87+
}
8188

82-
MyLua.GC = nodelua.GC;
83-
MyLua.STATUS = nodelua.STATUS;
84-
MyLua.INFO = nodelua.INFO;
85-
// { MyLuaState: [Function: MyLuaState],
86-
// GC:
87-
// { STOP: 0,
88-
// RESTART: 1,
89-
// COLLECT: 2,
90-
// COUNT: 3,
91-
// COUNTB: 4,
92-
// STEP: 5,
93-
// SETPAUSE: 6,
94-
// SETSTEPMUL: 7 },
95-
// STATUS: { YIELD: 1, ERRRUN: 2, ERRSYNTAX: 3, ERRMEM: 4, ERRERR: 5 },
96-
// INFO:
97-
// { VERSION: 'Lua 5.1',
98-
// VERSION_NUM: 501,
99-
// COPYRIGHT: 'Copyright (C) 1994-2012 Lua.org, PUC-Rio',
100-
// AUTHORS: 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes' } }
10189
module.exports = MyLua;

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MyLuaState.cpp

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,61 +45,59 @@ MyLuaState::~MyLuaState() {
4545
lua_close(_L);
4646
}
4747

48-
void MyLuaState::normalCallBack(Napi::Function& cb,MyLuaWorker* worker){
49-
if(!cb){
48+
void MyLuaState::normalCallBack(Napi::Function& _cb,MyLuaWorker* worker){
49+
if(!_cb){
5050
return;
5151
}
52-
Napi::HandleScope scope(cb.Env());
52+
auto env = worker->Callback().Env();
53+
// Napi::HandleScope scope(env);
5354
auto ret = worker->getUserData();
5455
Napi::Value err;
5556
Napi::Value arg2;
5657
if(ret.hasErr){
57-
err = Napi::String::New(cb.Env(),ret.buff);
58-
arg2 = cb.Env().Undefined();
58+
err = Napi::String::New(env,ret.buff);
59+
arg2 = env.Undefined();
5960
}else{
60-
err = cb.Env().Null();
61-
arg2 = Napi::String::New(cb.Env(),ret.buff);
61+
err = env.Null();
62+
arg2 = Napi::String::New(env,ret.buff);
6263
}
63-
64-
cb.Call(cb.Env().Global(),{err,arg2});
64+
worker->Callback().Call({err,arg2});
6565
}
6666

6767

6868
// worker->userParam = obj; 要设置成 MyLuaState 实例
69-
void MyLuaState::normalGetRetCallBack(Napi::Function& cb,MyLuaWorker* worker){
70-
if(!cb){
69+
void MyLuaState::normalGetRetCallBack(Napi::Function& ccx,MyLuaWorker* worker){
70+
if(!ccx){
7171
return;
7272
}
73-
Napi::HandleScope scope(cb.Env());
73+
auto env = worker->Callback().Env();
74+
// Napi::HandleScope scope(env);
7475
auto ret = worker->getUserData();
7576
Napi::Value err;
7677
if(ret.hasErr){
7778
Napi::Value arg2;
78-
err = Napi::String::New(cb.Env(),ret.buff);
79-
arg2 = cb.Env().Undefined();
80-
cb.Call(cb.Env().Global(), {err,arg2});
79+
err = Napi::String::New(env,ret.buff);
80+
arg2 = env.Undefined();
81+
worker->Callback().Call({err,arg2});
8182
}else{
8283
MyLuaState* obj = (MyLuaState*)worker->userParam;
83-
err = cb.Env().Null();
84+
err = env.Null();
8485
auto L = obj->getLuaState();
8586
int len = lua_gettop(L) - worker->getUserData().customVal;
8687
if(len){
8788
//lua_to_value
88-
Napi::Value *argv = new Napi::Value[len+1];
89-
argv[0] = cb.Env().Null();
89+
napi_value *argv = new napi_value[len+1];
90+
argv[0] = env.Null();
9091
int i = 1;
9192
while (i<=len) {
92-
argv[i] = lua_to_value(L, i);
93+
argv[i] = lua_to_value(env,L, i);
9394
i++;
9495
}
9596
lua_settop(L, worker->getUserData().customVal);
96-
cb.Call(cb.Env().Null(), size_t(len+1), (napi_value*)argv);
97+
worker->Callback().Call(env.Null(), size_t(len+1), (napi_value*)argv);
9798
delete []argv;
9899
}else{
99-
Napi::Value argv[] = {
100-
err,Napi::String::New(cb.Env(),ret.buff)
101-
};
102-
cb.Call(size_t(2), (napi_value*)argv);
100+
worker->Callback().Call({err,Napi::String::New(env,ret.buff)});
103101
}
104102
}
105103

@@ -109,7 +107,7 @@ void MyLuaState::normalGetRetCallBack(Napi::Function& cb,MyLuaWorker* worker){
109107

110108
// exports.MyLuaState = class { constructor(id){},doFile,doString,status,callGlobalFunction, static New}
111109
Napi::Object MyLuaState::Init(Napi::Env env,Napi::Object exports) {
112-
Napi::HandleScope scope(env);
110+
// Napi::HandleScope scope(env);
113111

114112
Napi::Function func = DefineClass(env, "MyLuaState", {
115113
InstanceMethod<&MyLuaState::DoFile>("doFile"),
@@ -186,19 +184,15 @@ Napi::Value MyLuaState::DoFile(const Napi::CallbackInfo& info){
186184
worker->setUserData(true,lua_tostring(L, -1));
187185
lua_pop(L, 1);
188186
}else{
189-
const char * buff = nullptr;
190-
if(lua_gettop(L) - worker->getUserData().customVal){
191-
buff = lua_tostring(L, -1);
192-
lua_pop(L,1);
193-
}
194-
worker->setUserData(false,buff);
187+
worker->setUserData(false,"");
195188
}
196189
// printf("do file end:%s\n",(char*)nfn);
197190
//要释放 fn
198191
delete []nfn;
199192
},[=](Napi::Function& cb,MyLuaWorker* worker){
200-
this->normalCallBack(cb, worker);
193+
this->normalGetRetCallBack(cb, worker);
201194
});
195+
worker->userParam = this;
202196
// printf("worker out:%p\n",nfn);
203197
int nowqueue = this->workerQueue.addQueue(worker);
204198
return Napi::Number::New(env, nowqueue);
@@ -228,15 +222,16 @@ Napi::Value MyLuaState::DoString(const Napi::CallbackInfo& info){
228222
char* nfn = new char[filename.length()+1];
229223
// printf(" fnfnfnfnfn :%s\n",fn);
230224
strcpy(nfn,fn);
231-
// printf(" fnfnfnfnfn1111 :%s\n",nfn);
225+
// printf(" fnfnfnfnfn1111 :%s\n",nfn);
232226
Napi::Function callback;
233227
if(info.Length()>1){
234228
callback = info[1].As<Napi::Function>();
235229
}
236230

237231
MyLuaWorker* worker = new MyLuaWorker(callback,[=](MyLuaWorker* worker){
232+
// printf("DoString worker int:%p\n",nfn);
238233
auto L = this->getLuaState();
239-
// printf("worker int:%p\n",nfn);
234+
//
240235
// printf("begin do file :%s\n",nfn);
241236
int top = lua_gettop(L);
242237
worker->getUserData().customVal = top;
@@ -249,10 +244,11 @@ Napi::Value MyLuaState::DoString(const Napi::CallbackInfo& info){
249244
}else{
250245
worker->setUserData(false,"");
251246
}
252-
// printf("do file end:%s\n",(char*)nfn);
253-
//要释放 fn
247+
// printf("DoString end:%s\n",(char*)nfn);
248+
// 要释放 fn
254249
delete []nfn;
255250
},[=](Napi::Function& cb,MyLuaWorker* worker){
251+
// printf("DoString callback\n");
256252
this->normalGetRetCallBack(cb, worker);
257253
});
258254
worker->userParam = this;
@@ -279,12 +275,10 @@ Napi::Value MyLuaState::Status(const Napi::CallbackInfo& info){
279275
int status = lua_status(L);
280276
worker->userParam = reinterpret_cast<void*>(status);
281277
worker->setUserData(false,"");
282-
},[=](Napi::Function& cb,MyLuaWorker* worker){
278+
},[=](Napi::Function& __cb,MyLuaWorker* worker){
279+
auto env = worker->Callback().Env();
283280
long status = reinterpret_cast<long>(worker->userParam);
284-
Napi::Value argv[] = {
285-
env.Null(),Napi::Number::New(env, status)
286-
};
287-
cb.Call(env.Global(),size_t(2),(napi_value*)argv);
281+
worker->Callback().Call({env.Null(),Napi::Number::New(env, status)});
288282
});
289283
int nowqueue = this->workerQueue.addQueue(worker);
290284
return Napi::Number::New(env, nowqueue);

src/MyLuaWorker.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ void MyLuaWorker::Execute () {
1818

1919

2020

21-
void MyLuaWorker::HandleOKCallback () {
22-
// printf("WorkerCall handle ok callback:%ld\n",time(0));
21+
void MyLuaWorker::OnOK () {
22+
// printf("OnOK handle ok callback:%ld\n",time(0));
2323
_finishCb(callback,this);
2424
// printf("WorkerCall handle ok callback end:%ld\n",time(0));
2525
_queueNotify(this);
26+
// clear no need will delete by node
27+
// delete this;
2628
// Nan::HandleScope scope;
2729
//
2830
// Local<Value> argv[] = {

src/MyLuaWorker.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class MyLuaWorker : public Napi::AsyncWorker {
3131
_userData.customVal = 0;
3232
_userData.buff[0] = 0;
3333
}
34-
~MyLuaWorker(){}
34+
~MyLuaWorker(){
35+
// printf("~MyLuaWorker\n");
36+
}
3537

3638
void Execute ();
3739
struct UserDatas{
@@ -43,7 +45,7 @@ class MyLuaWorker : public Napi::AsyncWorker {
4345
// Executed when the async work is complete
4446
// this function will be run inside the main event loop
4547
// so it is safe to use V8 again
46-
void HandleOKCallback ();
48+
void OnOK();
4749

4850
void setQueueNotify(WorkerCall qn){
4951
_queueNotify = qn;

0 commit comments

Comments
 (0)