aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/lauxlib.js23
-rw-r--r--src/loslib.js33
3 files changed, 54 insertions, 5 deletions
diff --git a/README.md b/README.md
index a92dd46..c14ff90 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@
- [x] utf8
- [ ] Package
- [ ] os
+ - [x] `os.execute()`
- [x] `os.exit()`
- [x] `os.getenv()`
- [x] `os.remove()`
@@ -33,7 +34,6 @@
- [ ] `os.clock()`
- [ ] `os.date()`
- [ ] `os.difftime()`
- - [ ] `os.execute()`
- [ ] `os.setlocale()`
- [ ] io
- [ ] `io.stdin`
@@ -75,7 +75,6 @@
- [ ] luaL_checkversion
- [ ] luaL_dofile
- [ ] luaL_dostring
- - [ ] luaL_execresult
- [ ] luaL_gsub
- [ ] luaL_newlibtable
- [ ] luaL_optnumber
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) {