mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
Updated environment handling to localize the impact (#15).
This commit is contained in:
parent
3fcbb72531
commit
a5bec96f46
@ -103,7 +103,14 @@ local function s(t, opts)
|
||||
return not name and body..warn or "do local "..body..sepr..tail.."return "..name..sepr.."end"
|
||||
end
|
||||
|
||||
if not setfenv then -- Lua 5.2+
|
||||
local function deserialize(data, opts)
|
||||
local f, res = (loadstring or load)('return '..data)
|
||||
if not f then f, res = (loadstring or load)(data) end
|
||||
if not f then return f, res end
|
||||
if opts and opts.safe == false then return pcall(f) end
|
||||
|
||||
local setfenv, getfenv = setfenv, getfenv
|
||||
if not setfenv then -- Lua 5.2+
|
||||
-- based on http://lua-users.org/lists/lua-l/2010-06/msg00314.html
|
||||
-- this assumes f is a function
|
||||
local function findenv(f)
|
||||
@ -119,13 +126,7 @@ if not setfenv then -- Lua 5.2+
|
||||
local level = findenv(f)
|
||||
if level then debug.setupvalue(f, level, t) end
|
||||
return f end
|
||||
end
|
||||
|
||||
local function deserialize(data, opts)
|
||||
local f, res = (loadstring or load)('return '..data)
|
||||
if not f then f, res = (loadstring or load)(data) end
|
||||
if not f then return f, res end
|
||||
if opts and opts.safe == false then return pcall(f) end
|
||||
end
|
||||
|
||||
local env = {}
|
||||
env._G = setmetatable(env, { __index = getfenv(f) })
|
||||
|
Loading…
Reference in New Issue
Block a user