Skip to content

Commit

Permalink
Merge pull request #5009 from xmake-io/scope
Browse files Browse the repository at this point in the history
improve apis
  • Loading branch information
waruqi authored Apr 24, 2024
2 parents b6d0f62 + e1910ae commit cde37d4
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 113 deletions.
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

0 comments on commit cde37d4

Please sign in to comment.