diff options
| author | ruki <[email protected]> | 2022-05-11 00:16:36 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-11 00:16:36 +0800 |
| commit | b03e02af298b69a9f22a664b0607cfb524a430c4 (patch) | |
| tree | be52863dd114237128bb7fb9034ac34fcda51b4e | |
| parent | 24c144ab6be8c6a86cdc555b371b86ea50e2322a (diff) | |
fix open
| -rw-r--r-- | core/src/xmake/lz4/compress_stream_open.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/lz4/decompress_stream_open.c | 2 | ||||
| -rw-r--r-- | xmake/core/compress/lz4.lua | 10 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/compress/lz4.lua | 36 |
4 files changed, 46 insertions, 4 deletions
diff --git a/core/src/xmake/lz4/compress_stream_open.c b/core/src/xmake/lz4/compress_stream_open.c index f578bf5fc..44d45aeba 100644 --- a/core/src/xmake/lz4/compress_stream_open.c +++ b/core/src/xmake/lz4/compress_stream_open.c @@ -40,7 +40,7 @@ tb_int_t xm_lz4_compress_stream_open(lua_State* lua) xm_lz4_cstream_t* stream = xm_lz4_cstream_init(); if (stream) xm_lua_pushpointer(lua, (tb_pointer_t)stream); - lua_pushnil(lua); + else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/lz4/decompress_stream_open.c b/core/src/xmake/lz4/decompress_stream_open.c index a1b930d61..ef997511c 100644 --- a/core/src/xmake/lz4/decompress_stream_open.c +++ b/core/src/xmake/lz4/decompress_stream_open.c @@ -40,7 +40,7 @@ tb_int_t xm_lz4_decompress_stream_open(lua_State* lua) xm_lz4_dstream_t* stream = xm_lz4_dstream_init(); if (stream) xm_lua_pushpointer(lua, (tb_pointer_t)stream); - lua_pushnil(lua); + else lua_pushnil(lua); return 1; } diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua index a6af2a77e..88b854760 100644 --- a/xmake/core/compress/lz4.lua +++ b/xmake/core/compress/lz4.lua @@ -54,6 +54,11 @@ function _cstream:cdata() return self._HANDLE end +-- tostring(stream) +function _cstream:__tostring() + return string.format("<lz4/cstream: %s>", self:cdata()) +end + -- gc(stream) function _cstream:__gc() if self:cdata() and lz4._compress_stream_close(self:cdata()) then @@ -74,6 +79,11 @@ function _dstream:cdata() return self._HANDLE end +-- tostring(stream) +function _dstream:__tostring() + return string.format("<lz4/dstream: %s>", self:cdata()) +end + -- gc(stream) function _dstream:__gc() if self:cdata() and lz4._decompress_stream_close(self:cdata()) then diff --git a/xmake/core/sandbox/modules/import/core/compress/lz4.lua b/xmake/core/sandbox/modules/import/core/compress/lz4.lua index c22ae43e1..f450609f8 100644 --- a/xmake/core/sandbox/modules/import/core/compress/lz4.lua +++ b/xmake/core/sandbox/modules/import/core/compress/lz4.lua @@ -20,11 +20,43 @@ -- define module local sandbox_core_compress_lz4 = sandbox_core_compress_lz4 or {} +local sandbox_core_compress_lz4_cstream = sandbox_core_compress_lz4_cstream or {} +local sandbox_core_compress_lz4_dstream = sandbox_core_compress_lz4_dstream or {} -- load modules local lz4 = require("compress/lz4") local raise = require("sandbox/modules/raise") +-- wrap compress stream +function _cstream_wrap(instance) + local hooked = {} + for name, func in pairs(sandbox_core_compress_lz4_cstream) do + if not name:startswith("_") and type(func) == "function" then + hooked["_" .. name] = instance["_" .. name] or instance[name] + hooked[name] = func + end + end + for name, func in pairs(hooked) do + instance[name] = func + end + return instance +end + +-- wrap decompress stream +function _dstream_wrap(instance) + local hooked = {} + for name, func in pairs(sandbox_core_compress_lz4_dstream) do + if not name:startswith("_") and type(func) == "function" then + hooked["_" .. name] = instance["_" .. name] or instance[name] + hooked[name] = func + end + end + for name, func in pairs(hooked) do + instance[name] = func + end + return instance +end + -- compress frame data function sandbox_core_compress_lz4.compress(data, opt) local result, errors = lz4.compress(data, opt) @@ -83,7 +115,7 @@ function sandbox_core_compress_lz4.compress_stream(opt) if not result and errors then raise(errors) end - return result + return _cstream_wrap(result) end -- new a decompress stream @@ -92,7 +124,7 @@ function sandbox_core_compress_lz4.decompress_stream(opt) if not result and errors then raise(errors) end - return result + return _dstream_wrap(result) end -- return module |
