diff options
| author | ruki <[email protected]> | 2016-01-29 09:12:58 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-02-15 19:10:26 +0800 |
| commit | def096cfe9e22d363dffa29cd02c134778e62966 (patch) | |
| tree | 2d12e3ebdd9968cac0784e4ebe49bba0643a903d | |
| parent | d76193bc445d3c81993a92fd897d3e3d93edeaf2 (diff) | |
load script in sandbox
| -rw-r--r-- | xmake/core/action/run.lua | 28 | ||||
| -rw-r--r-- | xmake/core/base/install.lua | 55 | ||||
| -rw-r--r-- | xmake/core/base/interpreter.lua | 11 | ||||
| -rw-r--r-- | xmake/core/base/package.lua | 54 | ||||
| -rw-r--r-- | xmake/core/base/project.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/sandbox.lua | 38 |
6 files changed, 83 insertions, 105 deletions
diff --git a/xmake/core/action/run.lua b/xmake/core/action/run.lua index 247170d45..75978622b 100644 --- a/xmake/core/action/run.lua +++ b/xmake/core/action/run.lua @@ -29,6 +29,7 @@ local path = require("base/path") local utils = require("base/utils") local config = require("base/config") local project = require("base/project") +local sandbox = require("base/sandbox") local platform = require("base/platform") -- need access to the given file? @@ -85,21 +86,24 @@ function action_run.done() -- run script local runscript = target.runscript if runscript ~= nil then - if type(runscript) == "function" then - - -- make passed target - local target_passed = {} - target_passed.name = name - target_passed.arguments = arguments - target_passed.targetfile = targetfile - -- run it - local ok = runscript(target_passed) - if ok ~= 0 then return utils.ifelse(ok == 1, true, false) end - else - utils.error("invalid run script!") + -- make passed target + local target_passed = {} + target_passed.name = name + target_passed.arguments = arguments + target_passed.targetfile = targetfile + + -- run script + local ok, results = sandbox.load(runscript, target_passed) + if not ok then + utils.error(results) return false end + + -- check results + if results ~= 0 then + return utils.ifelse(results == 1, true, false) + end end -- not executale? diff --git a/xmake/core/base/install.lua b/xmake/core/base/install.lua index 5da8e55d1..38b2933c8 100644 --- a/xmake/core/base/install.lua +++ b/xmake/core/base/install.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") -- install target from the project script @@ -38,19 +39,24 @@ function install._done_from_project(target) -- check assert(target) - -- install it using the project script first - local installscript = target.installscript - if type(installscript) == "function" then + -- no script? continue + if target.installscript == nil then + return 0 + end - -- remove it - target.installscript = nil + -- get script + local script = target.installscript + target.installscript = nil - -- install it - return installscript(target) + -- install it using the project script first + local ok, results = sandbox.load(script, target) + if not ok then + utils.error(results) + return -1 end - -- continue - return 0 + -- ok? + return results end -- install target from the platform script @@ -59,30 +65,21 @@ function install._done_from_platform(target) -- check assert(target) - -- the platform install script file - local installscript = nil - local scriptfile = platform.directory() .. "/install.lua" - if os.isfile(scriptfile) then - - -- load the install script - local script, errors = loadfile(scriptfile) - if script then - installscript = script() - if type(installscript) == "table" and installscript.main then - installscript = installscript.main - end - else - utils.error(errors) - end + -- no script? continue + local scriptfile = path.join(platform.directory(), "install.lua") + if not os.isfile(scriptfile) then + return 0 end - -- install it - if type(installscript) == "function" then - return installscript(target) + -- the platform install script file + local ok, results = sandbox.load(scriptfile, target) + if not ok then + utils.error(results) + return -1 end - -- continue - return 0 + -- ok? + return results end -- install target from the given target configure diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index d65c55c0d..3ee1165a2 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -774,10 +774,13 @@ function interpreter.api_register_set_script(self, scope_kind, prefix, ...) -- define implementation local implementation = function (self, scope, name, script) - -- bind sandbox to script - local ok, errors = sandbox.bind(script) - if not ok then - utils.error("set_%s(\"%s\"): %s", scope, name, errors) + -- check + if script == nil then + utils.error("set_%s(\"%s\"): no script", scope, name) + utils.abort() + end + if type(script) == "string" and not os.isfile(script) then + utils.error("set_%s(\"%s\"): scriptfile(%s) not found!", scope, name, script) utils.abort() end diff --git a/xmake/core/base/package.lua b/xmake/core/base/package.lua index ce1f14927..1b3f154af 100644 --- a/xmake/core/base/package.lua +++ b/xmake/core/base/package.lua @@ -206,19 +206,24 @@ function package._done_from_project(target) -- check assert(target) - -- package it using the project script first - local packagescript = target.packagescript - if type(packagescript) == "function" then + -- no script? continue + if target.packagescript == nil then + return 0 + end - -- remove it - target.packagescript = nil + -- get script + local script = target.packagescript + target.packagescript = nil - -- package it - return packagescript(target) + -- package it using the project script first + local ok, results = sandbox.load(script, target) + if not ok then + utils.error(results) + return -1 end - -- continue - return 0 + -- ok? + return results end -- package target from the platform script @@ -227,30 +232,21 @@ function package._done_from_platform(target) -- check assert(target) - -- the platform package script file - local packagescript = nil - local scriptfile = platform.directory() .. "/package.lua" - if os.isfile(scriptfile) then - - -- load the package script - local script, errors = loadfile(scriptfile) - if script then - packagescript = script() - if type(packagescript) == "table" and packagescript.main then - packagescript = packagescript.main - end - else - utils.error(errors) - end + -- no script? continue + local scriptfile = path.join(platform.directory(), "package.lua") + if not os.isfile(scriptfile) then + return 0 end - -- package it - if type(packagescript) == "function" then - return packagescript(target) + -- the platform package script file + local ok, results = sandbox.load(scriptfile, target) + if not ok then + utils.error(results) + return -1 end - -- continue - return 0 + -- ok? + return results end -- get the configure file diff --git a/xmake/core/base/project.lua b/xmake/core/base/project.lua index ff022d9fa..c1c6c162b 100644 --- a/xmake/core/base/project.lua +++ b/xmake/core/base/project.lua @@ -330,7 +330,7 @@ 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" diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua index c74a7e422..2689c9dc5 100644 --- a/xmake/core/base/sandbox.lua +++ b/xmake/core/base/sandbox.lua @@ -113,9 +113,6 @@ function sandbox._init() end end - -- save self - self._PUBLIC._SELF = self - -- register the builtin interfaces self:_api_register("import", sandbox._api_builtin_import) @@ -133,8 +130,8 @@ function sandbox._init() return self end --- bind sandbox to script -function sandbox.bind(script) +-- load script in the sandbox +function sandbox.load(script, ...) -- init self local self = sandbox._init() @@ -143,7 +140,12 @@ function sandbox.bind(script) assert(self and self._PUBLIC) -- this script is file? load it first - if type(script) == "string" and os.isfile(script) then + if type(script) == "string" then + + -- check + if not os.isfile(script) then + return false, string.format("the script file(%s) not found!", script) + end -- load it local filescript, errors = loadfile(script) @@ -175,33 +177,9 @@ function sandbox.bind(script) -- bind public scope setfenv(script, self._PUBLIC) - -- ok - return true -end - --- load script -function sandbox.load(script, ...) - - -- check - assert(type(script) == "function") - - -- get public scope - local public = getfenv(script) - assert(public) - - -- get sandbox self - local self = public._SELF - if not self then - return false, "this script without sandbox!" - end - - -- clear the self - public._SELF = nil - -- load script return xpcall(script, sandbox._traceback, ...) end - -- return module: sandbox return sandbox |
