summaryrefslogtreecommitdiff
path: root/tests/test-suite/closure.js
blob: 99bdbe56c1f3c86d3aac8630aba40e96d65d4ff0 (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
"use strict";

const test     = require('tape');

global.WEB = false;

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


test("[test-suite] closure: testing equality", function (t) {
    let luaCode = `
        a = {}
        for i = 1, 5 do  a[i] = function (x) return x + a + _ENV end  end
        assert(a[3] == a[4] and a[4] == a[5])

        for i = 1, 5 do  a[i] = function (x) return i + a + _ENV end  end
        assert(a[3] ~= a[4] and a[4] ~= a[5])

        local function f()
          return function (x)  return math.sin(_ENV[x])  end
        end
        assert(f() == f())
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing closures with 'for' control variable", function (t) {
    let luaCode = `
        a = {}
        for i=1,10 do
          a[i] = {set = function(x) i=x end, get = function () return i end}
          if i == 3 then break end
        end
        assert(a[4] == nil)
        a[1].set(10)
        assert(a[2].get() == 2)
        a[2].set('a')
        assert(a[3].get() == 3)
        assert(a[2].get() == 'a')

        a = {}
        local t = {"a", "b"}
        for i = 1, #t do
          local k = t[i]
          a[i] = {set = function(x, y) i=x; k=y end,
                  get = function () return i, k end}
          if i == 2 then break end
        end
        a[1].set(10, 20)
        local r,s = a[2].get()
        assert(r == 2 and s == 'b')
        r,s = a[1].get()
        assert(r == 10 and s == 20)
        a[2].set('a', 'b')
        r,s = a[2].get()
        assert(r == "a" and s == "b")
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing closures with 'for' control variable x break", function (t) {
    let luaCode = `
        for i=1,3 do
          f = function () return i end
          break
        end
        assert(f() == 1)

        for k = 1, #t do
          local v = t[k]
          f = function () return k, v end
          break
        end
        assert(({f()})[1] == 1)
        assert(({f()})[2] == "a")
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing closure x break x return x errors", function (t) {
    let luaCode = `
        local b
        function f(x)
          local first = 1
          while 1 do
            if x == 3 and not first then return end
            local a = 'xuxu'
            b = function (op, y)
                  if op == 'set' then
                    a = x+y
                  else
                    return a
                  end
                end
            if x == 1 then do break end
            elseif x == 2 then return
            else if x ~= 3 then error() end
            end
            first = nil
          end
        end

        for i=1,3 do
          f(i)
          assert(b('get') == 'xuxu')
          b('set', 10); assert(b('get') == 10+i)
          b = nil
        end

        pcall(f, 4);
        assert(b('get') == 'xuxu')
        b('set', 10); assert(b('get') == 14)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing multi-level closure", function (t) {
    let luaCode = `
        local w
        function f(x)
          return function (y)
            return function (z) return w+x+y+z end
          end
        end

        y = f(10)
        w = 1.345
        assert(y(20)(30) == 60+w)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing closures x repeat-until", function (t) {
    let luaCode = `
        local a = {}
        local i = 1
        repeat
          local x = i
          a[i] = function () i = x+1; return x end
        until i > 10 or a[i]() ~= x
        assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4)
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: testing closures created in 'then' and 'else' parts of 'if's", function (t) {
    let luaCode = `
        a = {}
        for i = 1, 10 do
          if i % 3 == 0 then
            local y = 0
            a[i] = function (x) local t = y; y = x; return t end
          elseif i % 3 == 1 then
            goto L1
            error'not here'
          ::L1::
            local y = 1
            a[i] = function (x) local t = y; y = x; return t end
          elseif i % 3 == 2 then
            local t
            goto l4
            ::l4a:: a[i] = t; goto l4b
            error("should never be here!")
            ::l4::
            local y = 2
            t = function (x) local t = y; y = x; return t end
            goto l4a
            error("should never be here!")
            ::l4b::
          end
        end

        for i = 1, 10 do
          assert(a[i](i * 10) == i % 3 and a[i]() == i * 10)
        end
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.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] closure: test for correctly closing upvalues in tail calls of vararg functions", function (t) {
    let luaCode = `
        local function t ()
          local function c(a,b) assert(a=="test" and b=="OK") end
          local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end
          local x = 1
          return v(function() return x end)
        end
        t()
    `, L;
    
    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

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

});