summaryrefslogtreecommitdiff
path: root/src/loslib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-18 10:37:02 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-18 10:50:25 +0200
commit5f6d2e34f69d14c57e7f2964aa951c8dab4efc99 (patch)
treeb3caa950722eb8739bfa962f723e14d106be1b55 /src/loslib.js
parentc709857c7d6d1af740a5763ac0c78e010dd1757e (diff)
downloadfengari-5f6d2e34f69d14c57e7f2964aa951c8dab4efc99.tar.gz
fengari-5f6d2e34f69d14c57e7f2964aa951c8dab4efc99.tar.bz2
fengari-5f6d2e34f69d14c57e7f2964aa951c8dab4efc99.zip
os.time
Diffstat (limited to 'src/loslib.js')
-rw-r--r--src/loslib.js78
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;