diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-19 18:12:07 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-19 18:12:07 +1000 |
commit | 662381f6669f28f90a1d50c89647d265ca3804a4 (patch) | |
tree | 5ae3bce5dcf2a83302618280163d3088e5d090f8 /src/loslib.js | |
parent | 4d596650dff0417660874727964a32dae23dc9ea (diff) | |
parent | 6be8db07196c407cd321a7b04f5022939c4ffce3 (diff) | |
download | fengari-662381f6669f28f90a1d50c89647d265ca3804a4.tar.gz fengari-662381f6669f28f90a1d50c89647d265ca3804a4.tar.bz2 fengari-662381f6669f28f90a1d50c89647d265ca3804a4.zip |
Merge branch 'master' into cli
Diffstat (limited to 'src/loslib.js')
-rw-r--r-- | src/loslib.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/loslib.js b/src/loslib.js new file mode 100644 index 0000000..20675a1 --- /dev/null +++ b/src/loslib.js @@ -0,0 +1,78 @@ +"use strict"; + +const assert = require('assert'); + +const lua = require('./lua.js'); +const char = lua.char; +const lapi = require('./lapi.js'); +const lauxlib = require('./lauxlib.js'); +const ldebug = require('./ldebug.js'); + +const setfield = function(L, key, value) { + lapi.lua_pushinteger(L, value); + lapi.lua_setfield(L, -2, key); +}; + +const setallfields = function(L, time) { + setfield(L, "sec", time.getSeconds()); + setfield(L, "min", time.getMinutes()); + setfield(L, "hour", time.getHours()); + setfield(L, "day", time.getDate()); + setfield(L, "month", time.getMonth()); + setfield(L, "year", time.getYear()); + 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))); + // setboolfield(L, "isdst", time.get); +}; + +const L_MAXDATEFIELD = (Number.MAX_SAFE_INTEGER / 2); + +const getfield = function(L, key, d, delta) { + let t = lapi.lua_getfield(L, -1, lua.to_luastring(key)); /* get field and its type */ + let res = lapi.lua_tointegerx(L, -1); + if (res !== false) { /* field is not an integer? */ + if (t != lua.CT.LUA_TNIL) /* some other value? */ + return lauxlib.luaL_error(L, `field '${key}' is not an integer`); + else if (d < 0) /* absent field; no default? */ + return lauxlib.luaL_error(L, `field '${key}' missing in date table`); + res = d; + } + else { + if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + return lauxlib.luaL_error(L, `field '${key}' is out-of-bound`); + res -= delta; + } + lapi.lua_pop(L, 1); + return res; +}; + +const os_time = function(L) { + let t = new Date(); + if (!lapi.lua_isnoneornil(L, 1)) /* called with arg */{ + lauxlib.luaL_checktype(L, 1, lua.CT.LUA_TTABLE); /* make sure table is at the top */ + lapi.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)); + setallfields(L, t); + } + + lapi.lua_pushinteger(L, Math.floor(t / 1000)); + return 1; +}; + + +const syslib = { + "time": os_time +}; + +const luaopen_os = function(L) { + lauxlib.luaL_newlib(L, syslib); + return 1; +}; + +module.exports.luaopen_os = luaopen_os; |