From 30264bbc1998ab6640afdc31c348ca6167ca6191 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 28 Apr 2017 15:57:31 +0200 Subject: os.execute --- src/loslib.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/loslib.js') 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) { -- cgit v1.2.3-54-g00ecf