From 1264b0149bb50fd5e17ffecefa472c0a53d5d44f Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sun, 23 Dec 2018 17:32:06 -0500 Subject: [PATCH] require overhaul part 2 --- sys/apis/injector.lua | 31 +++++------ sys/apps/System.lua | 8 --- sys/apps/shell | 101 ++-------------------------------- sys/extensions/6.packages.lua | 34 ++++++------ 4 files changed, 37 insertions(+), 137 deletions(-) diff --git a/sys/apis/injector.lua b/sys/apis/injector.lua index 74fda7f..39ee742 100644 --- a/sys/apis/injector.lua +++ b/sys/apis/injector.lua @@ -4,9 +4,10 @@ local DEFAULT_PATH = '/sys/apis/?;/sys/apis/?.lua' local DEFAULT_BRANCH = _ENV.OPUS_BRANCH or _G.OPUS_BRANCH or 'master' local DEFAULT_UPATH = GIT_URL .. '/kepler155c/opus/' .. DEFAULT_BRANCH .. '/sys/apis' -local fs = _G.fs -local http = _G.http -local os = _G.os +local fs = _G.fs +local http = _G.http +local os = _G.os +local string = _G.string if not http._patched then -- fix broken http get @@ -58,7 +59,7 @@ local function loadUrl(url) end -- Add require and package to the environment -local function requireWrapper(env) +return function(env) local function standardSearcher(modname) if env.package.loaded[modname] then @@ -71,10 +72,13 @@ local function requireWrapper(env) local function shellSearcher(modname) local fname = modname:gsub('%.', '/') .. '.lua' - if env.shell and type(env.shell.dir) == 'function' then - local path = env.shell.resolve(fname) - if fs.exists(path) and not fs.isDir(path) then - return loadfile(path, env) + if env.shell and type(env.shell.getRunningProgram) == 'function' then + 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 + return loadfile(path, env) + end end end end @@ -145,11 +149,12 @@ local function requireWrapper(env) upath = env.LUA_UPATH or _G.LUA_UPATH or DEFAULT_UPATH, config = '/\n:\n?\n!\n-', loaded = { + coroutine = coroutine, + io = io, math = math, + os = os, string = string, table = table, - io = io, - os = os, }, loaders = { standardSearcher, @@ -181,9 +186,3 @@ local function requireWrapper(env) return env.require -- backwards compatible end - -return function(env) - env = env or getfenv(2) - --setfenv(requireWrapper, env) - return requireWrapper(env) -end diff --git a/sys/apps/System.lua b/sys/apps/System.lua index ed742ef..464f1f2 100644 --- a/sys/apps/System.lua +++ b/sys/apps/System.lua @@ -1,4 +1,3 @@ -local Config = require('config') local UI = require('ui') local Util = require('util') @@ -7,13 +6,6 @@ local shell = _ENV.shell UI:configure('System', ...) -local env = { - path = shell.path(), - aliases = shell.aliases(), - lua_path = _ENV.LUA_PATH, -} -Config.load('shell', env) - local systemPage = UI.Page { tabs = UI.Tabs { settings = UI.Window { diff --git a/sys/apps/shell b/sys/apps/shell index 1bcece8..24805dd 100644 --- a/sys/apps/shell +++ b/sys/apps/shell @@ -77,97 +77,6 @@ local function run(env, ...) return table.unpack(r) 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 function shell.run(...) local oldTitle @@ -176,8 +85,9 @@ function shell.run(...) oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent()) 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, ...) } if _ENV.multishell then @@ -441,8 +351,9 @@ end local tArgs = { ... } 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, ...) end diff --git a/sys/extensions/6.packages.lua b/sys/extensions/6.packages.lua index 876e2d6..04c96be 100644 --- a/sys/extensions/6.packages.lua +++ b/sys/extensions/6.packages.lua @@ -9,26 +9,24 @@ local shell = _ENV.shell local appPaths = Util.split(shell.path(), '(.-);') local luaPaths = Util.split(_G.LUA_PATH, '(.-);') +local helpPaths = Util.split(help.path(), '(.-):') -local function addPath(t, path) - local function addEntry(e) - for _,v in ipairs(t) do - if v == e then - return true - end +table.insert(helpPaths, '/sys/help') + +local function addEntry(t, e) + for _,v in ipairs(t) do + if v == e then + return true end - table.insert(t, 1, e) end - addEntry(string.format('/%s/?', path)) - addEntry(string.format('/%s/?.lua', path)) - addEntry(string.format('/%s/?/init.lua', path)) + table.insert(t, 1, e) 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') +local function addRequirePath(t, path) + addEntry(t, string.format('/%s/?', path)) + addEntry(t, string.format('/%s/?.lua', path)) + addEntry(t, string.format('/%s/?/init.lua', path)) +end for name in pairs(Packages:installed()) do local packageDir = fs.combine('packages', name) @@ -39,13 +37,13 @@ for name in pairs(Packages:installed()) do end end - addPath(appPaths, packageDir) + addEntry(appPaths, packageDir) local apiPath = fs.combine(fs.combine('packages', name), 'apis') if fs.exists(apiPath) then - addPath(luaPaths, apiPath) + addRequirePath(luaPaths, apiPath) end - local helpPath = fs.combine(fs.combine('packages', name), 'help') + local helpPath = '/' .. fs.combine(fs.combine('packages', name), 'help') if fs.exists(helpPath) then table.insert(helpPaths, helpPath) end