aboutsummaryrefslogtreecommitdiff
path: root/test/test-suite/goto.js
blob: dc52c73da1a3ac26c958164fd31aae72e08d8f9f (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
"use strict";

const test     = require('tape');

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");

test("[test-suite] goto: error messages", function (t) {
    let luaCode = `
        local function errmsg (code, m)
          local st, msg = load(code)
          assert(not st and string.find(msg, m))
        end

        -- cannot see label inside block
        errmsg([[ goto l1; do ::l1:: end ]], "label 'l1'")
        errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'")

        -- repeated label
        errmsg([[ ::l1:: ::l1:: ]], "label 'l1'")


        -- undefined label
        errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'")

        -- jumping over variable definition
        errmsg([[
        do local bb, cc; goto l1; end
        local aa
        ::l1:: print(3)
        ]], "local 'aa'")

        -- jumping into a block
        errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'")
        errmsg([[ goto l1 do ::l1:: end ]], "label 'l1'")

        -- cannot continue a repeat-until with variables
        errmsg([[
          repeat
            if x then goto cont end
            local xuxu = 10
            ::cont::
          until xuxu < x
        ]], "local 'xuxu'")
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, 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] goto", function (t) {
    let luaCode = `
        -- simple gotos
        local x
        do
          local y = 12
          goto l1
          ::l2:: x = x + 1; goto l3
          ::l1:: x = y; goto l2
        end
        ::l3:: ::l3_1:: assert(x == 13)

        -- long labels
        do
          local prog = [[
          do
            local a = 1
            goto l%sa; a = a + 1
           ::l%sa:: a = a + 10
            goto l%sb; a = a + 2
           ::l%sb:: a = a + 20
            return a
          end
          ]]
          local label = string.rep("0123456789", 40)
          prog = string.format(prog, label, label, label, label)
          assert(assert(load(prog))() == 31)
        end

        -- goto to correct label when nested
        do goto l3; ::l3:: end   -- does not loop jumping to previous label 'l3'

        -- ok to jump over local dec. to end of block
        do
          goto l1
          local a = 23
          x = a
          ::l1::;
        end

        while true do
          goto l4
          goto l1  -- ok to jump over local dec. to end of block
          goto l1  -- multiple uses of same label
          local x = 45
          ::l1:: ;;;
        end
        ::l4:: assert(x == 13)

        if print then
          goto l1   -- ok to jump over local dec. to end of block
          error("should not be here")
          goto l2   -- ok to jump over local dec. to end of block
          local x
          ::l1:: ; ::l2:: ;;
        else end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, 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] goto: to repeat a label in a different function is OK", function (t) {
    let luaCode = `
        local function foo ()
          local a = {}
          goto l3
          ::l1:: a[#a + 1] = 1; goto l2;
          ::l2:: a[#a + 1] = 2; goto l5;
          ::l3::
          ::l3a:: a[#a + 1] = 3; goto l1;
          ::l4:: a[#a + 1] = 4; goto l6;
          ::l5:: a[#a + 1] = 5; goto l4;
          ::l6:: assert(a[1] == 3 and a[2] == 1 and a[3] == 2 and
                      a[4] == 5 and a[5] == 4)
          if not a[6] then a[6] = true; goto l3a end   -- do it twice
        end

        ::l6:: foo()
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, 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] goto: bug in 5.2 -> 5.3.2", function (t) {
    let luaCode = `
        do   -- bug in 5.2 -> 5.3.2
          local x
          ::L1::
          local y             -- cannot join this SETNIL with previous one
          assert(y == nil)
          y = true
          if x == nil then
            x = 1
            goto L1
          else
            x = x + 1
          end
          assert(x == 2 and y == true)
        end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, 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] goto: testing closing of upvalues", function (t) {
    let luaCode = `
        local function foo ()
          local t = {}
          do
          local i = 1
          local a, b, c, d
          t[1] = function () return a, b, c, d end
          ::l1::
          local b
          do
            local c
            t[#t + 1] = function () return a, b, c, d end    -- t[2], t[4], t[6]
            if i > 2 then goto l2 end
            do
              local d
              t[#t + 1] = function () return a, b, c, d end   -- t[3], t[5]
              i = i + 1
              local a
              goto l1
            end
          end
          end
          ::l2:: return t
        end

        local a = foo()
        assert(#a == 6)

        -- all functions share same 'a'
        for i = 2, 6 do
          assert(debug.upvalueid(a[1], 1) == debug.upvalueid(a[i], 1))
        end

        -- 'b' and 'c' are shared among some of them
        for i = 2, 6 do
          -- only a[1] uses external 'b'/'b'
          assert(debug.upvalueid(a[1], 2) ~= debug.upvalueid(a[i], 2))
          assert(debug.upvalueid(a[1], 3) ~= debug.upvalueid(a[i], 3))
        end

        for i = 3, 5, 2 do
          -- inner functions share 'b'/'c' with previous ones
          assert(debug.upvalueid(a[i], 2) == debug.upvalueid(a[i - 1], 2))
          assert(debug.upvalueid(a[i], 3) == debug.upvalueid(a[i - 1], 3))
          -- but not with next ones
          assert(debug.upvalueid(a[i], 2) ~= debug.upvalueid(a[i + 1], 2))
          assert(debug.upvalueid(a[i], 3) ~= debug.upvalueid(a[i + 1], 3))
        end

        -- only external 'd' is shared
        for i = 2, 6, 2 do
          assert(debug.upvalueid(a[1], 4) == debug.upvalueid(a[i], 4))
        end

        -- internal 'd's are all different
        for i = 3, 5, 2 do
          for j = 1, 6 do
            assert((debug.upvalueid(a[i], 4) == debug.upvalueid(a[j], 4))
              == (i == j))
          end
        end
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

        lauxlib.luaL_loadstring(L, 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] goto: testing if x goto optimizations", function (t) {
    let luaCode = `
        local function testG (a)
          if a == 1 then
            goto l1
            error("should never be here!")
          elseif a == 2 then goto l2
          elseif a == 3 then goto l3
          elseif a == 4 then
            goto l1  -- go to inside the block
            error("should never be here!")
            ::l1:: a = a + 1   -- must go to 'if' end
          else
            goto l4
            ::l4a:: a = a * 2; goto l4b
            error("should never be here!")
            ::l4:: goto l4a
            error("should never be here!")
            ::l4b::
          end
          do return a end
          ::l2:: do return "2" end
          ::l3:: do return "3" end
          ::l1:: return "1"
        end

        assert(testG(1) == "1")
        assert(testG(2) == "2")
        assert(testG(3) == "3")
        assert(testG(4) == 5)
        assert(testG(5) == 10)
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});