This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
snoble2022's profile picture

lua help os.run. enviroment help please

Started by snoble2022, 10 December 2012 - 03:42 AM
snoble2022 #1
Posted 10 December 2012 - 04:42 AM
Hello, how do I create an enviroment for os.run. it needs to include everything exceept fs api io api and a file api its using. It also needs to include ( i mean be able to use my other api).vthanks
KaoS #2
Posted 10 December 2012 - 05:31 AM
well if you use os.run(_G,program) it will have the normal environment, if you want to give it its own environment you make a table for that and then let it access the _G table for anything it lacks (you have to include _G so it can still use your existing vars and functions)


local myenviron=setmetatable({},{__index=_G})
os.run(myenviron,program)
faubiguy #3
Posted 10 December 2012 - 03:14 PM
os.run automatically makes the environment inherit from _G, so you can't run a program with an environment that doesn't have things that are in _G (with os.run). However you can make it use an environment that overrides variables and functions in _G, such as:
local emptyAPI = setmetatable({}, {__index = function() return function() end end})
local env = {fs = emptyAPI, io = emptyAPI, file = myFileAPI}
os.run(env, "example/program", "argument")

This will make all the functions in fs and io do nothing in the program being run.
KaoS #4
Posted 11 December 2012 - 02:59 AM
hmmm…. it seems my information is out of date. faubiguy is right