diff options
Diffstat (limited to 'testes/gc.lua')
-rw-r--r-- | testes/gc.lua | 87 |
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 |
356 | do | 356 | if T then |
357 | collectgarbage("stop") -- stop collection | 357 | collectgarbage("stop") -- stop collection |
358 | local u = {} | 358 | local u = {} |
359 | local s = {}; setmetatable(s, {__mode = 'k'}) | 359 | local s = {}; setmetatable(s, {__mode = 'k'}) |
360 | setmetatable(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 |
365 | end}) | 365 | end}) |
366 | 366 | ||
367 | for 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 |
370 | end | 370 | end |
371 | |||
372 | assert(not pcall(collectgarbage)) | ||
373 | for i = 8, 10 do assert(s[i]) end | ||
374 | |||
375 | for i = 1, 5 do | ||
376 | local n = setmetatable({}, getmetatable(u)) | ||
377 | s[n] = i | ||
378 | end | ||
379 | 371 | ||
380 | collectgarbage() | 372 | collectgarbage() |
381 | for 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 | ||
383 | getmetatable(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 |
387 | setmetatable({}, {__gc = function () error{} end}) | ||
388 | local a, b = pcall(collectgarbage) | ||
389 | assert(not a and type(b) == "string" and string.find(b, "error in __gc")) | ||
390 | 386 | ||
391 | end | 387 | end |
392 | print '+' | 388 | print '+' |
@@ -478,9 +474,11 @@ end | |||
478 | 474 | ||
479 | 475 | ||
480 | -- errors during collection | 476 | -- errors during collection |
481 | u = setmetatable({}, {__gc = function () error "!!!" end}) | 477 | if T then |
482 | u = nil | 478 | u = setmetatable({}, {__gc = function () error "@expected error" end}) |
483 | assert(not pcall(collectgarbage)) | 479 | u = nil |
480 | collectgarbage() | ||
481 | end | ||
484 | 482 | ||
485 | 483 | ||
486 | if not _soft then | 484 | if not _soft then |
@@ -645,11 +643,26 @@ do | |||
645 | end | 643 | end |
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 |
648 | do | 646 | if 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 |
654 | end | 667 | end |
655 | 668 | ||