From 2899c85965c428974b5bc07ddec461bf2055c747 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 3 Mar 2016 13:49:00 +0800 Subject: move sandbox files --- xmake/core/base/deprecated/interpreter.lua | 2 +- xmake/core/base/interpreter.lua | 2 +- xmake/core/base/sandbox.lua | 302 ----------------------------- xmake/core/base/task.lua | 2 +- xmake/core/sandbox/assert.lua | 25 --- xmake/core/sandbox/format.lua | 26 --- xmake/core/sandbox/ifelse.lua | 25 --- xmake/core/sandbox/import.lua | 250 ------------------------ xmake/core/sandbox/ipairs.lua | 25 --- xmake/core/sandbox/modules/assert.lua | 25 +++ xmake/core/sandbox/modules/format.lua | 26 +++ xmake/core/sandbox/modules/ifelse.lua | 25 +++ xmake/core/sandbox/modules/import.lua | 250 ++++++++++++++++++++++++ xmake/core/sandbox/modules/ipairs.lua | 25 +++ xmake/core/sandbox/modules/os.lua | 206 ++++++++++++++++++++ xmake/core/sandbox/modules/pairs.lua | 25 +++ xmake/core/sandbox/modules/path.lua | 25 +++ xmake/core/sandbox/modules/print.lua | 25 +++ xmake/core/sandbox/modules/printf.lua | 25 +++ xmake/core/sandbox/modules/raise.lua | 25 +++ xmake/core/sandbox/modules/string.lua | 63 ++++++ xmake/core/sandbox/modules/table.lua | 25 +++ xmake/core/sandbox/modules/utils.lua | 46 +++++ xmake/core/sandbox/modules/vformat.lua | 26 +++ xmake/core/sandbox/os.lua | 206 -------------------- xmake/core/sandbox/pairs.lua | 25 --- xmake/core/sandbox/path.lua | 25 --- xmake/core/sandbox/print.lua | 25 --- xmake/core/sandbox/printf.lua | 25 --- xmake/core/sandbox/raise.lua | 25 --- xmake/core/sandbox/sandbox.lua | 302 +++++++++++++++++++++++++++++ xmake/core/sandbox/string.lua | 63 ------ xmake/core/sandbox/table.lua | 25 --- xmake/core/sandbox/utils.lua | 46 ----- xmake/core/sandbox/vformat.lua | 26 --- 35 files changed, 1147 insertions(+), 1147 deletions(-) delete mode 100644 xmake/core/base/sandbox.lua delete mode 100644 xmake/core/sandbox/assert.lua delete mode 100644 xmake/core/sandbox/format.lua delete mode 100644 xmake/core/sandbox/ifelse.lua delete mode 100644 xmake/core/sandbox/import.lua delete mode 100644 xmake/core/sandbox/ipairs.lua create mode 100644 xmake/core/sandbox/modules/assert.lua create mode 100644 xmake/core/sandbox/modules/format.lua create mode 100644 xmake/core/sandbox/modules/ifelse.lua create mode 100644 xmake/core/sandbox/modules/import.lua create mode 100644 xmake/core/sandbox/modules/ipairs.lua create mode 100644 xmake/core/sandbox/modules/os.lua create mode 100644 xmake/core/sandbox/modules/pairs.lua create mode 100644 xmake/core/sandbox/modules/path.lua create mode 100644 xmake/core/sandbox/modules/print.lua create mode 100644 xmake/core/sandbox/modules/printf.lua create mode 100644 xmake/core/sandbox/modules/raise.lua create mode 100644 xmake/core/sandbox/modules/string.lua create mode 100644 xmake/core/sandbox/modules/table.lua create mode 100644 xmake/core/sandbox/modules/utils.lua create mode 100644 xmake/core/sandbox/modules/vformat.lua delete mode 100644 xmake/core/sandbox/os.lua delete mode 100644 xmake/core/sandbox/pairs.lua delete mode 100644 xmake/core/sandbox/path.lua delete mode 100644 xmake/core/sandbox/print.lua delete mode 100644 xmake/core/sandbox/printf.lua delete mode 100644 xmake/core/sandbox/raise.lua create mode 100644 xmake/core/sandbox/sandbox.lua delete mode 100644 xmake/core/sandbox/string.lua delete mode 100644 xmake/core/sandbox/table.lua delete mode 100644 xmake/core/sandbox/utils.lua delete mode 100644 xmake/core/sandbox/vformat.lua diff --git a/xmake/core/base/deprecated/interpreter.lua b/xmake/core/base/deprecated/interpreter.lua index 44c8314d7..8ed452588 100644 --- a/xmake/core/base/deprecated/interpreter.lua +++ b/xmake/core/base/deprecated/interpreter.lua @@ -29,7 +29,7 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") -local sandbox = require("base/sandbox") +local sandbox = require("sandbox/sandbox") -- register api for set_scope() function deprecated_interpreter.api_register_set_scope(self, ...) diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua index 551dad5fc..490da9405 100644 --- a/xmake/core/base/interpreter.lua +++ b/xmake/core/base/interpreter.lua @@ -29,7 +29,7 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") -local sandbox = require("base/sandbox") +local sandbox = require("sandbox/sandbox") -- traceback function interpreter._traceback(errors) diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua deleted file mode 100644 index 3c4d1aceb..000000000 --- a/xmake/core/base/sandbox.lua +++ /dev/null @@ -1,302 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file sandbox.lua --- - --- define module: sandbox -local sandbox = sandbox or {} - --- load modules -local os = require("base/os") -local path = require("base/path") -local table = require("base/table") -local utils = require("base/utils") -local string = require("base/string") - --- traceback -function sandbox._traceback(errors) - - -- init results - local results = "" - if errors then - results = errors .. "\n" - end - results = results .. "stack traceback:\n" - - -- make results - local level = 2 - while true do - - -- get debug info - local info = debug.getinfo(level, "Sln") - - -- end? - if not info or (info.name and (info.name == "xpcall" or info.name == "load")) then - break - end - - -- function? - if info.what == "C" then - results = results .. string.format(" [C]: in function '%s'\n", info.name) - elseif info.name then - results = results .. string.format(" [%s:%d]: in function '%s'\n", info.short_src, info.currentline, info.name) - elseif info.what == "main" then - results = results .. string.format(" [%s:%d]: in main chunk\n", info.short_src, info.currentline) - break - else - results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline) - end - - -- next - level = level + 1 - end - - -- ok? - return results -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() - - -- init an sandbox instance - local self = {_PUBLIC = {}, _PRIVATE = {}} - - -- inherit the interfaces of sandbox - for k, v in pairs(sandbox) do - if type(v) == "function" then - self[k] = v - end - end - - -- load builtin module files - local builtin_module_files = os.match(path.join(xmake._CORE_DIR, "sandbox/*.lua")) - if builtin_module_files then - for _, builtin_module_file in ipairs(builtin_module_files) do - - -- the module name - local module_name = path.basename(builtin_module_file) - assert(module_name) - - -- load script - local script, errors = loadfile(builtin_module_file) - if script then - - -- load module - local ok, results = xpcall(script, debug.traceback) - if not ok then - os.raise(results) - end - - -- register module - self:_api_register_builtin(module_name, results) - else - -- error - os.raise(errors) - end - end - end - - -- save self - setmetatable(self._PUBLIC, { __index = function (tbl, key) - if type(key) == "string" and key == "_SANDBOX" and rawget(tbl, "_SANDBOX_READABLE") then - return self - end - return rawget(tbl, key) - end - , __newindex = function (tbl, key, val) - if type(key) == "string" and (key == "_SANDBOX" or key == "_SANDBOX_READABLE") then - return - end - rawset(tbl, key, val) - end}) - - -- ok? - return self -end - --- make sandbox instance with the given script -function sandbox.make(script, interp) - - -- check - assert(script and interp) - - -- init self - local self = sandbox._init() - - -- check - assert(self and self._PUBLIC and self._PRIVATE) - - -- save filter - self._PRIVATE._FILTER = interp:filter() - - -- save root directory - self._PRIVATE._ROOTDIR = interp:rootdir() - - -- this script is module name? import it first - if type(script) == "string" then - - -- import module as script - local modulename = script - script = function () - - -- import it - import(modulename).main() - end - end - - -- no script? - if script == nil then - return nil, "no script!" - end - - -- invalid script? - if script ~= nil and type(script) ~= "function" then - return nil, "invalid script!" - end - - -- bind public scope - setfenv(script, self._PUBLIC) - - -- save script - self._PRIVATE._SCRIPT = script - - -- ok - return self -end - --- load script in the sandbox -function sandbox.load(script, ...) - - -- load script - return xpcall(script, sandbox._traceback, ...) -end - --- fork a new sandbox from the given sandbox -function sandbox.fork(self, script) - - -- no script? - if script == nil then - return nil, "no script!" - end - - -- invalid script? - if script ~= nil and type(script) ~= "function" then - return nil, "invalid script!" - end - - -- init a new sandbox instance - local instance = sandbox._init() - - -- check - assert(instance and instance._PUBLIC and instance._PRIVATE) - - -- inherit the filter - instance._PRIVATE._FILTER = self:filter() - - -- inherit the root directory - instance._PRIVATE._ROOTDIR = self:rootdir() - - -- bind public scope - setfenv(script, instance._PUBLIC) - - -- save script - instance._PRIVATE._SCRIPT = script - - -- ok? - return instance -end - --- get script from the given sandbox -function sandbox.script(self) - - -- check - assert(self and self._PRIVATE) - - -- get it - return self._PRIVATE._SCRIPT -end - --- get filter from the given sandbox -function sandbox.filter(self) - - -- check - assert(self and self._PRIVATE) - - -- get it - return self._PRIVATE._FILTER -end - --- get root directory from the given sandbox -function sandbox.rootdir(self) - - -- check - assert(self and self._PRIVATE) - - -- get it - return self._PRIVATE._ROOTDIR -end - --- get current instance in the sandbox modules -function sandbox.instance() - - -- find self instance for the current sandbox - local instance = nil - local level = 2 - while level < 16 do - - -- get scope - local scope = getfenv(level) - if scope then - - -- enable to read _SANDBOX - rawset(scope, "_SANDBOX_READABLE", true) - - -- attempt to get it - instance = scope._SANDBOX - - -- disable to read _SANDBOX - rawset(scope, "_SANDBOX_READABLE", nil) - end - - -- found? - if instance then - break - end - - -- next - level = level + 1 - end - - -- ok? - return instance -end - --- return module: sandbox -return sandbox diff --git a/xmake/core/base/task.lua b/xmake/core/base/task.lua index c7eb18f5a..d77c58710 100644 --- a/xmake/core/base/task.lua +++ b/xmake/core/base/task.lua @@ -29,7 +29,7 @@ local table = require("base/table") local utils = require("base/utils") local filter = require("base/filter") local string = require("base/string") -local sandbox = require("base/sandbox") +local sandbox = require("sandbox/sandbox") local interpreter = require("base/interpreter") -- the directories of tasks diff --git a/xmake/core/sandbox/assert.lua b/xmake/core/sandbox/assert.lua deleted file mode 100644 index 1e64c73db..000000000 --- a/xmake/core/sandbox/assert.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file assert.lua --- - --- load module -return assert - diff --git a/xmake/core/sandbox/format.lua b/xmake/core/sandbox/format.lua deleted file mode 100644 index efcea0682..000000000 --- a/xmake/core/sandbox/format.lua +++ /dev/null @@ -1,26 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file format.lua --- - --- return module -return require("sandbox/string").format - - diff --git a/xmake/core/sandbox/ifelse.lua b/xmake/core/sandbox/ifelse.lua deleted file mode 100644 index bbd032704..000000000 --- a/xmake/core/sandbox/ifelse.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file ifelse.lua --- - --- load module -return require("sandbox/utils").ifelse - diff --git a/xmake/core/sandbox/import.lua b/xmake/core/sandbox/import.lua deleted file mode 100644 index 626554600..000000000 --- a/xmake/core/sandbox/import.lua +++ /dev/null @@ -1,250 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file import.lua --- - --- define module -local sandbox_import = sandbox_import or {} - --- load modules -local os = require("base/os") -local path = require("base/path") -local utils = require("base/utils") -local table = require("base/table") -local string = require("base/string") -local sandbox = require("base/sandbox") - --- get module name -function sandbox_import._modulename(name) - - -- check - assert(name) - - -- find modulename - local i = name:find_last(".", true) - if i then - name = name:sub(i + 1) - end - - -- get it - return name -end - --- load module from file -function sandbox_import._loadfile(filepath, instance) - - -- check - assert(filepath) - - -- load module script - local script, errors = loadfile(filepath) - if not script then - return nil, errors - end - - -- with sandbox? - if instance then - - -- fork a new sandbox for this script - instance, errors = instance:fork(script) - if not instance then - return nil, errors - end - - -- backup the scope variables first - local scope_public = getfenv(instance:script()) - local scope_backup = {} - table.copy2(scope_backup, scope_public) - - -- load module with sandbox - local ok, errors = sandbox.load(instance:script()) - if not ok then - return nil, errors - end - - -- only export new public functions - local result = {} - for k, v in pairs(scope_public) do - if type(v) == "function" and scope_backup[k] == nil then - result[k] = v - end - end - - -- get module - return result - end - - -- load module without sandbox - local ok, result = xpcall(script, debug.traceback) - if not ok then - return nil, result - end - - -- ok? - return result -end - --- load module -function sandbox_import._load(dir, name, instance) - - -- check - assert(dir and name) - - -- replace "package.module" => "package/module" - name = (name:gsub("%.", "/")) - assert(name) - - -- load the single module? - local module = nil - if os.isfile(path.join(dir, name .. ".lua")) then - - -- load module - local result, errors = sandbox_import._loadfile(path.join(dir, name .. ".lua"), instance) - if not result then - return nil, errors - end - - -- save module - module = result - - -- load modules - elseif os.isdir(path.join(dir, name)) then - - -- get modulefiles - local moduleroot = path.join(path.join(dir, name)) - local modulefiles = os.match(path.join(moduleroot, "**.lua")) - if modulefiles then - for _, modulefile in ipairs(modulefiles) do - - -- load module - local result, errors = sandbox_import._loadfile(modulefile, instance) - if not result then - return nil, errors - end - - -- get the module path - local modulepath = path.relative(modulefile, moduleroot) - if not modulepath then - return nil, string.format("cannot get the path for module: %s", name) - end - - -- init the root module - module = module or {} - - -- save module - local scope = module - for _, modulename in ipairs(path.split(modulepath)) do - - -- is end? - local pos = modulename:find(".lua", 1, true) - if pos then - - -- get the module name - modulename = modulename:sub(1, pos - 1) - assert(modulename) - - -- save module - scope[modulename] = result - - -- is scope? - else - - -- enter submodule - scope[modulename] = scope[modulename] or {} - scope = scope[modulename] - end - end - end - end - end - - -- this module not found? - if not module then - return nil, string.format("module: %s not found!", name) - end - - -- return it - return module -end - --- import module --- --- .e.g --- --- import("core.platform") --- => platform --- --- import("core.platform", "p") --- => p --- --- import("core") --- => core --- => core.platform --- -function sandbox_import.import(name, alias) - - -- check - assert(name) - - -- get the current sandbox instance - local instance = sandbox.instance() - assert(instance) - - -- the root directory for this sandbox script - local rootdir = instance:rootdir() - assert(rootdir) - - -- load module from the sandbox root directory first - local module, errors = sandbox_import._load(rootdir, name, instance) - if not module then - -- load module from the sandbox core directory - module, errors = sandbox_import._load(path.join(xmake._CORE_DIR, "sandbox/import"), name) - end - - -- check - if not module then - os.raise("cannot import module: %s, %s", name, errors) - end - - -- get module name - local modulename = sandbox_import._modulename(name) - if not modulename then - os.raise("cannot get module name for %s", name) - end - - -- get the parent scope - local scope_parent = getfenv(2) - assert(scope_parent) - - -- this module has been imported? - if rawget(scope_parent, modulename) then - os.raise("this module: %s has been imported!", name) - end - - -- import this module into the parent scope - scope_parent[alias or modulename] = module - - -- return it - return module -end - --- load module -return sandbox_import.import - diff --git a/xmake/core/sandbox/ipairs.lua b/xmake/core/sandbox/ipairs.lua deleted file mode 100644 index a3d2f636e..000000000 --- a/xmake/core/sandbox/ipairs.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file ipairs.lua --- - --- load module -return ipairs - diff --git a/xmake/core/sandbox/modules/assert.lua b/xmake/core/sandbox/modules/assert.lua new file mode 100644 index 000000000..1e64c73db --- /dev/null +++ b/xmake/core/sandbox/modules/assert.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file assert.lua +-- + +-- load module +return assert + diff --git a/xmake/core/sandbox/modules/format.lua b/xmake/core/sandbox/modules/format.lua new file mode 100644 index 000000000..3f7465098 --- /dev/null +++ b/xmake/core/sandbox/modules/format.lua @@ -0,0 +1,26 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file format.lua +-- + +-- return module +return require("sandbox/modules/string").format + + diff --git a/xmake/core/sandbox/modules/ifelse.lua b/xmake/core/sandbox/modules/ifelse.lua new file mode 100644 index 000000000..02e84a276 --- /dev/null +++ b/xmake/core/sandbox/modules/ifelse.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file ifelse.lua +-- + +-- load module +return require("sandbox/modules/utils").ifelse + diff --git a/xmake/core/sandbox/modules/import.lua b/xmake/core/sandbox/modules/import.lua new file mode 100644 index 000000000..bdb23f896 --- /dev/null +++ b/xmake/core/sandbox/modules/import.lua @@ -0,0 +1,250 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file import.lua +-- + +-- define module +local sandbox_import = sandbox_import or {} + +-- load modules +local os = require("base/os") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local string = require("base/string") +local sandbox = require("sandbox/sandbox") + +-- get module name +function sandbox_import._modulename(name) + + -- check + assert(name) + + -- find modulename + local i = name:find_last(".", true) + if i then + name = name:sub(i + 1) + end + + -- get it + return name +end + +-- load module from file +function sandbox_import._loadfile(filepath, instance) + + -- check + assert(filepath) + + -- load module script + local script, errors = loadfile(filepath) + if not script then + return nil, errors + end + + -- with sandbox? + if instance then + + -- fork a new sandbox for this script + instance, errors = instance:fork(script) + if not instance then + return nil, errors + end + + -- backup the scope variables first + local scope_public = getfenv(instance:script()) + local scope_backup = {} + table.copy2(scope_backup, scope_public) + + -- load module with sandbox + local ok, errors = sandbox.load(instance:script()) + if not ok then + return nil, errors + end + + -- only export new public functions + local result = {} + for k, v in pairs(scope_public) do + if type(v) == "function" and scope_backup[k] == nil then + result[k] = v + end + end + + -- get module + return result + end + + -- load module without sandbox + local ok, result = xpcall(script, debug.traceback) + if not ok then + return nil, result + end + + -- ok? + return result +end + +-- load module +function sandbox_import._load(dir, name, instance) + + -- check + assert(dir and name) + + -- replace "package.module" => "package/module" + name = (name:gsub("%.", "/")) + assert(name) + + -- load the single module? + local module = nil + if os.isfile(path.join(dir, name .. ".lua")) then + + -- load module + local result, errors = sandbox_import._loadfile(path.join(dir, name .. ".lua"), instance) + if not result then + return nil, errors + end + + -- save module + module = result + + -- load modules + elseif os.isdir(path.join(dir, name)) then + + -- get modulefiles + local moduleroot = path.join(path.join(dir, name)) + local modulefiles = os.match(path.join(moduleroot, "**.lua")) + if modulefiles then + for _, modulefile in ipairs(modulefiles) do + + -- load module + local result, errors = sandbox_import._loadfile(modulefile, instance) + if not result then + return nil, errors + end + + -- get the module path + local modulepath = path.relative(modulefile, moduleroot) + if not modulepath then + return nil, string.format("cannot get the path for module: %s", name) + end + + -- init the root module + module = module or {} + + -- save module + local scope = module + for _, modulename in ipairs(path.split(modulepath)) do + + -- is end? + local pos = modulename:find(".lua", 1, true) + if pos then + + -- get the module name + modulename = modulename:sub(1, pos - 1) + assert(modulename) + + -- save module + scope[modulename] = result + + -- is scope? + else + + -- enter submodule + scope[modulename] = scope[modulename] or {} + scope = scope[modulename] + end + end + end + end + end + + -- this module not found? + if not module then + return nil, string.format("module: %s not found!", name) + end + + -- return it + return module +end + +-- import module +-- +-- .e.g +-- +-- import("core.platform") +-- => platform +-- +-- import("core.platform", "p") +-- => p +-- +-- import("core") +-- => core +-- => core.platform +-- +function sandbox_import.import(name, alias) + + -- check + assert(name) + + -- get the current sandbox instance + local instance = sandbox.instance() + assert(instance) + + -- the root directory for this sandbox script + local rootdir = instance:rootdir() + assert(rootdir) + + -- load module from the sandbox root directory first + local module, errors = sandbox_import._load(rootdir, name, instance) + if not module then + -- load module from the sandbox core directory + module, errors = sandbox_import._load(path.join(xmake._CORE_DIR, "sandbox/modules/import"), name) + end + + -- check + if not module then + os.raise("cannot import module: %s, %s", name, errors) + end + + -- get module name + local modulename = sandbox_import._modulename(name) + if not modulename then + os.raise("cannot get module name for %s", name) + end + + -- get the parent scope + local scope_parent = getfenv(2) + assert(scope_parent) + + -- this module has been imported? + if rawget(scope_parent, modulename) then + os.raise("this module: %s has been imported!", name) + end + + -- import this module into the parent scope + scope_parent[alias or modulename] = module + + -- return it + return module +end + +-- load module +return sandbox_import.import + diff --git a/xmake/core/sandbox/modules/ipairs.lua b/xmake/core/sandbox/modules/ipairs.lua new file mode 100644 index 000000000..a3d2f636e --- /dev/null +++ b/xmake/core/sandbox/modules/ipairs.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file ipairs.lua +-- + +-- load module +return ipairs + diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua new file mode 100644 index 000000000..89e07ca96 --- /dev/null +++ b/xmake/core/sandbox/modules/os.lua @@ -0,0 +1,206 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file os.lua +-- + +-- load modules +local io = require("base/io") +local os = require("base/os") +local utils = require("base/utils") +local vformat = require("sandbox/modules/vformat") + +-- define module +local sandbox_os = sandbox_os or {} + +-- inherit the public interfaces of os +sandbox_os.curdir = os.curdir + +-- copy file or directory +function sandbox_os.cp(src, dst) + + -- check + assert(src and dst) + + -- format it first + src = vformat(src) + dst = vformat(dst) + + -- done + local ok, errors = os.cp(src, dst) + if not ok then + os.raise(errors) + end + +end + +-- move file or directory +function sandbox_os.mv(src, dst) + + -- check + assert(src and dst) + + -- format it first + src = vformat(src) + dst = vformat(dst) + + -- done + local ok, errors = os.mv(src, dst) + if not ok then + os.raise(errors) + end + +end + +-- remove file or directory +function sandbox_os.rm(file_or_dir, emptydir) + + -- check + assert(file_or_dir) + + -- format it first + file_or_dir = vformat(file_or_dir) + + -- done + local ok, errors = os.rm(file_or_dir, emptydir) + if not ok then + os.raise(errors) + end + +end + +-- change to directory +function sandbox_os.cd(dir) + + -- check + assert(dir) + + -- format it first + dir = vformat(dir) + + -- done + local ok, errors = os.cd(dir) + if not ok then + os.raise(errors) + end + +end + +-- create directory +function sandbox_os.mkdir(dir) + + -- check + assert(dir) + + -- format it first + dir = vformat(dir) + + -- done + if not os.mkdir(dir) then + os.raise("create directory: %s failed!", dir) + end + +end + +-- remove directory +function sandbox_os.rmdir(dir) + + -- check + assert(dir) + + -- format it first + dir = vformat(dir) + + -- done + if os.isdir(dir) then + if not os.rmdir(dir) then + os.raise("remove directory: %s failed!", dir) + end + end + +end + +-- run shell +function sandbox_os.run(cmd, ...) + + -- make command + cmd = vformat(cmd, ...) + + -- make temporary log file + log = os.tmpname() + + -- run command + if 0 ~= os.execute(cmd .. string.format(" > %s 2>&1", log)) then + io.cat(log) + os.raise("run: %s failed!", cmd) + end + + -- remove the temporary log file + os.rm(log) +end + +-- match files or directories +function sandbox_os.match(pattern, findir) + + -- check + assert(pattern) + + -- format it first + pattern = vformat(pattern) + + -- match it + return os.match(pattern, findir) +end + +-- is directory? +function sandbox_os.isdir(dirpath) + + -- check + assert(dirpath) + + -- format it first + dirpath = vformat(dirpath) + + -- done + return os.isdir(dirpath) +end + +-- is directory? +function sandbox_os.isfile(filepath) + + -- check + assert(filepath) + + -- format it first + filepath = vformat(filepath) + + -- done + return os.isfile(filepath) +end + +-- raise an exception and abort the current script +function sandbox_os.raise(msg, ...) + + -- raise it + os.raise(msg, ...) +end + +-- return module +return sandbox_os + diff --git a/xmake/core/sandbox/modules/pairs.lua b/xmake/core/sandbox/modules/pairs.lua new file mode 100644 index 000000000..ce7729bd5 --- /dev/null +++ b/xmake/core/sandbox/modules/pairs.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file pairs.lua +-- + +-- load module +return pairs + diff --git a/xmake/core/sandbox/modules/path.lua b/xmake/core/sandbox/modules/path.lua new file mode 100644 index 000000000..d46b09252 --- /dev/null +++ b/xmake/core/sandbox/modules/path.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file path.lua +-- + +-- load module +return require("base/path") + diff --git a/xmake/core/sandbox/modules/print.lua b/xmake/core/sandbox/modules/print.lua new file mode 100644 index 000000000..9e8502c16 --- /dev/null +++ b/xmake/core/sandbox/modules/print.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file print.lua +-- + +-- load module +return print + diff --git a/xmake/core/sandbox/modules/printf.lua b/xmake/core/sandbox/modules/printf.lua new file mode 100644 index 000000000..541a13a91 --- /dev/null +++ b/xmake/core/sandbox/modules/printf.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file printf.lua +-- + +-- load module +return require("sandbox/modules/utils").printf + diff --git a/xmake/core/sandbox/modules/raise.lua b/xmake/core/sandbox/modules/raise.lua new file mode 100644 index 000000000..4038e5d36 --- /dev/null +++ b/xmake/core/sandbox/modules/raise.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file raise.lua +-- + +-- load module +return require("sandbox/modules/os").raise + diff --git a/xmake/core/sandbox/modules/string.lua b/xmake/core/sandbox/modules/string.lua new file mode 100644 index 000000000..69ddbb723 --- /dev/null +++ b/xmake/core/sandbox/modules/string.lua @@ -0,0 +1,63 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file string.lua +-- + +-- load modules +local string = require("base/string") +local sandbox = require("sandbox/sandbox") + +-- define module +local sandbox_string = sandbox_string or {} + +-- inherit the public interfaces of string +for k, v in pairs(string) do + if not k:startswith("_") and type(v) == "function" then + sandbox_string[k] = v + end +end + +-- format string with the builtin variables +function sandbox_string.vformat(format, ...) + + -- check + assert(format) + + -- get the current sandbox instance + local instance = sandbox.instance() + assert(instance) + + -- format string + local result = string.format(format, ...) + assert(result) + + -- get filter from the current sandbox + local filter = instance:filter() + if filter then + result = filter:handle(result) + end + + -- ok? + return result +end + +-- return module +return sandbox_string + diff --git a/xmake/core/sandbox/modules/table.lua b/xmake/core/sandbox/modules/table.lua new file mode 100644 index 000000000..bab0439b9 --- /dev/null +++ b/xmake/core/sandbox/modules/table.lua @@ -0,0 +1,25 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file table.lua +-- + +-- load module +return require("base/table") + diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua new file mode 100644 index 000000000..f0cb95db9 --- /dev/null +++ b/xmake/core/sandbox/modules/utils.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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file utils.lua +-- + +-- load modules +local utils = require("base/utils") +local vformat = require("sandbox/modules/vformat") + +-- define module +local sandbox_utils = sandbox_utils or {} + +-- inherit the public interfaces of utils +for k, v in pairs(utils) do + if not k:startswith("_") and type(v) == "function" then + sandbox_utils[k] = v + end +end + +-- printf with the builtin variables +function sandbox_utils.printf(format, ...) + + -- done + return print(vformat(format, ...)) +end + +-- return module +return sandbox_utils + diff --git a/xmake/core/sandbox/modules/vformat.lua b/xmake/core/sandbox/modules/vformat.lua new file mode 100644 index 000000000..f69e70fd7 --- /dev/null +++ b/xmake/core/sandbox/modules/vformat.lua @@ -0,0 +1,26 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file vformat.lua +-- + +-- return module +return require("sandbox/modules/string").vformat + + diff --git a/xmake/core/sandbox/os.lua b/xmake/core/sandbox/os.lua deleted file mode 100644 index 52945efbc..000000000 --- a/xmake/core/sandbox/os.lua +++ /dev/null @@ -1,206 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file os.lua --- - --- load modules -local io = require("base/io") -local os = require("base/os") -local utils = require("base/utils") -local vformat = require("sandbox/vformat") - --- define module -local sandbox_os = sandbox_os or {} - --- inherit the public interfaces of os -sandbox_os.curdir = os.curdir - --- copy file or directory -function sandbox_os.cp(src, dst) - - -- check - assert(src and dst) - - -- format it first - src = vformat(src) - dst = vformat(dst) - - -- done - local ok, errors = os.cp(src, dst) - if not ok then - os.raise(errors) - end - -end - --- move file or directory -function sandbox_os.mv(src, dst) - - -- check - assert(src and dst) - - -- format it first - src = vformat(src) - dst = vformat(dst) - - -- done - local ok, errors = os.mv(src, dst) - if not ok then - os.raise(errors) - end - -end - --- remove file or directory -function sandbox_os.rm(file_or_dir, emptydir) - - -- check - assert(file_or_dir) - - -- format it first - file_or_dir = vformat(file_or_dir) - - -- done - local ok, errors = os.rm(file_or_dir, emptydir) - if not ok then - os.raise(errors) - end - -end - --- change to directory -function sandbox_os.cd(dir) - - -- check - assert(dir) - - -- format it first - dir = vformat(dir) - - -- done - local ok, errors = os.cd(dir) - if not ok then - os.raise(errors) - end - -end - --- create directory -function sandbox_os.mkdir(dir) - - -- check - assert(dir) - - -- format it first - dir = vformat(dir) - - -- done - if not os.mkdir(dir) then - os.raise("create directory: %s failed!", dir) - end - -end - --- remove directory -function sandbox_os.rmdir(dir) - - -- check - assert(dir) - - -- format it first - dir = vformat(dir) - - -- done - if os.isdir(dir) then - if not os.rmdir(dir) then - os.raise("remove directory: %s failed!", dir) - end - end - -end - --- run shell -function sandbox_os.run(cmd, ...) - - -- make command - cmd = vformat(cmd, ...) - - -- make temporary log file - log = os.tmpname() - - -- run command - if 0 ~= os.execute(cmd .. string.format(" > %s 2>&1", log)) then - io.cat(log) - os.raise("run: %s failed!", cmd) - end - - -- remove the temporary log file - os.rm(log) -end - --- match files or directories -function sandbox_os.match(pattern, findir) - - -- check - assert(pattern) - - -- format it first - pattern = vformat(pattern) - - -- match it - return os.match(pattern, findir) -end - --- is directory? -function sandbox_os.isdir(dirpath) - - -- check - assert(dirpath) - - -- format it first - dirpath = vformat(dirpath) - - -- done - return os.isdir(dirpath) -end - --- is directory? -function sandbox_os.isfile(filepath) - - -- check - assert(filepath) - - -- format it first - filepath = vformat(filepath) - - -- done - return os.isfile(filepath) -end - --- raise an exception and abort the current script -function sandbox_os.raise(msg, ...) - - -- raise it - os.raise(msg, ...) -end - --- return module -return sandbox_os - diff --git a/xmake/core/sandbox/pairs.lua b/xmake/core/sandbox/pairs.lua deleted file mode 100644 index ce7729bd5..000000000 --- a/xmake/core/sandbox/pairs.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file pairs.lua --- - --- load module -return pairs - diff --git a/xmake/core/sandbox/path.lua b/xmake/core/sandbox/path.lua deleted file mode 100644 index d46b09252..000000000 --- a/xmake/core/sandbox/path.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file path.lua --- - --- load module -return require("base/path") - diff --git a/xmake/core/sandbox/print.lua b/xmake/core/sandbox/print.lua deleted file mode 100644 index 9e8502c16..000000000 --- a/xmake/core/sandbox/print.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file print.lua --- - --- load module -return print - diff --git a/xmake/core/sandbox/printf.lua b/xmake/core/sandbox/printf.lua deleted file mode 100644 index 803514971..000000000 --- a/xmake/core/sandbox/printf.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file printf.lua --- - --- load module -return require("sandbox/utils").printf - diff --git a/xmake/core/sandbox/raise.lua b/xmake/core/sandbox/raise.lua deleted file mode 100644 index fb1996082..000000000 --- a/xmake/core/sandbox/raise.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file raise.lua --- - --- load module -return require("sandbox/os").raise - diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua new file mode 100644 index 000000000..6245acb1c --- /dev/null +++ b/xmake/core/sandbox/sandbox.lua @@ -0,0 +1,302 @@ +--!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) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file sandbox.lua +-- + +-- define module: sandbox +local sandbox = sandbox or {} + +-- load modules +local os = require("base/os") +local path = require("base/path") +local table = require("base/table") +local utils = require("base/utils") +local string = require("base/string") + +-- traceback +function sandbox._traceback(errors) + + -- init results + local results = "" + if errors then + results = errors .. "\n" + end + results = results .. "stack traceback:\n" + + -- make results + local level = 2 + while true do + + -- get debug info + local info = debug.getinfo(level, "Sln") + + -- end? + if not info or (info.name and (info.name == "xpcall" or info.name == "load")) then + break + end + + -- function? + if info.what == "C" then + results = results .. string.format(" [C]: in function '%s'\n", info.name) + elseif info.name then + results = results .. string.format(" [%s:%d]: in function '%s'\n", info.short_src, info.currentline, info.name) + elseif info.what == "main" then + results = results .. string.format(" [%s:%d]: in main chunk\n", info.short_src, info.currentline) + break + else + results = results .. string.format(" [%s:%d]:\n", info.short_src, info.currentline) + end + + -- next + level = level + 1 + end + + -- ok? + return results +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() + + -- init an sandbox instance + local self = {_PUBLIC = {}, _PRIVATE = {}} + + -- inherit the interfaces of sandbox + for k, v in pairs(sandbox) do + if type(v) == "function" then + self[k] = v + end + end + + -- load builtin module files + local builtin_module_files = os.match(path.join(xmake._CORE_DIR, "sandbox/modules/*.lua")) + if builtin_module_files then + for _, builtin_module_file in ipairs(builtin_module_files) do + + -- the module name + local module_name = path.basename(builtin_module_file) + assert(module_name) + + -- load script + local script, errors = loadfile(builtin_module_file) + if script then + + -- load module + local ok, results = xpcall(script, debug.traceback) + if not ok then + os.raise(results) + end + + -- register module + self:_api_register_builtin(module_name, results) + else + -- error + os.raise(errors) + end + end + end + + -- save self + setmetatable(self._PUBLIC, { __index = function (tbl, key) + if type(key) == "string" and key == "_SANDBOX" and rawget(tbl, "_SANDBOX_READABLE") then + return self + end + return rawget(tbl, key) + end + , __newindex = function (tbl, key, val) + if type(key) == "string" and (key == "_SANDBOX" or key == "_SANDBOX_READABLE") then + return + end + rawset(tbl, key, val) + end}) + + -- ok? + return self +end + +-- make sandbox instance with the given script +function sandbox.make(script, interp) + + -- check + assert(script and interp) + + -- init self + local self = sandbox._init() + + -- check + assert(self and self._PUBLIC and self._PRIVATE) + + -- save filter + self._PRIVATE._FILTER = interp:filter() + + -- save root directory + self._PRIVATE._ROOTDIR = interp:rootdir() + + -- this script is module name? import it first + if type(script) == "string" then + + -- import module as script + local modulename = script + script = function () + + -- import it + import(modulename).main() + end + end + + -- no script? + if script == nil then + return nil, "no script!" + end + + -- invalid script? + if script ~= nil and type(script) ~= "function" then + return nil, "invalid script!" + end + + -- bind public scope + setfenv(script, self._PUBLIC) + + -- save script + self._PRIVATE._SCRIPT = script + + -- ok + return self +end + +-- load script in the sandbox +function sandbox.load(script, ...) + + -- load script + return xpcall(script, sandbox._traceback, ...) +end + +-- fork a new sandbox from the given sandbox +function sandbox.fork(self, script) + + -- no script? + if script == nil then + return nil, "no script!" + end + + -- invalid script? + if script ~= nil and type(script) ~= "function" then + return nil, "invalid script!" + end + + -- init a new sandbox instance + local instance = sandbox._init() + + -- check + assert(instance and instance._PUBLIC and instance._PRIVATE) + + -- inherit the filter + instance._PRIVATE._FILTER = self:filter() + + -- inherit the root directory + instance._PRIVATE._ROOTDIR = self:rootdir() + + -- bind public scope + setfenv(script, instance._PUBLIC) + + -- save script + instance._PRIVATE._SCRIPT = script + + -- ok? + return instance +end + +-- get script from the given sandbox +function sandbox.script(self) + + -- check + assert(self and self._PRIVATE) + + -- get it + return self._PRIVATE._SCRIPT +end + +-- get filter from the given sandbox +function sandbox.filter(self) + + -- check + assert(self and self._PRIVATE) + + -- get it + return self._PRIVATE._FILTER +end + +-- get root directory from the given sandbox +function sandbox.rootdir(self) + + -- check + assert(self and self._PRIVATE) + + -- get it + return self._PRIVATE._ROOTDIR +end + +-- get current instance in the sandbox modules +function sandbox.instance() + + -- find self instance for the current sandbox + local instance = nil + local level = 2 + while level < 16 do + + -- get scope + local scope = getfenv(level) + if scope then + + -- enable to read _SANDBOX + rawset(scope, "_SANDBOX_READABLE", true) + + -- attempt to get it + instance = scope._SANDBOX + + -- disable to read _SANDBOX + rawset(scope, "_SANDBOX_READABLE", nil) + end + + -- found? + if instance then + break + end + + -- next + level = level + 1 + end + + -- ok? + return instance +end + +-- return module: sandbox +return sandbox diff --git a/xmake/core/sandbox/string.lua b/xmake/core/sandbox/string.lua deleted file mode 100644 index 5812c40dd..000000000 --- a/xmake/core/sandbox/string.lua +++ /dev/null @@ -1,63 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file string.lua --- - --- load modules -local string = require("base/string") -local sandbox = require("base/sandbox") - --- define module -local sandbox_string = sandbox_string or {} - --- inherit the public interfaces of string -for k, v in pairs(string) do - if not k:startswith("_") and type(v) == "function" then - sandbox_string[k] = v - end -end - --- format string with the builtin variables -function sandbox_string.vformat(format, ...) - - -- check - assert(format) - - -- get the current sandbox instance - local instance = sandbox.instance() - assert(instance) - - -- format string - local result = string.format(format, ...) - assert(result) - - -- get filter from the current sandbox - local filter = instance:filter() - if filter then - result = filter:handle(result) - end - - -- ok? - return result -end - --- return module -return sandbox_string - diff --git a/xmake/core/sandbox/table.lua b/xmake/core/sandbox/table.lua deleted file mode 100644 index bab0439b9..000000000 --- a/xmake/core/sandbox/table.lua +++ /dev/null @@ -1,25 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file table.lua --- - --- load module -return require("base/table") - diff --git a/xmake/core/sandbox/utils.lua b/xmake/core/sandbox/utils.lua deleted file mode 100644 index edc23e032..000000000 --- a/xmake/core/sandbox/utils.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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file utils.lua --- - --- load modules -local utils = require("base/utils") -local vformat = require("sandbox/vformat") - --- define module -local sandbox_utils = sandbox_utils or {} - --- inherit the public interfaces of utils -for k, v in pairs(utils) do - if not k:startswith("_") and type(v) == "function" then - sandbox_utils[k] = v - end -end - --- printf with the builtin variables -function sandbox_utils.printf(format, ...) - - -- done - return print(vformat(format, ...)) -end - --- return module -return sandbox_utils - diff --git a/xmake/core/sandbox/vformat.lua b/xmake/core/sandbox/vformat.lua deleted file mode 100644 index ad2c25529..000000000 --- a/xmake/core/sandbox/vformat.lua +++ /dev/null @@ -1,26 +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) 2015 - 2016, ruki All rights reserved. --- --- @author ruki --- @file vformat.lua --- - --- return module -return require("sandbox/string").vformat - - -- cgit v1.3.1