aboutsummaryrefslogtreecommitdiff
path: root/test/test-suite/api.test.js
blob: 152ddcfaa6c90f75f0314090db26e86237b6b4a5 (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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
"use strict";

const lua = require('../../src/lua.js');
const lauxlib = require('../../src/lauxlib.js');
const lualib = require('../../src/lualib.js');
const {to_luastring} = require("../../src/fengaricore.js");

const ltests = require('./ltests.js');

// TODO: a lot of gc related tests are skipped
// TODO: io.read is used in several tests, uncomment them when it's implemented

const prefix = `
    local pack = table.pack


    function tcheck (t1, t2)
      assert(t1.n == (t2.n or #t2) + 1)
      for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
    end


    local function checkerr (msg, f, ...)
      local stat, err = pcall(f, ...)
      assert(not stat and string.find(err, msg))
    end
`;

test("[test-suite] api: registry", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a = T.testC("pushvalue R; return 1")
        assert(a == debug.getregistry())
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: absindex", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(T.testC("settop 10; absindex -1; return 1") == 10)
        assert(T.testC("settop 5; absindex -5; return 1") == 1)
        assert(T.testC("settop 10; absindex 1; return 1") == 1)
        assert(T.testC("settop 10; absindex R; return 1") < -10)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing alignment", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        -- Useless tests in fengari since we do the same thing in d2s than in string.pack
        a = T.d2s(12458954321123.0)
        assert(a == string.pack("d", 12458954321123.0))
        assert(T.s2d(a) == 12458954321123.0)

        a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
        assert(a == 2 and b == 3 and not c)

        f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
        a,b,c = f()
        assert(a == 2 and b == 3 and not c)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: test that all trues are equal", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
        assert(a == b and a == true and c == false)
        a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\\
                              tobool -3; tobool -3; tobool -3; return 3"
        assert(a==false and b==true and c==false)


        a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
        assert(a == 40 and b == 5 and not c)

        t = pack(T.testC("settop 5; return *", 2, 3))
        tcheck(t, {n=4,2,3})

        t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
        assert(t.n == 10 and t[1] == nil and t[10] == nil)

        t = pack(T.testC("remove -2; return *", 2, 3, 4))
        tcheck(t, {n=2,2,4})

        t = pack(T.testC("insert -1; return *", 2, 3))
        tcheck(t, {n=2,2,3})

        t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
        tcheck(t, {n=4,2,5,3,4})

        t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
        tcheck(t, {n=3,5,3,4})

        t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
        tcheck(t, {n=3,2,3,5})

        t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
        tcheck(t, {n=3,2,4,5})

        t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
        tcheck(t, {n=4,2,3,3,5})

        t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
        tcheck(t, {n=4,2,3,4,3})
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing 'rotate'", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do   -- testing 'rotate'
          local t = {10, 20, 30, 40, 50, 60}
          for i = -6, 6 do
            local s = string.format("rotate 2 %d; return 7", i)
            local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
            tcheck(t1, t)
            table.insert(t, 1, table.remove(t))
          end

          t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40))
          tcheck(t, {10, 20, 40, 30})
          t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40))
          tcheck(t, {10, 20, 40, 30})

          -- some corner cases
          t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40))
          tcheck(t, {10, 20, 30, 40})
          t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40))
          tcheck(t, {10, 20, 30, 40})
          t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40))
          tcheck(t, {10, 20, 30, 40})
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing non-function message handlers", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local f = T.makeCfunc[[
            getglobal error
            pushstring bola
            pcall 1 1 1   # call 'error' with given handler
            pushstatus
            return 2     # return error message and status
          ]]

          local msg, st = f({})     -- invalid handler
          assert(st == "ERRERR" and string.find(msg, "error handling"))
          local msg, st = f(nil)     -- invalid handler
          assert(st == "ERRERR" and string.find(msg, "error handling"))

          local a = setmetatable({}, {__call = function (_, x) return x:upper() end})
          local msg, st = f(a)   -- callable handler
          assert(st == "ERRRUN" and msg == "BOLA")
        end

        t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \\
                          insert 2; pushvalue 1; remove 1; insert 1; \\
              insert -2; pushvalue -2; remove -3; return *",
              2, 3, 4, 5, 10, 40, 90))
        tcheck(t, {n=7,2,3,4,5,10,40,90})

        t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
        tcheck(t, {n=1,"alo23joao12"})
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing MULTRET", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        t = pack(T.testC("call 2,-1; return *",
             function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
        tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: test returning more results than fit in the caller stack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do  -- test returning more results than fit in the caller stack
          local a = {}
          for i=1,1000 do a[i] = true end; a[999] = 10
          local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
                            table.unpack, a)
          assert(b == "10")
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing globals", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        _G.a = 14; _G.b = "a31"
        local a = {T.testC[[
          getglobal a;
          getglobal b;
          getglobal b;
          setglobal a;
          return *
        ]]}
        assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing arith", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
        assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
        assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
        assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
        assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
        assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
        assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
        assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
        assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
        a = T.testC("pushnum 10; pushint 3; arith \\\\; return 1")
        assert(a == 3.0 and math.type(a) == "float")
        a = T.testC("pushint 10; pushint 3; arith \\\\; return 1")
        assert(a == 3 and math.type(a) == "integer")
        a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
        assert(a == 13 and math.type(a) == "integer")
        a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
        assert(a == 13 and math.type(a) == "float")
        a,b,c = T.testC([[pushnum 1;
                          pushstring 10; arith _;
                          pushstring 5; return 3]])
        assert(a == 1 and b == -10 and c == "5")
        mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
              __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
              __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
        a,b,c = setmetatable({4}, mt),
                setmetatable({8}, mt),
                setmetatable({-3}, mt)
        x,y,z = T.testC("arith +; return 2", 10, a, b)
        assert(x == 10 and y[1] == 12 and z == nil)
        assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
        assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
                       8 % (4 + (-3)*2))
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: errors in arithmetic", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        checkerr("divide by zero", T.testC, "arith \\\\", 10, 0)
        checkerr("%%0", T.testC, "arith %", 10, 0)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing lessthan and lessequal", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
        assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
        assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
        assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
        assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
        assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
        assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: non-valid indices produce false", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(not T.testC("compare LT 1 4, return 1"))
        assert(not T.testC("compare LE 9 1, return 1"))
        assert(not T.testC("compare EQ 9 9, return 1"))

        local b = {__lt = function (a,b) return a[1] < b[1] end}
        local a1,a3,a4 = setmetatable({1}, b),
                         setmetatable({3}, b),
                         setmetatable({4}, b)
        assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
        assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
        assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
        a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
        assert(a == 20 and b == false)
        a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
        assert(a == 20 and b == false)
        a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
        assert(a == 20 and b == true)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing length", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
        a,b,c = T.testC([[
           len 2;
           Llen 2;
           objsize 2;
           return 3
        ]], t)
        assert(a == 20 and b == 20 and c == 0)

        t.x = "234"; t[1] = 20
        a,b,c = T.testC([[
           len 2;
           Llen 2;
           objsize 2;
           return 3
        ]], t)
        assert(a == "234" and b == 234 and c == 1)

        t.x = print; t[1] = 20
        a,c = T.testC([[
           len 2;
           objsize 2;
           return 2
        ]], t)
        assert(a == print and c == 1)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing __concat", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
        x,y = T.testC([[
          pushnum 5
          pushvalue 2;
          pushvalue 2;
          concat 2;
          pushvalue -2;
          return 2;
        ]], a, a)
        assert(x == a..a and y == 5)

        -- concat with 0 elements
        assert(T.testC("concat 0; return 1") == "")

        -- concat with 1 element
        assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing lua_is", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function B(x) return x and 1 or 0 end

        function count (x, n)
          n = n or 2
          local prog = [[
            isnumber %d;
            isstring %d;
            isfunction %d;
            iscfunction %d;
            istable %d;
            isuserdata %d;
            isnil %d;
            isnull %d;
            return 8
          ]]
          prog = string.format(prog, n, n, n, n, n, n, n, n)
          local a,b,c,d,e,f,g,h = T.testC(prog, x)
          return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
        end

        assert(count(3) == 2)
        assert(count('alo') == 1)
        assert(count('32') == 2)
        assert(count({}) == 1)
        assert(count(print) == 2)
        assert(count(function () end) == 1)
        assert(count(nil) == 1)
        assert(count(io.stdin) == 1)
        assert(count(nil, 15) == 100)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing lua_to...", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function to (s, x, n)
          n = n or 2
          return T.testC(string.format("%s %d; return 1", s, n), x)
        end

        local hfunc = string.gmatch("", "")    -- a "heavy C function" (with upvalues)
        assert(debug.getupvalue(hfunc, 1))
        assert(to("tostring", {}) == nil)
        assert(to("tostring", "alo") == "alo")
        assert(to("tostring", 12) == "12")
        assert(to("tostring", 12, 3) == nil)
        assert(to("objsize", {}) == 0)
        assert(to("objsize", {1,2,3}) == 3)
        assert(to("objsize", "alo\\0\\0a") == 6)
        assert(to("objsize", T.newuserdata(0)) == 0)
        assert(to("objsize", T.newuserdata(101)) == 101)
        assert(to("objsize", 124) == 0)
        assert(to("objsize", true) == 0)
        assert(to("tonumber", {}) == 0)
        assert(to("tonumber", "12") == 12)
        assert(to("tonumber", "s2") == 0)
        assert(to("tonumber", 1, 20) == 0)
        assert(to("topointer", 10) == 0)
        assert(to("topointer", true) == 0)
        assert(to("topointer", T.pushuserdata(20)) == 20)
        --assert(to("topointer", io.read) ~= 0)           -- light C function
        assert(to("topointer", hfunc) ~= 0)        -- "heavy" C function
        assert(to("topointer", function () end) ~= 0)   -- Lua function
        assert(to("topointer", io.stdin) ~= 0)   -- full userdata
        assert(to("func2num", 20) == 0)
        assert(to("func2num", T.pushuserdata(10)) == 0)
        -- assert(to("func2num", io.read) ~= 0)     -- light C function
        assert(to("func2num", hfunc) ~= 0)  -- "heavy" C function (with upvalue)
        a = to("tocfunction", math.deg)
        assert(a(3) == math.deg(3) and a == math.deg)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});

