aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-04-20 22:04:26 +1000
committerdaurnimator <quae@daurnimator.com>2018-04-20 22:04:26 +1000
commit6934a870564f8fd2d5cd7df3446e293d070c9a9e (patch)
tree52e3faf0dfa3cd67af52bf6268721f5e9b93e0c4
parenta260f381d30e036b39078fee0295e539067c10a3 (diff)
downloadfengari-6934a870564f8fd2d5cd7df3446e293d070c9a9e.tar.gz
fengari-6934a870564f8fd2d5cd7df3446e293d070c9a9e.tar.bz2
fengari-6934a870564f8fd2d5cd7df3446e293d070c9a9e.zip
src/loslib.js: Fix os.time() with out-of-range values
Fixes #126
-rw-r--r--src/loslib.js24
-rw-r--r--test/loslib.test.js21
2 files changed, 35 insertions, 10 deletions
diff --git a/src/loslib.js b/src/loslib.js
index 6c6b2f4..814d1b2 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -169,16 +169,20 @@ const os_date = function(L) {
};
const os_time = function(L) {
- let t = new Date();
- if (!lua_isnoneornil(L, 1)) /* called with arg */{
- luaL_checktype(L, 1, LUA_TTABLE); /* make sure table is at the top */
- lua_settop(L, 1);
- t.setSeconds(getfield(L, "sec", 0, 0));
- 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));
+ let t;
+ if (lua_isnoneornil(L, 1)) /* called without args? */
+ t = new Date(); /* get current time */
+ else {
+ luaL_checktype(L, 1, LUA_TTABLE);
+ lua_settop(L, 1); /* make sure table is at the top */
+ t = new Date(
+ getfield(L, "year", -1, 0),
+ getfield(L, "month", -1, 1),
+ getfield(L, "day", -1, 0),
+ getfield(L, "hour", 12, 0),
+ getfield(L, "min", 0, 0),
+ getfield(L, "sec", 0, 0)
+ )
setallfields(L, t);
}
diff --git a/test/loslib.test.js b/test/loslib.test.js
index a270d49..5f06a06 100644
--- a/test/loslib.test.js
+++ b/test/loslib.test.js
@@ -84,6 +84,27 @@ test('os.date', () => {
});
+test('os.date normalisation', () => {
+ let L = lauxlib.luaL_newstate();
+ if (!L) throw Error("failed to create lua state");
+
+ let luaCode = `
+ return os.date('%Y-%m-%d', os.time({
+ day = 0,
+ month = 0,
+ year = 2014
+ }))
+ `;
+ {
+ lualib.luaL_openlibs(L);
+ expect(lauxlib.luaL_loadstring(L, to_luastring(luaCode))).toBe(lua.LUA_OK);
+ lua.lua_call(L, 0, -1);
+ }
+
+ expect(lua.lua_tojsstring(L, -1)).toBe("2013-11-30");
+});
+
+
test('os.getenv', () => {
let L = lauxlib.luaL_newstate();
if (!L) throw Error("failed to create lua state");