@@ -23,12 +23,6 @@ const {
2323} = require ( '../luaconf.js' ) ;
2424const { lua_assert } = require ( './llimits.js' ) ;
2525const I64 = require ( './lint64.js' ) ;
26- const allTables = new Set ( ) ;
27- let allocationsSinceCollection = 0 ;
28- let instructionsSinceCollection = 0 ;
29- let simulatedMemoryKb = 1 ;
30- let gcRunning = true ;
31- let collecting = false ;
3226const ldebug = require ( './ldebug.js' ) ;
3327const lobject = require ( './lobject.js' ) ;
3428const {
@@ -130,7 +124,7 @@ class Table {
130124 this . finalized = false ;
131125 this . finalizerPending = false ;
132126 this . flags = ~ 0 ;
133- allTables . add ( this ) ;
127+ L . l_G . gc . tables . add ( this ) ;
134128 }
135129}
136130
@@ -410,24 +404,31 @@ const needsDeferredFinalization = function(table) {
410404 return false ;
411405} ;
412406
413- const luaH_setrunning = function ( running ) {
414- gcRunning = ! ! running ;
407+ const luaH_setrunning = function ( L , running ) {
408+ L . l_G . gc . running = ! ! running ;
415409} ;
416410
417- const luaH_memory = function ( ) {
418- return simulatedMemoryKb ;
411+ const luaH_isrunning = function ( L ) {
412+ return L . l_G . gc . running ;
413+ } ;
414+
415+ const luaH_memory = function ( L ) {
416+ return L . l_G . gc . simulatedMemoryKb ;
419417} ;
420418
421419const luaH_maybe_gc = function ( L ) {
422- if ( collecting || ! gcRunning ) return ;
423- instructionsSinceCollection ++ ;
424- if ( instructionsSinceCollection >= 10000 )
420+ const gc = L . l_G . gc ;
421+ if ( gc . collecting || ! gc . running || gc . closed ) return ;
422+ gc . instructionsSinceCollection ++ ;
423+ if ( gc . instructionsSinceCollection >= 10000 )
425424 luaH_collectgarbage ( L ) ;
426425} ;
427426
428427const luaH_collectgarbage = function ( L ) {
429- if ( collecting ) return ;
430- collecting = true ;
428+ const gc = L . l_G . gc ;
429+ const allTables = gc . tables ;
430+ if ( gc . collecting || gc . closed ) return ;
431+ gc . collecting = true ;
431432 const marked = new Set ( ) ;
432433 const visitedTables = new Set ( ) ;
433434 let firstFinalizerError = null ;
@@ -637,18 +638,61 @@ const luaH_collectgarbage = function(L) {
637638 if ( ! marked . has ( table ) && ! table . finalizerPending )
638639 allTables . delete ( table ) ;
639640 }
640- simulatedMemoryKb = Math . max ( 1 , visitedTables . size ) ;
641+ gc . simulatedMemoryKb = Math . max ( 1 , visitedTables . size ) ;
641642 return firstFinalizerError ;
642643 } finally {
643- allocationsSinceCollection = 0 ;
644- instructionsSinceCollection = 0 ;
645- collecting = false ;
644+ gc . allocationsSinceCollection = 0 ;
645+ gc . instructionsSinceCollection = 0 ;
646+ gc . collecting = false ;
646647 if ( typeof console !== "undefined" &&
647648 typeof process !== "undefined" && process . env . LUANODE_GC_DEBUG )
648649 console . error ( "GC_END" , allTables . size , visitedTables . size ) ;
649650 }
650651} ;
651652
653+ /*
654+ ** Release the VM-owned table graph when its main state closes. Lua finalizers
655+ ** still run while the stack and registry are available; afterward all roots
656+ ** and collector bookkeeping are detached from the state.
657+ */
658+ const luaH_close = function ( L ) {
659+ const gc = L . l_G . gc ;
660+ if ( gc . closed ) return ;
661+ gc . collecting = true ;
662+ const lapi = require ( './lapi.js' ) ;
663+ const gcName = luaS_bless ( L , to_luastring ( "__gc" , true ) ) ;
664+
665+ try {
666+ const finalizableTables = Array . from ( gc . tables ) . reverse ( ) ;
667+ for ( const table of finalizableTables ) {
668+ if ( table . finalized || ! table . metatable ) continue ;
669+ const finalizer = luaH_getstr ( table . metatable , gcName ) ;
670+ if ( ! finalizer . ttisfunction ( ) ) continue ;
671+
672+ table . finalized = true ;
673+ table . finalizerPending = false ;
674+ const base = L . top ;
675+ lobject . pushobj2s ( L , finalizer ) ;
676+ lobject . pushobj2s ( L , new lobject . TValue ( LUA_TTABLE , table ) ) ;
677+ try {
678+ lapi . lua_pcall ( L , 1 , 0 , 0 ) ;
679+ } catch ( error ) {
680+ L . status = 0 ;
681+ } finally {
682+ L . top = base ;
683+ }
684+ }
685+ } finally {
686+ gc . tables . clear ( ) ;
687+ gc . allocationsSinceCollection = 0 ;
688+ gc . instructionsSinceCollection = 0 ;
689+ gc . simulatedMemoryKb = 1 ;
690+ gc . running = false ;
691+ gc . collecting = false ;
692+ gc . closed = true ;
693+ }
694+ } ;
695+
652696/*
653697** Try to find a boundary in table 't'. A 'boundary' is an integer index
654698** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
@@ -721,17 +765,21 @@ module.exports.luaH_getint = luaH_getint;
721765module . exports . luaH_getn = luaH_getn ;
722766module . exports . luaH_getstr = luaH_getstr ;
723767module . exports . luaH_collectgarbage = luaH_collectgarbage ;
768+ module . exports . luaH_close = luaH_close ;
724769module . exports . luaH_maybe_gc = luaH_maybe_gc ;
725770module . exports . luaH_setrunning = luaH_setrunning ;
771+ module . exports . luaH_isrunning = luaH_isrunning ;
726772module . exports . luaH_memory = luaH_memory ;
727773module . exports . luaH_setfrom = luaH_setfrom ;
728774module . exports . luaH_setint = luaH_setint ;
729775module . exports . luaH_setmeta = luaH_setmode ;
730776module . exports . luaH_new = function ( L ) {
777+ const gc = L . l_G . gc ;
778+ if ( gc . closed ) throw new Error ( "cannot allocate a Lua table after lua_close" ) ;
731779 const table = new Table ( L ) ;
732- simulatedMemoryKb ++ ;
733- allocationsSinceCollection ++ ;
734- if ( ! collecting && gcRunning && allocationsSinceCollection >= 100 ) {
780+ gc . simulatedMemoryKb ++ ;
781+ gc . allocationsSinceCollection ++ ;
782+ if ( ! gc . collecting && gc . running && gc . allocationsSinceCollection >= 100 ) {
735783 try {
736784 luaH_collectgarbage ( L ) ;
737785 } catch ( e ) {
0 commit comments