aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-28 15:57:31 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-28 15:57:31 +0200
commit30264bbc1998ab6640afdc31c348ca6167ca6191 (patch)
treed66080ac55ba07ace41b828652c5a6419e650b66 /src
parent5d4b124a2fd10e3c156a5cf440a901cff13bfadf (diff)
downloadfengari-30264bbc1998ab6640afdc31c348ca6167ca6191.tar.gz
fengari-30264bbc1998ab6640afdc31c348ca6167ca6191.tar.bz2
fengari-30264bbc1998ab6640afdc31c348ca6167ca6191.zip
os.execute
Diffstat (limited to 'src')
-rw-r--r--src/lauxlib.js23
-rw-r--r--src/loslib.js33
2 files changed, 53 insertions, 3 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js
index bb2cd00..68e7c12 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -200,8 +200,7 @@ const luaL_fileresult = function(L, stat, fname, e) {
if (stat) {
lua.lua_pushboolean(L, 1);
return 1;
- }
- else {
+ } else {
lua.lua_pushnil(L);
if (fname)
lua.lua_pushstring(L, lua.to_luastring(`${lua.to_jsstring(fname)}: ${e.message}`));
@@ -212,6 +211,23 @@ const luaL_fileresult = function(L, stat, fname, e) {
}
};
+/* Unlike normal lua, we pass in an error object */
+const luaL_execresult = function(L, stat, e) {
+ let what = lua.to_luastring("exit"); /* type of termination */
+ if (e && e.status === -1) /* error? */
+ return luaL_fileresult(L, 0, null, e);
+ else {
+ if (e && e.signal) {
+ lua.lua_pushnil(L);
+ lua.lua_pushliteral(L, "signal");
+ } else {
+ lua.lua_pushboolean(L, 1);
+ lua.lua_pushliteral(L, "exit");
+ }
+ lua.lua_pushinteger(L, e ? e.status : 0);
+ return 3;
+ }
+};
const luaL_getmetatable = function(L, n) {
return lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, n);
@@ -693,8 +709,8 @@ const lua_writestringerror = function(s) {
else console.error(s);
};
-module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE;
module.exports.LUA_FILEHANDLE = LUA_FILEHANDLE;
+module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE;
module.exports.luaL_Buffer = luaL_Buffer;
module.exports.luaL_addchar = luaL_addchar;
module.exports.luaL_addlstring = luaL_addlstring;
@@ -714,6 +730,7 @@ module.exports.luaL_checkstring = luaL_checkstring;
module.exports.luaL_checktype = luaL_checktype;
module.exports.luaL_checkudata = luaL_checkudata;
module.exports.luaL_error = luaL_error;
+module.exports.luaL_execresult = luaL_execresult;
module.exports.luaL_fileresult = luaL_fileresult;
module.exports.luaL_getmetafield = luaL_getmetafield;
module.exports.luaL_getmetatable = luaL_getmetatable;
diff --git a/src/loslib.js b/src/loslib.js
index 1cc7d84..cb82bc3 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -95,9 +95,11 @@ if (typeof require === "function") {
let fs = false;
let tmp = false;
+ let child_process = false;
try {
fs = require('fs');
tmp = require('tmp');
+ child_process = require('child_process');
} catch (e) {}
if (fs && tmp) {
@@ -144,6 +146,37 @@ if (typeof require === "function") {
syslib.tmpname = os_tmpname;
}
+ if (child_process) {
+ const os_execute = function(L) {
+ let cmd = lauxlib.luaL_optstring(L, 1, null);
+ let out = false;
+ if (cmd !== null) {
+ try {
+ out = child_process.execSync(lua.to_jsstring(cmd));
+ } catch (e) {
+ return lauxlib.luaL_execresult(L, false, e);
+ }
+
+ // if (out) console.log(out);
+
+ return lauxlib.luaL_execresult(L, true);
+ } else {
+ try {
+ out = child_process.execSync(lua.to_jsstring(cmd));
+ lua.lua_pushboolean(L, 1);
+ } catch (e) {
+ lua.lua_pushboolean(L, 0);
+ }
+
+ // if (out) console.log(out);
+
+ return 1;
+ }
+ };
+
+ syslib.execute = os_execute;
+ }
+
}
const luaopen_os = function(L) {