Skip to content

Commit 050e073

Browse files
committed
Fixed Program custom env
1 parent d6be0ad commit 050e073

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/elements/Program.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ BasaltProgram.__index = BasaltProgram
2323
local newPackage = dofile("rom/modules/main/cc/require.lua").make
2424

2525
---@private
26-
function BasaltProgram.new(program)
26+
function BasaltProgram.new(program, env, addEnvironment)
2727
local self = setmetatable({}, BasaltProgram)
28-
self.env = {}
28+
self.env = env or {}
2929
self.args = {}
30+
self.addEnvironment = addEnvironment == nil and true or addEnvironment
3031
self.program = program
3132
return self
3233
end
@@ -60,9 +61,14 @@ function BasaltProgram:run(path, width, height)
6061
env.term.native = function ()
6162
return self.window
6263
end
63-
for k,v in pairs(self.env) do
64-
env[k] = v
64+
if(self.addEnvironment)then
65+
for k,v in pairs(self.env) do
66+
env[k] = v
67+
end
68+
else
69+
env = self.env
6570
end
71+
6672

6773
self.coroutine = coroutine.create(function()
6874
local program = load(content, "@/" .. path, nil, env)
@@ -162,11 +168,13 @@ end
162168
--- Executes a program
163169
--- @shortDescription Executes a program
164170
--- @param path string The path to the program
171+
--- @param env? table The environment to run the program in
172+
--- @param addEnvironment? boolean Whether to add the environment to the program's environment (false = overwrite instead of adding)
165173
--- @return Program self The Program instance
166-
function Program:execute(path)
174+
function Program:execute(path, env, addEnvironment)
167175
self.set("path", path)
168176
self.set("running", true)
169-
local program = BasaltProgram.new(self)
177+
local program = BasaltProgram.new(self, env, addEnvironment)
170178
self.set("program", program)
171179
program:run(path, self.get("width"), self.get("height"))
172180
self:updateRender()

0 commit comments

Comments
 (0)