You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
987 B
Lua

-- https://github.com/mah0x211/lua-loadchunk
local load = load
local loadstring = loadstring
local loadfile = loadfile
local setfenv = setfenv
--- constants
local LUA_VERSION = tonumber(string.match(_VERSION, 'Lua (.+)$'))
--- evalstr
-- @param src
-- @param env
-- @param ident
-- @return fn
-- @return err
local function evalstr(src, env, ident)
if LUA_VERSION > 5.1 then
return load(src, ident, nil, env)
else
local fn, err = loadstring(src, ident)
if not err and env ~= nil then
setfenv(fn, env)
end
return fn, err
end
end
--- evalfile
-- @param file
-- @param env
-- @return fn
-- @return err
local function evalfile(file, env)
if LUA_VERSION > 5.1 then
return loadfile(file, nil, env)
else
local fn, err = loadfile(file)
if not err and env ~= nil then
setfenv(fn, env)
end
return fn, err
end
end
return {
string = evalstr,
file = evalfile,
}