mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
loadx for compat lua 5.1
This commit is contained in:
@@ -1,44 +1,4 @@
|
|||||||
local cookie
|
|
||||||
|
|
||||||
local function make_ld(ld)
|
|
||||||
local load_cookie
|
|
||||||
return function()
|
|
||||||
if load_cookie then
|
|
||||||
return ld()
|
|
||||||
else
|
|
||||||
load_cookie = true
|
|
||||||
return cookie
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if _VERSION == "Lua 5.2" then
|
if _VERSION == "Lua 5.2" then
|
||||||
|
|
||||||
cookie = "local function __setenv() end;"
|
|
||||||
|
|
||||||
local load = load
|
|
||||||
|
|
||||||
function _G.load(ld, chunkname, mode, env)
|
|
||||||
local f,err
|
|
||||||
if type(ld) == "string" then
|
|
||||||
f,err = load(cookie .. ld, chunkname, mode, env)
|
|
||||||
else
|
|
||||||
f,err = load(make_ld(ld), chunkname)
|
|
||||||
end
|
|
||||||
|
|
||||||
return f,err
|
|
||||||
end
|
|
||||||
|
|
||||||
function _G.loadfile(filename, mode, env)
|
|
||||||
local f = io.open(filename, "rb")
|
|
||||||
assert(f, filename)
|
|
||||||
local source = f:read "*a"
|
|
||||||
f:close()
|
|
||||||
return _G.load(source, "@" .. filename, mode, env)
|
|
||||||
end
|
|
||||||
|
|
||||||
_G.loadstring = _G.load
|
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -47,17 +7,10 @@ local assert = assert
|
|||||||
assert(_VERSION == "Lua 5.1")
|
assert(_VERSION == "Lua 5.1")
|
||||||
|
|
||||||
local setfenv = setfenv
|
local setfenv = setfenv
|
||||||
local getfenv = getfenv
|
|
||||||
local loadstring = loadstring
|
local loadstring = loadstring
|
||||||
local load = load
|
local load = load
|
||||||
local io = io
|
local io = io
|
||||||
|
|
||||||
local function __setenv(env)
|
|
||||||
setfenv(2,env)
|
|
||||||
end
|
|
||||||
|
|
||||||
cookie = "local _ENV,__setenv = __getenv();"
|
|
||||||
|
|
||||||
function _G.load(ld, chunkname, mode, env)
|
function _G.load(ld, chunkname, mode, env)
|
||||||
local f,err
|
local f,err
|
||||||
if type(ld) == "string" then
|
if type(ld) == "string" then
|
||||||
@@ -68,11 +21,9 @@ function _G.load(ld, chunkname, mode, env)
|
|||||||
if f == nil then
|
if f == nil then
|
||||||
return f, err
|
return f, err
|
||||||
end
|
end
|
||||||
env = env or getfenv(1)
|
if env then
|
||||||
setfenv(f, { __getenv = function()
|
setfenv(f, env)
|
||||||
setfenv(2, env)
|
end
|
||||||
return env, __setenv
|
|
||||||
end })
|
|
||||||
|
|
||||||
return f,err
|
return f,err
|
||||||
end
|
end
|
||||||
@@ -86,4 +37,4 @@ function _G.loadfile(filename, mode, env)
|
|||||||
end
|
end
|
||||||
|
|
||||||
_G.loadstring = _G.load
|
_G.loadstring = _G.load
|
||||||
_G._ENV = _G
|
|
||||||
|
|||||||
Reference in New Issue
Block a user