summaryrefslogtreecommitdiff
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
parentb627fef0a708077fcd41808aa61e1807eed66b0e (diff)
add os.scriptdir() in interpreter
-rw-r--r--xmake/core/base/interpreter.lua64
-rw-r--r--xmake/core/sandbox/modules/interpreter/os.lua22
2 files changed, 76 insertions, 10 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
diff --git a/xmake/core/sandbox/modules/interpreter/os.lua b/xmake/core/sandbox/modules/interpreter/os.lua
index 68aa3fca7..098de5047 100644
--- a/xmake/core/sandbox/modules/interpreter/os.lua
+++ b/xmake/core/sandbox/modules/interpreter/os.lua
@@ -23,13 +23,16 @@
--
-- load modules
-local os = require("base/os")
-local string = require("base/string")
+local os = require("base/os")
+local string = require("base/string")
+local interpreter = require("base/interpreter")
-- define module
local sandbox_os = sandbox_os or {}
-- export some readonly interfaces
+sandbox_os.host = os.host
+sandbox_os.arch = os.arch
sandbox_os.date = os.date
sandbox_os.time = os.time
sandbox_os.mtime = os.mtime
@@ -61,14 +64,15 @@ function sandbox_os.filedirs(pattern, ...)
return os.filedirs(string.format(pattern, ...))
end
--- get the system host
-function sandbox_os.host()
- return xmake._HOST
-end
+-- get the script directory
+function sandbox_os.scriptdir()
+
+ -- get the current interpreter instance
+ local instance = interpreter.instance()
+ assert(instance)
--- get the system architecture
-function sandbox_os.arch()
- return xmake._ARCH
+ -- get the script directory
+ return instance:scriptdir()
end
-- return module