summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-01-09 00:50:52 +0800
committerruki <[email protected]>2025-01-09 00:50:52 +0800
commited685ecd74c5267781713f3bd00ce4e4e40b27d7 (patch)
tree3e3e9bee672f1570ac4712edb31c2fd2d5abb27d
parenta74774a809de30e6e473157233aa47bbabb272d4 (diff)
add namespace to sandbox
-rw-r--r--xmake/core/base/interpreter.lua2
-rw-r--r--xmake/core/base/task.lua9
-rw-r--r--xmake/core/package/package.lua2
-rw-r--r--xmake/core/project/project.lua12
-rw-r--r--xmake/core/sandbox/modules/has_config.lua8
-rw-r--r--xmake/core/sandbox/sandbox.lua25
6 files changed, 33 insertions, 25 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index 72886e0d2..4e5aaf8c7 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -647,7 +647,7 @@ function interpreter:_script(script)
end
-- make sandbox instance with the given script
- local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
+ local instance, errors = sandbox.new(script, {filter = self:filter(), rootdir = self:scriptdir(), namespace = self:namespace()})
if not instance then
return nil, errors
end
diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua
index 7258aeb21..a3fde1913 100644
--- a/xmake/core/base/task.lua
+++ b/xmake/core/base/task.lua
@@ -221,17 +221,12 @@ end
-- bind script with a sandbox instance
function task._bind_script(interp, script)
-
- -- make sandbox instance with the given script
- local instance, errors = sandbox.new(script, interp:filter(), interp:rootdir())
+ local instance, errors = sandbox.new(script, {
+ filter = interp:filter(), rootdir = interp:rootdir(), namespace = interp:namespace()})
if not instance then
return nil, errors
end
-
- -- check
assert(instance:script())
-
- -- update option script
return instance:script()
end
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index bb12282e1..741ca318c 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -2945,7 +2945,7 @@ function package.load_from_system(packagename)
end
-- make sandbox instance with the given script
- instance, errors = sandbox.new(on_install, interp:filter())
+ instance, errors = sandbox.new(on_install, {filter = interp:filter(), namespace = interp:namespace()})
if not instance then
return nil, errors
end
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 61c1361db..ee2b55a63 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -979,8 +979,16 @@ function project.ordertargets()
end
-- get the given option
-function project.option(name)
- return project.options()[name]
+function project.option(name, opt)
+ opt = opt or {}
+ local options = project.options()
+ if options then
+ local o = options[name]
+ if not o and opt.namespace then
+ o = options[opt.namespace .. "::" .. name]
+ end
+ return o
+ end
end
-- get options
diff --git a/xmake/core/sandbox/modules/has_config.lua b/xmake/core/sandbox/modules/has_config.lua
index 1dc36278a..2cf5a86d0 100644
--- a/xmake/core/sandbox/modules/has_config.lua
+++ b/xmake/core/sandbox/modules/has_config.lua
@@ -20,8 +20,14 @@
-- return module
local config = require("project/config")
+local sandbox = require("sandbox/sandbox")
return function (...)
- return config.has(table.pack(...))
+ local namespace
+ local instance = sandbox.instance()
+ if instance then
+ namespace = instance:namespace()
+ end
+ return config.has(table.pack(...), {namespace = namespace})
end
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 9bad3eb9f..2d624da98 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -107,21 +107,20 @@ function sandbox._new()
-- bind instance to the public script envirnoment
instance:bind(instance._PUBLIC)
-
- -- ok?
return instance
end
-- new a sandbox instance with the given script
-function sandbox.new(script, filter, rootdir)
- assert(script)
+function sandbox.new(script, opt)
+ opt = opt or {}
-- new instance
local self = sandbox._new()
assert(self and self._PUBLIC and self._PRIVATE)
- self._PRIVATE._FILTER = filter
- self._PRIVATE._ROOTDIR = rootdir
+ self._PRIVATE._FILTER = opt.filter
+ self._PRIVATE._ROOTDIR = opt.rootdir
+ self._PRIVATE._NAMESPACE = opt.namespace
-- invalid script?
if type(script) ~= "function" then
@@ -178,23 +177,17 @@ function sandbox:fork(script, rootdir)
-- init a new sandbox instance
local instance = sandbox._new()
-
- -- check
assert(instance and instance._PUBLIC and instance._PRIVATE)
- -- inherit the filter
instance._PRIVATE._FILTER = self:filter()
-
- -- inherit the root directory
instance._PRIVATE._ROOTDIR = rootdir or self:rootdir()
+ instance._PRIVATE._NAMESPACE = self:namespace()
-- bind public scope
if script then
setfenv(script, instance._PUBLIC)
instance._PRIVATE._SCRIPT = script
end
-
- -- ok?
return instance
end
@@ -246,6 +239,12 @@ function sandbox:rootdir()
return self._PRIVATE._ROOTDIR
end
+-- get current namespace
+function sandbox:namespace()
+ assert(self and self._PRIVATE)
+ return self._PRIVATE._NAMESPACE
+end
+
-- get current instance in the sandbox modules
function sandbox.instance(script)