aboutsummaryrefslogtreecommitdiff
path: root/src/loslib.js
blob: 1cc7d842fdfe39b0ed4d88e8b39a72a3ca293fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";

const lua     = require('./lua.js');
const lauxlib = require('./lauxlib.js');
const llimit  = require('./llimit.js');

const setfield = function(L, key, value) {
    lua.lua_pushinteger(L, value);
    lua.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 = (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 res = lua.lua_tointegerx(L, -1);
    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);
        res = d;
    }
    else {
        if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
            return lauxlib.luaL_error(L, lua.to_luastring(`field '${key}' is out-of-bound`), true);
        res -= delta;
    }
    lua.lua_pop(L, 1);
    return res;
};

const os_time = function(L) {
    let t = new Date();
    if (!lua.lua_isnoneornil(L, 1))  /* called with arg */{
        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));
        setallfields(L, t);
    }

    lua.lua_pushinteger(L, Math.floor(t / 1000));
    return 1;
};


const syslib = {
    "time": os_time
};

// Only with Node
if (process && process.exit && process.env) {
    const os_exit = function(L) {
        let status;
        if (lua.lua_isboolean(L, 1))
            status = (lua.lua_toboolean(L, 1) ? 0 : 1);
        else
            status = lauxlib.luaL_optinteger(L, 1, 0);
        if (lua.lua_toboolean(L, 2))
            lua.lua_close(L);
        if (L) process.exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
        return 0;
    };

    const os_getenv = function(L) {
        lua.lua_pushliteral(L, process.env[lua.to_jsstring(lauxlib.luaL_checkstring(L, 1))]);  /* if NULL push nil */
        return 1;
    };

    syslib.exit = os_exit;
    syslib.getenv = os_getenv;
}


// Only with Node
if (typeof require === "function") {

    let fs = false;
    let tmp = false;
    try {
        fs = require('fs');
        tmp = require('tmp');
    } catch (e) {}

    if (fs && tmp) {
        // TODO: on POSIX system, should create the file
        const lua_tmpname = function() {
            return tmp.tmpNameSync();
        };

        const os_remove = function(L) {
            let filename = lauxlib.luaL_checkstring(L, 1);
            try {
                if (fs.lstatSync(lua.to_jsstring(filename)).isDirectory()) {
                    fs.rmdirSync(lua.to_jsstring(filename));
                } else {
                    fs.unlinkSync(lua.to_jsstring(filename));
                }
            } catch (e) {
                return lauxlib.luaL_fileresult(L, false, filename, e);
            }
            return lauxlib.luaL_fileresult(L, true);
        };

        const os_rename = function(L) {
            let fromname = lua.to_jsstring(lauxlib.luaL_checkstring(L, 1));
            let toname = lua.to_jsstring(lauxlib.luaL_checkstring(L, 2));
            try {
                fs.renameSync(fromname, toname);
            } catch (e) {
                return lauxlib.luaL_fileresult(L, false, false, e);
            }
            return lauxlib.luaL_fileresult(L, true);
        };

        const os_tmpname = function(L) {
            let name = lua_tmpname();
            if (!name)
                return lauxlib.luaL_error(L, lua.to_luastring("unable to generate a unique filename"));
            lua.lua_pushstring(L, lua.to_luastring(name));
            return 1;
        };

        syslib.remove = os_remove;
        syslib.rename = os_rename;
        syslib.tmpname = os_tmpname;
    }

}

const luaopen_os = function(L) {
    lauxlib.luaL_newlib(L, syslib);
    return 1;
};

module.exports.luaopen_os = luaopen_os;