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