test("[test-suite] api: testing panic function", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          -- trivial error
          assert(T.checkpanic("pushstring hi; error") == "hi")

          -- using the stack inside panic
          assert(T.checkpanic("pushstring hi; error;",
            [[checkstack 5 XX
              pushstring ' alo'
              pushstring ' mundo'
              concat 3]]) == "hi alo mundo")

          -- "argerror" without frames
          assert(T.checkpanic("loadstring 4") ==
              "bad argument #4 (string expected, got no value)")


          --[[ TODO: T.totalmem
          -- memory error
          T.totalmem(T.totalmem()+10000)   -- set low memory limit (+10k)
          assert(T.checkpanic("newuserdata 20000") == "not enough memory")
          T.totalmem(0)          -- restore high limit
          ]]

          -- stack error
          if not _soft then
            local msg = T.checkpanic[[
              pushstring "function f() f() end"
              loadstring -1; call 0 0
              getglobal f; call 0 0
            ]]
            assert(string.find(msg, "stack overflow"))
          end

        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing deep JS stack", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          checkerr("XXXX", T.testC, "checkstack 1000023 XXXX")   -- too deep
          -- too deep (with no message)
          checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
          local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
          checkerr("overflow", T.testC, s)
        end

        local lim = _soft and 500 or 12000
        local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
        for i = 1,lim do
          prog[#prog + 1] = "pushnum " .. i
          prog[#prog + 1] = "pushnum " .. i * 10
        end

        prog[#prog + 1] = "rawgeti R 2"   -- get global table in registry
        prog[#prog + 1] = "insert " .. -(2*lim + 2)

        for i = 1,lim do
          prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
        end

        prog[#prog + 1] = "return 2"

        prog = table.concat(prog, ";")
        local g, t = T.testC(prog)
        assert(g == _G)
        for i = 1,lim do assert(t[i] == i*10); t[i] = nil end
        assert(next(t) == nil)
        prog, g, t = nil
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing errors", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a = T.testC([[
          loadstring 2; pcall 0 1 0;
          pushvalue 3; insert -2; pcall 1 1 0;
          pcall 0 0 0;
          return 1
        ]], "x=150", function (a) assert(a==nil); return 3 end)

        assert(type(a) == 'string' and x == 150)

        function check3(p, ...)
          local arg = {...}
          assert(#arg == 3)
          assert(string.find(arg[3], p))
        end
        check3(":1:", T.testC("loadstring 2; return *", "x="))
        check3("%.", T.testC("loadfile 2; return *", "."))
        check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: test errors in non protected threads", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function checkerrnopro (code, msg)
          local th = coroutine.create(function () end)  -- create new thread
          local stt, err = pcall(T.testC, th, code)   -- run code there
          assert(not stt and string.find(err, msg))
        end

        if not _soft then
          checkerrnopro("pushnum 3; call 0 0", "attempt to call")
          print"testing stack overflow in unprotected thread"
          function f () f() end
          checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing table access", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do   -- getp/setp
          local a = {}
          T.testC("rawsetp 2 1", a, 20)
          assert(a[T.pushuserdata(1)] == 20)
          assert(T.testC("rawgetp 2 1; return 1", a) == 20)
        end

        a = {x=0, y=12}
        x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
                        a, 3, "y", 4, "x")
        assert(x == 0 and y == 12)
        T.testC("settable -5", a, 3, 4, "x", 15)
        assert(a.x == 15)
        a[a] = print
        x = T.testC("gettable 2; return 1", a)  -- table and key are the same object!
        assert(x == print)
        T.testC("settable 2", a, "x")    -- table and key are the same object!
        assert(a[a] == "x")

        b = setmetatable({p = a}, {})
        getmetatable(b).__index = function (t, i) return t.p[i] end
        k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
        assert(x == 15 and k == 35)
        k = T.testC("getfield 2 y, return 1", b)
        assert(k == 12)
        getmetatable(b).__index = function (t, i) return a[i] end
        getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
        y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
        assert(y == 12)
        k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
        assert(a.x == 16 and k == 4)
        a[b] = 'xuxu'
        y = T.testC("gettable 2, return 1", b)
        assert(y == 'xuxu')
        T.testC("settable 2", b, 19)
        assert(a[b] == 19)

    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing getfield/setfield with long keys", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do   -- testing getfield/setfield with long keys
          local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
          local a = T.testC([[
            getfield 2 _012345678901234567890123456789012345678901234567890123456789
            return 1
          ]], t)
          assert(a == 32)
          local a = T.testC([[
            pushnum 33
            setglobal _012345678901234567890123456789012345678901234567890123456789
          ]])
          assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
          _012345678901234567890123456789012345678901234567890123456789 = nil
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing next", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        a = {}
        t = pack(T.testC("next; return *", a, nil))
        tcheck(t, {n=1,a})
        a = {a=3}
        t = pack(T.testC("next; return *", a, nil))
        tcheck(t, {n=3,a,'a',3})
        t = pack(T.testC("next; pop 1; next; return *", a, nil))
        tcheck(t, {n=1,a})
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing upvalues", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
          t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
          assert(b == 10 and c == 20 and type(t) == 'table')
          a, b = A([[tostring U3; tonumber U4; return 2]])
          assert(a == nil and b == 0)
          A([[pushnum 100; pushnum 200; replace U2; replace U1]])
          b, c = A([[pushvalue U1; pushvalue U2; return 2]])
          assert(b == 100 and c == 200)
          A([[replace U2; replace U1]], {x=1}, {x=2})
          b, c = A([[pushvalue U1; pushvalue U2; return 2]])
          assert(b.x == 1 and c.x == 2)
          -- T.checkmemory()
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing absent upvalues from JS-function pointers", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(T.testC[[isnull U1; return 1]] == true)
        assert(T.testC[[isnull U100; return 1]] == true)
        assert(T.testC[[pushvalue U1; return 1]] == nil)

        local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
        assert(T.upvalue(f, 1) == 10 and
               T.upvalue(f, 2) == 20 and
               T.upvalue(f, 3) == nil)
        T.upvalue(f, 2, "xuxu")
        assert(T.upvalue(f, 2) == "xuxu")
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: large closures", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local A = "checkstack 300 msg;" ..
                    string.rep("pushnum 10;", 255) ..
                    "pushcclosure 255; return 1"
          A = T.testC(A)
          for i=1,255 do
            assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
          end
          assert(A("isnull U256; return 1"))
          assert(not A("isnil U256; return 1"))
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing get/setuservalue", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        -- bug in 5.1.2
        checkerr("got number", debug.setuservalue, 3, {})
        checkerr("got nil", debug.setuservalue, nil, {})
        checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})

        local b = T.newuserdata(0)
        assert(debug.getuservalue(b) == nil)
        for _, v in pairs{true, false, 4.56, print, {}, b, "XYZ"} do
          assert(debug.setuservalue(b, v) == b)
          assert(debug.getuservalue(b) == v)
        end

        assert(debug.getuservalue(4) == nil)

        debug.setuservalue(b, function () return 10 end)
        -- collectgarbage()   -- function should not be collected
        assert(debug.getuservalue(b)() == 10)

        debug.setuservalue(b, 134)
        -- collectgarbage()   -- number should not be a problem for collector
        assert(debug.getuservalue(b) == 134)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing get/setuservalue", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        T.gcstate("atomic")
        assert(T.gccolor(b) == "black")
        debug.setuservalue(b, {x = 100})
        T.gcstate("pause")  -- complete collection
        assert(debug.getuservalue(b).x == 100)  -- uvalue should be there
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: long chain of userdata", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        for i = 1, 1000 do
          local bb = T.newuserdata(0)
          debug.setuservalue(bb, b)
          b = bb
        end
        -- collectgarbage()     -- nothing should not be collected
        for i = 1, 1000 do
          b = debug.getuservalue(b)
        end
        assert(debug.getuservalue(b).x == 100)
        b = nil
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: reuse of references", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local i = T.ref{}
        T.unref(i)
        assert(T.ref{} == i)

        Arr = {}
        Lim = 100
        for i=1,Lim do   -- lock many objects
          Arr[i] = T.ref({})
        end

        assert(T.ref(nil) == -1 and T.getref(-1) == nil)
        T.unref(-1); T.unref(-1)

        for i=1,Lim do   -- unlock all them
          T.unref(Arr[i])
        end

        function printlocks ()
          local f = T.makeCfunc("gettable R; return 1")
          local n = f("n")
          print("n", n)
          for i=0,n do
            print(i, f(i))
          end
        end


        for i=1,Lim do   -- lock many objects
          Arr[i] = T.ref({})
        end

        for i=1,Lim,2 do   -- unlock half of them
          T.unref(Arr[i])
        end

        assert(type(T.getref(Arr[2])) == 'table')


        assert(T.getref(-1) == nil)


        a = T.ref({})

        -- collectgarbage()

        assert(type(T.getref(a)) == 'table')
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: collect in cl the `val' of all collected userdata", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        tt = {}
        cl = {n=0}
        A = nil; B = nil
        local F
        F = function (x)
          local udval = T.udataval(x)
          table.insert(cl, udval)
          local d = T.newuserdata(100)   -- cria lixo
          d = nil
          assert(debug.getmetatable(x).__gc == F)
          assert(load("table.insert({}, {})"))()   -- cria mais lixo
          collectgarbage()   -- forca coleta de lixo durante coleta!
          assert(debug.getmetatable(x).__gc == F)   -- coleta anterior nao melou isso?
          local dummy = {}    -- cria lixo durante coleta
          if A ~= nil then
            assert(type(A) == "userdata")
            assert(T.udataval(A) == B)
            debug.getmetatable(A)    -- just acess it
          end
          A = x   -- ressucita userdata
          B = udval
          return 1,2,3
        end
        tt.__gc = F

        collectgarbage("stop")

        -- create 3 userdatas with tag 'tt'
        a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
        b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
        c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)

        -- create userdata without meta table
        x = T.newuserdata(4)
        y = T.newuserdata(0)

        checkerr("FILE%* expected, got userdata", io.input, a)
        checkerr("FILE%* expected, got userdata", io.input, x)

        assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)

        d=T.ref(a);
        e=T.ref(b);
        f=T.ref(c);
        t = {T.getref(d), T.getref(e), T.getref(f)}
        assert(t[1] == a and t[2] == b and t[3] == c)

        t=nil; a=nil; c=nil;
        T.unref(e); T.unref(f)

        collectgarbage()

        -- check that unref objects have been collected
        assert(#cl == 1 and cl[1] == nc)

        x = T.getref(d)
        assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
        x =nil
        tt.b = b  -- create cycle
        tt=nil    -- frees tt for GC
        A = nil
        b = nil
        T.unref(d);
        n5 = T.newuserdata(0)
        debug.setmetatable(n5, {__gc=F})
        n5 = T.udataval(n5)
        collectgarbage()
        assert(#cl == 4)
        -- check order of collection
        assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)

        collectgarbage"restart"


        a, na = {}, {}
        for i=30,1,-1 do
          a[i] = T.newuserdata(0)
          debug.setmetatable(a[i], {__gc=F})
          na[i] = T.udataval(a[i])
        end
        cl = {}
        a = nil; collectgarbage()
        assert(#cl == 30)
        for i=1,30 do assert(cl[i] == na[i]) end
        na = nil


        for i=2,Lim,2 do   -- unlock the other half
          T.unref(Arr[i])
        end

        x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
        assert(T.testC("objsize 2; return 1", x) == 41)
        cl = {}
        a = {[x] = 1}
        x = T.udataval(x)
        collectgarbage()
        -- old 'x' cannot be collected ('a' still uses it)
        assert(#cl == 0)
        for n in pairs(a) do a[n] = nil end
        collectgarbage()
        assert(#cl == 1 and cl[1] == x)   -- old 'x' must be collected
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: test whether udate collection frees memory in the right time", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          collectgarbage();
          collectgarbage();
          local x = collectgarbage("count");
          local a = T.newuserdata(5001)
          assert(T.testC("objsize 2; return 1", a) == 5001)
          assert(collectgarbage("count") >= x+4)
          a = nil
          collectgarbage();
          assert(collectgarbage("count") <= x+1)
          -- udata without finalizer
          x = collectgarbage("count")
          collectgarbage("stop")
          for i=1,1000 do T.newuserdata(0) end
          assert(collectgarbage("count") > x+10)
          collectgarbage()
          assert(collectgarbage("count") <= x+1)
          -- udata with finalizer
          collectgarbage()
          x = collectgarbage("count")
          collectgarbage("stop")
          a = {__gc = function () end}
          for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
          assert(collectgarbage("count") >= x+10)
          collectgarbage()  -- this collection only calls TM, without freeing memory
          assert(collectgarbage("count") >= x+10)
          collectgarbage()  -- now frees memory
          assert(collectgarbage("count") <= x+1)
          collectgarbage("restart")
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing lua_equal", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
        assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
        assert(T.testC("compare EQ 2 3; return 1", nil, nil))
        assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
        assert(not T.testC("compare EQ 2 3; return 1"))
        assert(not T.testC("compare EQ 2 3; return 1", 3))
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing lua_equal with fallbacks", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local map = {}
          local t = {__eq = function (a,b) return map[a] == map[b] end}
          local function f(x)
            local u = T.newuserdata(0)
            debug.setmetatable(u, t)
            map[u] = x
            return u
          end
          assert(f(10) == f(10))
          assert(f(10) ~= f(11))
          assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
          assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
          t.__eq = nil
          assert(f(10) ~= f(10))
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing changing hooks during hooks", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        _G.t = {}
        T.sethook([[
          # set a line hook after 3 count hooks
          sethook 4 0 '
            getglobal t;
            pushvalue -3; append -2
            pushvalue -2; append -2
          ']], "c", 3)
        local a = 1   -- counting
        a = 1   -- counting
        a = 1   -- count hook (set line hook)
        a = 1   -- line hook
        a = 1   -- line hook
        debug.sethook()
        t = _G.t
        assert(t[1] == "line")
        line = t[2]
        assert(t[3] == "line" and t[4] == line + 1)
        assert(t[5] == "line" and t[6] == line + 2)
        assert(t[7] == nil)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing errors during GC", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do   -- testing errors during GC
          local a = {}
          for i=1,20 do
            a[i] = T.newuserdata(i)   -- creates several udata
          end
          for i=1,20,2 do   -- mark half of them to raise errors during GC
            debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end})
          end
          for i=2,20,2 do   -- mark the other half to count and to create more garbage
            debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
          end
          _G.A = 0
          a = 0
          while 1 do
            local stat, msg = pcall(collectgarbage)
            if stat then
              break   -- stop when no more errors
            else
              a = a + 1
              assert(string.find(msg, "__gc"))
            end
          end
          assert(a == 10)  -- number of errors

          assert(A == 10)  -- number of normal collections
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: test for userdata vals", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local a = {}; local lim = 30
          for i=0,lim do a[i] = T.pushuserdata(i) end
          for i=0,lim do assert(T.udataval(a[i]) == i) end
          for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
          for i=0,lim do a[a[i]] = i end
          for i=0,lim do a[T.pushuserdata(i)] = i end
          assert(type(tostring(a[1])) == "string")
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing multiple states", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        T.closestate(T.newstate());
        L1 = T.newstate()
        assert(L1)

        assert(T.doremote(L1, "X='a'; return 'a'") == 'a')


        assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)

        a, b = T.doremote(L1, "return f()")
        assert(a == 'alo' and b == '3')

        T.doremote(L1, "_ERRORMESSAGE = nil")
        -- error: 'sin' is not defined
        a, _, b = T.doremote(L1, "return sin(1)")
        assert(a == nil and b == 2)   -- 2 == run-time error

        -- error: syntax error
        a, b, c = T.doremote(L1, "return a+")
        assert(a == nil and c == 3 and type(b) == "string")   -- 3 == syntax error

        T.loadlib(L1)
        a, b, c = T.doremote(L1, [[
          string = require'string'
          a = require'_G'; assert(a == _G and require("_G") == a)
          -- io = require'io'; assert(type(io.read) == "function")
          -- assert(require("io") == io)
          a = require'table'; assert(type(a.insert) == "function")
          a = require'debug'; assert(type(a.getlocal) == "function")
          a = require'math'; assert(type(a.sin) == "function")
          return string.sub('okinama', 1, 2)
        ]])
        assert(a == "ok")

        T.closestate(L1);


        L1 = T.newstate()
        T.loadlib(L1)
        T.doremote(L1, "a = {}")
        T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
                     settable -3]])
        assert(T.doremote(L1, "return a.x") == "1")

        T.closestate(L1)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing memory limits", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        checkerr("block too big", T.newuserdata, math.maxinteger)
        collectgarbage()
        T.totalmem(T.totalmem()+5000)   -- set low memory limit (+5k)
        checkerr("not enough memory", load"local a={}; for i=1,100000 do a[i]=i end")
        T.totalmem(0)          -- restore high limit
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


const memprefix = `
    -- test memory errors; increase memory limit in small steps, so that
    -- we get memory errors in different parts of a given task, up to there
    -- is enough memory to complete the task without errors
    function testamem (s, f)
      collectgarbage(); collectgarbage()
      local M = T.totalmem()
      local oldM = M
      local a,b = nil
      while 1 do
        M = M+7   -- increase memory limit in small steps
        T.totalmem(M)
        a, b = pcall(f)
        T.totalmem(0)  -- restore high limit
        if a and b then break end       -- stop when no more errors
        collectgarbage()
        if not a and not    -- 'real' error?
          (string.find(b, "memory") or string.find(b, "overflow")) then
          error(b, 0)   -- propagate it
        end
      end
      print("\\nlimit for " .. s .. ": " .. M-oldM)
      return b
    end
`;


test.skip("[test-suite] api: testing memory errors when creating a new state", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        b = testamem("state creation", T.newstate)
        T.closestate(b);  -- close new state
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});

test("[test-suite] api: get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        mt = T.testC("rawgeti R 1; return 1")
        assert(type(mt) == "thread" and coroutine.running() == mt)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});

test.skip("[test-suite] api: test thread creation after stressing GC", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        function expand (n,s)
          if n==0 then return "" end
          local e = string.rep("=", n)
          return string.format("T.doonnewstack([%s[ %s;\\n collectgarbage(); %s]%s])\\n",
                                      e, s, expand(n-1,s), e)
        end

        G=0; collectgarbage(); a =collectgarbage("count")
        load(expand(20,"G=G+1"))()
        assert(G==20); collectgarbage();  -- assert(gcinfo() <= a+1)

        testamem("thread creation", function ()
          return T.doonnewstack("x=1") == 0  -- try to create thread
        end)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing memory x compiler", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        testamem("loadstring", function ()
          return load("x=1")  -- try to do load a string
        end)
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: testing memory x dofile", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local testprog = [[
        local function foo () return end
        local t = {"x"}
        a = "aaa"
        for i = 1, #t do a=a..t[i] end
        return true
        ]]

        -- testing memory x dofile
        _G.a = nil
        local t =os.tmpname()
        local f = assert(io.open(t, "w"))
        f:write(testprog)
        f:close()
        testamem("dofile", function ()
          local a = loadfile(t)
          return a and a()
        end)
        assert(os.remove(t))
        assert(_G.a == "aaax")
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test.skip("[test-suite] api: other generic tests", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        testamem("string creation", function ()
          local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
          return (a == 'ablo ablo')
        end)

        testamem("dump/undump", function ()
          local a = load(testprog)
          local b = a and string.dump(a)
          a = b and load(b)
          return a and a()
        end)

        local t = os.tmpname()
        testamem("file creation", function ()
          local f = assert(io.open(t, 'w'))
          assert (not io.open"nomenaoexistente")
          io.close(f);
          return not loadfile'nomenaoexistente'
        end)
        assert(os.remove(t))

        testamem("table creation", function ()
          local a, lim = {}, 10
          for i=1,lim do a[i] = i; a[i..'a'] = {} end
          return (type(a[lim..'a']) == 'table' and a[lim] == lim)
        end)

        testamem("constructors", function ()
          local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
          return (type(a) == 'table' and a.e == 5)
        end)

        local a = 1
        close = nil
        testamem("closure creation", function ()
          function close (b,c)
           return function (x) return a+b+c+x end
          end
          return (close(2,3)(4) == 10)
        end)

        testamem("coroutines", function ()
          local a = coroutine.wrap(function ()
                      coroutine.yield(string.rep("a", 10))
                      return {}
                    end)
          assert(string.len(a()) == 10)
          return a()
        end)

        do   -- auxiliary buffer
          local lim = 100
          local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
          testamem("auxiliary buffer", function ()
            return (#table.concat(a, ",") == 20*lim + lim - 1)
          end)
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + memprefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing some auxlib functions", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local function gsub (a, b, c)
          a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
          assert(b == 5)
          return a
        end

        assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
        assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
        assert(gsub("", "alo", "//") == "")
        assert(gsub("...", ".", "/.") == "/././.")
        assert(gsub("...", "...", "") == "")
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test("[test-suite] api: testing luaL_newmetatable", () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
        assert(type(mt_xuxu) == "table" and res and top == 3)
        local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
        assert(mt_xuxu == d and not res and top == 3)
        d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
        assert(mt_xuxu ~= d and res and top == 3)

        x = T.newuserdata(0);
        y = T.newuserdata(0);
        T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
        assert(getmetatable(x) == mt_xuxu)

        -- correct metatable
        local res1, res2, top = T.testC([[testudata -1 xuxu
                          testudata 2 xuxu
                          gettop
                          return 3]], x)
        assert(res1 and res2 and top == 4)

        -- wrong metatable
        res1, res2, top = T.testC([[testudata -1 xuxu1
                        testudata 2 xuxu1
                        gettop
                        return 3]], x)
        assert(not res1 and not res2 and top == 4)

        -- non-existent type
        res1, res2, top = T.testC([[testudata -1 xuxu2
                        testudata 2 xuxu2
                        gettop
                        return 3]], x)
        assert(not res1 and not res2 and top == 4)

        -- userdata has no metatable
        res1, res2, top = T.testC([[testudata -1 xuxu
                        testudata 2 xuxu
                        gettop
                        return 3]], y)
        assert(not res1 and not res2 and top == 4)

        -- erase metatables
        do
          local r = debug.getregistry()
          assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
          r.xuxu = nil; r.xuxu1 = nil
        end
    `;
    lualib.luaL_openlibs(L);
    ltests.luaopen_tests(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});