aboutsummaryrefslogtreecommitdiff
path: root/test/test-suite/locals.test.js
blob: 27015194c25f8a917d7d9b524114c63521ec2518 (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
"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");

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

    let luaCode = `
        local function f(x) x = nil; return x end
        assert(f(10) == nil)

        local function f() local x; return x end
        assert(f(10) == nil)

        local function f(x) x = nil; local y; return x, y end
        assert(f(10) == nil and select(2, f(20)) == nil)
    `;
    lualib.luaL_openlibs(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('[test-suite] locals: local scope', () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local i = 10
          do local i = 100; assert(i==100) end
          do local i = 1000; assert(i==1000) end
          assert(i == 10)
          if i ~= 10 then
            local i = 20
          else
            local i = 30
            assert(i == 30)
          end
        end

        f = nil

        local f
        x = 1

        a = nil
        load('local a = {}')()
        assert(a == nil)

        function f (a)
          local _1, _2, _3, _4, _5
          local _6, _7, _8, _9, _10
          local x = 3
          local b = a
          local c,d = a,b
          if (d == b) then
            local x = 'q'
            x = b
            assert(x == 2)
          else
            assert(nil)
          end
          assert(x == 3)
          local f = 10
        end

        local b=10
        local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3


        assert(x == 1)

        f(2)
        assert(type(f) == 'function')
    `;
    lualib.luaL_openlibs(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);
});


const getenv = `
    local function getenv (f)
      local a,b = debug.getupvalue(f, 1)
      assert(a == '_ENV')
      return b
    end
`;


test('[test-suite] locals: test for global table of loaded chunks', () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(getenv(load"a=3") == _G)
        local c = {}; local f = load("a = 3", nil, nil, c)
        assert(getenv(f) == c)
        assert(c.a == nil)
        f()
        assert(c.a == 3)
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(getenv + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});


test('[test-suite] locals: old test for limits for special instructions (now just a generic test)', () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        do
          local i = 2
          local p = 4    -- p == 2^i
          repeat
            for j=-3,3 do
              assert(load(string.format([[local a=%s;
                                                a=a+%s;
                                                assert(a ==2^%s)]], j, p-j, i), '')) ()
              assert(load(string.format([[local a=%s;
                                                a=a-%s;
                                                assert(a==-2^%s)]], -j, p-j, i), '')) ()
              assert(load(string.format([[local a,b=0,%s;
                                                a=b-%s;
                                                assert(a==-2^%s)]], -j, p-j, i), '')) ()
            end
            p = 2 * p;  i = i + 1
          until p <= 0
        end
    `;
    lualib.luaL_openlibs(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('[test-suite] locals: testing lexical environments', () => {
    let L = lauxlib.luaL_newstate();
    if (!L) throw Error("failed to create lua state");

    let luaCode = `
        assert(_ENV == _G)

        do
          local dummy
          local _ENV = (function (...) return ... end)(_G, dummy)   -- {

          do local _ENV = {assert=assert}; assert(true) end
          mt = {_G = _G}
          local foo,x
          A = false    -- "declare" A
          do local _ENV = mt
            function foo (x)
              A = x
              do local _ENV =  _G; A = 1000 end
              return function (x) return A .. x end
            end
          end
          assert(getenv(foo) == mt)
          x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
          assert(x('*') == mt.A .. '*')

          do local _ENV = {assert=assert, A=10};
            do local _ENV = {assert=assert, A=20};
              assert(A==20);x=A
            end
            assert(A==10 and x==20)
          end
          assert(x==20)
        end
    `;
    lualib.luaL_openlibs(L);
    if (lauxlib.luaL_loadstring(L, to_luastring(getenv + luaCode)) === lua.LUA_ERRSYNTAX)
        throw new SyntaxError(lua.lua_tojsstring(L, -1));
    lua.lua_call(L, 0, 0);
});