lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

tankxx wrote:
> Excellent design!!! But I found a bug, or my misunderstanding.
>  gc will treat the BASE[-1] as a number, so the function in BASE[-1]
> will not be marked by gc. If the BASE[-1] is the only reference to the
> function, the function will be garbage collected. Then the program
> will be corrupted.

Good catch -- I've elided this detail in the interest of brevity:

The GC has to traverse all frames anyway, since it needs to find
the highest frame extent of all frames (*). The function objects
are already marked by this pass -- no point in marking them during
stack slot traversal, too.

(*) A caller (outer function) may have a higher top than a callee
(inner function). The extra slots must be cleaned up to avoid a
peculiar marking problem:

[aa[bb.X|   X created.
[aa[cc|     GC called from (small) inner frame, X destroyed.
[aa....X.|  GC called again in (larger) outer frame, X resurrected (ouch).

During GC in step 2 the stack must be cleaned up to the max. frame extent:

      ***|  Slots cleaned
   [cc|      from top of last frame
[aa......|   to max. frame extent.