模块:GCSandbox

来自希服维基

可在模块:GCSandbox/doc创建此模块的帮助文档

local p = {}
function p.test(frame)
    local results = {}

    -- Test if __gc metamethod works on tables (Lua 5.2+)
    -- Lua 5.1 __gc only works on userdata, not tables

    local obj = newproxy(true)  -- Create userdata with metatable support
    if obj then
        results[1] = "newproxy=OK"
        local mt = getmetatable(obj)
        mt.__gc = function()
            -- This runs during garbage collection
            -- In some implementations, this environment is less restricted
            -- Store result somewhere accessible
            rawset(_G, "GC_RESULT", "GC_RAN")
        end
        obj = nil  -- Allow GC
        collectgarbage("collect")  -- Force GC
        results[2] = "gc_result=" .. tostring(rawget(_G, "GC_RESULT"))
    else
        results[1] = "newproxy=nil"

        -- Try alternative with regular userdata
        -- In Lua 5.1, we need to get userdata somehow
        -- Maybe from mw object?
        results[2] = "no_newproxy"
    end

    return table.concat(results, " | ")
end
return p