From 354d659f577fc27969784400c8c1e6090756da7b Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 28 Apr 2017 14:42:41 +0200 Subject: os.exit --- README.md | 13 +++++++++++-- src/lapi.js | 5 +++++ src/loslib.js | 17 +++++++++++++++++ src/lstate.js | 12 ++++++++++++ src/lua.js | 6 ++++-- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7fc4e98..6e7cf5c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,17 @@ - [x] utf8 - [ ] Package - [ ] os + - [x] `os.exit()` + - [x] `os.time()` + - [ ] `os.clock()` + - [ ] `os.date()` + - [ ] `os.difftime()` + - [ ] `os.execute()` + - [ ] `os.getenv()` + - [ ] `os.remove()` + - [ ] `os.rename()` + - [ ] `os.setlocale()` + - [ ] `os.tmpname()` - [ ] io - [ ] `io.stdin` - [ ] `io.stdout` @@ -50,8 +61,6 @@ - [ ] C API - [x] ... - [ ] lua_arith - - [ ] lua_close - - [ ] lua_isboolean - [ ] lua_islightuserdata - [ ] lua_pushfstring - [ ] lua_pushvfstring diff --git a/src/lapi.js b/src/lapi.js index a758a66..e813313 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -757,6 +757,10 @@ const lua_isnil = function(L, n) { return lua_type(L, n) === CT.LUA_TNIL; }; +const lua_isboolean = function(L, n) { + return lua_type(L, n) === CT.LUA_TBOOLEAN; +}; + const lua_isnone = function(L, n) { return lua_type(L, n) === CT.LUA_TNONE; }; @@ -1037,6 +1041,7 @@ module.exports.lua_gettop = lua_gettop; module.exports.lua_getupvalue = lua_getupvalue; module.exports.lua_getuservalue = lua_getuservalue; module.exports.lua_insert = lua_insert; +module.exports.lua_isboolean = lua_isboolean; module.exports.lua_iscfunction = lua_iscfunction; module.exports.lua_isfunction = lua_isfunction; module.exports.lua_isinteger = lua_isinteger; diff --git a/src/loslib.js b/src/loslib.js index 3892d35..a715aec 100644 --- a/src/loslib.js +++ b/src/loslib.js @@ -66,6 +66,23 @@ const syslib = { "time": os_time }; +// Only with Node +if (process && process.exit) { + const os_exit = function(L) { + let status; + if (lua.lua_isboolean(L, 1)) + status = (lua.lua_toboolean(L, 1) ? 0 : 1); + else + status = lauxlib.luaL_optinteger(L, 1, 0); + if (lua.lua_toboolean(L, 2)) + lua.lua_close(L); + if (L) process.exit(status); /* 'if' to avoid warnings for unreachable 'return' */ + return 0; + }; + + syslib.exit = os_exit; +} + const luaopen_os = function(L) { lauxlib.luaL_newlib(L, syslib); return 1; diff --git a/src/lstate.js b/src/lstate.js index 4ecb92f..4b4aaa5 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -8,6 +8,7 @@ const lobject = require('./lobject.js'); const ldo = require('./ldo.js'); const lapi = require('./lapi.js'); const ltable = require('./ltable.js'); +const lfunc = require('./lfunc.js'); const luaT_init = require('./ltm.js').luaT_init; const CT = defs.constant_types; const TS = defs.thread_status; @@ -167,6 +168,16 @@ const lua_newstate = function() { return L; }; +const close_state = function(L) { + let g = L.l_G; + lfunc.luaF_close(L, L.stack); /* close all upvalues for this thread */ +}; + +const lua_close = function(L) { + L = L.l_G.mainthread; /* only the main thread can be closed */ + close_state(L); +}; + module.exports.lua_State = lua_State; module.exports.CallInfo = CallInfo; module.exports.CIST_OAH = (1<<0); /* original value of 'allowhook' */ @@ -178,5 +189,6 @@ module.exports.CIST_TAIL = (1<<5); /* call was tail called */ module.exports.CIST_HOOKYIELD = (1<<6); /* last hook called yielded */ module.exports.CIST_LEQ = (1<<7); /* using __lt for __le */ module.exports.CIST_FIN = (1<<8); /* call is running a finalizer */ +module.exports.lua_close = lua_close; module.exports.lua_newstate = lua_newstate; module.exports.lua_newthread = lua_newthread; diff --git a/src/lua.js b/src/lua.js index e6e3f02..09fb0e6 100644 --- a/src/lua.js +++ b/src/lua.js @@ -87,6 +87,7 @@ module.exports.lua_atpanic = lapi.lua_atpanic; module.exports.lua_call = lapi.lua_call; module.exports.lua_callk = lapi.lua_callk; module.exports.lua_checkstack = lapi.lua_checkstack; +module.exports.lua_close = lstate.lua_close; module.exports.lua_compare = lapi.lua_compare; module.exports.lua_concat = lapi.lua_concat; module.exports.lua_copy = lapi.lua_copy; @@ -111,6 +112,7 @@ module.exports.lua_gettop = lapi.lua_gettop; module.exports.lua_getupvalue = lapi.lua_getupvalue; module.exports.lua_getuservalue = lapi.lua_getuservalue; module.exports.lua_insert = lapi.lua_insert; +module.exports.lua_isboolean = lapi.lua_isboolean; module.exports.lua_iscfunction = lapi.lua_iscfunction; module.exports.lua_isfunction = lapi.lua_isfunction; module.exports.lua_isinteger = lapi.lua_isinteger; @@ -125,8 +127,8 @@ module.exports.lua_isuserdata = lapi.lua_isuserdata; module.exports.lua_isyieldable = ldo.lua_isyieldable; module.exports.lua_len = lapi.lua_len; module.exports.lua_load = lapi.lua_load; -module.exports.lua_newtable = lapi.lua_newtable; module.exports.lua_newstate = lstate.lua_newstate; +module.exports.lua_newtable = lapi.lua_newtable; module.exports.lua_newthread = lstate.lua_newthread; module.exports.lua_newuserdata = lapi.lua_newuserdata; module.exports.lua_next = lapi.lua_next; @@ -157,8 +159,8 @@ module.exports.lua_rawlen = lapi.lua_rawlen; module.exports.lua_rawset = lapi.lua_rawset; module.exports.lua_rawsetp = lapi.lua_rawsetp; module.exports.lua_remove = lapi.lua_remove; -module.exports.lua_resume = ldo.lua_resume; module.exports.lua_replace = lapi.lua_replace; +module.exports.lua_resume = ldo.lua_resume; module.exports.lua_rotate = lapi.lua_rotate; module.exports.lua_setfield = lapi.lua_setfield; module.exports.lua_setglobal = lapi.lua_setglobal; -- cgit v1.2.3-70-g09d2