aboutsummaryrefslogtreecommitdiff
path: root/src/ltablib.js
blob: a7963d34bcc99788d27a4479032aced06d4e45d8 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"use strict";

const assert  = require('assert');

const lua     = require('./lua.js');
const lapi    = require('./lapi.js');
const lauxlib = require('./lauxlib.js');
const lstate  = require('./lstate.js');
const ldo     = require('./ldo.js');
const ldebug  = require('./ldebug.js');
const llimit  = require('./llimit.js');
const CT      = lua.constant_types;
const TS      = lua.thread_status;


/*
** Operations that an object must define to mimic a table
** (some functions only need some of them)
*/
const TAB_R  = 1;               /* read */
const TAB_W  = 2;               /* write */
const TAB_L  = 4;               /* length */
const TAB_RW = (TAB_R | TAB_W); /* read/write */

const checkfield = function(L, key, n) {
    lapi.lua_pushstring(L, key);
    return lapi.lua_rawget(L, -n) !== CT.LUA_TNIL;
};

/*
** Check that 'arg' either is a table or can behave like one (that is,
** has a metatable with the required metamethods)
*/
const checktab = function(L, arg, what) {
    if (lapi.lua_type(L, arg) !== CT.LUA_TTABLE) {  /* is it not a table? */
        let n = 1;
        if (lapi.lua_getmetatable(L, arg) &&  /* must have metatable */
            (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
            (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
            (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
            lapi.lua_pop(L, n);  /* pop metatable and tested metamethods */
        }
        else
            lauxlib.luaL_checktype(L, arg, CT.LUA_TTABLE);  /* force an error */
    }
};

const aux_getn = function(L, n, w) {
    checktab(L, n, w | TAB_L);
    return lauxlib.luaL_len(L, n);
};

const addfield = function(L, b, i) {
    lapi.lua_geti(L, 1, i);
    if (!lapi.lua_isstring(L, -1))
        lauxlib.luaL_error(L, `invalid value (${lauxlib.luaL_typename(L, -1)}) at index ${i} in table for 'concat'`);

    lauxlib.luaL_addvalue(b);
};

const tinsert = function(L) {
    let e = aux_getn(L, 1, TAB_RW) + 1;  /* first empty element */
    let pos;
    switch (lapi.lua_gettop(L)) {
        case 2:
            pos = e;
            break;
        case 3: {
            pos = lauxlib.luaL_checkinteger(L, 2);  /* 2nd argument is the position */
            lauxlib.luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
            for (let i = e; i > pos; i--) {  /* move up elements */
                lapi.lua_geti(L, 1, i - 1);
                lapi.lua_seti(L, 1, i);  /* t[i] = t[i - 1] */
            }
            break;
        }
        default: {
            return lauxlib.luaL_error(L, "wrong number of arguments to 'insert'");
        }
    }

    lapi.lua_seti(L, 1, pos);  /* t[pos] = v */
    return 0;
};

const tremove = function(L) {
    let size = aux_getn(L, 1, TAB_RW);
    let pos = lauxlib.luaL_optinteger(L, 2, size);
    if (pos !== size)  /* validate 'pos' if given */
        lauxlib.luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
    lapi.lua_geti(L, 1, pos);  /* result = t[pos] */
    for (; pos < size; pos++) {
        lapi.lua_geti(L, 1, pos + 1);
        lapi.lua_seti(L, 1, pos);  /* t[pos] = t[pos + 1] */
    }
    lapi.lua_pushnil(L);
    lapi.lua_seti(L, 1, pos);  /* t[pos] = nil */
    return 1;
};

/*
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
** possible, copy in increasing order, which is better for rehashing.
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
const tmove = function(L) {
    let f = lauxlib.luaL_checkinteger(L, 2);
    let e = lauxlib.luaL_checkinteger(L, 3);
    let t = lauxlib.luaL_checkinteger(L, 4);
    let tt = !lapi.lua_isnoneornil(L, 5) ? 5 : 1;  /* destination table */
    checktab(L, 1, TAB_R);
    checktab(L, tt, TAB_W);
    if (e >= f) {  /* otherwise, nothing to move */
        lauxlib.luaL_argcheck(L, f > 0 || e < llimit.LUA_MAXINTEGER + f, 3, "too many elements to move");
        let n = e - f + 1;  /* number of elements to move */
        lauxlib.luaL_argcheck(L, t <= llimit.LUA_MAXINTEGER - n + 1, 4, "destination wrap around");

        if (t > e || t <= f || (tt !== 1 && lapi.lua_compare(L, 1, tt, lua.LUA_OPEQ) !== 1)) {
            for (let i = 0; i < n; i++) {
                lapi.lua_geti(L, 1, f + i);
                lapi.lua_seti(L, tt, t + i);
            }
        } else {
            for (let i = n - 1; i >= 0; i--) {
                lapi.lua_geti(L, 1, f + i);
                lapi.lua_seti(L, tt, t + i);
            }
        }
    }

    lapi.lua_pushvalue(L, tt);  /* return destination table */
    return 1;
};

const tconcat = function(L) {
    let last = aux_getn(L, 1, TAB_R);
    let sep = lauxlib.luaL_optlstring(L, 2, "");
    let i = lauxlib.luaL_optinteger(L, 3, 1);
    last = lauxlib.luaL_optinteger(L, 4, last);

    let b = new lauxlib.luaL_Buffer();
    lauxlib.luaL_buffinit(L, b);

    for (; i < last; i++) {
        addfield(L, b, i);
        lauxlib.luaL_addlstring(b, sep);
    }

    if (i === last)
        addfield(L, b, i);

    lauxlib.luaL_pushresult(b);

    return 1;
};

const pack = function(L) {
    let n = lapi.lua_gettop(L);  /* number of elements to pack */
    lapi.lua_createtable(L, n, 1);  /* create result table */
    lapi.lua_insert(L, 1);  /* put it at index 1 */
    for (let i = n; i >= 1; i--)  /* assign elements */
        lapi.lua_seti(L, 1, i);
    lapi.lua_pushinteger(L, n);
    lapi.lua_setfield(L, 1, "n");  /* t.n = number of elements */
    return 1;  /* return table */
};

const unpack = function(L) {
    let i = lauxlib.luaL_optinteger(L, 2, 1);
    let e = lauxlib.luaL_opt(L, lauxlib.luaL_checkinteger, 3, lapi.lua_len(L, 1));
    if (i > e) return 0;  /* empty range */
    let n = e - i;  /* number of elements minus 1 (avoid overflows) */
    if (n >= Number.MAX_SAFE_INTEGER || !lapi.lua_checkstack(L, ++n))
        return lauxlib.luaL_error(L, "too many results to unpack");
    for (; i < e; i++)  /* push arg[i..e - 1] (to avoid overflows) */
        lapi.lua_geti(L, 1, i);
    lapi.lua_geti(L, 1, e);  /* push last element */
    return n;
};


// TODO: Maybe do the quicksort after all
const auxsort = function(L) {
    let t = lapi.index2addr(L, 1);

    if (lapi.lua_type(L, 2) !== CT.LUA_TFUNCTION) {  /* no function? */
        [...t.value.entries()]
            .sort(function (a, b) {
                if (typeof a[0] !== 'number') return 1;
                else if (typeof b[0] !== 'number') return -1;
                return lapi.lua_compare_(L, a[1], b[1], lua.LUA_OPLT) === 1 ? -1 : 1;  /* a < b */
            })
            .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true);
    } else {
        [...t.value.entries()]
            .sort(function (a, b) {
                if (typeof a[0] !== 'number') return 1;
                else if (typeof b[0] !== 'number') return -1;

                lapi.lua_pushvalue(L, 2);  /* push function */
                lapi.lua_pushtvalue(L, a[1]);  /* since we use Map.sort, a and b are not on the stack */
                lapi.lua_pushtvalue(L, b[1]);
                lapi.lua_call(L, 2, 1);  /* call function */
                let res = lapi.lua_toboolean(L, -1);  /* get result */
                lapi.lua_pop(L, 1);  /* pop result */
                return res ? -1 : 1;
            })
            .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i, e[1]) : true);
    }
};

const sort = function(L) {
    let n = aux_getn(L, 1, TAB_RW);
    if (n > 1) {  /* non-trivial interval? */
        lauxlib.luaL_argcheck(L, n < Number.MAX_SAFE_INTEGER, 1, "array too big");
        if (!lapi.lua_isnoneornil(L, 2))  /* is there a 2nd argument? */
            lauxlib.luaL_checktype(L, 2, CT.LUA_TFUNCTION);  /* must be a function */
        lapi.lua_settop(L, 2);  /* make sure there are two arguments */
        auxsort(L);
    }
    return 0;
};

const tab_funcs = {
    "concat": tconcat,
    "insert": tinsert,
    "move":   tmove,
    "pack":   pack,
    "remove": tremove,
    "sort":   sort,
    "unpack": unpack
};

const luaopen_table = function(L) {
    lauxlib.luaL_newlib(L, tab_funcs);
    return 1;
};

module.exports.luaopen_table = luaopen_table;