diff options
| author | ruki <[email protected]> | 2020-02-03 23:04:09 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-02-07 22:45:56 +0800 |
| commit | 33797f692bfe78c58f82bafbf40f36640c6fdf13 (patch) | |
| tree | ca7a6ee38c67fd7964c1e765d52faa432b145508 | |
| parent | 0320b5a0731fca04011d5149a0c7d956033e9140 (diff) | |
improve bytes
| -rw-r--r-- | tests/modules/process/sched_process_pipe.lua | 20 | ||||
| -rw-r--r-- | xmake/core/base/bytes.lua | 17 |
2 files changed, 24 insertions, 13 deletions
diff --git a/tests/modules/process/sched_process_pipe.lua b/tests/modules/process/sched_process_pipe.lua index d3690d619..deb350372 100644 --- a/tests/modules/process/sched_process_pipe.lua +++ b/tests/modules/process/sched_process_pipe.lua @@ -4,34 +4,32 @@ import("core.base.scheduler") function _session_read_pipe(id, rpipeopt) local results = {} - local pipe = rpipeopt.pipe - print("%s/%d: read ..", pipe, id) + local rpipe = rpipeopt.rpipe + print("%s/%d: read ..", rpipe, id) while not rpipeopt.stop do - local real, data = pipe:read(8192) + local real, data = rpipe:read(8192) if real > 0 then table.insert(results, bytes(data, 1, real)) elseif real == 0 then - if pipe:wait(pipe.EV_READ, -1) < 0 then + if rpipe:wait(pipe.EV_READ, -1) < 0 then break end else break end end - if #results > 0 then - results = bytes(results) - end - print("%s/%d: read ok, size: %d", pipe, id, results:size()) + results = bytes(results) + print("%s/%d: read ok, size: %d", rpipe, id, results:size()) if results:size() > 0 then results:dump() end - pipe:close() + rpipe:close() end function _session(id, program, ...) local rpipe, wpipe = pipe.openpair(10) - local rpipeopt = {pipe = rpipe, stop = false} - scheduler.co_start(_session_read_pipe, i, rpipeopt) + local rpipeopt = {rpipe = rpipe, stop = false} + scheduler.co_start(_session_read_pipe, id, rpipeopt) local proc = process.openv(program, table.pack(...), {stdout = wpipe}) local ok, status = proc:wait(-1) rpipeopt.stop = true diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index d1d6d8e36..6cf9747fe 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -44,6 +44,7 @@ ffi.cdef[[ -- bytes(bytes1, bytes2, bytes3, ...): allocates and concat buffer from list of byte buffers -- bytes(bytes): allocates a buffer from another one (strict replica, sharing memory) -- bytes({bytes1, bytes2, ...}): allocates and concat buffer from a list of byte buffers (table) +-- bytes({})/bytes(): allocate an empty buffer -- function _instance.new(...) local args = {...} @@ -134,7 +135,7 @@ function _instance.new(...) end instance._MANAGED = true instance._READONLY = false - elseif not arg2 and arg1:size() then + elseif not arg2 and arg1.size and arg1:size() > 0 then -- bytes(bytes): allocates a buffer from another one (strict replica, sharing memory) local b = arg1 local start = 1 @@ -147,9 +148,21 @@ function _instance.new(...) instance._REF = b -- keep lua ref for GC instance._MANAGED = false instance._READONLY = b:readonly() + else + -- bytes({}): allocate an empty buffer + instance._SIZE = 0 + instance._CDATA = nil + instance._MANAGED = false + instance._READONLY = true end + elseif arg1 == nil then + -- bytes(): allocate an empty buffer + instance._SIZE = 0 + instance._CDATA = nil + instance._MANAGED = false + instance._READONLY = true end - if instance:cdata() == nil then + if instance:size() == nil then os.raise("invalid arguments for bytes(...)!") end setmetatable(instance, _instance) |
