summaryrefslogtreecommitdiff
path: root/xmake/core/base
diff options
context:
space:
mode:
authorruki <[email protected]>2017-01-10 09:30:02 +0800
committerruki <[email protected]>2017-01-10 20:29:16 +0800
commit022b1a411cdb692b3690d0f3f176b2420b8272aa (patch)
treebad5d57973ddb366a341c65164ebe04765101a92 /xmake/core/base
parentf1d83ac2119e609b872f4481e8991964d326031f (diff)
add set_module
Diffstat (limited to 'xmake/core/base')
-rw-r--r--xmake/core/base/interpreter.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index a284732e8..1220aa5e7 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -922,6 +922,51 @@ function interpreter:api_register_add_array(scope_kind, ...)
self:_api_register_xxx_values(scope_kind, "add", implementation, ...)
end
+-- register api for set_module
+function interpreter:api_register_set_module(scope_kind, ...)
+
+ -- check
+ assert(self)
+
+ -- define implementation
+ local implementation = function (self, scope, name, modulename)
+
+ -- is module name?
+ if type(modulename) ~= "string" then
+ os.raise("set_%s(): invalid module name!", name)
+ end
+
+ -- import module as script
+ local script, errors = loadstring(string.format("import(\"%s\", {inherit = true})", modulename))
+ if not script then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- make sandbox instance with the given script
+ local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
+ if not instance then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- import the module
+ local module, errors = instance:import()
+ if not module then
+ os.raise("set_%s(): %s", name, errors)
+ end
+
+ -- init the module
+ if module.init then
+ module.init()
+ end
+
+ -- save module
+ scope[name] = module
+ end
+
+ -- register implementation
+ self:_api_register_xxx_values(scope_kind, "set", implementation, ...)
+end
+
-- register api for on_script
function interpreter:api_register_on_script(scope_kind, ...)
@@ -931,6 +976,18 @@ function interpreter:api_register_on_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then
@@ -954,6 +1011,18 @@ function interpreter:api_register_before_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then
@@ -977,6 +1046,18 @@ function interpreter:api_register_after_script(scope_kind, ...)
-- define implementation
local implementation = function (self, scope, name, script)
+ -- this script is module name? import it first
+ if type(script) == "string" then
+
+ -- import module as script
+ local modulename = script
+ script = function (...)
+
+ -- import it
+ return import(modulename).main(...)
+ end
+ end
+
-- make sandbox instance with the given script
local instance, errors = sandbox.new(script, self:filter(), self:scriptdir())
if not instance then