From 4d9ea35a4cc8e4a2ac0d7e2633f088ca0be7e7f1 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 1 Feb 2016 11:36:44 +0800 Subject: load builtin modules for sandbox --- xmake/core/base/sandbox.lua | 38 +++++++++++++----- xmake/core/modules/os.lua | 46 --------------------- xmake/core/modules/project.lua | 73 ---------------------------------- xmake/core/sandbox/builtin/ipairs.lua | 25 ++++++++++++ xmake/core/sandbox/builtin/os.lua | 46 +++++++++++++++++++++ xmake/core/sandbox/builtin/pairs.lua | 25 ++++++++++++ xmake/core/sandbox/builtin/path.lua | 25 ++++++++++++ xmake/core/sandbox/builtin/print.lua | 25 ++++++++++++ xmake/core/sandbox/builtin/project.lua | 73 ++++++++++++++++++++++++++++++++++ xmake/core/sandbox/builtin/string.lua | 25 ++++++++++++ xmake/core/sandbox/builtin/table.lua | 25 ++++++++++++ 11 files changed, 296 insertions(+), 130 deletions(-) delete mode 100644 xmake/core/modules/os.lua delete mode 100644 xmake/core/modules/project.lua create mode 100644 xmake/core/sandbox/builtin/ipairs.lua create mode 100644 xmake/core/sandbox/builtin/os.lua create mode 100644 xmake/core/sandbox/builtin/pairs.lua create mode 100644 xmake/core/sandbox/builtin/path.lua create mode 100644 xmake/core/sandbox/builtin/print.lua create mode 100644 xmake/core/sandbox/builtin/project.lua create mode 100644 xmake/core/sandbox/builtin/string.lua create mode 100644 xmake/core/sandbox/builtin/table.lua diff --git a/xmake/core/base/sandbox.lua b/xmake/core/base/sandbox.lua index 2689c9dc5..37c9edab7 100644 --- a/xmake/core/base/sandbox.lua +++ b/xmake/core/base/sandbox.lua @@ -76,7 +76,7 @@ end function sandbox._api_builtin_import(self, module) -- import - return require("modules/" .. module) + return require("sandbox/" .. module) end -- register api @@ -113,18 +113,34 @@ function sandbox._init() end end - -- register the builtin interfaces - self:_api_register("import", sandbox._api_builtin_import) + -- load builtin module files + local builtin_module_files = os.match(path.join(xmake._CORE_DIR, "sandbox/builtin/*.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 = loadfile(builtin_module_file) + if script then + + -- load module + local ok, results = xpcall(script, debug.traceback) + if not ok then + utils.error(results) + utils.abort() + end - -- register the builtin interfaces for lua - self:_api_register_builtin("print", print) - self:_api_register_builtin("pairs", pairs) - self:_api_register_builtin("ipairs", ipairs) + -- register module + self:_api_register_builtin(module_name, results) + end + end + end - -- register the builtin modules for lua - self:_api_register_builtin("path", path) - self:_api_register_builtin("table", table) - self:_api_register_builtin("string", string) + -- register import() for importing the extensional sandbox modules + self:_api_register("import", sandbox._api_builtin_import) -- ok? return self diff --git a/xmake/core/modules/os.lua b/xmake/core/modules/os.lua deleted file mode 100644 index 3689086f2..000000000 --- a/xmake/core/modules/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/modules/project.lua b/xmake/core/modules/project.lua deleted file mode 100644 index f3f726c6f..000000000 --- a/xmake/core/modules/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/sandbox/builtin/ipairs.lua b/xmake/core/sandbox/builtin/ipairs.lua new file mode 100644 index 000000000..b3551f862 --- /dev/null +++ b/xmake/core/sandbox/builtin/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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file ipairs.lua +-- + +-- load module +return ipairs + diff --git a/xmake/core/sandbox/builtin/os.lua b/xmake/core/sandbox/builtin/os.lua new file mode 100644 index 000000000..3689086f2 --- /dev/null +++ b/xmake/core/sandbox/builtin/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/sandbox/builtin/pairs.lua b/xmake/core/sandbox/builtin/pairs.lua new file mode 100644 index 000000000..f4f74b7e4 --- /dev/null +++ b/xmake/core/sandbox/builtin/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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file pairs.lua +-- + +-- load module +return pairs + diff --git a/xmake/core/sandbox/builtin/path.lua b/xmake/core/sandbox/builtin/path.lua new file mode 100644 index 000000000..925ad7cf8 --- /dev/null +++ b/xmake/core/sandbox/builtin/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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file path.lua +-- + +-- load module +return require("base/path") + diff --git a/xmake/core/sandbox/builtin/print.lua b/xmake/core/sandbox/builtin/print.lua new file mode 100644 index 000000000..b9e9b9f86 --- /dev/null +++ b/xmake/core/sandbox/builtin/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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file print.lua +-- + +-- load module +return print + diff --git a/xmake/core/sandbox/builtin/project.lua b/xmake/core/sandbox/builtin/project.lua new file mode 100644 index 000000000..f3f726c6f --- /dev/null +++ b/xmake/core/sandbox/builtin/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 diff --git a/xmake/core/sandbox/builtin/string.lua b/xmake/core/sandbox/builtin/string.lua new file mode 100644 index 000000000..e9e75acb9 --- /dev/null +++ b/xmake/core/sandbox/builtin/string.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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file string.lua +-- + +-- load module +return require("base/string") + diff --git a/xmake/core/sandbox/builtin/table.lua b/xmake/core/sandbox/builtin/table.lua new file mode 100644 index 000000000..a697504fd --- /dev/null +++ b/xmake/core/sandbox/builtin/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) 2009 - 2015, ruki All rights reserved. +-- +-- @author ruki +-- @file table.lua +-- + +-- load module +return require("base/table") + -- cgit v1.3.1