@@ -31,30 +31,43 @@ function BasaltProgram.new(program)
3131 return self
3232end
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
3541function 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 )
90103
91104--- @private
92105function 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
118131
119132--- @private
120133function 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
124137end
0 commit comments