You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This might not be an issue with sol2, but how lua itself works with environments, but here is the deal:
When loading a lua file via sol::script_file which contains e.g. two functions, setting the environment of one functions also changes the environment of the other. Same goes for when a call to sol::script defines two functions in one go.
Is this behaviour expected? (might be related to #1079 (comment))
The example below covers two scenarios (1 OK and 1 NOK):
First I create two functions with two seperate calls to sol::script. Setting the environment of one of these functions does not modify the environment of the other function - OK.
The second time I create the two functions using only one call to sol::script. Now, modifying the environment of one of these functions will change the environment of the other function too - NOK.
Personally, I expected the second case to behave just like the first one did.
I am completely off into the blue, but it looks like changing the environment for one functions, changes it for every function defined in that chunk 🤔
A potential workaround for the issue:
• dump the bytecode of the to-be-isolated function using sol::function::dump
• load it again using sol::script(dump.as_string_view())
• set the environment on the newly-loaded function
The text was updated successfully, but these errors were encountered:
When loading a chunk, the top-level function gets a new _ENV upvalue, and any nested functions inside it can see it. You can pretend that when loading works something like this:
local _ENV = _G
return function (...) -- this function is what's returned from load
-- code you passed to load goes here, with all global variable names replaced with _ENV lookups
-- so, for example "a = b" becomes "_ENV.a = _ENV.b" if neither a nor b were declared local
end
Now you can see that _ENV is an ordinary local variable, how all the functions have access to the _ENV, and why if one function changes _ENV all other functions in the loaded chunks will see the change. That's why if you want a function to only change its own environment, you need to make a new _ENV local that shadows the original one.
This might not be an issue with sol2, but how lua itself works with environments, but here is the deal:
When loading a lua file via
sol::script_file
which contains e.g. two functions, setting the environment of one functions also changes the environment of the other. Same goes for when a call tosol::script
defines two functions in one go.Is this behaviour expected? (might be related to #1079 (comment))
The example below covers two scenarios (1 OK and 1 NOK):
First I create two functions with two seperate calls to
sol::script
. Setting the environment of one of these functions does not modify the environment of the other function - OK.The second time I create the two functions using only one call to
sol::script
. Now, modifying the environment of one of these functions will change the environment of the other function too - NOK.Personally, I expected the second case to behave just like the first one did.
I am completely off into the blue, but it looks like changing the environment for one functions, changes it for every function defined in that chunk 🤔
A potential workaround for the issue:
• dump the bytecode of the to-be-isolated function using
sol::function::dump
• load it again using
sol::script(dump.as_string_view())
• set the environment on the newly-loaded function
The text was updated successfully, but these errors were encountered: