aboutsummaryrefslogtreecommitdiff
path: root/tests/test-suite/inprogress/db.js
blob: af1d04211214526f7204d32869d21513aac3a8d5 (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
"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');


const prefix = `
    local function dostring(s) return assert(load(s))() end

    local testline = 4          -- line where 'test' is defined
    function test (s, l, p)     -- this must be line 4
      -- collectgarbage()   -- avoid gc during trace
      local function f (event, line)
        assert(event == 'line')
        local l = table.remove(l, 1)
        if p then print(l, line) end
        assert(l == line, "wrong trace!!")
      end
      debug.sethook(f,"l"); load(s)(); debug.sethook()
      assert(#l == 0)
    end
`;

test("[test-suite] db: getinfo, ...line...", function (t) {
    let luaCode = `-- $Id: db.lua,v 1.79 2016/11/07 13:02:34 roberto Exp $
        -- See Copyright Notice in file all.lua

        -- testing debug library

        local debug = require "debug"

        local function dostring(s) return assert(load(s))() end

        print"testing debug library and debug information"

        do
        local a=1
        end

        assert(not debug.gethook())

        local testline = 19         -- line where 'test' is defined
        function test (s, l, p)     -- this must be line 19
          collectgarbage()   -- avoid gc during trace
          local function f (event, line)
            assert(event == 'line')
            local l = table.remove(l, 1)
            if p then print(l, line) end
            assert(l == line, "wrong trace!!")
          end
          debug.sethook(f,"l"); load(s)(); debug.sethook()
          assert(#l == 0)
        end


        do
          assert(not pcall(debug.getinfo, print, "X"))   -- invalid option
          assert(not debug.getinfo(1000))   -- out of range level
          assert(not debug.getinfo(-1))     -- out of range level
          local a = debug.getinfo(print)
          assert(a.what == "J" and a.short_src == "[JS]")
          a = debug.getinfo(print, "L")
          assert(a.activelines == nil)
          local b = debug.getinfo(test, "SfL")
          assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and
                 b.lastlinedefined == b.linedefined + 10 and
                 b.func == test and string.find(b.short_src, "%["))
          assert(b.activelines[b.linedefined + 1] and
                 b.activelines[b.lastlinedefined])
          assert(not b.activelines[b.linedefined] and
                 not b.activelines[b.lastlinedefined + 1])
        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] db: test file ad string names truncation", function (t) {
    let luaCode = `
        a = "function f () end"
        local function dostring (s, x) return load(s, x)() end
        dostring(a)
        assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
        dostring(a..string.format("; %s\\n=1", string.rep('p', 400)))
        assert(string.find(debug.getinfo(f).short_src, '^%[string [^\\n]*%.%.%."%]$'))
        dostring(a..string.format("; %s=1", string.rep('p', 400)))
        assert(string.find(debug.getinfo(f).short_src, '^%[string [^\\n]*%.%.%."%]$'))
        dostring("\\n"..a)
        assert(debug.getinfo(f).short_src == '[string "..."]')
        dostring(a, "")
        assert(debug.getinfo(f).short_src == '[string ""]')
        dostring(a, "@xuxu")
        assert(debug.getinfo(f).short_src == "xuxu")
        dostring(a, "@"..string.rep('p', 1000)..'t')
        assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
        dostring(a, "=xuxu")
        assert(debug.getinfo(f).short_src == "xuxu")
        dostring(a, string.format("=%s", string.rep('x', 500)))
        assert(string.find(debug.getinfo(f).short_src, "^x*$"))
        dostring(a, "=")
        assert(debug.getinfo(f).short_src == "")
        a = nil; f = nil;
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});

test("[test-suite] db: local", function (t) {
    let luaCode = `
        repeat
          local g = {x = function ()
            local a = debug.getinfo(2)
            assert(a.name == 'f' and a.namewhat == 'local')
            a = debug.getinfo(1)
            assert(a.name == 'x' and a.namewhat == 'field')
            return 'xixi'
          end}
          local f = function () return 1+1 and (not 1 or g.x()) end
          assert(f() == 'xixi')
          g = debug.getinfo(f)
          assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)

          function f (x, name)   -- local!
            name = name or 'f'
            local a = debug.getinfo(1)
            assert(a.name == name and a.namewhat == 'local')
            return x
          end

          -- breaks in different conditions
          if 3>4 then break end; f()
          if 3<4 then a=1 else break end; f()
          while 1 do local x=10; break end; f()
          local b = 1
          if 3>4 then return math.sin(1) end; f()
          a = 3<4; f()
          a = 3<4 or 1; f()
          repeat local x=20; if 4>3 then f() else break end; f() until 1
          g = {}
          f(g).x = f(2) and f(10)+f(9)
          assert(g.x == f(19))
          function g(x) if not x then return 3 end return (x('a', 'x')) end
          assert(g(f) == 'a')
        until 1
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});


test("[test-suite] db: line hook", function (t) {
    let luaCode = `
        test([[if
        math.sin(1)
        then
          a=1
        else
          a=2
        end
        ]], {2,3,4,7})

        test([[--
        if nil then
          a=1
        else
          a=2
        end
        ]], {2,5,6})

        test([[a=1
        repeat
          a=a+1
        until a==3
        ]], {1,3,4,3,4})

        test([[ do
          return
        end
        ]], {2})

        test([[local a
        a=1
        while a<=3 do
          a=a+1
        end
        ]], {1,2,3,4,3,4,3,4,3,5})

        test([[while math.sin(1) do
          if math.sin(1)
          then break
          end
        end
        a=1]], {1,2,3,6})

        test([[for i=1,3 do
          a=i
        end
        ]], {1,2,1,2,1,2,1,3})

        test([[for i,v in pairs{'a','b'} do
          a=tostring(i) .. v
        end
        ]], {1,2,1,2,1,3})

        test([[for i=1,4 do a=1 end]], {1,1,1,1,1})
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});


test("[test-suite] db: invalid levels in [gs]etlocal", function (t) {
    let luaCode = `
        assert(not pcall(debug.getlocal, 20, 1))
        assert(not pcall(debug.setlocal, -1, 1, 10))
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});


test("[test-suite] db: parameter names", function (t) {
    let luaCode = `
        local function foo (a,b,...) local d, e end
        local co = coroutine.create(foo)

        assert(debug.getlocal(foo, 1) == 'a')
        assert(debug.getlocal(foo, 2) == 'b')
        assert(not debug.getlocal(foo, 3))
        assert(debug.getlocal(co, foo, 1) == 'a')
        assert(debug.getlocal(co, foo, 2) == 'b')
        assert(not debug.getlocal(co, foo, 3))

        assert(not debug.getlocal(print, 1))
    `, L;

    t.plan(2);

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        lualib.luaL_openlibs(L);

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

    }, "Lua program loaded without error");

    t.doesNotThrow(function () {

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

    }, "Lua program ran without error");

});