summaryrefslogtreecommitdiff
path: root/xmake/core/sandbox/sandbox.lua
diff options
context:
space:
mode:
Diffstat (limited to 'xmake/core/sandbox/sandbox.lua')
-rw-r--r--xmake/core/sandbox/sandbox.lua43
1 files changed, 39 insertions, 4 deletions
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 880e803e4..c4da87b05 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -151,10 +151,10 @@ function sandbox._new()
end
-- new a sandbox instance with the given script
-function sandbox.new(script, interp)
+function sandbox.new(script, filter, rootdir)
-- check
- assert(script and interp)
+ assert(script)
-- new instance
local self = sandbox._new()
@@ -163,10 +163,10 @@ function sandbox.new(script, interp)
assert(self and self._PUBLIC and self._PRIVATE)
-- save filter
- self._PRIVATE._FILTER = interp:filter()
+ self._PRIVATE._FILTER = filter
-- save root directory
- self._PRIVATE._ROOTDIR = interp:rootdir()
+ self._PRIVATE._ROOTDIR = rootdir
-- this script is module name? import it first
if type(script) == "string" then
@@ -242,6 +242,41 @@ function sandbox:fork(script)
return instance
end
+-- load script and import module
+function sandbox:import()
+
+ -- this module has been imported?
+ if self._PRIVATE._MODULE then
+ return self._PRIVATE._MODULE
+ end
+
+ -- backup the scope variables first
+ local scope_public = getfenv(self:script())
+ local scope_backup = {}
+ table.copy2(scope_backup, scope_public)
+
+ -- load module with sandbox
+ local ok, errors = sandbox.load(self:script())
+ if not ok then
+ return nil, errors
+ end
+
+ -- only export new public functions
+ local module = {}
+ for k, v in pairs(scope_public) do
+ if type(v) == "function" and not k:startswith("_") and scope_backup[k] == nil then
+ module[k] = v
+ end
+ end
+
+ -- save module
+ self._PRIVATE._MODULE = module
+
+ -- ok
+ return module
+
+end
+
-- get script from the given sandbox
function sandbox:script()