From d0e21bb8be22356b462aa1b266b43d1f2475535e Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Apr 2024 22:40:46 +0800 Subject: [PATCH 1/2] improve apis --- xmake/core/base/interpreter.lua | 30 +-- .../modules/import/core/base/interpreter.lua | 14 +- xmake/plugins/show/lists/apis.lua | 230 +++++++++++------- 3 files changed, 161 insertions(+), 113 deletions(-) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 1c85d870eb8..c8cdd97739a 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -604,32 +604,24 @@ function interpreter:_script(script) end -- get builtin modules -function interpreter._builtin_modules() +function interpreter.builtin_modules() local builtin_modules = interpreter._BUILTIN_MODULES if builtin_modules == nil then builtin_modules = {} local builtin_module_files = os.match(path.join(os.programdir(), "core/sandbox/modules/interpreter/*.lua")) if builtin_module_files then for _, builtin_module_file in ipairs(builtin_module_files) do - - -- the module name local module_name = path.basename(builtin_module_file) assert(module_name) - -- load script local script, errors = loadfile(builtin_module_file) if script then - - -- load module local ok, results = utils.trycall(script) if not ok then os.raise(results) end - - -- save module builtin_modules[module_name] = results else - -- error os.raise(errors) end end @@ -697,7 +689,7 @@ function interpreter.new() instance:api_register(nil, "interp_add_scopeapis", interpreter.api_interp_add_scopeapis) -- register the builtin modules - for module_name, module in pairs(interpreter._builtin_modules()) do + for module_name, module in pairs(interpreter.builtin_modules()) do instance:api_register_builtin(module_name, module) end @@ -1949,14 +1941,8 @@ function interpreter.instance(script) if script then local scope = getfenv(script) if scope then - - -- enable to read _INTERPRETER rawset(scope, "_INTERPRETER_READABLE", true) - - -- attempt to get it instance = scope._INTERPRETER - - -- disable to read _INTERPRETER rawset(scope, "_INTERPRETER_READABLE", nil) end if instance then return instance end @@ -1965,27 +1951,15 @@ function interpreter.instance(script) -- find self instance for the current sandbox local level = 2 while level < 32 do - - -- get scope local scope = getfenv(level) if scope then - - -- enable to read _INTERPRETER rawset(scope, "_INTERPRETER_READABLE", true) - - -- attempt to get it instance = scope._INTERPRETER - - -- disable to read _INTERPRETER rawset(scope, "_INTERPRETER_READABLE", nil) end - - -- found? if instance then break end - - -- next level = level + 1 end return instance diff --git a/xmake/core/sandbox/modules/import/core/base/interpreter.lua b/xmake/core/sandbox/modules/import/core/base/interpreter.lua index 7882f558495..dab53de26a3 100644 --- a/xmake/core/sandbox/modules/import/core/base/interpreter.lua +++ b/xmake/core/sandbox/modules/import/core/base/interpreter.lua @@ -18,5 +18,17 @@ -- @file interpreter.lua -- +-- load modules +local interpreter = require("base/interpreter") + +-- define module +local sandbox_core_base_interpreter = sandbox_core_base_interpreter or {} + +-- inherit some builtin interfaces +sandbox_core_base_interpreter.instance = interpreter.instance +sandbox_core_base_interpreter.builtin_modules = interpreter.builtin_modules + -- return module -return require("base/interpreter") +return sandbox_core_base_interpreter + + diff --git a/xmake/plugins/show/lists/apis.lua b/xmake/plugins/show/lists/apis.lua index 70e521c9739..f4a7554a25a 100644 --- a/xmake/plugins/show/lists/apis.lua +++ b/xmake/plugins/show/lists/apis.lua @@ -28,6 +28,7 @@ import("core.package.package") import("core.sandbox.sandbox") import("core.sandbox.module") import("core.tool.toolchain") +import("core.base.interpreter") import(".showlist") function _is_callable(func) @@ -41,24 +42,21 @@ function _is_callable(func) end end --- get project scope apis -function project_scope_apis() +-- get target description scope apis +function description_target_scope_apis() local result = {} - for _, names in pairs(project.apis()) do + for _, names in pairs(target.apis()) do for _, name in ipairs(names) do - if type(name) == "table" then - name = name[1] - end table.insert(result, name) end end return result end --- get target scope apis -function target_scope_apis() +-- get option description scope apis +function description_option_scope_apis() local result = {} - for _, names in pairs(target.apis()) do + for _, names in pairs(option.apis()) do for _, name in ipairs(names) do table.insert(result, name) end @@ -66,104 +64,141 @@ function target_scope_apis() return result end --- get target instance apis -function target_instance_apis() +-- get rule description scope apis +function description_rule_scope_apis() local result = {} - local instance = target.new() - for k, v in pairs(instance) do - if not k:startswith("_") and type(v) == "function" then - table.insert(result, "target:" .. k) + for _, names in pairs(rule.apis()) do + for _, name in ipairs(names) do + table.insert(result, name) end end return result end --- get option scope apis -function option_scope_apis() +-- get package description scope apis +function description_package_scope_apis() local result = {} - for _, names in pairs(option.apis()) do + for _, names in pairs(package.apis()) do for _, name in ipairs(names) do + if type(name) == "table" then + name = name[1] + end table.insert(result, name) end end return result end --- get option instance apis -function option_instance_apis() +-- get toolchain description scope apis +function description_toolchain_scope_apis() local result = {} - local instance = option.new() - for k, v in pairs(instance) do - if not k:startswith("_") and type(v) == "function" then - table.insert(result, "option:" .. k) + for _, names in pairs(toolchain.apis()) do + for _, name in ipairs(names) do + table.insert(result, name) end end return result end --- get rule scope apis -function rule_scope_apis() +-- get description scope apis +function description_scope_apis() local result = {} - for _, names in pairs(rule.apis()) do + table.join2(result, description_target_scope_apis()) + table.join2(result, description_option_scope_apis()) + table.join2(result, description_rule_scope_apis()) + table.join2(result, description_package_scope_apis()) + table.join2(result, description_toolchain_scope_apis()) + table.sort(result) + return result +end + +-- get description builtin apis +function description_builtin_apis() + -- add builtin interpreter apis + local builtin_module_apis = table.clone(interpreter.builtin_modules()) + builtin_module_apis.pairs = nil + builtin_module_apis.ipairs = nil + local result = {} + for name, value in pairs(builtin_module_apis) do + if type(value) == "function" then + table.insert(result, name) + end + end + table.insert(result, "ipairs") + table.insert(result, "pairs") + table.insert(result, "includes") + table.insert(result, "set_xmakever") + + -- add root project apis + for _, names in pairs(project.apis()) do for _, name in ipairs(names) do + if type(name) == "table" then + name = name[1] + end table.insert(result, name) end end + table.sort(result) return result end --- get rule instance apis -function rule_instance_apis() +-- get description builtin module apis +function description_builtin_module_apis() + local builtin_module_apis = table.clone(interpreter.builtin_modules()) + builtin_module_apis.pairs = nil + builtin_module_apis.ipairs = nil local result = {} - local instance = rule.new() - for k, v in pairs(instance) do - if not k:startswith("_") and type(v) == "function" then - table.insert(result, "rule:" .. k) + for name, value in pairs(builtin_module_apis) do + if type(value) == "table" then + for k, v in pairs(value) do + if not k:startswith("_") and type(v) == "function" then + table.insert(result, name .. "." .. k) + end + end end end + table.sort(result) return result end - --- get package scope apis -function package_scope_apis() +-- get script target instance apis +function script_target_instance_apis() local result = {} - for _, names in pairs(package.apis()) do - for _, name in ipairs(names) do - if type(name) == "table" then - name = name[1] - end - table.insert(result, name) + local instance = target.new() + for k, v in pairs(instance) do + if not k:startswith("_") and type(v) == "function" then + table.insert(result, "target:" .. k) end end return result end --- get package instance apis -function package_instance_apis() +-- get script option instance apis +function script_option_instance_apis() local result = {} - local instance = package.new() + local instance = option.new() for k, v in pairs(instance) do if not k:startswith("_") and type(v) == "function" then - table.insert(result, "package:" .. k) + table.insert(result, "option:" .. k) end end return result end --- get toolchain scope apis -function toolchain_scope_apis() +-- get script rule instance apis +function script_rule_instance_apis() local result = {} - for _, names in pairs(toolchain.apis()) do - for _, name in ipairs(names) do - table.insert(result, name) + local instance = rule.new() + for k, v in pairs(instance) do + if not k:startswith("_") and type(v) == "function" then + table.insert(result, "rule:" .. k) end end return result end --- get toolchain instance apis -function toolchain_instance_apis() +-- get script toolchain instance apis +function script_toolchain_instance_apis() local result = {} local instance = toolchain.load("clang") for k, v in pairs(instance) do @@ -174,56 +209,68 @@ function toolchain_instance_apis() return result end --- get scope apis -function scope_apis() +-- get script package instance apis +function script_package_instance_apis() local result = {} - table.join2(result, project_scope_apis()) - table.join2(result, target_scope_apis()) - table.join2(result, option_scope_apis()) - table.join2(result, rule_scope_apis()) - table.join2(result, package_scope_apis()) - table.join2(result, toolchain_scope_apis()) + local instance = package.new() + for k, v in pairs(instance) do + if not k:startswith("_") and type(v) == "function" then + table.insert(result, "package:" .. k) + end + end + return result +end + +-- get script instance apis +function script_instance_apis() + local result = {} + table.join2(result, script_target_instance_apis()) + table.join2(result, script_option_instance_apis()) + table.join2(result, script_rule_instance_apis()) + table.join2(result, script_package_instance_apis()) + table.join2(result, script_toolchain_instance_apis()) table.sort(result) return result end --- get instance apis -function instance_apis() +-- get script builtin apis +function script_builtin_apis() + local builtin_module_apis = table.clone(sandbox.builtin_modules()) + builtin_module_apis.pairs = nil + builtin_module_apis.ipairs = nil local result = {} - table.join2(result, target_instance_apis()) - table.join2(result, option_instance_apis()) - table.join2(result, rule_instance_apis()) - table.join2(result, package_instance_apis()) - table.join2(result, toolchain_instance_apis()) + for name, value in pairs(builtin_module_apis) do + if type(value) == "function" then + table.insert(result, name) + end + end + table.insert(result, "ipairs") + table.insert(result, "pairs") table.sort(result) return result end --- get builtin module apis -function builtin_module_apis() - local builtin_modules = table.clone(sandbox.builtin_modules()) - builtin_modules.pairs = nil - builtin_modules.ipairs = nil +-- get script builtin module apis +function script_builtin_module_apis() + local builtin_module_apis = table.clone(sandbox.builtin_modules()) + builtin_module_apis.pairs = nil + builtin_module_apis.ipairs = nil local result = {} - for name, value in pairs(builtin_modules) do + for name, value in pairs(builtin_module_apis) do if type(value) == "table" then for k, v in pairs(value) do if not k:startswith("_") and type(v) == "function" then table.insert(result, name .. "." .. k) end end - elseif type(value) == "function" then - table.insert(result, name) end end - table.insert(result, "ipairs") - table.insert(result, "pairs") table.sort(result) return result end --- get import module apis -function import_module_apis() +-- get script extension modules +function script_extension_module_apis() local result = {} local moduledirs = module.directories() for _, moduledir in ipairs(moduledirs) do @@ -254,11 +301,26 @@ function import_module_apis() end -- get all apis +-- +-- the api kind: +-- - description +-- - builtin api +-- - builtin module api +-- - scope api +-- - script +-- - builtin api +-- - builtin module api +-- - extension module api +-- - instance api function apis() - return {scope = scope_apis(), - instance = instance_apis(), - builtin_module = builtin_module_apis(), - import_module = import_module_apis()} + return {description_scope_apis = description_scope_apis(), + description_builtin_apis = description_builtin_apis(), + description_builtin_module_apis = description_builtin_module_apis(), + script_builtin_apis = script_builtin_apis(), + script_builtin_module_apis = script_builtin_module_apis(), + script_extension_module_apis = script_extension_module_apis(), + script_instance_apis = script_instance_apis() + } end -- show all apis From e1910aeb5129ecfabd9ff9d024ae0d1d5938717d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Apr 2024 22:41:58 +0800 Subject: [PATCH 2/2] fix package scope api --- xmake/plugins/show/lists/apis.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/plugins/show/lists/apis.lua b/xmake/plugins/show/lists/apis.lua index f4a7554a25a..1a70263e600 100644 --- a/xmake/plugins/show/lists/apis.lua +++ b/xmake/plugins/show/lists/apis.lua @@ -81,7 +81,7 @@ function description_package_scope_apis() for _, names in pairs(package.apis()) do for _, name in ipairs(names) do if type(name) == "table" then - name = name[1] + name = "package." .. name[1] end table.insert(result, name) end