aboutsummaryrefslogtreecommitdiff
path: root/tests/test-suite/calls.js
blob: 15e99984c6a20936cd9091c4b1f92849c772ca31 (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
"use strict";

const test     = require('tape');

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


test("[test-suite] calls: test 'type'", function (t) {
    let luaCode = `
        assert(type(1<2) == 'boolean')
        assert(type(true) == 'boolean' and type(false) == 'boolean')
        assert(type(nil) == 'nil'
           and type(-3) == 'number'
           and type'x' == 'string'
           and type{} == 'table'
           and type(type) == 'function')

        assert(type(assert) == type(print))
        function f (x) return a:x (x) end
        assert(type(f) == 'function')
        assert(not pcall(type))
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test error in 'print'", function (t) {
    let luaCode = `
        do    -- test error in 'print' too...
          local tostring = _ENV.tostring

          _ENV.tostring = nil
          local st, msg = pcall(print, 1)
          assert(st == false and string.find(msg, "attempt to call a nil value"))

          _ENV.tostring = function () return {} end
          local st, msg = pcall(print, 1)
          assert(st == false and string.find(msg, "must return a string"))
          
          _ENV.tostring = tostring
        end
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing local-function recursion", function (t) {
    let luaCode = `
        fact = false
        do
          local res = 1
          local function fact (n)
            if n==0 then return res
            else return n*fact(n-1)
            end
          end
          assert(fact(5) == 120)
        end
        assert(fact == false)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing declarations", function (t) {
    let luaCode = `
        a = {i = 10}
        self = 20
        function a:x (x) return x+self.i end
        function a.y (x) return x+self end

        assert(a:x(1)+10 == a.y(1))

        a.t = {i=-100}
        a["t"].x = function (self, a,b) return self.i+a+b end

        assert(a.t:x(2,3) == -95)

        do
          local a = {x=0}
          function a:add (x) self.x, a.y = self.x+x, 20; return self end
          assert(a:add(10):add(20):add(30).x == 60 and a.y == 20)
        end

        local a = {b={c={}}}

        function a.b.c.f1 (x) return x+1 end
        function a.b.c:f2 (x,y) self[x] = y end
        assert(a.b.c.f1(4) == 5)
        a.b.c:f2('k', 12); assert(a.b.c.k == 12)


        t = nil   -- 'declare' t
        function f(a,b,c) local d = 'a'; t={a,b,c,d} end

        f(      -- this line change must be valid
          1,2)
        assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
        f(1,2,   -- this one too
              3,4)
        assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')

        function fat(x)
          if x <= 1 then return 1
          else return x*load("return fat(" .. x-1 .. ")", "")()
          end
        end

        assert(load "load 'assert(fat(6)==720)' () ")()
        a = load('return fat(5), 3')
        a,b = a()
        assert(a == 120 and b == 3)

        function err_on_n (n)
          if n==0 then error(); exit(1);
          else err_on_n (n-1); exit(1);
          end
        end

        do
          function dummy (n)
            if n > 0 then
              assert(not pcall(err_on_n, n))
              dummy(n-1)
            end
          end
        end

        dummy(10)

        function deep (n)
          if n>0 then deep(n-1) end
        end
        deep(10)
        deep(200)

        -- testing tail call
        function deep (n) if n>0 then return deep(n-1) else return 101 end end
        assert(deep(30000) == 101)
        a = {}
        function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
        assert(a:deep(30000) == 101)



        a = nil
        (function (x) a=x end)(23)
        assert(a == 23 and (function (x) return x*2 end)(20) == 40)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing closures", function (t) {
    let luaCode = `
        -- fixed-point operator
        Z = function (le)
              local function a (f)
                return le(function (x) return f(f)(x) end)
              end
              return a(a)
            end


        -- non-recursive factorial

        F = function (f)
              return function (n)
                       if n == 0 then return 1
                       else return n*f(n-1) end
                     end
            end

        fat = Z(F)

        assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))

        local function g (z)
          local function f (a,b,c,d)
            return function (x,y) return a+b+c+d+a+x+y+z end
          end
          return f(z,z+1,z+2,z+3)
        end

        f = g(10)
        assert(f(9, 16) == 10+11+12+13+10+9+16+10)

        Z, F, f = nil
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing multiple returns", function (t) {
    let luaCode = `
        function unlpack (t, i)
          i = i or 1
          if (i <= #t) then
            return t[i], unlpack(t, i+1)
          end
        end

        function equaltab (t1, t2)
          assert(#t1 == #t2)
          for i = 1, #t1 do
            assert(t1[i] == t2[i])
          end
        end

        local pack = function (...) return (table.pack(...)) end

        function f() return 1,2,30,4 end
        function ret2 (a,b) return a,b end

        local a,b,c,d = unlpack{1,2,3}
        assert(a==1 and b==2 and c==3 and d==nil)
        a = {1,2,3,4,false,10,'alo',false,assert}
        equaltab(pack(unlpack(a)), a)
        equaltab(pack(unlpack(a), -1), {1,-1})
        a,b,c,d = ret2(f()), ret2(f())
        assert(a==1 and b==1 and c==2 and d==nil)
        a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
        assert(a==1 and b==1 and c==2 and d==nil)
        a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
        assert(a==1 and b==1 and c==nil and d==nil)

        a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
        assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing calls with 'incorrect' arguments", function (t) {
    let luaCode = `
        rawget({}, "x", 1)
        rawset({}, "x", 1, 2)
        assert(math.sin(1,2) == math.sin(1))
        table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test for generic load", function (t) {
    let luaCode = `
        local x = "-- a comment\\0\\0\\0\\n  x = 10 + \\n23; \\
             local a = function () x = 'hi' end; \\
             return '\\0'"

        function read1 (x)
          local i = 0
          return function ()
            i=i+1
            return string.sub(x, i, i)
          end
        end

        function cannotload (msg, a,b)
          assert(not a and string.find(b, msg))
        end

        a = assert(load(read1(x), "modname", "t", _G))
        assert(a() == "\0" and _G.x == 33)
        assert(debug.getinfo(a).source == "modname")
        -- cannot read text in binary mode
        cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
        cannotload("attempt to load a text chunk", load(x, "modname", "b"))

        a = assert(load(function () return nil end))
        a()  -- empty chunk

        assert(not load(function () return true end))

        local t = {nil, "return ", "3"}
        f, msg = load(function () return table.remove(t, 1) end)
        assert(f() == nil)   -- should read the empty chunk

        f = load(string.dump(function () return 1 end), nil, "b", {})
        assert(type(f) == "function" and f() == 1)


        x = string.dump(load("x = 1; return x"))
        a = assert(load(read1(x), nil, "b"))
        assert(a() == 1 and _G.x == 1)
        cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
        cannotload("attempt to load a binary chunk", load(x, nil, "t"))

        assert(not pcall(string.dump, print))  -- no dump of C functions

        cannotload("unexpected symbol", load(read1("*a = 123")))
        cannotload("unexpected symbol", load("*a = 123"))
        cannotload("hhi", load(function () error("hhi") end))
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: any value is valid for _ENV", function (t) {
    let luaCode = `
        assert(load("return _ENV", nil, nil, 123)() == 123)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: load when _ENV is not first upvalue", function (t) {
    let luaCode = `
        local x; XX = 123
        local function h ()
          local y=x   -- use 'x', so that it becomes 1st upvalue
          return XX   -- global name
        end
        local d = string.dump(h)
        x = load(d, "", "b")
        assert(debug.getupvalue(x, 2) == '_ENV')
        debug.setupvalue(x, 2, _G)
        assert(x() == 123)

        assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test generic load with nested functions", function (t) {
    let luaCode = `
        function read1 (x)
          local i = 0
          return function ()
            i=i+1
            return string.sub(x, i, i)
          end
        end

        x = [[
          return function (x)
            return function (y)
             return function (z)
               return x+y+z
             end
           end
          end
        ]]

        a = assert(load(read1(x)))
        assert(a()(2)(3)(10) == 15)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test for dump/undump with upvalues", function (t) {
    let luaCode = `
        local a, b = 20, 30
        x = load(string.dump(function (x)
          if x == "set" then a = 10+b; b = b+1 else
          return a
          end
        end), "", "b", nil)
        assert(x() == nil)
        assert(debug.setupvalue(x, 1, "hi") == "a")
        assert(x() == "hi")
        assert(debug.setupvalue(x, 2, 13) == "b")
        assert(not debug.setupvalue(x, 3, 10))   -- only 2 upvalues
        x("set")
        assert(x() == 23)
        x("set")
        assert(x() == 24)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test for dump/undump with many upvalues", function (t) {
    let luaCode = `
        do
          local nup = 200    -- maximum number of local variables
          local prog = {"local a1"}
          for i = 2, nup do prog[#prog + 1] = ", a" .. i end
          prog[#prog + 1] = " = 1"
          for i = 2, nup do prog[#prog + 1] = ", " .. i end
          local sum = 1
          prog[#prog + 1] = "; return function () return a1"
          for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
          prog[#prog + 1] = " end"
          prog = table.concat(prog)
          local f = assert(load(prog))()
          assert(f() == sum)

          f = load(string.dump(f))   -- main chunk now has many upvalues
          local a = 10
          local h = function () return a end
          for i = 1, nup do
            debug.upvaluejoin(f, i, h, 1)
          end
          assert(f() == 10 * nup)
        end
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test for long method names", function (t) {
    let luaCode = `
        do
          local t = {x = 1}
          function t:_012345678901234567890123456789012345678901234567890123456789 ()
            return self.x
          end
          assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
        end
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: test for bug in parameter adjustment", function (t) {
    let luaCode = `
        assert((function () return nil end)(4) == nil)
        assert((function () local a; return a end)(4) == nil)
        assert((function (a) return a end)() == nil)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});


test("[test-suite] calls: testing binary chunks", function (t) {
    let luaCode = `
        do
          local header = string.pack("c4BBc6BBBBBj",
            "\\27Lua",                -- signature
            5*16 + 3,                -- version 5.3
            0,                       -- format
            "\\x19\\x93\\r\\n\\x1a\\n",    -- data
            string.packsize("i"),    -- sizeof(int)
            string.packsize("T"),    -- sizeof(size_t)
            4,                       -- size of instruction
            string.packsize("j"),    -- sizeof(lua integer)
            string.packsize("n"),    -- sizeof(lua number)
            0x5678                   -- LUAC_INT
            -- LUAC_NUM may not have a unique binary representation (padding...)
          )
          local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)

          assert(string.sub(c, 1, #header) == header)

          -- corrupted header
          for i = 1, #header do
            local s = string.sub(c, 1, i - 1) ..
                      string.char(string.byte(string.sub(c, i, i)) + 1) ..
                      string.sub(c, i + 1, -1)
            assert(#s == #c)
            assert(not load(s))
          end

          -- loading truncated binary chunks
          for i = 1, #c - 1 do
            local st, msg = load(string.sub(c, 1, i))
            assert(not st and string.find(msg, "truncated"))
          end
          assert(assert(load(c))() == 10)
        end
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lauxlib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

        lua.lua_call(L, 0, -1);

    }, "Lua program ran without error");

});