summaryrefslogtreecommitdiff
path: root/src/loslib.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/loslib.js')
-rw-r--r--src/loslib.js54
1 files changed, 36 insertions, 18 deletions
diff --git a/src/loslib.js b/src/loslib.js
index cb82bc3..4c3331d 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -6,7 +6,7 @@ const llimit = require('./llimit.js');
const setfield = function(L, key, value) {
lua.lua_pushinteger(L, value);
- lua.lua_setfield(L, -2, key);
+ lua.lua_setfield(L, -2, lua.to_luastring(key, true));
};
const setallfields = function(L, time) {
@@ -15,7 +15,7 @@ const setallfields = function(L, time) {
setfield(L, "hour", time.getHours());
setfield(L, "day", time.getDate());
setfield(L, "month", time.getMonth());
- setfield(L, "year", time.getYear());
+ setfield(L, "year", time.getFullYear());
setfield(L, "wday", time.getDay());
let now = new Date();
setfield(L, "yday", Math.floor((now - (new Date(now.getFullYear(), 0, 0))) / (1000 * 60 * 60 * 24)));
@@ -25,10 +25,10 @@ const setallfields = function(L, time) {
const L_MAXDATEFIELD = (llimit.MAX_INT / 2);
const getfield = function(L, key, d, delta) {
- let t = lua.lua_getfield(L, -1, lua.to_luastring(key)); /* get field and its type */
+ let t = lua.lua_getfield(L, -1, lua.to_luastring(key, true)); /* get field and its type */
let res = lua.lua_tointegerx(L, -1);
- if (res !== false) { /* field is not an integer? */
- if (t != lua.LUA_TNIL) /* some other value? */
+ if (res === false) { /* field is not an integer? */
+ if (t !== lua.LUA_TNIL) /* some other value? */
return lauxlib.luaL_error(L, lua.to_luastring(`field '${key}' is not an integer`), true);
else if (d < 0) /* absent field; no default? */
return lauxlib.luaL_error(L, lua.to_luastring(`field '${key}' missing in date table`), true);
@@ -49,11 +49,11 @@ const os_time = function(L) {
lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); /* make sure table is at the top */
lua.lua_settop(L, 1);
t.setSeconds(getfield(L, "sec", 0, 0));
- t.setSeconds(getfield(L, "min", 0, 0));
- t.setSeconds(getfield(L, "hour", 12, 0));
- t.setSeconds(getfield(L, "day", -1, 0));
- t.setSeconds(getfield(L, "month", -1, 1));
- t.setSeconds(getfield(L, "year", -1, 1900));
+ t.setMinutes(getfield(L, "min", 0, 0));
+ t.setHours(getfield(L, "hour", 12, 0));
+ t.setDate(getfield(L, "day", -1, 0));
+ t.setMonth(getfield(L, "month", -1, 1));
+ t.setFullYear(getfield(L, "year", -1, 0));
setallfields(L, t);
}
@@ -61,9 +61,22 @@ const os_time = function(L) {
return 1;
};
+const l_checktime = function(L, arg) {
+ let t = lauxlib.luaL_checkinteger(L, arg);
+ // lauxlib.luaL_argcheck(L, t, arg, lua.to_luastring("time out-of-bounds"));
+ return t;
+};
+
+const os_difftime = function(L) {
+ let t1 = l_checktime(L, 1);
+ let t2 = l_checktime(L, 2);
+ lua.lua_pushnumber(L, new Date(t1) - new Date(t2));
+ return 1;
+};
const syslib = {
- "time": os_time
+ "time": os_time,
+ "difftime": os_difftime
};
// Only with Node
@@ -149,27 +162,32 @@ if (typeof require === "function") {
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));
+ child_process.execSync(
+ lua.to_jsstring(cmd),
+ {
+ stdio: [process.stdin, process.stdout, process.stderr]
+ }
+ );
} 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));
+ child_process.execSync(
+ lua.to_jsstring(cmd),
+ {
+ stdio: [process.stdin, process.stdout, process.stderr]
+ }
+ );
lua.lua_pushboolean(L, 1);
} catch (e) {
lua.lua_pushboolean(L, 0);
}
- // if (out) console.log(out);
-
return 1;
}
};