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

Editing env, after loading a chunk.

Started by Sewbacca, 27 September 2016 - 09:01 AM
Sewbacca #1
Posted 27 September 2016 - 11:01 AM
Hey guys,
i wanna load a constructor with different environments. First, i thought something like that:

local sData = File.readAll()
sData = '_ENV = ...\n' .. sData
local constructor = load(sData, sName, 't', _)
local object = constructor(setmetatable({}, { __index = _ENV}))
but would brake with binary constructors. It is an advantage that the program has to read and compile the string one time.

My next idea was:

local sData = File.readAll()

local constructor = function (tEnv)
  return load(sData, sName, 't', tEnv)
end
Now, the program won't be decreased by reading the hard disk, but by compiling the string again and again.

Is there any possibility to use the first mentioned option without editing the string?
So:
local object = constructor(tEnv)
Sewbacca

P.S. I don't want to use setfenv, because Lua 5.2 doesn't support setfenv
Edited on 28 September 2016 - 01:53 PM
Bomb Bloke #2
Posted 28 September 2016 - 12:48 AM
Is there any possibility to use the first mentioned option without editing the string?

I believe not.
Sewbacca #3
Posted 28 September 2016 - 03:55 PM
=(
Maybe, i will use a buffer, to load constructors and edit the _ENV, after requesting one or
i will do something like term.redirect with environments.
Edited on 28 September 2016 - 01:56 PM
TYKUHN2 #4
Posted 28 September 2016 - 07:45 PM
Yes but it is a complicated procedure I'd rather not try and design. It would exploit meta-tables and locality.