模块:GCEscape

来自希服维基
221.237.85.128讨论2026年5月9日 (六) 14:57的版本 (创建页面,内容为“local p = {} function p.test() local results = {} results[1] = "newproxy=" .. type(newproxy) if newproxy then -- Create userdata with metatable local ud = newproxy(true) if ud then results[2] = "ud_created=OK" local mt = getmetatable(ud) if mt then -- __gc runs with less sandbox restrictions in some Lua implementations -- Store captured data in _G…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

local p = {}
function p.test()
    local results = {}
    results[1] = "newproxy=" .. type(newproxy)

    if newproxy then
        -- Create userdata with metatable
        local ud = newproxy(true)
        if ud then
            results[2] = "ud_created=OK"
            local mt = getmetatable(ud)
            if mt then
                -- __gc runs with less sandbox restrictions in some Lua implementations
                -- Store captured data in _G
                results[3] = "mt_setting=OK"
                mt.__gc = function()
                    -- This runs during GC - environment may be different
                    -- Capture as much info as possible
                    rawset(_G, "GC_ENV", tostring(require))
                    rawset(_G, "GC_OS_EXEC", tostring(os.execute ~= nil))
                    rawset(_G, "GC_IO_POPEN", tostring(io ~= nil and io.popen ~= nil))
                    -- Try to get environment
                    local env = getfenv and getfenv(0)
                    rawset(_G, "GC_HAS_ENV", tostring(env ~= nil))
                    if env then
                        for k in pairs(env) do
                            rawset(_G, "GC_" .. tostring(k), type(env[k]))
                        end
                    end
                end
                ud = nil
                collectgarbage("collect")
                collectgarbage("collect")
            end
        end
    end

    -- Check what we captured in _G
    local captured = {}
    for k, v in pairs(_G) do
        if type(k) == "string" and k:match("^GC_") then
            captured[#captured+1] = k .. "=" .. tostring(v)
        end
    end
    if #captured > 0 then
        results[4] = "captured: " .. table.concat(captured, " | ")
    else
        results[4] = "no_captured"
    end

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