Skip to content

Commit b042f1b

Browse files
committed
- Added floor, ceil and abs to reactive env
- Changed BasaltProgram env (not finished) - Fixed program not setting running to false once coroutine is dead
1 parent 743e2f2 commit b042f1b

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/elements/Program.lua

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,43 @@ function BasaltProgram.new(program)
3131
return self
3232
end
3333

34+
local function createShellEnv(dir)
35+
local env = { shell = shell, multishell = multishell }
36+
env.require, env.package = newPackage(env, dir)
37+
return env
38+
end
39+
3440
---@private
3541
function BasaltProgram:run(path, width, height)
36-
self.window = window.create(term.current(), 1, 1, width, height, false)
42+
self.window = window.create(self.program:getBaseFrame():getTerm(), 1, 1, width, height, false)
3743
local pPath = shell.resolveProgram(path)
3844
if(pPath~=nil)then
3945
if(fs.exists(pPath)) then
4046
local file = fs.open(pPath, "r")
4147
local content = file.readAll()
4248
file.close()
43-
local env = setmetatable(self.env, {__index=_ENV})
49+
--[[local env = setmetatable(self.env, {__index=_ENV})
4450
env.shell = shell
4551
env.term = self.window
4652
env.require, env.package = newPackage(env, fs.getDir(pPath))
4753
env.term.current = term.current
4854
env.term.redirect = term.redirect
49-
env.term.native = term.native
55+
env.term.native = term.native]]
5056

57+
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
58+
env.term = self.window
59+
env.term.current = term.current
60+
env.term.native = function ()
61+
return self.window
62+
end
63+
for k,v in pairs(self.env) do
64+
env[k] = v
65+
end
66+
5167
self.coroutine = coroutine.create(function()
52-
local program = load(content, path, "bt", env)
68+
local program = load(content, "@/" .. path, nil, env)
5369
if program then
54-
local current = term.current()
55-
term.redirect(self.window)
5670
local result = program(path, table.unpack(self.args))
57-
term.redirect(current)
5871
return result
5972
end
6073
end)
@@ -90,7 +103,7 @@ end
90103

91104
---@private
92105
function BasaltProgram:resume(event, ...)
93-
if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then return end
106+
if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then self.program.set("running", false) return end
94107
if(self.filter~=nil)then
95108
if(event~=self.filter)then return end
96109
self.filter=nil
@@ -118,7 +131,7 @@ end
118131

119132
---@private
120133
function BasaltProgram:stop()
121-
if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then return end
134+
if self.coroutine==nil or coroutine.status(self.coroutine)=="dead" then self.program.set("running", false) return end
122135
coroutine.close(self.coroutine)
123136
self.coroutine = nil
124137
end

src/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ local function errorHandler(err)
1616
end
1717

1818
local ok, result = pcall(require, "main")
19+
package.loaded.main = nil
20+
package.loaded.log = nil
1921

2022
package.path = defaultPath
2123
if not ok then

src/main.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ local lazyElementCount = 10
3939
local lazyElementsTimer = 0
4040
local isLazyElementsTimerActive = false
4141

42-
4342
local function queueLazyElements()
4443
if(isLazyElementsTimerActive)then return end
4544
lazyElementsTimer = os.startTimer(0.2)
@@ -237,6 +236,7 @@ local function updateEvent(event, ...)
237236
for _, frame in pairs(activeFrames) do
238237
frame:dispatchEvent(event, ...)
239238
end
239+
--activeFrames[main]:dispatchEvent(event, ...) -- continue here
240240
end
241241

242242
for _, func in ipairs(basalt._schedule) do

src/plugins/reactive.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ local mathEnv = {
1414
end,
1515
round = function(val)
1616
return math.floor(val + 0.5)
17-
end
17+
end,
18+
floor = math.floor,
19+
ceil = math.ceil,
20+
abs = math.abs
1821
}
1922

2023
local function parseExpression(expr, element, propName)

0 commit comments

Comments
 (0)