From d76193bc445d3c81993a92fd897d3e3d93edeaf2 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Jan 2016 21:32:50 +0800 Subject: modify sandbox interfaces --- tests/interpreter/scope.lua | 2 +- xmake/core/base/interpreter.lua | 29 +++++++------- xmake/core/base/package.lua | 1 + xmake/core/base/sandbox.lua | 83 +++++++++++++++++++++++++++-------------- xmake/core/module/os.lua | 46 ----------------------- xmake/core/module/project.lua | 73 ------------------------------------ xmake/core/modules/os.lua | 46 +++++++++++++++++++++++ xmake/core/modules/project.lua | 73 ++++++++++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 162 deletions(-) delete mode 100644 xmake/core/module/os.lua delete mode 100644 xmake/core/module/project.lua create mode 100644 xmake/core/modules/os.lua create mode 100644 xmake/core/modules/project.lua 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/module/os.lua deleted file mode 100644 index 3689086f2..000000000 --- a/xmake/core/module/os.lua +++ /dev/null @@ -1,46 +0,0 @@ ---!The Automatic Cross-platform Build Tool --- --- XMake is free software; you can redistribute it and/or modify --- it under the terms of the GNU Lesser General Public License as published by --- the Free Software Foundation; either version 2.1 of the License, or --- (at your option) any later version. --- --- XMake is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU Lesser General Public License for more details. --- --- You should have received a copy of the GNU Lesser General Public License --- along with XMake; --- If not, see http://www.gnu.org/licenses/ --- --- Copyright (C) 2009 - 2015, ruki All rights reserved. --- --- @author ruki --- @file os.lua --- - --- load modules -local io = require("base/io") -local os = require("base/os") - --- define module: _os -local _os = _os or {} - --- cat the given file -function _os.cat(filepath, linecount) - - -- cat it - return io.cat(filepath, linecount) -end - --- only copy the interfaces of os -for k, v in pairs(os) do - if type(v) == "function" then - _os[k] = v - end -end - --- return module: _os -return _os - diff --git a/xmake/core/module/project.lua b/xmake/core/module/project.lua deleted file mode 100644 index f3f726c6f..000000000 --- a/xmake/core/module/project.lua +++ /dev/null @@ -1,73 +0,0 @@ ---!The Automatic Cross-platform Build Tool --- --- XMake is free software; you can redistribute it and/or modify --- it under the terms of the GNU Lesser General Public License as published by --- the Free Software Foundation; either version 2.1 of the License, or --- (at your option) any later version. --- --- XMake is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU Lesser General Public License for more details. --- --- You should have received a copy of the GNU Lesser General Public License --- along with XMake; --- If not, see http://www.gnu.org/licenses/ --- --- Copyright (C) 2009 - 2015, ruki All rights reserved. --- --- @author ruki --- @file project.lua --- - --- define module: project -local project = project or {} - --- load modules -local rule = require("base/rule") -local config = require("base/config") - --- get the build directory -function project.buildir() - - -- get it - return config.get("buildir") -end - --- get the project directory -function project.projectdir() - - -- get it - return xmake._PROJECT_DIR -end - --- get the log file -function project.logfile() - - -- get it - return rule.logfile() -end - --- get the current platform -function project.plat() - - -- get it - return config.get("plat") -end - --- get the current architecture -function project.arch() - - -- get it - return config.get("arch") -end - --- get the current mode -function project.mode() - - -- get it - return config.get("mode") -end - --- return module: project -return project diff --git a/xmake/core/modules/os.lua b/xmake/core/modules/os.lua new file mode 100644 index 000000000..3689086f2 --- /dev/null +++ b/xmake/core/modules/os.lua @@ -0,0 +1,46 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see http://www.gnu.org/licenses/ +-- +-- Copyright (C) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file os.lua +-- + +-- load modules +local io = require("base/io") +local os = require("base/os") + +-- define module: _os +local _os = _os or {} + +-- cat the given file +function _os.cat(filepath, linecount) + + -- cat it + return io.cat(filepath, linecount) +end + +-- only copy the interfaces of os +for k, v in pairs(os) do + if type(v) == "function" then + _os[k] = v + end +end + +-- return module: _os +return _os + diff --git a/xmake/core/modules/project.lua b/xmake/core/modules/project.lua new file mode 100644 index 000000000..f3f726c6f --- /dev/null +++ b/xmake/core/modules/project.lua @@ -0,0 +1,73 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see http://www.gnu.org/licenses/ +-- +-- Copyright (C) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file project.lua +-- + +-- define module: project +local project = project or {} + +-- load modules +local rule = require("base/rule") +local config = require("base/config") + +-- get the build directory +function project.buildir() + + -- get it + return config.get("buildir") +end + +-- get the project directory +function project.projectdir() + + -- get it + return xmake._PROJECT_DIR +end + +-- get the log file +function project.logfile() + + -- get it + return rule.logfile() +end + +-- get the current platform +function project.plat() + + -- get it + return config.get("plat") +end + +-- get the current architecture +function project.arch() + + -- get it + return config.get("arch") +end + +-- get the current mode +function project.mode() + + -- get it + return config.get("mode") +end + +-- return module: project +return project -- cgit v1.3.1