1-- $Id: testes/api.lua $
2-- See Copyright Notice in file all.lua
3
4if T==nil then
5 (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
6 return
7end
8
9local debug = require "debug"
10
11local pack = table.pack
12
13
14-- standard error message for memory errors
15local MEMERRMSG = "not enough memory"
16
17local function tcheck (t1, t2)
18 assert(t1.n == (t2.n or #t2) + 1)
19 for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
20end
21
22
23local function checkerr (msg, f, ...)
24 local stat, err = pcall(f, ...)
25 assert(not stat and string.find(err, msg))
26end
27
28
29print('testing C API')
30
31local a = T.testC("pushvalue R; return 1")
32assert(a == debug.getregistry())
33
34
35-- absindex
36assert(T.testC("settop 10; absindex -1; return 1") == 10)
37assert(T.testC("settop 5; absindex -5; return 1") == 1)
38assert(T.testC("settop 10; absindex 1; return 1") == 1)
39assert(T.testC("settop 10; absindex R; return 1") < -10)
40
41-- testing alignment
42a = T.d2s(12458954321123.0)
43assert(a == string.pack("d", 12458954321123.0))
44assert(T.s2d(a) == 12458954321123.0)
45
46local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
47assert(a == 2 and b == 3 and not c)
48
49local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
50a,b,c = f()
51assert(a == 2 and b == 3 and not c)
52
53-- test that all trues are equal
54a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
55assert(a == b and a == true and c == false)
56a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
57 tobool -3; tobool -3; tobool -3; return 3"
58assert(a==false and b==true and c==false)
59
60
61a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
62assert(a == 40 and b == 5 and not c)
63
64local t = pack(T.testC("settop 5; return *", 2, 3))
65tcheck(t, {n=4,2,3})
66
67t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
68assert(t.n == 10 and t[1] == nil and t[10] == nil)
69
70t = pack(T.testC("remove -2; return *", 2, 3, 4))
71tcheck(t, {n=2,2,4})
72
73t = pack(T.testC("insert -1; return *", 2, 3))
74tcheck(t, {n=2,2,3})
75
76t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
77tcheck(t, {n=4,2,5,3,4})
78
79t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
80tcheck(t, {n=3,5,3,4})
81
82t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
83tcheck(t, {n=3,2,3,5})
84
85t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
86tcheck(t, {n=3,2,4,5})
87
88t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
89tcheck(t, {n=4,2,3,3,5})
90
91t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
92tcheck(t, {n=4,2,3,4,3})
93
94do -- testing 'rotate'
95 local t = {10, 20, 30, 40, 50, 60}
96 for i = -6, 6 do
97 local s = string.format("rotate 2 %d; return 7", i)
98 local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
99 tcheck(t1, t)
100 table.insert(t, 1, table.remove(t))
101 end
102
103 t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40))
104 tcheck(t, {10, 20, 40, 30})
105 t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40))
106 tcheck(t, {10, 20, 40, 30})
107
108 -- some corner cases
109 t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40))
110 tcheck(t, {10, 20, 30, 40})
111 t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40))
112 tcheck(t, {10, 20, 30, 40})
113 t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40))
114 tcheck(t, {10, 20, 30, 40})
115end
116
117
118-- testing warnings
119T.testC([[
120 warningC "#This shold be a"
121 warningC " single "
122 warning "warning"
123 warningC "#This should be "
124 warning "another one"
125]])
126
127
128-- testing message handlers
129do
130 local f = T.makeCfunc[[
131 getglobal error
132 pushstring bola
133 pcall 1 1 1 # call 'error' with given handler
134 pushstatus
135 return 2 # return error message and status
136 ]]
137
138 local msg, st = f(string.upper) -- function handler
139 assert(st == "ERRRUN" and msg == "BOLA")
140 local msg, st = f(string.len) -- function handler
141 assert(st == "ERRRUN" and msg == 4)
142
143end
144
145t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
146 insert 2; pushvalue 1; remove 1; insert 1; \
147 insert -2; pushvalue -2; remove -3; return *",
148 2, 3, 4, 5, 10, 40, 90))
149tcheck(t, {n=7,2,3,4,5,10,40,90})
150
151t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
152tcheck(t, {n=1,"alo23joao12"})
153
154-- testing MULTRET
155t = pack(T.testC("call 2,-1; return *",
156 function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
157tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
158
159do -- test returning more results than fit in the caller stack
160 local a = {}
161 for i=1,1000 do a[i] = true end; a[999] = 10
162 local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
163 table.unpack, a)
164 assert(b == "10")
165end
166
167
168-- testing globals
169_G.AA = 14; _G.BB = "a31"
170local a = {T.testC[[
171 getglobal AA;
172 getglobal BB;
173 getglobal BB;
174 setglobal AA;
175 return *
176]]}
177assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
178
179_G.AA, _G.BB = nil
180
181-- testing arith
182assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
183assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
184assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
185assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
186assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
187assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
188assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
189assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
190assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
191a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
192assert(a == 3.0 and math.type(a) == "float")
193a = T.testC("pushint 10; pushint 3; arith \\; return 1")
194assert(a == 3 and math.type(a) == "integer")
195a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
196assert(a == 13 and math.type(a) == "integer")
197a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
198assert(a == 13 and math.type(a) == "float")
199a,b,c = T.testC([[pushnum 1;
200 pushstring 10; arith _;
201 pushstring 5; return 3]])
202assert(a == 1 and b == -10 and c == "5")
203local mt = {
204 __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
205 __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
206 __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
207a,b,c = setmetatable({4}, mt),
208 setmetatable({8}, mt),
209 setmetatable({-3}, mt)
210local x,y,z = T.testC("arith +; return 2", 10, a, b)
211assert(x == 10 and y[1] == 12 and z == nil)
212assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
213assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
214 8 % (4 + (-3)*2))
215
216-- errors in arithmetic
217checkerr("divide by zero", T.testC, "arith \\", 10, 0)
218checkerr("%%0", T.testC, "arith %", 10, 0)
219
220
221-- testing lessthan and lessequal
222assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
223assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
224assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
225assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
226assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
227assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
228assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
229
230-- non-valid indices produce false
231assert(not T.testC("compare LT 1 4, return 1"))
232assert(not T.testC("compare LE 9 1, return 1"))
233assert(not T.testC("compare EQ 9 9, return 1"))
234
235local b = {__lt = function (a,b) return a[1] < b[1] end}
236local a1,a3,a4 = setmetatable({1}, b),
237 setmetatable({3}, b),
238 setmetatable({4}, b)
239assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
240assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
241assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
242a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
243assert(a == 20 and b == false)
244a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
245assert(a == 20 and b == false)
246a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
247assert(a == 20 and b == true)
248
249
250do -- testing lessthan and lessequal with metamethods
251 local mt = {__lt = function (a,b) return a[1] < b[1] end,
252 __le = function (a,b) return a[1] <= b[1] end,
253 __eq = function (a,b) return a[1] == b[1] end}
254 local function O (x)
255 return setmetatable({x}, mt)
256 end
257
258 local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
259 assert(a == true and b == 10)
260 local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
261 assert(a == false and b == 10)
262 local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
263 assert(a == true and b == 10)
264end
265
266-- testing length
267local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
268a,b,c = T.testC([[
269 len 2;
270 Llen 2;
271 objsize 2;
272 return 3
273]], t)
274assert(a == 20 and b == 20 and c == 0)
275
276t.x = "234"; t[1] = 20
277a,b,c = T.testC([[
278 len 2;
279 Llen 2;
280 objsize 2;
281 return 3
282]], t)
283assert(a == "234" and b == 234 and c == 1)
284
285t.x = print; t[1] = 20
286a,c = T.testC([[
287 len 2;
288 objsize 2;
289 return 2
290]], t)
291assert(a == print and c == 1)
292
293
294-- testing __concat
295
296a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
297x,y = T.testC([[
298 pushnum 5
299 pushvalue 2;
300 pushvalue 2;
301 concat 2;
302 pushvalue -2;
303 return 2;
304]], a, a)
305assert(x == a..a and y == 5)
306
307-- concat with 0 elements
308assert(T.testC("concat 0; return 1") == "")
309
310-- concat with 1 element
311assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
312
313
314
315-- testing lua_is
316
317local function B (x) return x and 1 or 0 end
318
319local function count (x, n)
320 n = n or 2
321 local prog = [[
322 isnumber %d;
323 isstring %d;
324 isfunction %d;
325 iscfunction %d;
326 istable %d;
327 isuserdata %d;
328 isnil %d;
329 isnull %d;
330 return 8
331 ]]
332 prog = string.format(prog, n, n, n, n, n, n, n, n)
333 local a,b,c,d,e,f,g,h = T.testC(prog, x)
334 return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
335end
336
337assert(count(3) == 2)
338assert(count('alo') == 1)
339assert(count('32') == 2)
340assert(count({}) == 1)
341assert(count(print) == 2)
342assert(count(function () end) == 1)
343assert(count(nil) == 1)
344assert(count(io.stdin) == 1)
345assert(count(nil, 15) == 100)
346
347
348-- testing lua_to...
349
350local function to (s, x, n)
351 n = n or 2
352 return T.testC(string.format("%s %d; return 1", s, n), x)
353end
354
355local null = T.pushuserdata(0)
356local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
357assert(debug.getupvalue(hfunc, 1))
358assert(to("tostring", {}) == nil)
359assert(to("tostring", "alo") == "alo")
360assert(to("tostring", 12) == "12")
361assert(to("tostring", 12, 3) == nil)
362assert(to("objsize", {}) == 0)
363assert(to("objsize", {1,2,3}) == 3)
364assert(to("objsize", "alo\0\0a") == 6)
365assert(to("objsize", T.newuserdata(0)) == 0)
366assert(to("objsize", T.newuserdata(101)) == 101)
367assert(to("objsize", 124) == 0)
368assert(to("objsize", true) == 0)
369assert(to("tonumber", {}) == 0)
370assert(to("tonumber", "12") == 12)
371assert(to("tonumber", "s2") == 0)
372assert(to("tonumber", 1, 20) == 0)
373assert(to("topointer", 10) == null)
374assert(to("topointer", true) == null)
375assert(to("topointer", nil) == null)
376assert(to("topointer", "abc") ~= null)
377assert(to("topointer", string.rep("x", 10)) ==
378 to("topointer", string.rep("x", 10))) -- short strings
379do -- long strings
380 local s1 = string.rep("x", 300)
381 local s2 = string.rep("x", 300)
382 assert(to("topointer", s1) ~= to("topointer", s2))
383end
384assert(to("topointer", T.pushuserdata(20)) ~= null)
385assert(to("topointer", io.read) ~= null) -- light C function
386assert(to("topointer", hfunc) ~= null) -- "heavy" C function
387assert(to("topointer", function () end) ~= null) -- Lua function
388assert(to("topointer", io.stdin) ~= null) -- full userdata
389assert(to("func2num", 20) == 0)
390assert(to("func2num", T.pushuserdata(10)) == 0)
391assert(to("func2num", io.read) ~= 0) -- light C function
392assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
393a = to("tocfunction", math.deg)
394assert(a(3) == math.deg(3) and a == math.deg)
395
396
397print("testing panic function")
398do
399 -- trivial error
400 assert(T.checkpanic("pushstring hi; error") == "hi")
401
402 -- using the stack inside panic
403 assert(T.checkpanic("pushstring hi; error;",
404 [[checkstack 5 XX
405 pushstring ' alo'
406 pushstring ' mundo'
407 concat 3]]) == "hi alo mundo")
408
409 -- "argerror" without frames
410 assert(T.checkpanic("loadstring 4") ==
411 "bad argument #4 (string expected, got no value)")
412
413
414 -- memory error
415 T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
416 assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
417 T.totalmem(0) -- restore high limit
418
419 -- stack error
420 if not _soft then
421 local msg = T.checkpanic[[
422 pushstring "function f() f() end"
423 loadstring -1; call 0 0
424 getglobal f; call 0 0
425 ]]
426 assert(string.find(msg, "stack overflow"))
427 end
428
429 -- exit in panic still close to-be-closed variables
430 assert(T.checkpanic([[
431 pushstring "return {__close = function () Y = 'ho'; end}"
432 newtable
433 loadstring -2
434 call 0 1
435 setmetatable -2
436 toclose -1
437 pushstring "hi"
438 error
439 ]],
440 [[
441 getglobal Y
442 concat 2 # concat original error with global Y
443 ]]) == "hiho")
444
445
446end
447
448-- testing deep C stack
449if not _soft then
450 print("testing stack overflow")
451 collectgarbage("stop")
452 checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep
453 -- too deep (with no message)
454 checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
455 local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
456 checkerr("overflow", T.testC, s)
457 collectgarbage("restart")
458 print'+'
459end
460
461local lim = _soft and 500 or 12000
462local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
463for i = 1,lim do
464 prog[#prog + 1] = "pushnum " .. i
465 prog[#prog + 1] = "pushnum " .. i * 10
466end
467
468prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
469prog[#prog + 1] = "insert " .. -(2*lim + 2)
470
471for i = 1,lim do
472 prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
473end
474
475prog[#prog + 1] = "return 2"
476
477prog = table.concat(prog, ";")
478local g, t = T.testC(prog)
479assert(g == _G)
480for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
481assert(next(t) == nil)
482prog, g, t = nil
483
484-- testing errors
485
486a = T.testC([[
487 loadstring 2; pcall 0 1 0;
488 pushvalue 3; insert -2; pcall 1 1 0;
489 pcall 0 0 0;
490 return 1
491]], "XX=150", function (a) assert(a==nil); return 3 end)
492
493assert(type(a) == 'string' and XX == 150)
494_G.XX = nil
495
496local function check3(p, ...)
497 local arg = {...}
498 assert(#arg == 3)
499 assert(string.find(arg[3], p))
500end
501check3(":1:", T.testC("loadstring 2; return *", "x="))
502check3("%.", T.testC("loadfile 2; return *", "."))
503check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
504
505-- test errors in non protected threads
506local function checkerrnopro (code, msg)
507 local th = coroutine.create(function () end) -- create new thread
508 local stt, err = pcall(T.testC, th, code) -- run code there
509 assert(not stt and string.find(err, msg))
510end
511
512if not _soft then
513 collectgarbage("stop") -- avoid __gc with full stack
514 checkerrnopro("pushnum 3; call 0 0", "attempt to call")
515 print"testing stack overflow in unprotected thread"
516 function F () F() end
517 checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
518 F = nil
519 collectgarbage("restart")
520end
521print"+"
522
523
524-- testing table access
525
526do -- getp/setp
527 local a = {}
528 local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
529 assert(a == a1)
530 assert(a[T.pushuserdata(1)] == 20)
531 local a1, res = T.testC("rawgetp -1 1; return 2", a)
532 assert(a == a1 and res == 20)
533end
534
535
536do -- using the table itself as index
537 local a = {}
538 a[a] = 10
539 local prog = "gettable -1; return *"
540 local res = {T.testC(prog, a)}
541 assert(#res == 2 and res[1] == prog and res[2] == 10)
542
543 local prog = "settable -2; return *"
544 local res = {T.testC(prog, a, 20)}
545 assert(a[a] == 20)
546 assert(#res == 1 and res[1] == prog)
547
548 -- raw
549 a[a] = 10
550 local prog = "rawget -1; return *"
551 local res = {T.testC(prog, a)}
552 assert(#res == 2 and res[1] == prog and res[2] == 10)
553
554 local prog = "rawset -2; return *"
555 local res = {T.testC(prog, a, 20)}
556 assert(a[a] == 20)
557 assert(#res == 1 and res[1] == prog)
558
559 -- using the table as the value to set
560 local prog = "rawset -1; return *"
561 local res = {T.testC(prog, 30, a)}
562 assert(a[30] == a)
563 assert(#res == 1 and res[1] == prog)
564
565 local prog = "settable -1; return *"
566 local res = {T.testC(prog, 40, a)}
567 assert(a[40] == a)
568 assert(#res == 1 and res[1] == prog)
569
570 local prog = "rawseti -1 100; return *"
571 local res = {T.testC(prog, a)}
572 assert(a[100] == a)
573 assert(#res == 1 and res[1] == prog)
574
575 local prog = "seti -1 200; return *"
576 local res = {T.testC(prog, a)}
577 assert(a[200] == a)
578 assert(#res == 1 and res[1] == prog)
579end
580
581a = {x=0, y=12}
582x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
583 a, 3, "y", 4, "x")
584assert(x == 0 and y == 12)
585T.testC("settable -5", a, 3, 4, "x", 15)
586assert(a.x == 15)
587a[a] = print
588x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
589assert(x == print)
590T.testC("settable 2", a, "x") -- table and key are the same object!
591assert(a[a] == "x")
592
593b = setmetatable({p = a}, {})
594getmetatable(b).__index = function (t, i) return t.p[i] end
595local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
596assert(x == 15 and k == 35)
597k = T.testC("getfield 2 y, return 1", b)
598assert(k == 12)
599getmetatable(b).__index = function (t, i) return a[i] end
600getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
601y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
602assert(y == 12)
603k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
604assert(a.x == 16 and k == 4)
605a[b] = 'xuxu'
606y = T.testC("gettable 2, return 1", b)
607assert(y == 'xuxu')
608T.testC("settable 2", b, 19)
609assert(a[b] == 19)
610
611--
612do -- testing getfield/setfield with long keys
613 local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
614 local a = T.testC([[
615 getfield 2 _012345678901234567890123456789012345678901234567890123456789
616 return 1
617 ]], t)
618 assert(a == 32)
619 local a = T.testC([[
620 pushnum 33
621 setglobal _012345678901234567890123456789012345678901234567890123456789
622 ]])
623 assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
624 _012345678901234567890123456789012345678901234567890123456789 = nil
625end
626
627-- testing next
628a = {}
629t = pack(T.testC("next; return *", a, nil))
630tcheck(t, {n=1,a})
631a = {a=3}
632t = pack(T.testC("next; return *", a, nil))
633tcheck(t, {n=3,a,'a',3})
634t = pack(T.testC("next; pop 1; next; return *", a, nil))
635tcheck(t, {n=1,a})
636
637
638
639-- testing upvalues
640
641do
642 local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
643 t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
644 assert(b == 10 and c == 20 and type(t) == 'table')
645 a, b = A([[tostring U3; tonumber U4; return 2]])
646 assert(a == nil and b == 0)
647 A([[pushnum 100; pushnum 200; replace U2; replace U1]])
648 b, c = A([[pushvalue U1; pushvalue U2; return 2]])
649 assert(b == 100 and c == 200)
650 A([[replace U2; replace U1]], {x=1}, {x=2})
651 b, c = A([[pushvalue U1; pushvalue U2; return 2]])
652 assert(b.x == 1 and c.x == 2)
653 T.checkmemory()
654end
655
656
657-- testing absent upvalues from C-function pointers
658assert(T.testC[[isnull U1; return 1]] == true)
659assert(T.testC[[isnull U100; return 1]] == true)
660assert(T.testC[[pushvalue U1; return 1]] == nil)
661
662local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
663assert(T.upvalue(f, 1) == 10 and
664 T.upvalue(f, 2) == 20 and
665 T.upvalue(f, 3) == nil)
666T.upvalue(f, 2, "xuxu")
667assert(T.upvalue(f, 2) == "xuxu")
668
669
670-- large closures
671do
672 local A = "checkstack 300 msg;" ..
673 string.rep("pushnum 10;", 255) ..
674 "pushcclosure 255; return 1"
675 A = T.testC(A)
676 for i=1,255 do
677 assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
678 end
679 assert(A("isnull U256; return 1"))
680 assert(not A("isnil U256; return 1"))
681end
682
683
684
685-- testing get/setuservalue
686-- bug in 5.1.2
687checkerr("got number", debug.setuservalue, 3, {})
688checkerr("got nil", debug.setuservalue, nil, {})
689checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
690
691-- testing multiple user values
692local b = T.newuserdata(0, 10)
693for i = 1, 10 do
694 local v, p = debug.getuservalue(b, i)
695 assert(v == nil and p)
696end
697do -- indices out of range
698 local v, p = debug.getuservalue(b, -2)
699 assert(v == nil and not p)
700 local v, p = debug.getuservalue(b, 11)
701 assert(v == nil and not p)
702end
703local t = {true, false, 4.56, print, {}, b, "XYZ"}
704for k, v in ipairs(t) do
705 debug.setuservalue(b, v, k)
706end
707for k, v in ipairs(t) do
708 local v1, p = debug.getuservalue(b, k)
709 assert(v1 == v and p)
710end
711
712assert(not debug.getuservalue(4))
713
714debug.setuservalue(b, function () return 10 end, 10)
715collectgarbage() -- function should not be collected
716assert(debug.getuservalue(b, 10)() == 10)
717
718debug.setuservalue(b, 134)
719collectgarbage() -- number should not be a problem for collector
720assert(debug.getuservalue(b) == 134)
721
722
723-- test barrier for uservalues
724do
725 local oldmode = collectgarbage("incremental")
726 T.gcstate("atomic")
727 assert(T.gccolor(b) == "black")
728 debug.setuservalue(b, {x = 100})
729 T.gcstate("pause") -- complete collection
730 assert(debug.getuservalue(b).x == 100) -- uvalue should be there
731 collectgarbage(oldmode)
732end
733
734-- long chain of userdata
735for i = 1, 1000 do
736 local bb = T.newuserdata(0, 1)
737 debug.setuservalue(bb, b)
738 b = bb
739end
740collectgarbage() -- nothing should not be collected
741for i = 1, 1000 do
742 b = debug.getuservalue(b)
743end
744assert(debug.getuservalue(b).x == 100)
745b = nil
746
747
748-- testing locks (refs)
749
750-- reuse of references
751local i = T.ref{}
752T.unref(i)
753assert(T.ref{} == i)
754
755local Arr = {}
756local Lim = 100
757for i=1,Lim do -- lock many objects
758 Arr[i] = T.ref({})
759end
760
761assert(T.ref(nil) == -1 and T.getref(-1) == nil)
762T.unref(-1); T.unref(-1)
763
764for i=1,Lim do -- unlock all them
765 T.unref(Arr[i])
766end
767
768local function printlocks ()
769 local f = T.makeCfunc("gettable R; return 1")
770 local n = f("n")
771 print("n", n)
772 for i=0,n do
773 print(i, f(i))
774 end
775end
776
777
778for i=1,Lim do -- lock many objects
779 Arr[i] = T.ref({})
780end
781
782for i=1,Lim,2 do -- unlock half of them
783 T.unref(Arr[i])
784end
785
786assert(type(T.getref(Arr[2])) == 'table')
787
788
789assert(T.getref(-1) == nil)
790
791
792a = T.ref({})
793
794collectgarbage()
795
796assert(type(T.getref(a)) == 'table')
797
798
799-- colect in cl the `val' of all collected userdata
800local tt = {}
801local cl = {n=0}
802A = nil; B = nil
803local F
804F = function (x)
805 local udval = T.udataval(x)
806 table.insert(cl, udval)
807 local d = T.newuserdata(100) -- create garbage
808 d = nil
809 assert(debug.getmetatable(x).__gc == F)
810 assert(load("table.insert({}, {})"))() -- create more garbage
811 assert(not collectgarbage()) -- GC during GC (no op)
812 local dummy = {} -- create more garbage during GC
813 if A ~= nil then
814 assert(type(A) == "userdata")
815 assert(T.udataval(A) == B)
816 debug.getmetatable(A) -- just access it
817 end
818 A = x -- ressurect userdata
819 B = udval
820 return 1,2,3
821end
822tt.__gc = F
823
824
825-- test whether udate collection frees memory in the right time
826do
827 collectgarbage();
828 collectgarbage();
829 local x = collectgarbage("count");
830 local a = T.newuserdata(5001)
831 assert(T.testC("objsize 2; return 1", a) == 5001)
832 assert(collectgarbage("count") >= x+4)
833 a = nil
834 collectgarbage();
835 assert(collectgarbage("count") <= x+1)
836 -- udata without finalizer
837 x = collectgarbage("count")
838 collectgarbage("stop")
839 for i=1,1000 do T.newuserdata(0) end
840 assert(collectgarbage("count") > x+10)
841 collectgarbage()
842 assert(collectgarbage("count") <= x+1)
843 -- udata with finalizer
844 collectgarbage()
845 x = collectgarbage("count")
846 collectgarbage("stop")
847 a = {__gc = function () end}
848 for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
849 assert(collectgarbage("count") >= x+10)
850 collectgarbage() -- this collection only calls TM, without freeing memory
851 assert(collectgarbage("count") >= x+10)
852 collectgarbage() -- now frees memory
853 assert(collectgarbage("count") <= x+1)
854 collectgarbage("restart")
855end
856
857
858collectgarbage("stop")
859
860-- create 3 userdatas with tag `tt'
861a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
862b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
863c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
864
865-- create userdata without meta table
866x = T.newuserdata(4)
867y = T.newuserdata(0)
868
869checkerr("FILE%* expected, got userdata", io.input, a)
870checkerr("FILE%* expected, got userdata", io.input, x)
871
872assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
873
874local d = T.ref(a);
875local e = T.ref(b);
876local f = T.ref(c);
877t = {T.getref(d), T.getref(e), T.getref(f)}
878assert(t[1] == a and t[2] == b and t[3] == c)
879
880t=nil; a=nil; c=nil;
881T.unref(e); T.unref(f)
882
883collectgarbage()
884
885-- check that unref objects have been collected
886assert(#cl == 1 and cl[1] == nc)
887
888x = T.getref(d)
889assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
890x =nil
891tt.b = b -- create cycle
892tt=nil -- frees tt for GC
893A = nil
894b = nil
895T.unref(d);
896local n5 = T.newuserdata(0)
897debug.setmetatable(n5, {__gc=F})
898n5 = T.udataval(n5)
899collectgarbage()
900assert(#cl == 4)
901-- check order of collection
902assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
903
904collectgarbage"restart"
905
906
907a, na = {}, {}
908for i=30,1,-1 do
909 a[i] = T.newuserdata(0)
910 debug.setmetatable(a[i], {__gc=F})
911 na[i] = T.udataval(a[i])
912end
913cl = {}
914a = nil; collectgarbage()
915assert(#cl == 30)
916for i=1,30 do assert(cl[i] == na[i]) end
917na = nil
918
919
920for i=2,Lim,2 do -- unlock the other half
921 T.unref(Arr[i])
922end
923
924x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
925assert(T.testC("objsize 2; return 1", x) == 41)
926cl = {}
927a = {[x] = 1}
928x = T.udataval(x)
929collectgarbage()
930-- old `x' cannot be collected (`a' still uses it)
931assert(#cl == 0)
932for n in pairs(a) do a[n] = undef end
933collectgarbage()
934assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
935
936-- testing lua_equal
937assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
938assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
939assert(T.testC("compare EQ 2 3; return 1", nil, nil))
940assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
941assert(not T.testC("compare EQ 2 3; return 1"))
942assert(not T.testC("compare EQ 2 3; return 1", 3))
943
944-- testing lua_equal with fallbacks
945do
946 local map = {}
947 local t = {__eq = function (a,b) return map[a] == map[b] end}
948 local function f(x)
949 local u = T.newuserdata(0)
950 debug.setmetatable(u, t)
951 map[u] = x
952 return u
953 end
954 assert(f(10) == f(10))
955 assert(f(10) ~= f(11))
956 assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
957 assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
958 t.__eq = nil
959 assert(f(10) ~= f(10))
960end
961
962print'+'
963
964
965
966-- testing changing hooks during hooks
967_G.TT = {}
968T.sethook([[
969 # set a line hook after 3 count hooks
970 sethook 4 0 '
971 getglobal TT;
972 pushvalue -3; append -2
973 pushvalue -2; append -2
974 ']], "c", 3)
975local a = 1 -- counting
976a = 1 -- counting
977a = 1 -- count hook (set line hook)
978a = 1 -- line hook
979a = 1 -- line hook
980debug.sethook()
981local t = _G.TT
982assert(t[1] == "line")
983local line = t[2]
984assert(t[3] == "line" and t[4] == line + 1)
985assert(t[5] == "line" and t[6] == line + 2)
986assert(t[7] == nil)
987_G.TT = nil
988
989
990-------------------------------------------------------------------------
991do -- testing errors during GC
992 warn("@off")
993 collectgarbage("stop")
994 local a = {}
995 for i=1,20 do
996 a[i] = T.newuserdata(i) -- creates several udata
997 end
998 for i=1,20,2 do -- mark half of them to raise errors during GC
999 debug.setmetatable(a[i],
1000 {__gc = function (x) error("@expected error in gc") end})
1001 end
1002 for i=2,20,2 do -- mark the other half to count and to create more garbage
1003 debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
1004 end
1005 a = nil
1006 _G.A = 0
1007 collectgarbage()
1008 assert(A == 10) -- number of normal collections
1009 collectgarbage("restart")
1010 warn("@on")
1011end
1012_G.A = nil
1013-------------------------------------------------------------------------
1014-- test for userdata vals
1015do
1016 local a = {}; local lim = 30
1017 for i=0,lim do a[i] = T.pushuserdata(i) end
1018 for i=0,lim do assert(T.udataval(a[i]) == i) end
1019 for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
1020 for i=0,lim do a[a[i]] = i end
1021 for i=0,lim do a[T.pushuserdata(i)] = i end
1022 assert(type(tostring(a[1])) == "string")
1023end
1024
1025
1026-------------------------------------------------------------------------
1027-- testing multiple states
1028T.closestate(T.newstate());
1029L1 = T.newstate()
1030assert(L1)
1031
1032assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
1033
1034
1035assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
1036
1037a, b = T.doremote(L1, "return f()")
1038assert(a == 'alo' and b == '3')
1039
1040T.doremote(L1, "_ERRORMESSAGE = nil")
1041-- error: `sin' is not defined
1042a, b, c = T.doremote(L1, "return sin(1)")
1043assert(a == nil and c == 2) -- 2 == run-time error
1044
1045-- error: syntax error
1046a, b, c = T.doremote(L1, "return a+")
1047assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
1048
1049T.loadlib(L1)
1050a, b, c = T.doremote(L1, [[
1051 string = require'string'
1052 a = require'_G'; assert(a == _G and require("_G") == a)
1053 io = require'io'; assert(type(io.read) == "function")
1054 assert(require("io") == io)
1055 a = require'table'; assert(type(a.insert) == "function")
1056 a = require'debug'; assert(type(a.getlocal) == "function")
1057 a = require'math'; assert(type(a.sin) == "function")
1058 return string.sub('okinama', 1, 2)
1059]])
1060assert(a == "ok")
1061
1062T.closestate(L1);
1063
1064
1065L1 = T.newstate()
1066T.loadlib(L1)
1067T.doremote(L1, "a = {}")
1068T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
1069 settable -3]])
1070assert(T.doremote(L1, "return a.x") == "1")
1071
1072T.closestate(L1)
1073
1074L1 = nil
1075
1076print('+')
1077-------------------------------------------------------------------------
1078-- testing to-be-closed variables
1079-------------------------------------------------------------------------
1080print"testing to-be-closed variables"
1081
1082do
1083 local openresource = {}
1084
1085 local function newresource ()
1086 local x = setmetatable({10}, {__close = function(y)
1087 assert(openresource[#openresource] == y)
1088 openresource[#openresource] = nil
1089 y[1] = y[1] + 1
1090 end})
1091 openresource[#openresource + 1] = x
1092 return x
1093 end
1094
1095 local a, b = T.testC([[
1096 call 0 1 # create resource
1097 pushnil
1098 toclose -2 # mark call result to be closed
1099 toclose -1 # mark nil to be closed (will be ignored)
1100 return 2
1101 ]], newresource)
1102 assert(a[1] == 11 and b == nil)
1103 assert(#openresource == 0) -- was closed
1104
1105 -- repeat the test, but calling function in a 'multret' context
1106 local a = {T.testC([[
1107 call 0 1 # create resource
1108 toclose 2 # mark it to be closed
1109 return 2
1110 ]], newresource)}
1111 assert(type(a[1]) == "string" and a[2][1] == 11)
1112 assert(#openresource == 0) -- was closed
1113
1114 -- closing by error
1115 local a, b = pcall(T.makeCfunc[[
1116 call 0 1 # create resource
1117 toclose -1 # mark it to be closed
1118 error # resource is the error object
1119 ]], newresource)
1120 assert(a == false and b[1] == 11)
1121 assert(#openresource == 0) -- was closed
1122
1123 -- non-closable value
1124 local a, b = pcall(T.makeCfunc[[
1125 newtable # create non-closable object
1126 toclose -1 # mark it to be closed (should raise an error)
1127 abort # will not be executed
1128 ]])
1129 assert(a == false and
1130 string.find(b, "non%-closable value"))
1131
1132 local function check (n)
1133 assert(#openresource == n)
1134 end
1135
1136 -- closing resources with 'closeslot'
1137 _ENV.xxx = true
1138 local a = T.testC([[
1139 pushvalue 2 # stack: S, NR, CH, NR
1140 call 0 1 # create resource; stack: S, NR, CH, R
1141 toclose -1 # mark it to be closed
1142 pushvalue 2 # stack: S, NR, CH, R, NR
1143 call 0 1 # create another resource; stack: S, NR, CH, R, R
1144 toclose -1 # mark it to be closed
1145 pushvalue 3 # stack: S, NR, CH, R, R, CH
1146 pushint 2 # there should be two open resources
1147 call 1 0 # stack: S, NR, CH, R, R
1148 closeslot -1 # close second resource
1149 pushvalue 3 # stack: S, NR, CH, R, R, CH
1150 pushint 1 # there should be one open resource
1151 call 1 0 # stack: S, NR, CH, R, R
1152 closeslot 4
1153 setglobal "xxx" # previous op. erased the slot
1154 pop 1 # pop other resource from the stack
1155 pushint *
1156 return 1 # return stack size
1157 ]], newresource, check)
1158 assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack
1159
1160 -- closing resources with 'pop'
1161 local a = T.testC([[
1162 pushvalue 2 # stack: S, NR, CH, NR
1163 call 0 1 # create resource; stack: S, NR, CH, R
1164 toclose -1 # mark it to be closed
1165 pushvalue 2 # stack: S, NR, CH, R, NR
1166 call 0 1 # create another resource; stack: S, NR, CH, R, R
1167 toclose -1 # mark it to be closed
1168 pushvalue 3 # stack: S, NR, CH, R, R, CH
1169 pushint 2 # there should be two open resources
1170 call 1 0 # stack: S, NR, CH, R, R
1171 pop 1 # pop second resource
1172 pushvalue 3 # stack: S, NR, CH, R, CH
1173 pushint 1 # there should be one open resource
1174 call 1 0 # stack: S, NR, CH, R
1175 pop 1 # pop other resource from the stack
1176 pushvalue 3 # stack: S, NR, CH, CH
1177 pushint 0 # there should be no open resources
1178 call 1 0 # stack: S, NR, CH
1179 pushint *
1180 return 1 # return stack size
1181 ]], newresource, check)
1182 assert(a == 3) -- no extra items left in the stack
1183
1184 -- non-closable value
1185 local a, b = pcall(T.makeCfunc[[
1186 pushint 32
1187 toclose -1
1188 ]])
1189 assert(not a and string.find(b, "(C temporary)"))
1190
1191end
1192
1193
1194--[[
1195** {==================================================================
1196** Testing memory limits
1197** ===================================================================
1198--]]
1199
1200print("memory-allocation errors")
1201
1202checkerr("block too big", T.newuserdata, math.maxinteger)
1203collectgarbage()
1204local f = load"local a={}; for i=1,100000 do a[i]=i end"
1205T.alloccount(10)
1206checkerr(MEMERRMSG, f)
1207T.alloccount() -- remove limit
1208
1209
1210-- test memory errors; increase limit for maximum memory by steps,
1211-- o that we get memory errors in all allocations of a given
1212-- task, until there is enough memory to complete the task without
1213-- errors.
1214local function testbytes (s, f)
1215 collectgarbage()
1216 local M = T.totalmem()
1217 local oldM = M
1218 local a,b = nil
1219 while true do
1220 collectgarbage(); collectgarbage()
1221 T.totalmem(M)
1222 a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
1223 T.totalmem(0) -- remove limit
1224 if a and b == "OK" then break end -- stop when no more errors
1225 if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
1226 error(a, 0) -- propagate it
1227 end
1228 M = M + 7 -- increase memory limit
1229 end
1230 print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
1231 return a
1232end
1233
1234-- test memory errors; increase limit for number of allocations one
1235-- by one, so that we get memory errors in all allocations of a given
1236-- task, until there is enough allocations to complete the task without
1237-- errors.
1238
1239local function testalloc (s, f)
1240 collectgarbage()
1241 local M = 0
1242 local a,b = nil
1243 while true do
1244 collectgarbage(); collectgarbage()
1245 T.alloccount(M)
1246 a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
1247 T.alloccount() -- remove limit
1248 if a and b == "OK" then break end -- stop when no more errors
1249 if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
1250 error(a, 0) -- propagate it
1251 end
1252 M = M + 1 -- increase allocation limit
1253 end
1254 print(string.format("minimum allocations for %s: %d allocations", s, M))
1255 return a
1256end
1257
1258
1259local function testamem (s, f)
1260 testalloc(s, f)
1261 return testbytes(s, f)
1262end
1263
1264
1265-- doing nothing
1266b = testamem("doing nothing", function () return 10 end)
1267assert(b == 10)
1268
1269-- testing memory errors when creating a new state
1270
1271testamem("state creation", function ()
1272 local st = T.newstate()
1273 if st then T.closestate(st) end -- close new state
1274 return st
1275end)
1276
1277testamem("empty-table creation", function ()
1278 return {}
1279end)
1280
1281testamem("string creation", function ()
1282 return "XXX" .. "YYY"
1283end)
1284
1285testamem("coroutine creation", function()
1286 return coroutine.create(print)
1287end)
1288
1289
1290-- testing to-be-closed variables
1291testamem("to-be-closed variables", function()
1292 local flag
1293 do
1294 local x <close> =
1295 setmetatable({}, {__close = function () flag = true end})
1296 flag = false
1297 local x = {}
1298 end
1299 return flag
1300end)
1301
1302
1303-- testing threads
1304
1305-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
1306local mt = T.testC("rawgeti R 1; return 1")
1307assert(type(mt) == "thread" and coroutine.running() == mt)
1308
1309
1310
1311local function expand (n,s)
1312 if n==0 then return "" end
1313 local e = string.rep("=", n)
1314 return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
1315 e, s, expand(n-1,s), e)
1316end
1317
1318G=0; collectgarbage(); a =collectgarbage("count")
1319load(expand(20,"G=G+1"))()
1320assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
1321G = nil
1322
1323testamem("running code on new thread", function ()
1324 return T.doonnewstack("local x=1") == 0 -- try to create thread
1325end)
1326
1327
1328-- testing memory x compiler
1329
1330testamem("loadstring", function ()
1331 return load("x=1") -- try to do load a string
1332end)
1333
1334
1335local testprog = [[
1336local function foo () return end
1337local t = {"x"}
1338AA = "aaa"
1339for i = 1, #t do AA = AA .. t[i] end
1340return true
1341]]
1342
1343-- testing memory x dofile
1344_G.AA = nil
1345local t =os.tmpname()
1346local f = assert(io.open(t, "w"))
1347f:write(testprog)
1348f:close()
1349testamem("dofile", function ()
1350 local a = loadfile(t)
1351 return a and a()
1352end)
1353assert(os.remove(t))
1354assert(_G.AA == "aaax")
1355
1356
1357-- other generic tests
1358
1359testamem("gsub", function ()
1360 local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
1361 return (a == 'ablo ablo')
1362end)
1363
1364testamem("dump/undump", function ()
1365 local a = load(testprog)
1366 local b = a and string.dump(a)
1367 a = b and load(b)
1368 return a and a()
1369end)
1370
1371_G.AA = nil
1372
1373local t = os.tmpname()
1374testamem("file creation", function ()
1375 local f = assert(io.open(t, 'w'))
1376 assert (not io.open"nomenaoexistente")
1377 io.close(f);
1378 return not loadfile'nomenaoexistente'
1379end)
1380assert(os.remove(t))
1381
1382testamem("table creation", function ()
1383 local a, lim = {}, 10
1384 for i=1,lim do a[i] = i; a[i..'a'] = {} end
1385 return (type(a[lim..'a']) == 'table' and a[lim] == lim)
1386end)
1387
1388testamem("constructors", function ()
1389 local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
1390 return (type(a) == 'table' and a.e == 5)
1391end)
1392
1393local a = 1
1394local close = nil
1395testamem("closure creation", function ()
1396 function close (b)
1397 return function (x) return b + x end
1398 end
1399 return (close(2)(4) == 6)
1400end)
1401
1402testamem("using coroutines", function ()
1403 local a = coroutine.wrap(function ()
1404 coroutine.yield(string.rep("a", 10))
1405 return {}
1406 end)
1407 assert(string.len(a()) == 10)
1408 return a()
1409end)
1410
1411do -- auxiliary buffer
1412 local lim = 100
1413 local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
1414 testamem("auxiliary buffer", function ()
1415 return (#table.concat(a, ",") == 20*lim + lim - 1)
1416 end)
1417end
1418
1419testamem("growing stack", function ()
1420 local function foo (n)
1421 if n == 0 then return 1 else return 1 + foo(n - 1) end
1422 end
1423 return foo(100)
1424end)
1425
1426-- }==================================================================
1427
1428
1429do -- testing failing in 'lua_checkstack'
1430 local res = T.testC([[rawcheckstack 500000; return 1]])
1431 assert(res == false)
1432 local L = T.newstate()
1433 T.alloccount(0) -- will be unable to reallocate the stack
1434 res = T.testC(L, [[rawcheckstack 5000; return 1]])
1435 T.alloccount()
1436 T.closestate(L)
1437 assert(res == false)
1438end
1439
1440do -- closing state with no extra memory
1441 local L = T.newstate()
1442 T.alloccount(0)
1443 T.closestate(L)
1444 T.alloccount()
1445end
1446
1447do -- garbage collection with no extra memory
1448 local L = T.newstate()
1449 T.loadlib(L)
1450 local res = (T.doremote(L, [[
1451 _ENV = require"_G"
1452 local T = require"T"
1453 local a = {}
1454 for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
1455 local stsize, stuse = T.querystr()
1456 assert(stuse > 1000)
1457 local function foo (n)
1458 if n > 0 then foo(n - 1) end
1459 end
1460 foo(180) -- grow stack
1461 local _, stksize = T.stacklevel()
1462 assert(stksize > 180)
1463 a = nil
1464 T.alloccount(0)
1465 collectgarbage()
1466 T.alloccount()
1467 -- stack and string table could not be reallocated,
1468 -- so they kept their sizes (without errors)
1469 assert(select(2, T.stacklevel()) == stksize)
1470 assert(T.querystr() == stsize)
1471 return 'ok'
1472 ]]))
1473 assert(res == 'ok')
1474 T.closestate(L)
1475end
1476
1477print'+'
1478
1479-- testing some auxlib functions
1480local function gsub (a, b, c)
1481 a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
1482 assert(b == 5)
1483 return a
1484end
1485
1486assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
1487assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
1488assert(gsub("", "alo", "//") == "")
1489assert(gsub("...", ".", "/.") == "/././.")
1490assert(gsub("...", "...", "") == "")
1491
1492
1493-- testing luaL_newmetatable
1494local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1495assert(type(mt_xuxu) == "table" and res and top == 3)
1496local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
1497assert(mt_xuxu == d and not res and top == 3)
1498d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
1499assert(mt_xuxu ~= d and res and top == 3)
1500
1501x = T.newuserdata(0);
1502y = T.newuserdata(0);
1503T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
1504assert(getmetatable(x) == mt_xuxu)
1505
1506-- testing luaL_testudata
1507-- correct metatable
1508local res1, res2, top = T.testC([[testudata -1 xuxu
1509 testudata 2 xuxu
1510 gettop
1511 return 3]], x)
1512assert(res1 and res2 and top == 4)
1513
1514-- wrong metatable
1515res1, res2, top = T.testC([[testudata -1 xuxu1
1516 testudata 2 xuxu1
1517 gettop
1518 return 3]], x)
1519assert(not res1 and not res2 and top == 4)
1520
1521-- non-existent type
1522res1, res2, top = T.testC([[testudata -1 xuxu2
1523 testudata 2 xuxu2
1524 gettop
1525 return 3]], x)
1526assert(not res1 and not res2 and top == 4)
1527
1528-- userdata has no metatable
1529res1, res2, top = T.testC([[testudata -1 xuxu
1530 testudata 2 xuxu
1531 gettop
1532 return 3]], y)
1533assert(not res1 and not res2 and top == 4)
1534
1535-- erase metatables
1536do
1537 local r = debug.getregistry()
1538 assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
1539 r.xuxu = nil; r.xuxu1 = nil
1540end
1541
1542print'OK'
1543