Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve apis #5009

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions xmake/core/base/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion xmake/core/sandbox/modules/import/core/base/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Loading
Loading