Skip to content

Commit

Permalink
add namespace to sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Jan 8, 2025
1 parent a74774a commit ed685ec
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion xmake/core/base/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions xmake/core/base/task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion xmake/core/package/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions xmake/core/project/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion xmake/core/sandbox/modules/has_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

25 changes: 12 additions & 13 deletions xmake/core/sandbox/sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ed685ec

Please sign in to comment.