[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: GC and Locals
 
- From: Xmilia Hermit <xmilia.hermit@...>
 
- Date: Wed, 21 Jul 2021 23:17:02 +0200
 
I found this part in the lua manual:
An object is considered dead as soon as the collector can be sure the 
object will not be accessed again in the normal execution of the 
program. ("Normal execution" here excludes finalizers, which can 
resurrect dead objects (see §2.5.3), and excludes also operations using 
the debug library.)
This got me wondering if in the following example the local variable 
"tab" could get collected before the call to "f" since "tab" isn't 
accessed any more in the normal execution due to the exlusion of the 
debug library.
local function f(format)
   return (string.gsub(format, "{(%w+)}", function(var)
      local i, n, v = 2, debug.getlocal(4, 1)
      while n ~= var do
         n, v = debug.getlocal(4, i)
         if n == nil then error("Var '" .. var .. "' not found") end
         i = i + 1
      end
      return tostring(v)
   end))
end
local tab = {}
collectgarbage("collect") -- tab still alive since it will be used in 
the next line
print(tab)
collectgarbage("collect") -- tab collected here since it will never be 
accessed in "Normal execution"
-- local tab still in scope but was collected
print(f"tab = {tab}") -- tab might be undefined
-- More sophisticated (Lua 5.1) example of string interpolation can be 
found here:
--
http://lua-users.org/wiki/StringInterpolation#:~:text=Hack:%20Using%20debug%20to%20Access%20Lexicals
-- https://luarocks.org/modules/hisham/f-strings
Regards,
Xmilia