local oldload = load function _G.load(str, arg1, arg2, arg3) local func = oldload(str, arg1, arg2, arg3) local env = getfenv(func) if (env == _G) then env = {} end setmetatable(env, {__index = _G, __newindex = function(table, var, val) rawset(env, var, val) end}) return func end
#1
Posted 10 February 2016 - 04:44 AM
#2
Posted 10 February 2016 - 10:21 AM
ViperLordX, on 10 February 2016 - 04:44 AM, said:
local oldload = load function _G.load(str, arg1, arg2, arg3) local func = oldload(str, arg1, arg2, arg3) local env = getfenv(func) if (env == _G) then env = {} end setmetatable(env, {__index = _G, __newindex = function(table, var, val) rawset(env, var, val) end}) return func end
Isn't env the same as _G for a given function?
#3
Posted 10 February 2016 - 04:10 PM
Creator, on 10 February 2016 - 10:21 AM, said:
Yes, but comparing the two will return false.
local function test() end getfenv( test ).randomvariable = true print( randomvariable ) print( getfenv( test ) == _G )
Edited by KingofGamesYami, 10 February 2016 - 04:30 PM.
#4
Posted 10 February 2016 - 07:37 PM
#5
Posted 10 February 2016 - 08:00 PM
1. Will behave differently than normal load() if a syntax error is encountered.
2. Doesn't handle loadfile, loadstring, or dofile.
3. No sanity checking.
#6
Posted 10 February 2016 - 08:28 PM
setfenv( 1, setmetatable( {}, { __index = _G } ) )
For the record, your __newindex entry isn't nessacary as new entries would be assigned to env anyways. Hell most of your code isn't nessacary actually, as load already accepts an environment argument by default.
local load = _G.load _G.load = function(str, name, mode, env) return load(str, name, mode, env or setmetatable({},{__index = _G})) end
Do note however that both your implementation, as well as mine, prevent access to the shell API as it is not stored in _G.
To be honest im confused as to how your implementation works in the first place, provided getfenv() returns _G you never set the functions environment to env.
#7
Posted 10 February 2016 - 08:44 PM
CometWolf, on 10 February 2016 - 08:28 PM, said:
setfenv( 1, setmetatable( {}, { __index = _G } ) )
For the record, your __newindex entry isn't nessacary as new entries would be assigned to env anyways. Hell most of your code isn't nessacary actually, as load already accepts an environment argument by default.
local load = _G.load _G.load = function(str, name, mode, env) return load(str, name, mode, env or setmetatable({},{__index = _G})) end
Do note however that both your implementation, as well as mine, prevent access to the shell API as it is not stored in _G.
To be honest im confused as to how your implementation works in the first place, provided getfenv() returns _G you never set the functions environment to env.
His code works because he getfenv's the function and then manipulates the table. He's using a weird setup - load from 5.2 and getfenv from 5.1...
#8
Posted 10 February 2016 - 08:47 PM
local oldload = load function _G.load(str, arg1, arg2, arg3) local func,err = oldload(str, arg1, arg2, arg3) if not (func) then return func, err end local env = getfenv(func) if (env == _G) then env = {} end setmetatable(env, {__index = _G}) setfenv(func, env) return func,err end loadstring = load
#9
Posted 10 February 2016 - 08:59 PM
ViperLordX, on 10 February 2016 - 08:47 PM, said:
...
You still have a weird amalgamation of Lua 5.2 and 5.1 by using load and getfenv. Choose one (preferably 5.2) and stick with it.
Edited by apemanzilla, 10 February 2016 - 08:59 PM.
#10
Posted 10 February 2016 - 09:06 PM
#11
Posted 10 February 2016 - 09:09 PM
apemanzilla, on 10 February 2016 - 08:44 PM, said:
ViperLordX, on 10 February 2016 - 08:47 PM, said:
Edited by CometWolf, 10 February 2016 - 09:10 PM.
#12
Posted 10 February 2016 - 09:40 PM
#13
Posted 11 February 2016 - 01:11 AM
local oldload = load function _G.load(str, arg1, arg2, arg3) local func,err = oldload(str, arg1, arg2, arg3) if not (func) then return func, err end local env = getfenv(func) for k, v in pairs(env) do _G[k] = v end if (env == _G) then env = {} end setmetatable(env, {__index = _G}) setfenv(func, env) return func,err end loadstring = load
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users