summaryrefslogtreecommitdiff
path: root/xmake/core/base/pipe.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-06 22:51:24 +0800
committerruki <[email protected]>2022-04-06 22:51:24 +0800
commit313fc50dbd586e43c7516dc12202db02e6b94945 (patch)
tree446a2ce9bab6d148974b5fa2624c57722b9936ed /xmake/core/base/pipe.lua
parent3d378500c304db2776ff2969491084ce4d320e58 (diff)
improve pipe write
Diffstat (limited to 'xmake/core/base/pipe.lua')
-rw-r--r--xmake/core/base/pipe.lua23
1 files changed, 12 insertions, 11 deletions
diff --git a/xmake/core/base/pipe.lua b/xmake/core/base/pipe.lua
index c204f4f07..ddf62411d 100644
--- a/xmake/core/base/pipe.lua
+++ b/xmake/core/base/pipe.lua
@@ -67,21 +67,22 @@ function _instance:write(data, opt)
return -1, errors
end
- -- data is bytes? table.unpack the raw address
- local datasize = #data
- if bytes.instance_of(data) then
- datasize = data:size()
- data = {data = data:caddr(), size = data:size()}
+ -- get data address and size for bytes and string
+ if type(data) == "string" then
+ data = bytes(data)
end
+ local datasize = data:size()
+ local dataaddr = data:caddr()
-- init start and last
opt = opt or {}
local start = opt.start or 1
local last = opt.last or datasize
-
- -- check start and last
- if start > last or start < 1 then
- return -1, string.format("%s: invalid start(%d) and last(%d)!", self, start, last)
+ if start < 1 or start > datasize then
+ return -1, string.format("%s: invalid start(%d)!", self, start)
+ end
+ if last < start - 1 or last > datasize + start - 1 then
+ return -1, string.format("%s: invalid last(%d)!", self, last)
end
-- write it
@@ -91,7 +92,7 @@ function _instance:write(data, opt)
if opt.block then
local size = last + 1 - start
while start <= last do
- real, errors = io.pipe_write(self:cdata(), data, start, last)
+ real, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
if real > 0 then
write = write + real
start = start + real
@@ -109,7 +110,7 @@ function _instance:write(data, opt)
write = -1
end
else
- write, errors = io.pipe_write(self:cdata(), data, start, last)
+ write, errors = io.pipe_write(self:cdata(), dataaddr + start - 1, last + 1 - start)
if write < 0 and errors then
errors = string.format("%s: %s", self, errors)
end