summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-01-28 20:33:54 +0800
committerruki <[email protected]>2016-02-15 19:10:26 +0800
commit80a87ab432cbe72ab772f02d13daa75bc1010085 (patch)
treeedece4e5a3066589401b2c6ebf0f5e32e0ec45f7
parent780a88f1b2e7cb90be32cd20216bd94460824c20 (diff)
register some builtin api for sandbox
-rw-r--r--xmake/core/action/install.lua19
-rw-r--r--xmake/core/action/package.lua19
-rw-r--r--xmake/core/action/run.lua16
-rw-r--r--xmake/core/base/interpreter.lua1
-rw-r--r--xmake/core/base/project.lua20
-rw-r--r--xmake/core/base/sandbox.lua47
6 files changed, 53 insertions, 69 deletions
diff --git a/xmake/core/action/install.lua b/xmake/core/action/install.lua
index 55e62bfbe..ae1ed79c2 100644
--- a/xmake/core/action/install.lua
+++ b/xmake/core/action/install.lua
@@ -50,24 +50,7 @@ function action_install._makeconf(configs, target_name, target)
local configs_target = configs[target_name]
-- save the install script
- local installscript = target.installscript
- if type(installscript) == "string" and os.isfile(installscript) then
- local script, errors = loadfile(installscript)
- if script then
- installscript = script()
- if type(installscript) == "table" and installscript.main then
- installscript = installscript.main
- end
- else
- utils.error(errors)
- return false
- end
- end
- if target.installscript and type(installscript) ~= "function" then
- utils.error("invalid install script!")
- return false
- end
- configs_target.installscript = installscript
+ configs_target.installscript = target.installscript
-- ok
return true
diff --git a/xmake/core/action/package.lua b/xmake/core/action/package.lua
index 53761aa50..0aba05c73 100644
--- a/xmake/core/action/package.lua
+++ b/xmake/core/action/package.lua
@@ -150,24 +150,7 @@ function action_package._makeconf(target_name, target)
configs_arch.targetfile = action_package._backup(configs_arch.targetdir, rule.targetfile(target_name, target))
-- save the package script
- local packagescript = target.packagescript
- if type(packagescript) == "string" and os.isfile(packagescript) then
- local script, errors = loadfile(packagescript)
- if script then
- packagescript = script()
- if type(packagescript) == "table" and packagescript.main then
- packagescript = packagescript.main
- end
- else
- utils.error(errors)
- return false
- end
- end
- if target.packagescript and type(packagescript) ~= "function" then
- utils.error("invalid package script!")
- return false
- end
- configs_target.packagescript = packagescript
+ configs_target.packagescript = target.packagescript
-- ok
return true
diff --git a/xmake/core/action/run.lua b/xmake/core/action/run.lua
index 3f10d4866..247170d45 100644
--- a/xmake/core/action/run.lua
+++ b/xmake/core/action/run.lua
@@ -82,22 +82,8 @@ function action_run.done()
targetfile = path.absolute(targetfile, xmake._PROJECT_DIR)
end
- -- load the run script
- local runscript = target.runscript
- if type(runscript) == "string" and os.isfile(runscript) then
- local script, errors = loadfile(runscript)
- if script then
- runscript = script()
- if type(runscript) == "table" and runscript.main then
- runscript = runscript.main
- end
- else
- utils.error(errors)
- return false
- end
- end
-
-- run script
+ local runscript = target.runscript
if runscript ~= nil then
if type(runscript) == "function" then
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index eae94ee9c..855480ebd 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -444,7 +444,6 @@ function interpreter.init()
interp: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)
diff --git a/xmake/core/base/project.lua b/xmake/core/base/project.lua
index 13876a2ac..ff022d9fa 100644
--- a/xmake/core/base/project.lua
+++ b/xmake/core/base/project.lua
@@ -36,13 +36,6 @@ local compiler = require("base/compiler")
local platform = require("base/platform")
local interpreter = require("base/interpreter")
--- import module
-function project._api_import(interp, module)
-
- -- import
- return require("module/" .. module)
-end
-
-- the current os is belong to the given os?
function project._api_os(interp, ...)
@@ -337,6 +330,11 @@ function project._interpreter()
-- register api: add_target() and add_option()
interp:api_register_add_scope("target", "option")
+
+ -- register api: set_script() for target
+ interp:api_register_set_script("target", nil, "runscript"
+ , "installscript"
+ , "packagescript")
-- register api: set_values() for target
interp:api_register_set_values("target", nil, "kind"
@@ -347,10 +345,7 @@ function project._interpreter()
, "symbols"
, "warnings"
, "optimize"
- , "languages"
- , "runscript"
- , "installscript"
- , "packagescript")
+ , "languages")
-- register api: add_values() for target
interp:api_register_add_values("target", nil, "deps"
@@ -418,9 +413,6 @@ function project._interpreter()
, "includedirs")
- -- register api: import()
- interp:api_register("import", project._api_import)
-
-- register api: os()
interp:api_register("os", project._api_os)
diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua
index b6d9f29a3..96057aca8 100644
--- a/xmake/core/base/sandbox.lua
+++ b/xmake/core/base/sandbox.lua
@@ -72,22 +72,42 @@ function sandbox._traceback(errors)
return results
end
+-- import module
+function sandbox._api_builtin_import(self, module)
+
+ -- import
+ return require("module/" .. module)
+end
+
-- init sandbox
function sandbox.init()
-- init an sandbox instance
- local box = { _PUBLIC = {}
+ local sbox = { _PUBLIC = {}
, _PRIVATE = {}}
-- inherit the interfaces of sandbox
for k, v in pairs(sandbox) do
if type(v) == "function" then
- box[k] = v
+ sbox[k] = v
end
end
+ -- register the builtin interfaces
+ sbox: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)
+
+ -- register the builtin modules for lua
+ sbox:api_register_builtin("path", path)
+-- sbox:api_register_builtin("table", table)
+ sbox:api_register_builtin("string", string)
+
-- ok?
- return box
+ return sbox
end
-- bind sandbox to script
@@ -133,5 +153,26 @@ function sandbox.bind(self, script)
return true
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
+
-- return module: sandbox
return sandbox