diff options
| author | ruki <[email protected]> | 2016-03-03 13:49:00 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-03-03 13:49:00 +0800 |
| commit | 2899c85965c428974b5bc07ddec461bf2055c747 (patch) | |
| tree | 416be40a9c77b98f921f760a49e371444b835b99 /xmake/core/sandbox | |
| parent | 5cf23fadbd11d5c6aaf6c69da322f9150d10cf49 (diff) | |
move sandbox files
Diffstat (limited to 'xmake/core/sandbox')
| -rw-r--r-- | xmake/core/sandbox/modules/assert.lua (renamed from xmake/core/sandbox/assert.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/format.lua (renamed from xmake/core/sandbox/format.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/ifelse.lua (renamed from xmake/core/sandbox/ifelse.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import.lua (renamed from xmake/core/sandbox/import.lua) | 4 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/ipairs.lua (renamed from xmake/core/sandbox/ipairs.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua (renamed from xmake/core/sandbox/os.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/pairs.lua (renamed from xmake/core/sandbox/pairs.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/path.lua (renamed from xmake/core/sandbox/path.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/print.lua (renamed from xmake/core/sandbox/print.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/printf.lua (renamed from xmake/core/sandbox/printf.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/raise.lua (renamed from xmake/core/sandbox/raise.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/string.lua (renamed from xmake/core/sandbox/string.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/table.lua (renamed from xmake/core/sandbox/table.lua) | 0 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua (renamed from xmake/core/sandbox/utils.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/vformat.lua (renamed from xmake/core/sandbox/vformat.lua) | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/sandbox.lua | 302 |
16 files changed, 312 insertions, 10 deletions
diff --git a/xmake/core/sandbox/assert.lua b/xmake/core/sandbox/modules/assert.lua index 1e64c73db..1e64c73db 100644 --- a/xmake/core/sandbox/assert.lua +++ b/xmake/core/sandbox/modules/assert.lua diff --git a/xmake/core/sandbox/format.lua b/xmake/core/sandbox/modules/format.lua index efcea0682..3f7465098 100644 --- a/xmake/core/sandbox/format.lua +++ b/xmake/core/sandbox/modules/format.lua @@ -21,6 +21,6 @@ -- -- return module -return require("sandbox/string").format +return require("sandbox/modules/string").format diff --git a/xmake/core/sandbox/ifelse.lua b/xmake/core/sandbox/modules/ifelse.lua index bbd032704..02e84a276 100644 --- a/xmake/core/sandbox/ifelse.lua +++ b/xmake/core/sandbox/modules/ifelse.lua @@ -21,5 +21,5 @@ -- -- load module -return require("sandbox/utils").ifelse +return require("sandbox/modules/utils").ifelse diff --git a/xmake/core/sandbox/import.lua b/xmake/core/sandbox/modules/import.lua index 626554600..bdb23f896 100644 --- a/xmake/core/sandbox/import.lua +++ b/xmake/core/sandbox/modules/import.lua @@ -29,7 +29,7 @@ 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") +local sandbox = require("sandbox/sandbox") -- get module name function sandbox_import._modulename(name) @@ -215,7 +215,7 @@ function sandbox_import.import(name, alias) 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) + module, errors = sandbox_import._load(path.join(xmake._CORE_DIR, "sandbox/modules/import"), name) end -- check diff --git a/xmake/core/sandbox/ipairs.lua b/xmake/core/sandbox/modules/ipairs.lua index a3d2f636e..a3d2f636e 100644 --- a/xmake/core/sandbox/ipairs.lua +++ b/xmake/core/sandbox/modules/ipairs.lua diff --git a/xmake/core/sandbox/os.lua b/xmake/core/sandbox/modules/os.lua index 52945efbc..89e07ca96 100644 --- a/xmake/core/sandbox/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -24,7 +24,7 @@ local io = require("base/io") local os = require("base/os") local utils = require("base/utils") -local vformat = require("sandbox/vformat") +local vformat = require("sandbox/modules/vformat") -- define module local sandbox_os = sandbox_os or {} diff --git a/xmake/core/sandbox/pairs.lua b/xmake/core/sandbox/modules/pairs.lua index ce7729bd5..ce7729bd5 100644 --- a/xmake/core/sandbox/pairs.lua +++ b/xmake/core/sandbox/modules/pairs.lua diff --git a/xmake/core/sandbox/path.lua b/xmake/core/sandbox/modules/path.lua index d46b09252..d46b09252 100644 --- a/xmake/core/sandbox/path.lua +++ b/xmake/core/sandbox/modules/path.lua diff --git a/xmake/core/sandbox/print.lua b/xmake/core/sandbox/modules/print.lua index 9e8502c16..9e8502c16 100644 --- a/xmake/core/sandbox/print.lua +++ b/xmake/core/sandbox/modules/print.lua diff --git a/xmake/core/sandbox/printf.lua b/xmake/core/sandbox/modules/printf.lua index 803514971..541a13a91 100644 --- a/xmake/core/sandbox/printf.lua +++ b/xmake/core/sandbox/modules/printf.lua @@ -21,5 +21,5 @@ -- -- load module -return require("sandbox/utils").printf +return require("sandbox/modules/utils").printf diff --git a/xmake/core/sandbox/raise.lua b/xmake/core/sandbox/modules/raise.lua index fb1996082..4038e5d36 100644 --- a/xmake/core/sandbox/raise.lua +++ b/xmake/core/sandbox/modules/raise.lua @@ -21,5 +21,5 @@ -- -- load module -return require("sandbox/os").raise +return require("sandbox/modules/os").raise diff --git a/xmake/core/sandbox/string.lua b/xmake/core/sandbox/modules/string.lua index 5812c40dd..69ddbb723 100644 --- a/xmake/core/sandbox/string.lua +++ b/xmake/core/sandbox/modules/string.lua @@ -22,7 +22,7 @@ -- load modules local string = require("base/string") -local sandbox = require("base/sandbox") +local sandbox = require("sandbox/sandbox") -- define module local sandbox_string = sandbox_string or {} diff --git a/xmake/core/sandbox/table.lua b/xmake/core/sandbox/modules/table.lua index bab0439b9..bab0439b9 100644 --- a/xmake/core/sandbox/table.lua +++ b/xmake/core/sandbox/modules/table.lua diff --git a/xmake/core/sandbox/utils.lua b/xmake/core/sandbox/modules/utils.lua index edc23e032..f0cb95db9 100644 --- a/xmake/core/sandbox/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -22,7 +22,7 @@ -- load modules local utils = require("base/utils") -local vformat = require("sandbox/vformat") +local vformat = require("sandbox/modules/vformat") -- define module local sandbox_utils = sandbox_utils or {} diff --git a/xmake/core/sandbox/vformat.lua b/xmake/core/sandbox/modules/vformat.lua index ad2c25529..f69e70fd7 100644 --- a/xmake/core/sandbox/vformat.lua +++ b/xmake/core/sandbox/modules/vformat.lua @@ -21,6 +21,6 @@ -- -- return module -return require("sandbox/string").vformat +return require("sandbox/modules/string").vformat 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 <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- 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 |
