summaryrefslogtreecommitdiff
path: root/xmake/core/base/interpreter.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2017-11-17 22:50:06 +0800
committerruki <[email protected]>2017-11-17 11:00:38 +0800
commit3d8fdc9ba554494223b85df4a8bc367478ec5a1d (patch)
tree70406de8e2d607fc9a9b454625042becf642b2f9 /xmake/core/base/interpreter.lua
parentb627fef0a708077fcd41808aa61e1807eed66b0e (diff)
add os.scriptdir() in interpreter
Diffstat (limited to 'xmake/core/base/interpreter.lua')
-rw-r--r--xmake/core/base/interpreter.lua64
1 files changed, 63 insertions, 1 deletions
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index 8f2877873..e796eaa8a 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -567,7 +567,12 @@ function interpreter.new()
table.inherit2(instance, interpreter)
-- dispatch the api calling for scope
- setmetatable(instance._PUBLIC, { __index = function (tbl, key)
+ setmetatable(instance._PUBLIC, { __index = function (tbl, key)
+
+ -- get interpreter instance
+ if type(key) == "string" and key == "_INTERPRETER" and rawget(tbl, "_INTERPRETER_READABLE") then
+ return instance
+ end
-- get the scope kind
local priv = instance._PRIVATE
@@ -584,6 +589,12 @@ function interpreter.new()
-- ok?
return apifunc
+ end
+ , __newindex = function (tbl, key, val)
+ if type(key) == "string" and (key == "_INTERPRETER" or key == "_INTERPRETER_READABLE") then
+ return
+ end
+ rawset(tbl, key, val)
end})
-- register the builtin interfaces
@@ -1568,7 +1579,58 @@ function interpreter:scope_restore(scope)
-- restore it
scopes._CURRENT = scope._CURRENT
scopes._CURRENT_KIND = scope._CURRENT_KIND
+end
+
+-- get current instance in the interpreter modules
+function interpreter.instance(script)
+
+ -- get the sandbox instance from the given script
+ local instance = nil
+ if script then
+ local scope = getfenv(script)
+ if scope then
+
+ -- enable to read _INTERPRETER
+ rawset(scope, "_INTERPRETER_READABLE", true)
+
+ -- attempt to get it
+ instance = scope._INTERPRETER
+
+ -- disable to read _INTERPRETER
+ rawset(scope, "_INTERPRETER_READABLE", nil)
+ end
+ if instance then return instance end
+ end
+
+ -- find self instance for the current sandbox
+ local level = 2
+ while level < 32 do
+
+ -- get scope
+ local scope = getfenv(level)
+ if scope then
+
+ -- enable to read _INTERPRETER
+ rawset(scope, "_INTERPRETER_READABLE", true)
+
+ -- attempt to get it
+ instance = scope._INTERPRETER
+
+ -- disable to read _INTERPRETER
+ rawset(scope, "_INTERPRETER_READABLE", nil)
+ end
+
+ -- found?
+ if instance then
+ break
+ end
+
+ -- next
+ level = level + 1
+ end
+ -- ok?
+ return instance
end
-- return module: interpreter