summaryrefslogtreecommitdiff
path: root/src/lapi.js
blob: 2f2802f1b8a2b0bb38700d4ee37768ff4892cabf (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/* jshint esversion: 6 */
"use strict";

const assert    = require('assert');

const ldo       = require('./ldo.js');
const lobject   = require('./lobject.js');
const ltm       = require('./ltm.js');
const lfunc     = require('./lfunc.js');
const lua       = require('./lua.js');
const luaconf   = require('./luaconf.js');
const lstate    = require('./lstate.js');
const lvm       = require('./lvm.js');
const lundump   = require('./lundump.js');
const ldebug    = require('./ldebug.js');
const MAXUPVAL  = lfunc.MAXUPVAL;
const CT        = lua.constant_types;
const TS        = lua.thread_status;
const TValue    = lobject.TValue;
const CClosure  = lobject.CClosure;

const lua_version = function(L) {
    if (L === null) return lua.LUA_VERSION_NUM;
    else return L.l_G.version;
};

const lua_atpanic = function(L, panicf) {
    let old = L.l_G.panic;
    L.l_G.panic = panicf;
    return old;
};

// Return value for idx on stack
const index2addr = function(L, idx) {
    let ci = L.ci;
    if (idx > 0) {
        let o = ci.funcOff + idx;
        assert(idx <= ci.top - (ci.funcOff + 1), "unacceptable index");
        if (o >= L.top) return ldo.nil;
        else return L.stack[o];
    } else if (idx > lua.LUA_REGISTRYINDEX) {
        assert(idx !== 0 && -idx <= L.top, "invalid index");
        return L.stack[L.top + idx];
    } else if (idx === lua.LUA_REGISTRYINDEX) {
        return L.l_G.l_registry;
    } else { /* upvalues */
        idx = lua.LUA_REGISTRYINDEX - idx;
        assert(idx <= MAXUPVAL + 1, "upvalue index too large");
        if (ci.func.ttislcf()) /* light C function? */
            return ldo.nil; /* it has no upvalues */
        else {
            return idx <= ci.func.nupvalues ? ci.func.upvalue[idx - 1] : ldo.nil;
        }
    }
};

// Like index2addr but returns the index on stack
const index2addr_ = function(L, idx) {
    let ci = L.ci;
    if (idx > 0) {
        let o = ci.funcOff + idx;
        assert(idx <= ci.top - (ci.funcOff + 1), "unacceptable index");
        if (o >= L.top) return null;
        else return o;
    } else if (idx > lua.LUA_REGISTRYINDEX) {
        assert(idx !== 0 && -idx <= L.top, "invalid index");
        return L.top + idx;
    } else if (idx === lua.LUA_REGISTRYINDEX) {
        return null;
    } else { /* upvalues */
        idx = lua.LUA_REGISTRYINDEX - idx;
        assert(idx <= MAXUPVAL + 1, "upvalue index too large");
        if (ci.func.ttislcf()) /* light C function? */
            return null; /* it has no upvalues */
        else {
            return idx <= ci.func.nupvalues ? idx - 1 : null;
        }
    }
};

const lua_checkstack = function(L, n) {
    return L.stack.length < luaconf.LUAI_MAXSTACK;
};

const lua_xmove = function(from, to, n) {
    if (from === to) return;
    assert(n < (from.top - from.ci.funcOff), "not enough elements in the stack");
    assert(from.l_G === to.l_G, "moving among independent states");
    assert(to.ci.top - to.top >= n, "stack overflow");

    from.top -= n;
    for (let i = 0; i < n; i++) {
        to.stack[to.top] = from.stack[from.top + i];
        to.top++;
    }
};

/*
** basic stack manipulation
*/

/*
** convert an acceptable stack index into an absolute index
*/
const lua_absindex = function(L, idx) {
    return (idx > 0 || idx <= lua.LUA_REGISTRYINDEX)
         ? idx
         : (L.top - L.ci.funcOff) + idx;
};

const lua_gettop = function(L) {
    return L.top - (L.ci.funcOff + 1);
};

const lua_pushvalue = function(L, idx) {
    L.stack[L.top] = index2addr(L, idx);

    L.top++;
    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_settop = function(L, idx) {
    let func = L.ci.funcOff;
    if (idx >= 0) {
        while (L.top < func + 1 + idx)
            L.stack[L.top++] = ldo.nil;
        L.top = func + 1 + idx;
    } else {
        assert(-(idx + 1) <= L.top - (func + 1), "invalid new top");
        L.top += idx + 1; /* 'subtract' index (index is negative) */
    }
};

const lua_pop = function(L, n) {
    lua_settop(L, -n - 1);
}

const reverse = function(L, from, to) {
    for (; from < to; from++, to--) {
        let temp = L.stack[from];
        L.stack[from] = L.stack[to];
        L.stack[to] = temp;
    }
};

/*
** Let x = AB, where A is a prefix of length 'n'. Then,
** rotate x n == BA. But BA == (A^r . B^r)^r.
*/
const lua_rotate = function(L, idx, n) {
    let t = L.stack[L.top - 1];
    let p = index2addr(L, idx);
    let pIdx = index2addr_(L, idx);

    assert(!p.ttisnil() && idx > lua.LUA_REGISTRYINDEX, "index not in the stack");
    assert((n >= 0 ? n : -n) <= (L.top - idx), "invalid 'n'");

    let m = n >= 0 ? L.top - 1 - n : pIdx - n - 1;  /* end of prefix */

    reverse(L, pIdx, m);
    reverse(L, m + 1, L.top - 1);
    reverse(L, pIdx, L.top - 1);
};

const lua_copy = function(L, fromidx, toidx) {
    let fr = index2addr_(L, fromidx);
    let to = index2addr_(L, toidx);
    L.stack[to] = fr;
};

const lua_remove = function(L, idx) {
    lua_rotate(L, idx, -1);
    lua_pop(L, 1);
};

const lua_insert = function(L, idx) {
    lua_rotate(L, idx, 1);
};

/*
** push functions (JS -> stack)
*/

const lua_pushnil = function(L) {
    L.stack[L.top++] = ldo.nil;

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushnumber = function(L, n) {
    assert(typeof n === "number");

    L.stack[L.top++] = new TValue(CT.LUA_TNUMFLT, n);

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushinteger = function(L, n) {
    assert(typeof n === "number");

    L.stack[L.top++] = new TValue(CT.LUA_TNUMINT, n|0);

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushlstring = function(L, s, len) { // TODO: embedded \0
    assert(typeof s === "string");
    assert(typeof n === "number");

    let ts = len === 0 ? new TValue(CT.LUA_TLNGSTR, "") : new TValue(CT.LUA_TLNGSTR, s.substr(0, len));
    L.stack[L.top++] = ts;

    assert(L.top <= L.ci.top, "stack overflow");

    return ts.value;
};

const lua_pushstring = function (L, s) {
    assert(typeof s === "string");
    if (!s)
        L.stack[L.top] = ldo.nil;
    else {
        let ts = new TValue(CT.LUA_TLNGSTR, s);
        L.stack[L.top] = ts;
        s = ts.value;
    }

    L.top++;
    assert(L.top <= L.ci.top, "stack overflow");

    return s;
};

const lua_pushliteral = lua_pushstring;

const lua_pushcclosure = function(L, fn, n) {
    assert(typeof fn === "function");
    assert(typeof n === "number");

    if (n === 0)
        L.stack[L.top] = new TValue(CT.LUA_TLCF, fn);
    else {
        assert(n < L.top - L.ci.funcOff, "not enough elements in the stack");
        assert(n <= MAXUPVAL, "upvalue index too large");

        let cl = new CClosure(fn, n);

        L.top -= n;
        while (n--) {
            cl.upvalue[n] = L.stack[L.top + n];
        }

        L.stack[L.top] = cl;
    }

    L.top++;
    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushjsclosure = lua_pushcclosure;

const lua_pushcfunction = function(L, fn) {
    lua_pushcclosure(L, fn, 0);
};

const lua_pushjsfunction = lua_pushcfunction;

const lua_pushboolean = function(L, b) {
    L.stack[L.top++] = new TValue(CT.LUA_TBOOLEAN, b ? true : false);

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushlightuserdata = function(L, p) {
    assert(typeof p === "object");

    L.stack[L.top++] = new TValue(CT.LUA_TLIGHTUSERDATA, p);

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_pushglobaltable = function(L) {
    lua_rawgeti(L, lua.LUA_REGISTRYINDEX, lua.LUA_RIDX_GLOBALS);
};

/*
** set functions (stack -> Lua)
*/

/*
** t[k] = value at the top of the stack (where 'k' is a string)
*/
const auxsetstr = function(L, t, k) {
    let str = new TValue(CT.LUA_TLNGSTR, k);

    assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");

    if (t.ttistable() && !t.__index(t, k).ttisnil()) {
        t.__newindex(t, k, L.stack[L.top - 1]);
        L.top--; /* pop value */
    } else {
        L.stack[L.top++] = str;
        lvm.settable(L, t, L.stack[L.top - 1], L.stack[L.top - 2]);
        L.top -= 2; /* pop value and key */
    }
};

const lua_setglobal = function(L, name) {
    auxsetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS - 1), name);
};

const lua_setmetatable = function(L, objindex) {
    assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
    let mt;
    let obj = index2addr(L, objindex);
    if (L.stack[L.top - 1].ttisnil())
        mt = null;
    else {
        assert(L.stack[L.top - 1].ttistable(), "table expected");
        mt = L.stack[L.top - 1];
    }

    switch (obj.ttnov()) {
        case CT.LUA_TUSERDATA:
        case CT.LUA_TTABLE: {
            obj.metatable = mt;
            break;
        }
        default: {
            L.l_G.mt[obj.ttnov()] = mt;
            break;
        }
    }

    L.top--;
    return true;
};

const lua_settable = function(L, idx) {
    assert(2 < L.top - L.ci.funcOff, "not enough elements in the stack");

    let t = index2addr(L, idx);
    lvm.settable(L, t, L.stack[L.top - 2], L.stack[L.top - 1]);
    L.top -= 2;
};

const lua_setfield = function(L, idx, k) {
    auxsetstr(L, index2addr(L, idx), k)
};

const lua_rawset = function(L, idx) {
    assert(2 < L.top - L.ci.funcOff, "not enough elements in the stack");
    let o = index2addr(L, idx);
    assert(o.ttistable(), "table expected");
    o.__newindex(o, L.stack[L.top - 2], L.stack[L.top - 1]);
    L.top -= 2;
};

/*
** get functions (Lua -> stack)
*/

const auxgetstr = function(L, t, k) {
    let str = new TValue(CT.LUA_TLNGSTR, k);
    let slot = t.__index(t, k);
    if (t.ttistable() && !slot.ttisnil()) {
        L.stack[L.top++] = slot;
        assert(L.top <= L.ci.top, "stack overflow");
    } else {
        L.stack[L.top++] = str;
        assert(L.top <= L.ci.top, "stack overflow");
        lvm.gettable(L, t, L.stack[L.top - 1], L.top - 1);
    }

    return L.stack[L.top - 1].ttnov();
};

const lua_rawgeti = function(L, idx, n) {
    let t = index2addr(L, idx);

    assert(t.ttistable(), "table expected");

    L.stack[L.top++] = t.__index(t, n);

    assert(L.top <= L.ci.top, "stack overflow");

    return L.stack[L.top - 1].ttnov();
};

const lua_rawget = function(L, idx) {
    let t = index2addr(L, idx);

    assert(t.ttistable(t), "table expected");

    L.stack[L.top - 1] = t.__index(t, L.stack[L.top - 1]);

    return L.stack[L.top - 1].ttnov();
};

// narray and nrec are mostly useless for this implementation
const lua_createtable = function(L, narray, nrec) {
    let t = new lobject.Table();
    L.stack[L.top++] = t;

    assert(L.top <= L.ci.top, "stack overflow");
};

const lua_newtable = function(L) {
    lua_createtable(L, 0, 0);
};

const lua_getmetatable = function(L, objindex) {
    let obj = index2addr(L, objindex);
    let mt;
    let res = false;
    switch (obj.ttnov()) {
        case CT.LUA_TTABLE:
        case CT.LUA_TUSERDATA:
            mt = obj.metatable;
            break;
        default:
            mt = L.l_G.mt[obj.ttnov];
            break;
    }

    if (mt !== null && mt !== undefined) {
        L.stack[L.top++] = mt;
        assert(L.top <= L.ci.top, "stack overflow");
        res = true;
    }

    return res;
};

const lua_gettable = function(L, idx) {
    let t = index2addr(L, idx);
    lvm.gettable(L, t, L.stack[L.top - 1], L.top - 1);
    return L.stack[L.top - 1].ttnov();
};

const lua_getfield = function(L, idx, k) {
    return auxgetstr(L, index2addr(L, idx), k);
};

const lua_geti = function(L, idx, n) {
    let t = index2addr(L, idx);
    let slot = t.__index(t, n);
    if (!slot.ttisnil()) {
        L.stack[L.top++] = slot;
        assert(L.top <= L.ci.top, "stack overflow");
    } else {
        L.stack[L.top++] = new TValue(CT.LUA_TNUMINT, n);
        assert(L.top <= L.ci.top, "stack overflow");
        lvm.gettable(L, t, L.stack[L.top - 1], L.top - 1);
    }
    return L.stack[L.top - 1].ttnov();
};

const lua_getglobal = function(L, name) {
    return auxgetstr(L, L.l_G.l_registry.value.get(lua.LUA_RIDX_GLOBALS - 1), name);
};

/*
** access functions (stack -> JS)
*/

const lua_toboolean = function(L, idx) {
    let o = index2addr(L, idx);
    return !o.l_isfalse();
};

const lua_tolstring = function(L, idx) {
    let o = index2addr(L, idx);

    if (!o.ttisstring() && !o.ttisnumber())
        return null;

    return `${o.value}`;
};

const lua_tostring =  lua_tolstring;

const lua_rawlen = function(L, idx) {
    let o = index2addr(L, idx);
    switch (o.ttype()) {
        case CT.LUA_TSHRSTR:
        case CT.LUA_TLNGSTR:
            return o.value.length;
        case CT.LUA_TUSERDATA:
            return o.len;
        case CT.LUA_TTABLE:
            return o.luaH_getn();
        default:
            return 0;
    }
};

const lua_tointeger = function(L, idx) {
    return lvm.tointeger(index2addr(L, idx))
};

const lua_tonumber = function(L, idx) {
    return lvm.tonumber(index2addr(L, idx))
};

const lua_tothread = function(L, idx) {
    let o = index2addr(L, idx);
    return o.ttisthread() ? o.value : null;
};

const lua_topointer = function(L, idx) {
    let o = index2addr(L, idx);
    switch (o.ttype()) {
        case CT.LUA_TTABLE:
        case CT.LUA_TLCL:
        case CT.LUA_TCCL:
        case CT.LUA_TLCF:
        case CT.LUA_TTHREAD:
        case CT.LUA_TUSERDATA:
        case CT.LUA_TLIGHTUSERDATA:
            return o.value;
        default:
            return null;
    }
};

const lua_stringtonumber = function(L, s) {
    let number = parseFloat(s);
    L.stack[L.top++] = new TValue(number % 1 !== 0 ? CT.LUA_TNUMFLT : CT.LUA_TNUMINT, number);
    assert(L.top <= L.ci.top, "stack overflow");
    return s.length;
};

const f_call = function(L, ud) {
    ldo.luaD_callnoyield(L, ud.funcOff, ud.nresults);
};

const lua_type = function(L, idx) {
    let o = index2addr(L, idx);
    return o.ttnov(); // TODO: isvalid ? luaO_nilobject != nil tvalue ?
};

const lua_typename = function(L, t) {
    assert(CT.LUA_TNONE <= t && t < CT.LUA_NUMTAGS, "invalid tag");
    return ltm.ttypename(t);
};

const lua_istable = function(L, idx) {
    return index2addr(L, idx).ttistable();
};

const lua_isstring = function(L, idx) {
    let o = index2addr(L, idx);
    return o.ttisstring() || o.ttisnumber();
};

const lua_rawequal = function(L, index1, index2) {
    let o1 = index2addr(L, index1);
    let o2 = index2addr(L, index2);
    return lvm.luaV_equalobj(null, o1, o2); // TODO: isvalid ?
};

/*
** 'load' and 'call' functions (run Lua code)
*/

const lua_load = function(L, data, chunckname) {
    if (!chunckname) chunckname = "?";
    
    let status = ldo.luaD_protectedparser(L, data, chunckname);
    if (status === TS.LUA_OK) {
        let f = L.stack[L.top - 1]; /* get newly created function */
        if (f.nupvalues >= 1) { /* does it have an upvalue? */
            /* get global table from registry */
            let reg = L.l_G.l_registry;
            let gt = reg.value.get(lua.LUA_RIDX_GLOBALS - 1);
            /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
            f.upvals[0].u.value = gt;
        }
    }

    return status;
};

const lua_status = function(L) {
    return L.status;
};

const lua_callk = function(L, nargs, nresults, ctx, k) {
    assert(k === null || !(L.ci.callstatus & lstate.CIST_LUA), "cannot use continuations inside hooks");
    assert(nargs + 1 < L.top - L.ci.funcOff, "not enough elements in the stack");
    assert(L.status === TS.LUA_OK, "cannot do calls on non-normal thread");
    assert(nargs === lua.LUA_MULTRET || (L.ci.top - L.top >= nargs - nresults, "results from function overflow current stack size"));

    let func = L.top - (nargs + 1);
    if (k !== null && L.nny === 0) { /* need to prepare continuation? */
        L.ci.u.c.k = k;
        L.ci.u.c.ctx = ctx;
        ldo.luaD_call(L, func, nresults);
    } else { /* no continuation or no yieldable */
        ldo.luaD_callnoyield(L, func, nresults);
    }

    if (nresults == lua.LUA_MULTRET && L.ci.top < L.top)
        L.ci.top = L.top;
};

const lua_call = function(L, n, r) {
    lua_callk(L, n, r, 0, null);
};

const lua_pcallk = function(L, nargs, nresults, errfunc, ctx, k) {
    assert(nargs + 1 < L.top - L.ci.funcOff, "not enough elements in the stack");
    assert(L.status === TS.LUA_OK, "cannot do calls on non-normal thread");
    assert(nargs === lua.LUA_MULTRET || (L.ci.top - L.top >= nargs - nresults, "results from function overflow current stack size"));

    let c = {
        func: null,
        funcOff: NaN,
        nresults: NaN
    };
    let status;
    let func;

    if (errfunc === 0)
        func = 0;
    else {
        // let o = index2addr(L, errfunc);
        // TODO: api_checkstackindex(L, errfunc, o);
        func = index2addr_(L, errfunc);
    }

    c.funcOff = L.top - (nargs + 1); /* function to be called */
    c.func = L.stack[c.funcOff];

    if (k === null || L.nny > 0) { /* no continuation or no yieldable? */
        c.nresults = nresults; /* do a 'conventional' protected call */
        status = ldo.luaD_pcall(L, f_call, c, c.funcOff, func);
    } else { /* prepare continuation (call is already protected by 'resume') */
        let ci = L.ci;
        ci.u.c.k = k;  /* prepare continuation (call is already protected by 'resume') */
        ci.u.c.ctx = ctx;  /* prepare continuation (call is already protected by 'resume') */
        /* save information for error recovery */
        ci.extra = c.funcOff;
        ci.u.c.old_errfunc = L.errfunc;
        L.errfunc = func;
        // TODO: setoah(ci->callstatus, L->allowhook);
        ci.callstatus |= lstate.CIST_YPCALL;  /* function can do error recovery */
        ldo.luaD_call(L, c.funcOff, nresults);  /* do the call */
        ci.callstatus &= ~lstate.CIST_YPCALL;
        L.errfunc = ci.u.c.old_errfunc;
        status = TS.LUA_OK;
    }

    if (nresults == lua.LUA_MULTRET && L.ci.top < L.top)
        L.ci.top = L.top;

    return status;
};

const lua_pcall = function(L, n, r, f) {
    return lua_pcallk(L, n, r, f, 0, null);
};

/*
** miscellaneous functions
*/

const lua_error = function(L) {
    assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
    ldebug.luaG_errormsg(L);
};

const lua_next = function(L, idx) {
    let t = index2addr(L, idx);
    assert(t.ttistable(), "table expected");
    let more = t.luaH_next(L, L.top - 1);
    if (more) {
        L.top++;
        assert(L.top <= L.ci.top, "stack overflow");
    } else
        L.top--;

    return more;
};

const lua_concat = function(L, n) {
    assert(n < L.top - L.ci.funcOff, "not enough elements in the stack");
    if (n >= 2)
        lvm.luaV_concat(L, n);
    else if (n === 0) {
        L.stack[L.top++] = new TValue("", CT.LUA_TLNGSTR);
        assert(L.top <= L.ci.top, "stack overflow");
    }
};

// This functions are only there for compatibility purposes
const lua_gc = function () {};

const lua_getallocf = function () {
    console.warn("lua_getallocf is not available and will always return null");
    return null;
};
const lua_getextraspace = function () {
    console.warn("lua_getextraspace is not available and will always return null");
    return null;
};

module.exports.index2addr          = index2addr;
module.exports.lua_absindex        = lua_absindex;
module.exports.lua_atpanic         = lua_atpanic;
module.exports.lua_call            = lua_call;
module.exports.lua_callk           = lua_callk;
module.exports.lua_checkstack      = lua_checkstack;
module.exports.lua_concat          = lua_concat;
module.exports.lua_copy            = lua_copy;
module.exports.lua_createtable     = lua_createtable;
module.exports.lua_error           = lua_error;
module.exports.lua_gc              = lua_gc;
module.exports.lua_getallocf       = lua_getallocf;
module.exports.lua_getextraspace   = lua_getextraspace;
module.exports.lua_getfield        = lua_getfield;
module.exports.lua_getglobal       = lua_getglobal;
module.exports.lua_geti            = lua_geti;
module.exports.lua_getmetatable    = lua_getmetatable;
module.exports.lua_gettable        = lua_gettable;
module.exports.lua_gettop          = lua_gettop;
module.exports.lua_insert          = lua_insert;
module.exports.lua_isstring        = lua_isstring;
module.exports.lua_istable         = lua_istable;
module.exports.lua_load            = lua_load;
module.exports.lua_newtable        = lua_newtable;
module.exports.lua_next            = lua_next;
module.exports.lua_pcall           = lua_pcall;
module.exports.lua_pcallk          = lua_pcallk;
module.exports.lua_pop             = lua_pop;
module.exports.lua_pushboolean     = lua_pushboolean;
module.exports.lua_pushcclosure    = lua_pushcclosure;
module.exports.lua_pushcfunction   = lua_pushcfunction;
module.exports.lua_pushglobaltable = lua_pushglobaltable;
module.exports.lua_pushinteger     = lua_pushinteger;
module.exports.lua_pushjsclosure   = lua_pushjsclosure;
module.exports.lua_pushjsfunction  = lua_pushjsfunction;
module.exports.lua_pushliteral     = lua_pushliteral;
module.exports.lua_pushlstring     = lua_pushlstring;
module.exports.lua_pushnil         = lua_pushnil;
module.exports.lua_pushnumber      = lua_pushnumber;
module.exports.lua_pushstring      = lua_pushstring;
module.exports.lua_pushvalue       = lua_pushvalue;
module.exports.lua_rawequal        = lua_rawequal;
module.exports.lua_rawget          = lua_rawget;
module.exports.lua_rawgeti         = lua_rawgeti;
module.exports.lua_rawlen          = lua_rawlen;
module.exports.lua_rawset          = lua_rawset;
module.exports.lua_remove          = lua_remove;
module.exports.lua_rotate          = lua_rotate;
module.exports.lua_setfield        = lua_setfield;
module.exports.lua_setglobal       = lua_setglobal;
module.exports.lua_setmetatable    = lua_setmetatable;
module.exports.lua_settable        = lua_settable;
module.exports.lua_settop          = lua_settop;
module.exports.lua_status          = lua_status;
module.exports.lua_stringtonumber  = lua_stringtonumber;
module.exports.lua_toboolean       = lua_toboolean;
module.exports.lua_tointeger       = lua_tointeger;
module.exports.lua_tolstring       = lua_tolstring;
module.exports.lua_tonumber        = lua_tonumber;
module.exports.lua_topointer       = lua_topointer;
module.exports.lua_tostring        = lua_tostring;
module.exports.lua_tothread        = lua_tothread;
module.exports.lua_type            = lua_type;
module.exports.lua_typename        = lua_typename;
module.exports.lua_version         = lua_version;
module.exports.lua_xmove           = lua_xmove;