-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add library support to Luacontrollers #557
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -459,6 +459,16 @@ local function get_digiline_send(pos, itbl, send_warning) | |||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
mesecon.luacontroller_libraries = {} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have this mentioned somewhere, eg. in the code comment at the top of the file. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
local function get_require(env) | ||||||||||||||||||||||||||||||
return function(name) | ||||||||||||||||||||||||||||||
if mesecon.luacontroller_libraries[name] then | ||||||||||||||||||||||||||||||
return mesecon.tablecopy_change_env(mesecon.luacontroller_libraries[name],env) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could make this more general by having library getter functions in
Suggested change
Note that I've also added Such a getter function could still change the env of their functions if it wants to. And it has to ensure there are no security flaws, but that's the case anyway, a simpler api without the deepcopy-with-env-change might make this even easier. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
local safe_globals = { | ||||||||||||||||||||||||||||||
-- Don't add pcall/xpcall unless willing to deal with the consequences (unless very careful, incredibly likely to allow killing server indirectly) | ||||||||||||||||||||||||||||||
"assert", "error", "ipairs", "next", "pairs", "select", | ||||||||||||||||||||||||||||||
|
@@ -546,6 +556,8 @@ local function create_environment(pos, mem, event, itbl, send_warning) | |||||||||||||||||||||||||||||
for _, name in pairs(safe_globals) do | ||||||||||||||||||||||||||||||
env[name] = _G[name] | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
env.require = get_require(env) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return env | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the new variable?
newfunc
is still the same function asv
.You are setting the env of
v
. This might be useful for functions that use functions from the luacontroller env. But in general I think this is at least rather inconvenient. If you want to make the wholeminetest
table accessible, like you suggested in the title comment, you will have to wrap nearly every function because otherwise it will not work, or crash because it's not a lua function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose I misunderstood the function passing behavior in Lua here - after reading up on that some, I see I could clean this up a little. But the setfenv() is still needed (I can just do it on v) in order for the main purpose of these libraries - interacting with LuaC capabilities - to work. Shoving the entire "minetest" table in there was more of just sort of a wild suggestion and not really an intended use case.