summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-28 14:42:41 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-28 14:42:41 +0200
commit354d659f577fc27969784400c8c1e6090756da7b (patch)
tree4aa17f23121794bc8b855e2ed0bf85951281b37e /src
parentcebad06a3429e28cb5e0c247c707bb5eb51943d8 (diff)
downloadfengari-354d659f577fc27969784400c8c1e6090756da7b.tar.gz
fengari-354d659f577fc27969784400c8c1e6090756da7b.tar.bz2
fengari-354d659f577fc27969784400c8c1e6090756da7b.zip
os.exit
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js5
-rw-r--r--src/loslib.js17
-rw-r--r--src/lstate.js12
-rw-r--r--src/lua.js6
4 files changed, 38 insertions, 2 deletions
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;