aboutsummaryrefslogtreecommitdiff
path: root/testes/gc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/gc.lua')
-rw-r--r--testes/gc.lua87
1 files changed, 50 insertions, 37 deletions
diff --git a/testes/gc.lua b/testes/gc.lua
index 8b9179c8..84e8ffb7 100644
--- a/testes/gc.lua
+++ b/testes/gc.lua
@@ -353,40 +353,36 @@ GC()
353 353
354 354
355-- testing errors during GC 355-- testing errors during GC
356do 356if T then
357collectgarbage("stop") -- stop collection 357 collectgarbage("stop") -- stop collection
358local u = {} 358 local u = {}
359local s = {}; setmetatable(s, {__mode = 'k'}) 359 local s = {}; setmetatable(s, {__mode = 'k'})
360setmetatable(u, {__gc = function (o) 360 setmetatable(u, {__gc = function (o)
361 local i = s[o] 361 local i = s[o]
362 s[i] = true 362 s[i] = true
363 assert(not s[i - 1]) -- check proper finalization order 363 assert(not s[i - 1]) -- check proper finalization order
364 if i == 8 then error("here") end -- error during GC 364 if i == 8 then error("@expected@") end -- error during GC
365end}) 365 end})
366 366
367for i = 6, 10 do 367 for i = 6, 10 do
368 local n = setmetatable({}, getmetatable(u)) 368 local n = setmetatable({}, getmetatable(u))
369 s[n] = i 369 s[n] = i
370end 370 end
371
372assert(not pcall(collectgarbage))
373for i = 8, 10 do assert(s[i]) end
374
375for i = 1, 5 do
376 local n = setmetatable({}, getmetatable(u))
377 s[n] = i
378end
379 371
380collectgarbage() 372 collectgarbage()
381for i = 1, 10 do assert(s[i]) end 373 assert(string.find(_WARN, "error in __gc metamethod"))
374 assert(string.match(_WARN, "@(.-)@") == "expected")
375 for i = 8, 10 do assert(s[i]) end
382 376
383getmetatable(u).__gc = false 377 for i = 1, 5 do
378 local n = setmetatable({}, getmetatable(u))
379 s[n] = i
380 end
384 381
382 collectgarbage()
383 for i = 1, 10 do assert(s[i]) end
385 384
386-- __gc errors with non-string messages 385 getmetatable(u).__gc = false
387setmetatable({}, {__gc = function () error{} end})
388local a, b = pcall(collectgarbage)
389assert(not a and type(b) == "string" and string.find(b, "error in __gc"))
390 386
391end 387end
392print '+' 388print '+'
@@ -478,9 +474,11 @@ end
478 474
479 475
480-- errors during collection 476-- errors during collection
481u = setmetatable({}, {__gc = function () error "!!!" end}) 477if T then
482u = nil 478 u = setmetatable({}, {__gc = function () error "@expected error" end})
483assert(not pcall(collectgarbage)) 479 u = nil
480 collectgarbage()
481end
484 482
485 483
486if not _soft then 484if not _soft then
@@ -645,11 +643,26 @@ do
645end 643end
646 644
647-- create several objects to raise errors when collected while closing state 645-- create several objects to raise errors when collected while closing state
648do 646if T then
649 local mt = {__gc = function (o) return o + 1 end} 647 local error, assert, warn, find = error, assert, warn, string.find
650 for i = 1,10 do 648 local n = 0
649 local lastmsg
650 local mt = {__gc = function (o)
651 n = n + 1
652 assert(n == o[1])
653 if n == 1 then
654 _WARN = nil
655 elseif n == 2 then
656 assert(find(_WARN, "@expected warning"))
657 lastmsg = _WARN -- get message from previous error (first 'o')
658 else
659 assert(lastmsg == _WARN) -- subsequent error messages are equal
660 end
661 error"@expected warning"
662 end}
663 for i = 10, 1, -1 do
651 -- create object and preserve it until the end 664 -- create object and preserve it until the end
652 table.insert(___Glob, setmetatable({}, mt)) 665 table.insert(___Glob, setmetatable({i}, mt))
653 end 666 end
654end 667end
655 668