github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/userdata.lua (about)

     1  -- This test file makes use of a custom "testudata" function that returns a new
     2  -- userdata instance which prints a message when garbage collected.  See
     3  -- runtime/lua_test.go for the implementation of testudata.
     4  
     5  u1 = testudata("u1") -- this will print the line "**release u1**" when collected
     6  u2 = testudata("u2")
     7  
     8  function withgc(s)
     9      local x = testudata(s)
    10      debug.setmetatable(x, {
    11          __gc=function() print("finalize " .. s) end,
    12      })
    13      return x 
    14  end
    15  
    16  -- Resources are released when the runtime is closed - their finalizers run
    17  -- first
    18  u3 = withgc("u3")
    19  u4 = withgc("u4")
    20  
    21  --> =finalize u4
    22  --> =finalize u3
    23  --> =**release u4**
    24  --> =**release u3**
    25  
    26  -- The following were defined at the top of the test file
    27  --> =**release u2**
    28  --> =**release u1**