diff options
| author | Saikari <[email protected]> | 2026-02-01 08:57:49 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-02-01 08:57:49 +0300 |
| commit | 09a702d482edc5fb7218900460442822e1303810 (patch) | |
| tree | 932237381f013c2f54c7826e5099c606e0287fdd | |
| parent | a6c7c34b0a3cba1f95a3411232bc566c62a661d0 (diff) | |
refactor: enhance script handling to support inline Lua content execution
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/sandbox/module.lua | 69 | ||||
| -rw-r--r-- | xmake/modules/utils/run_script.lua | 7 | ||||
| -rw-r--r-- | xmake/plugins/lua/main.lua | 49 |
3 files changed, 80 insertions, 45 deletions
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index dd7dac16e..3c76da6ff 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -67,6 +67,41 @@ function core_sandbox_module._modulepath(name) return modulepath end +-- load module from string +function core_sandbox_module._loadstring(content, instance, name) + assert(content) + + -- load module script + local script, errors = load(content, name) + if not script then + return nil, errors + end + + -- with sandbox? + if instance then + + -- fork a new sandbox for this script + instance, errors = instance:fork(script, instance:rootdir()) + if not instance then + return nil, errors + end + + -- load module + local result, errors = instance:module() + if not result then + return nil, errors + end + return result, instance:script() + end + + -- load module without sandbox + local ok, result = utils.trycall(script) + if not ok then + return nil, result + end + return result, script +end + -- load module from file function core_sandbox_module._loadfile(filepath, instance) assert(filepath) @@ -510,21 +545,37 @@ function core_sandbox_module.import(name, opt) local instance = sandbox.instance() assert(instance) - -- the root directory for this sandbox script + -- rootdir is optional local rootdir = opt.rootdir or instance:rootdir() - -- init module directories (disable local packages?) - local modules_directories = (opt.nolocal or not rootdir) and core_sandbox_module.directories() or table.join(rootdir, core_sandbox_module.directories()) + -- load module from content? + local module + local errors + local found = false + if opt.content then + found = true + module, errors = core_sandbox_module._loadstring(opt.content, instance, name) + if module then + if not opt.nocache then + modules[name] = {module, nil} + end + end + else - -- load module - local loadopt = table.clone(opt) or {} - loadopt.instance = instance - loadopt.modules = modules - loadopt.modules_directories = modules_directories - local found, module, errors = core_sandbox_module._find_and_load(name, loadopt) + -- init module directories (disable local packages?) + local modules_directories = (opt.nolocal or not rootdir) and core_sandbox_module.directories() or table.join(rootdir, core_sandbox_module.directories()) + -- load module + local loadopt = table.clone(opt) or {} + loadopt.instance = instance + loadopt.modules = modules + loadopt.modules_directories = modules_directories + found, module, errors = core_sandbox_module._find_and_load(name, loadopt) + end + -- not found? attempt to load module.interface if not found and not opt.inherit then + -- get module name local found2 = false local errors2 = nil diff --git a/xmake/modules/utils/run_script.lua b/xmake/modules/utils/run_script.lua index e6f8469f6..a51f4eef0 100644 --- a/xmake/modules/utils/run_script.lua +++ b/xmake/modules/utils/run_script.lua @@ -61,7 +61,12 @@ function _run_script(script, args, opt) local script_type, script_name -- import and run script - if path.extension(script) == ".lua" and os.isfile(script) then + if opt.content then + + -- run the given lua script content + script_type, script_name = "given lua script content", script + func = import(script, {anonymous = true, content = opt.content}) + elseif path.extension(script) == ".lua" and os.isfile(script) then -- run the given lua script file (xmake lua /tmp/script.lua) script_type, script_name = "given lua script file", path.relative(script) diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index b6951c276..0a608b10a 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -59,45 +59,24 @@ function main() if script or from_stdin then -- run script from stdin? - local scriptfile_stdin + local script_content if script == "-" or from_stdin then - local script_content = io.read("*a") - if script_content then - scriptfile_stdin = os.tmpfile("xmake_lua_stdin_" .. hash.uuid4()) .. ".lua" - io.writefile(scriptfile_stdin, script_content) - if from_stdin and script and script ~= "-" then - arguments = arguments or {} - table.insert(arguments, 1, script) - end - script = scriptfile_stdin + script_content = io.read("*a") + if not script or script == "-" then + script = "xmake_lua_stdin" end end - try { - function () - if script then - run_script(script, { - curdir = os.workingdir(), - verbose = option.get("verbose"), - diagnosis = option.get("diagnosis"), - command = option.get("command"), - arguments = arguments, - deserialize = option.get("deserialize")}) - end - end, - catch { - function (errors) - raise(errors) - end - }, - finally { - function () - if scriptfile_stdin then - os.rm(scriptfile_stdin) - end - end - } - } + if script then + run_script(script, { + curdir = os.workingdir(), + verbose = option.get("verbose"), + diagnosis = option.get("diagnosis"), + command = option.get("command"), + arguments = arguments, + content = script_content, + deserialize = option.get("deserialize")}) + end else -- enter interactive mode sandbox.interactive() |
