"use strict"; const { LUA_TNIL, LUA_TTABLE, lua_close, lua_createtable, lua_getfield, lua_isboolean, lua_isnoneornil, lua_pop, lua_pushboolean, lua_pushfstring, lua_pushinteger, lua_pushliteral, lua_pushnil, lua_pushnumber, lua_pushstring, lua_setfield, lua_settop, lua_toboolean, lua_tointegerx } = require('./lua.js'); const { luaL_Buffer, luaL_addchar, luaL_addstring, // luaL_argcheck, luaL_argerror, luaL_buffinit, luaL_checkinteger, luaL_checkstring, luaL_checktype, luaL_error, luaL_execresult, luaL_fileresult, luaL_newlib, luaL_opt, luaL_optinteger, luaL_optlstring, luaL_optstring, luaL_pushresult } = require('./lauxlib.js'); const { luastring_indexOf, to_jsstring, to_luastring } = require("./fengaricore.js"); const strftime = require('strftime'); /* options for ANSI C 89 (only 1-char options) */ const L_STRFTIMEC89 = to_luastring("aAbBcdHIjmMpSUwWxXyYZ%"); const LUA_STRFTIMEOPTIONS = L_STRFTIMEC89; /* options for ISO C 99 and POSIX */ // const L_STRFTIMEC99 = to_luastring("aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%||EcECExEXEyEYOdOeOHOIOmOMOSOuOUOVOwOWOy"); /* two-char options */ // const LUA_STRFTIMEOPTIONS = L_STRFTIMEC99; /* options for Windows */ // const L_STRFTIMEWIN = to_luastring("aAbBcdHIjmMpSUwWxXyYzZ%||#c#x#d#H#I#j#m#M#S#U#w#W#y#Y"); /* two-char options */ // const LUA_STRFTIMEOPTIONS = L_STRFTIMEWIN; const setfield = function(L, key, value) { lua_pushinteger(L, value); lua_setfield(L, -2, to_luastring(key, true)); }; const setallfields = function(L, time, utc) { setfield(L, "sec", !utc ? time.getSeconds() : time.getUTCSeconds()); setfield(L, "min", !utc ? time.getMinutes() : time.getUTCMinutes()); setfield(L, "hour", !utc ? time.getHours() : time.getUTCHours()); setfield(L, "day", !utc ? time.getDate() : time.getUTCDate()); setfield(L, "month", !utc ? time.getMonth() : time.getUTCMonth()); setfield(L, "year", !utc ? time.getFullYear() : time.getUTCFullYear()); setfield(L, "wday", !utc ? time.getDay() : time.getUTCDay()); 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 = lua_getfield(L, -1, to_luastring(key, true)); /* get field and its type */ let res = lua_tointegerx(L, -1); if (res === false) { /* field is not an integer? */ if (t !== LUA_TNIL) /* some other value? */ return luaL_error(L, to_luastring("field '%s' is not an integer"), key); else if (d < 0) /* absent field; no default? */ return luaL_error(L, to_luastring("field '%s' missing in date table"), key); res = d; } else { if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) return luaL_error(L, to_luastring("field '%s' is out-of-bound"), key); res -= delta; } lua_pop(L, 1); return res; }; const array_cmp = function(a, ai, b, bi, len) { for (let i=0; i