VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
|
| 11 Nov 2016 12:52 AM |
Ok, here's a pattern I'm going to use:
local api = require([path to api module]) local env = {api=api}
-- manually add safe functions -- lua-users wiki has a relevant page titled SandBoxes env.tostring = tostring ...
local func = loadstring([string with user script]) setfenv(func, env) func()
Is it true that setfenv works like setmetatable in that func will use env as it's environment even if called in different scope? |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 12:54 AM |
local function loadsafestring(code,env) local func=loadstring(code) setfenv(func,env) return func end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Nov 2016 12:55 AM |
| it'd only be truly safe if you make sure to sandbox getfenv among a couple of other things |
|
|
| Report Abuse |
|
|
VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
|
| 11 Nov 2016 12:56 AM |
| I won't add them to the environment in the first place |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 12:59 AM |
| I think setfenv replaces the environment table and not the metatable |
|
|
| Report Abuse |
|
|
| |
|
|
| 11 Nov 2016 01:03 AM |
| you can check by using rawget on the environment or next/pairs |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 11 Nov 2016 01:04 AM |
"I think setfenv replaces the environment table and not the metatable" That doesn't make sense. It replaces the whole table and the metatable is attached to that table. You overwrite the env you lose the table and thus it's metatable. |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 01:06 AM |
yea i said to remove "and"
'it replaces the environment table not the metatable' as in it sets the table to use as the environment |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 01:06 AM |
| not just the metatable** lol |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 01:08 AM |
if you want to mimic the way roblox does it to not confuse any scripts you could setfenv(func,setmetatable({},{__metatable="This metatable is locked";__index=myEnvironment})) |
|
|
| Report Abuse |
|
|
|
| 11 Nov 2016 12:40 PM |
@Wunder_Wufle
A little help, please!!
I get an error that says
"loadstring() is not available", but I already have the option enabled
|
|
|
| Report Abuse |
|
|
VilgO
|
  |
| Joined: 15 Feb 2011 |
| Total Posts: 518 |
|
|
| 11 Nov 2016 03:06 PM |
Are you sure you're calling it from a server script, not a LocalScript? http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#loadstring |
|
|
| Report Abuse |
|
|