1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-27 00:50:26 +00:00

require overhaul part 2

This commit is contained in:
kepler155c@gmail.com 2018-12-23 17:32:06 -05:00
parent 26564cbcc1
commit 1264b0149b
4 changed files with 37 additions and 137 deletions

View File

@ -7,6 +7,7 @@ local DEFAULT_UPATH = GIT_URL .. '/kepler155c/opus/' .. DEFAULT_BRANCH .. '/sys
local fs = _G.fs local fs = _G.fs
local http = _G.http local http = _G.http
local os = _G.os local os = _G.os
local string = _G.string
if not http._patched then if not http._patched then
-- fix broken http get -- fix broken http get
@ -58,7 +59,7 @@ local function loadUrl(url)
end end
-- Add require and package to the environment -- Add require and package to the environment
local function requireWrapper(env) return function(env)
local function standardSearcher(modname) local function standardSearcher(modname)
if env.package.loaded[modname] then if env.package.loaded[modname] then
@ -71,13 +72,16 @@ local function requireWrapper(env)
local function shellSearcher(modname) local function shellSearcher(modname)
local fname = modname:gsub('%.', '/') .. '.lua' local fname = modname:gsub('%.', '/') .. '.lua'
if env.shell and type(env.shell.dir) == 'function' then if env.shell and type(env.shell.getRunningProgram) == 'function' then
local path = env.shell.resolve(fname) local running = env.shell.getRunningProgram()
if running then
local path = fs.combine(fs.getDir(running), fname)
if fs.exists(path) and not fs.isDir(path) then if fs.exists(path) and not fs.isDir(path) then
return loadfile(path, env) return loadfile(path, env)
end end
end end
end end
end
local function pathSearcher(modname) local function pathSearcher(modname)
local fname = modname:gsub('%.', '/') local fname = modname:gsub('%.', '/')
@ -145,11 +149,12 @@ local function requireWrapper(env)
upath = env.LUA_UPATH or _G.LUA_UPATH or DEFAULT_UPATH, upath = env.LUA_UPATH or _G.LUA_UPATH or DEFAULT_UPATH,
config = '/\n:\n?\n!\n-', config = '/\n:\n?\n!\n-',
loaded = { loaded = {
coroutine = coroutine,
io = io,
math = math, math = math,
os = os,
string = string, string = string,
table = table, table = table,
io = io,
os = os,
}, },
loaders = { loaders = {
standardSearcher, standardSearcher,
@ -181,9 +186,3 @@ local function requireWrapper(env)
return env.require -- backwards compatible return env.require -- backwards compatible
end end
return function(env)
env = env or getfenv(2)
--setfenv(requireWrapper, env)
return requireWrapper(env)
end

View File

@ -1,4 +1,3 @@
local Config = require('config')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@ -7,13 +6,6 @@ local shell = _ENV.shell
UI:configure('System', ...) UI:configure('System', ...)
local env = {
path = shell.path(),
aliases = shell.aliases(),
lua_path = _ENV.LUA_PATH,
}
Config.load('shell', env)
local systemPage = UI.Page { local systemPage = UI.Page {
tabs = UI.Tabs { tabs = UI.Tabs {
settings = UI.Window { settings = UI.Window {

View File

@ -77,97 +77,6 @@ local function run(env, ...)
return table.unpack(r) return table.unpack(r)
end end
local function createShellEnv(sDir)
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
local package
package = {
loaded = {
_G = _G,
bit32 = bit32,
coroutine = coroutine,
math = math,
package = package,
string = string,
table = table,
},
path = _G.LUA_PATH,
config = "/\n;\n?\n!\n-",
preload = { },
loaders = {
function( name )
if package.preload[name] then
return package.preload[name]
else
return nil, "no field package.preload['" .. name .. "']"
end
end,
function( name )
local fname = string.gsub(name, "%.", "/")
local sError = ""
for pattern in string.gmatch(package.path, "[^;]+") do
local sPath = string.gsub(pattern, "%?", fname)
if sPath:sub(1,1) ~= "/" then
sPath = fs.combine(sDir, sPath)
end
if fs.exists(sPath) and not fs.isDir(sPath) then
local fnFile, sError = loadfile( sPath, tEnv )
if fnFile then
return fnFile, sPath
else
return nil, sError
end
else
if #sError > 0 then
sError = sError .. "\n"
end
sError = sError .. "no file '" .. sPath .. "'"
end
end
return nil, sError
end
}
}
local sentinel = {}
local function require( name )
if type( name ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
end
if package.loaded[name] == sentinel then
error("Loop detected requiring '" .. name .. "'", 0)
end
if package.loaded[name] then
return package.loaded[name]
end
local sError = "Error loading module '" .. name .. "':"
for _,searcher in ipairs(package.loaders) do
local loader, err = searcher(name)
if loader then
package.loaded[name] = sentinel
local result = loader( err )
if result ~= nil then
package.loaded[name] = result
return result
else
package.loaded[name] = true
return true
end
else
sError = sError .. "\n" .. err
end
end
error(sError, 2)
end
tEnv["package"] = package
tEnv["require"] = require
return tEnv
end
-- Install shell API -- Install shell API
function shell.run(...) function shell.run(...)
local oldTitle local oldTitle
@ -176,8 +85,9 @@ function shell.run(...)
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent()) oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
end end
local env = createShellEnv(shell.dir()) local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G }) _G.requireInjector(env)
local r = { pcall(run, env, ...) } local r = { pcall(run, env, ...) }
if _ENV.multishell then if _ENV.multishell then
@ -441,8 +351,9 @@ end
local tArgs = { ... } local tArgs = { ... }
if #tArgs > 0 then if #tArgs > 0 then
local env = createShellEnv(shell.dir()) local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G }) _G.requireInjector(env)
return run(env, ...) return run(env, ...)
end end

View File

@ -9,9 +9,11 @@ local shell = _ENV.shell
local appPaths = Util.split(shell.path(), '(.-);') local appPaths = Util.split(shell.path(), '(.-);')
local luaPaths = Util.split(_G.LUA_PATH, '(.-);') local luaPaths = Util.split(_G.LUA_PATH, '(.-);')
local helpPaths = Util.split(help.path(), '(.-):')
local function addPath(t, path) table.insert(helpPaths, '/sys/help')
local function addEntry(e)
local function addEntry(t, e)
for _,v in ipairs(t) do for _,v in ipairs(t) do
if v == e then if v == e then
return true return true
@ -19,17 +21,13 @@ local function addPath(t, path)
end end
table.insert(t, 1, e) table.insert(t, 1, e)
end end
addEntry(string.format('/%s/?', path))
addEntry(string.format('/%s/?.lua', path)) local function addRequirePath(t, path)
addEntry(string.format('/%s/?/init.lua', path)) addEntry(t, string.format('/%s/?', path))
addEntry(t, string.format('/%s/?.lua', path))
addEntry(t, string.format('/%s/?/init.lua', path))
end end
-- dependency graph
-- https://github.com/mpeterv/depgraph/blob/master/src/depgraph/init.lua
local helpPaths = Util.split(help.path(), '(.-):')
table.insert(helpPaths, '/sys/help')
for name in pairs(Packages:installed()) do for name in pairs(Packages:installed()) do
local packageDir = fs.combine('packages', name) local packageDir = fs.combine('packages', name)
if fs.exists(fs.combine(packageDir, '.install')) then if fs.exists(fs.combine(packageDir, '.install')) then
@ -39,13 +37,13 @@ for name in pairs(Packages:installed()) do
end end
end end
addPath(appPaths, packageDir) addEntry(appPaths, packageDir)
local apiPath = fs.combine(fs.combine('packages', name), 'apis') local apiPath = fs.combine(fs.combine('packages', name), 'apis')
if fs.exists(apiPath) then if fs.exists(apiPath) then
addPath(luaPaths, apiPath) addRequirePath(luaPaths, apiPath)
end end
local helpPath = fs.combine(fs.combine('packages', name), 'help') local helpPath = '/' .. fs.combine(fs.combine('packages', name), 'help')
if fs.exists(helpPath) then if fs.exists(helpPath) then
table.insert(helpPaths, helpPath) table.insert(helpPaths, helpPath)
end end