summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2019-05-24 22:32:53 +0800
committerruki <[email protected]>2019-05-24 09:17:19 +0800
commitdb1994fef8d6425e710bb9a99694ff80eead41aa (patch)
treecf83a262b03b90f61cd2225f769f022416561742 /xmake
parent06412d25342674387849e59d5c48b2b8aeb216b3 (diff)
improve to load builtin modules for sandbox and interpreter
Diffstat (limited to 'xmake')
-rw-r--r--xmake/core/base/interpreter.lua65
-rw-r--r--xmake/core/sandbox/modules/_g.lua23
-rw-r--r--xmake/core/sandbox/sandbox.lua66
3 files changed, 79 insertions, 75 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index bcf60dd5b..029fe3e60 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -595,6 +595,42 @@ function interpreter:_script(script)
return instance:script()
end
+-- get builtin modules
+function interpreter._builtin_modules()
+ local builtin_modules = interpreter._BUILTIN_MODULES
+ if builtin_modules == nil then
+ builtin_modules = {}
+ local builtin_module_files = os.match(path.join(os.programdir(), "core/sandbox/modules/interpreter/*.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 = utils.trycall(script)
+ if not ok then
+ os.raise(results)
+ end
+
+ -- save module
+ builtin_modules[module_name] = results
+ else
+ -- error
+ os.raise(errors)
+ end
+ end
+ end
+ interpreter._BUILTIN_MODULES = builtin_modules
+ end
+ return builtin_modules
+end
+
-- new an interpreter instance
function interpreter.new()
@@ -644,32 +680,9 @@ function interpreter.new()
instance:api_register(nil, "add_subfiles", interpreter.api_builtin_includes)
instance:api_register(nil, "set_xmakever", interpreter.api_builtin_set_xmakever)
- -- load builtin module files
- local builtin_module_files = os.match(path.join(os.programdir(), "core/sandbox/modules/interpreter/*.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 = utils.trycall(script)
- if not ok then
- os.raise(results)
- end
-
- -- register module
- instance:api_register_builtin(module_name, results)
- else
- -- error
- os.raise(errors)
- end
- end
+ -- register the builtin modules
+ for module_name, module in pairs(interpreter._builtin_modules()) do
+ instance:api_register_builtin(module_name, module)
end
-- ok?
diff --git a/xmake/core/sandbox/modules/_g.lua b/xmake/core/sandbox/modules/_g.lua
deleted file mode 100644
index 8c059666b..000000000
--- a/xmake/core/sandbox/modules/_g.lua
+++ /dev/null
@@ -1,23 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
---
--- @author ruki
--- @file _g.lua
---
-
--- return new instance
-return {}
-
diff --git a/xmake/core/sandbox/sandbox.lua b/xmake/core/sandbox/sandbox.lua
index 7f564f053..1c379ec07 100644
--- a/xmake/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/sandbox.lua
@@ -98,6 +98,42 @@ function sandbox._api_register_builtin(self, name, func)
self._PUBLIC[name] = func
end
+-- get builtin modules
+function sandbox._builtin_modules()
+ local builtin_modules = sandbox._BUILTIN_MODULES
+ if builtin_modules == nil then
+ builtin_modules = {}
+ local builtin_module_files = os.match(path.join(os.programdir(), "core/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 = utils.trycall(script)
+ if not ok then
+ os.raise(results)
+ end
+
+ -- save module
+ builtin_modules[module_name] = results
+ else
+ -- error
+ os.raise(errors)
+ end
+ end
+ end
+ sandbox._BUILTIN_MODULES = builtin_modules
+ end
+ return builtin_modules
+end
+
-- new a sandbox instance
function sandbox._new()
@@ -107,32 +143,10 @@ function sandbox._new()
-- inherit the interfaces of sandbox
table.inherit2(instance, sandbox)
- -- load builtin module files
- local builtin_module_files = os.match(path.join(os.programdir(), "core/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 = utils.trycall(script)
- if not ok then
- os.raise(results)
- end
-
- -- register module
- instance:_api_register_builtin(module_name, results)
- else
- -- error
- os.raise(errors)
- end
- end
+ -- register the builtin modules
+ instance:_api_register_builtin("_g", {})
+ for module_name, module in pairs(sandbox._builtin_modules()) do
+ instance:_api_register_builtin(module_name, module)
end
-- bind instance to the public script envirnoment