Fixed Program custom env

This commit is contained in:
Robert Jelic
2025-04-24 10:20:36 +02:00
parent d6be0ada49
commit 050e0736d1

View File

@@ -23,10 +23,11 @@ BasaltProgram.__index = BasaltProgram
local newPackage = dofile("rom/modules/main/cc/require.lua").make local newPackage = dofile("rom/modules/main/cc/require.lua").make
---@private ---@private
function BasaltProgram.new(program) function BasaltProgram.new(program, env, addEnvironment)
local self = setmetatable({}, BasaltProgram) local self = setmetatable({}, BasaltProgram)
self.env = {} self.env = env or {}
self.args = {} self.args = {}
self.addEnvironment = addEnvironment == nil and true or addEnvironment
self.program = program self.program = program
return self return self
end end
@@ -60,9 +61,14 @@ function BasaltProgram:run(path, width, height)
env.term.native = function () env.term.native = function ()
return self.window return self.window
end end
for k,v in pairs(self.env) do if(self.addEnvironment)then
env[k] = v for k,v in pairs(self.env) do
env[k] = v
end
else
env = self.env
end end
self.coroutine = coroutine.create(function() self.coroutine = coroutine.create(function()
local program = load(content, "@/" .. path, nil, env) local program = load(content, "@/" .. path, nil, env)
@@ -162,11 +168,13 @@ end
--- Executes a program --- Executes a program
--- @shortDescription Executes a program --- @shortDescription Executes a program
--- @param path string The path to the program --- @param path string The path to the program
--- @param env? table The environment to run the program in
--- @param addEnvironment? boolean Whether to add the environment to the program's environment (false = overwrite instead of adding)
--- @return Program self The Program instance --- @return Program self The Program instance
function Program:execute(path) function Program:execute(path, env, addEnvironment)
self.set("path", path) self.set("path", path)
self.set("running", true) self.set("running", true)
local program = BasaltProgram.new(self) local program = BasaltProgram.new(self, env, addEnvironment)
self.set("program", program) self.set("program", program)
program:run(path, self.get("width"), self.get("height")) program:run(path, self.get("width"), self.get("height"))
self:updateRender() self:updateRender()