summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-08-20 00:08:52 +0800
committerruki <[email protected]>2019-08-19 18:53:17 +0800
commit5dceabc08c22eb01c78dabab6d5a911d807ee850 (patch)
tree9fe020abc692dac632b86ee241cd340904facf85
parent45f213599deff032a2c64557bd2fadecdab6c272 (diff)
lazy loading stdfile
-rw-r--r--xmake/core/base/io.lua18
-rw-r--r--xmake/core/sandbox/modules/io.lua15
2 files changed, 25 insertions, 8 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index ac405dbac..883fdd19f 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -619,10 +619,20 @@ function io.tail(filepath, linecount, opt)
end
end
--- init stdfile
-io.stdin = io.stdfile("/dev/stdin")
-io.stdout = io.stdfile("/dev/stdout")
-io.stderr = io.stdfile("/dev/stderr")
+-- lazy loading stdfile
+io.stdin = nil
+io.stdout = nil
+io.stderr = nil
+setmetatable(io, { __index = function (tbl, key)
+ local val = rawget(tbl, key)
+ if val == nil and (key == "stdin" or key == "stdout" or key == "stderr") then
+ val = io.stdfile("/dev/" .. key)
+ if val ~= nil then
+ rawset(tbl, key, val)
+ end
+ end
+ return val
+ end})
-- return module
return io
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 8082a5299..525dd6af4 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -390,10 +390,17 @@ function sandbox_io.tail(filepath, linecount, opt)
io.tail(filepath, linecount, opt)
end
--- init stdfile
-sandbox_io.stdin = sandbox_io.stdin or sandbox_io.stdfile("/dev/stdin")
-sandbox_io.stdout = sandbox_io.stdout or sandbox_io.stdfile("/dev/stdout")
-sandbox_io.stderr = sandbox_io.stderr or sandbox_io.stdfile("/dev/stderr")
+-- lazy loading stdfile
+setmetatable(sandbox_io, { __index = function (tbl, key)
+ local val = rawget(tbl, key)
+ if val == nil and (key == "stdin" or key == "stdout" or key == "stderr") then
+ val = sandbox_io.stdfile("/dev/" .. key)
+ if val ~= nil then
+ rawset(tbl, key, val)
+ end
+ end
+ return val
+ end})
-- return module
return sandbox_io