summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-01-28 21:32:50 +0800
committerruki <[email protected]>2016-02-15 19:10:26 +0800
commitd76193bc445d3c81993a92fd897d3e3d93edeaf2 (patch)
tree8cbe3fdc21e8154d65f3a6617681999fe2cdb681
parent95c4c4f28d8ae3ff135e1e5ea568165987fe472e (diff)
modify sandbox interfaces
-rw-r--r--tests/interpreter/scope.lua2
-rw-r--r--xmake/core/base/interpreter.lua29
-rw-r--r--xmake/core/base/package.lua1
-rw-r--r--xmake/core/base/sandbox.lua83
-rw-r--r--xmake/core/modules/os.lua (renamed from xmake/core/module/os.lua)0
-rw-r--r--xmake/core/modules/project.lua (renamed from xmake/core/module/project.lua)0
6 files changed, 72 insertions, 43 deletions
diff --git a/tests/interpreter/scope.lua b/tests/interpreter/scope.lua
index 150f51374..2893dfeaf 100644
--- a/tests/interpreter/scope.lua
+++ b/tests/interpreter/scope.lua
@@ -1,5 +1,5 @@
-set_kind("static"
+set_kind("static")
add_files("root.c")
add_target("target1")
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index eae94ee9c..d65c55c0d 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -426,35 +426,34 @@ end
-- init interpreter
function interpreter.init()
-
-- init an interpreter instance
- local interp = { _PUBLIC = {}
- , _PRIVATE = { _SCOPES = {}
- , _MTIMES = {}}}
+ local self = { _PUBLIC = {}
+ , _PRIVATE = { _SCOPES = {}
+ , _MTIMES = {}}}
-- inherit the interfaces of interpreter
for k, v in pairs(interpreter) do
if type(v) == "function" then
- interp[k] = v
+ self[k] = v
end
end
-- register the builtin interfaces
- interp:api_register("add_subdirs", interpreter.api_builtin_add_subdirs)
- interp:api_register("add_subfiles", interpreter.api_builtin_add_subfiles)
+ self:api_register("add_subdirs", interpreter.api_builtin_add_subdirs)
+ self:api_register("add_subfiles", interpreter.api_builtin_add_subfiles)
-- register the builtin interfaces for lua
- interp:api_register_builtin("print", print)
- interp:api_register_builtin("pairs", pairs)
- interp:api_register_builtin("ipairs", ipairs)
+ self:api_register_builtin("print", print)
+ self:api_register_builtin("pairs", pairs)
+ self:api_register_builtin("ipairs", ipairs)
-- register the builtin modules for lua
- interp:api_register_builtin("path", path)
- interp:api_register_builtin("table", table)
- interp:api_register_builtin("string", string)
+ self:api_register_builtin("path", path)
+ self:api_register_builtin("table", table)
+ self:api_register_builtin("string", string)
-- ok?
- return interp
+ return self
end
-- load results
@@ -776,7 +775,7 @@ function interpreter.api_register_set_script(self, scope_kind, prefix, ...)
local implementation = function (self, scope, name, script)
-- bind sandbox to script
- local ok, errors = sandbox.init():bind(script)
+ local ok, errors = sandbox.bind(script)
if not ok then
utils.error("set_%s(\"%s\"): %s", scope, name, errors)
utils.abort()
diff --git a/xmake/core/base/package.lua b/xmake/core/base/package.lua
index 5bfe7850b..ce1f14927 100644
--- a/xmake/core/base/package.lua
+++ b/xmake/core/base/package.lua
@@ -30,6 +30,7 @@ local rule = require("base/rule")
local path = require("base/path")
local utils = require("base/utils")
local config = require("base/config")
+local sandbox = require("base/sandbox")
local platform = require("base/platform")
-- package target for the library file
diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua
index 06347cffe..c74a7e422 100644
--- a/xmake/core/base/sandbox.lua
+++ b/xmake/core/base/sandbox.lua
@@ -76,42 +76,68 @@ end
function sandbox._api_builtin_import(self, module)
-- import
- return require("module/" .. module)
+ return require("modules/" .. module)
+end
+
+-- register api
+function sandbox._api_register(self, name, func)
+
+ -- check
+ assert(self and self._PUBLIC)
+ assert(name and func)
+
+ -- register it
+ self._PUBLIC[name] = function (...) return func(self, ...) end
+end
+
+-- register api for builtin
+function sandbox._api_register_builtin(self, name, func)
+
+ -- check
+ assert(self and self._PUBLIC and func)
+
+ -- register it
+ self._PUBLIC[name] = func
end
-- init sandbox
-function sandbox.init()
+function sandbox._init()
-- init an sandbox instance
- local sbox = { _PUBLIC = {}
- , _PRIVATE = {}}
+ local self = {_PUBLIC = {}}
-- inherit the interfaces of sandbox
for k, v in pairs(sandbox) do
if type(v) == "function" then
- sbox[k] = v
+ self[k] = v
end
end
+ -- save self
+ self._PUBLIC._SELF = self
+
-- register the builtin interfaces
- sbox:api_register("import", sandbox._api_builtin_import)
+ self:_api_register("import", sandbox._api_builtin_import)
-- register the builtin interfaces for lua
- sbox:api_register_builtin("print", print)
- sbox:api_register_builtin("pairs", pairs)
- sbox:api_register_builtin("ipairs", ipairs)
+ self:_api_register_builtin("print", print)
+ self:_api_register_builtin("pairs", pairs)
+ self:_api_register_builtin("ipairs", ipairs)
-- register the builtin modules for lua
- sbox:api_register_builtin("path", path)
- sbox:api_register_builtin("table", table)
- sbox:api_register_builtin("string", string)
+ self:_api_register_builtin("path", path)
+ self:_api_register_builtin("table", table)
+ self:_api_register_builtin("string", string)
-- ok?
- return sbox
+ return self
end
-- bind sandbox to script
-function sandbox.bind(self, script)
+function sandbox.bind(script)
+
+ -- init self
+ local self = sandbox._init()
-- check
assert(self and self._PUBLIC)
@@ -153,26 +179,29 @@ function sandbox.bind(self, script)
return true
end
--- register api
-function sandbox.api_register(self, name, func)
+-- load script
+function sandbox.load(script, ...)
-- check
- assert(self and self._PUBLIC)
- assert(name and func)
+ assert(type(script) == "function")
- -- register it
- self._PUBLIC[name] = function (...) return func(self, ...) end
-end
+ -- get public scope
+ local public = getfenv(script)
+ assert(public)
--- register api for builtin
-function sandbox.api_register_builtin(self, name, func)
+ -- get sandbox self
+ local self = public._SELF
+ if not self then
+ return false, "this script without sandbox!"
+ end
- -- check
- assert(self and self._PUBLIC and func)
+ -- clear the self
+ public._SELF = nil
- -- register it
- self._PUBLIC[name] = func
+ -- load script
+ return xpcall(script, sandbox._traceback, ...)
end
+
-- return module: sandbox
return sandbox
diff --git a/xmake/core/module/os.lua b/xmake/core/modules/os.lua
index 3689086f2..3689086f2 100644
--- a/xmake/core/module/os.lua
+++ b/xmake/core/modules/os.lua
diff --git a/xmake/core/module/project.lua b/xmake/core/modules/project.lua
index f3f726c6f..f3f726c6f 100644
--- a/xmake/core/module/project.lua
+++ b/xmake/core/modules/project.lua