Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Do not throw an exception from msgpack.decode() if the supplied data cannot be parsed (mp_check() fails). Return {nil, nil, error} instead: obj, offset, err = msgpack.decode(data) if obj == nil then print(err) return end - Allow to pass a C buffer to msgpack.decode(). Syntax: buf = buffer.ibuf() ... obj, rpos, err = msgpack.decode(buf.rpos, buf:size()) - Introduce a version of msgpack.decode() that doesn't check the supplied msgpack - msgpack.decode_unchecked(). It has the same signature as msgpack.decode() except if called on a C buffer it doesn't require the buffer size. It is supposed to supplant msgpack.ibuf_decode() over time. - Introduce msgpack.ibuf_encode(buf, obj). It encodes a Lua object to an input buffer and returns the number of bytes encoded. - Add tests. See #2755
- Loading branch information
Showing
6 changed files
with
345 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- msgpack.lua (internal file) | ||
|
||
local ffi = require('ffi') | ||
local msgpack = require('msgpack') | ||
local internal = msgpack.internal | ||
|
||
local ibuf_t = ffi.typeof('struct ibuf') | ||
|
||
msgpack.ibuf_encode = function(buf, obj) | ||
if not ffi.istype(ibuf_t, buf) then | ||
error("Usage: msgpack.ibuf_encode(ibuf, obj)") | ||
end | ||
return internal.encode(msgpack, buf, obj) | ||
end | ||
|
||
-- Backward compatibility wrapper. | ||
msgpack.ibuf_decode = function(data) | ||
local obj, data_end = msgpack.decode_unchecked(data) | ||
return data_end, obj | ||
end | ||
|
||
return msgpack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
buffer = require 'buffer' | ||
--- | ||
... | ||
msgpack = require 'msgpack' | ||
--- | ||
... | ||
-- Arguments check. | ||
msgpack.decode() | ||
--- | ||
- error: 'msgpack.decode: a Lua string or cdata expected' | ||
... | ||
msgpack.decode(123) | ||
--- | ||
- error: 'msgpack.decode: a Lua string or cdata expected' | ||
... | ||
msgpack.decode('test', 0) | ||
--- | ||
- error: 'msgpack.decode: offset is out of bounds' | ||
... | ||
msgpack.decode('test', 5) | ||
--- | ||
- error: 'msgpack.decode: offset is out of bounds' | ||
... | ||
msgpack.decode_unchecked() | ||
--- | ||
- error: 'msgpack.decode_unchecked: a Lua string or cdata expected' | ||
... | ||
msgpack.decode_unchecked(123) | ||
--- | ||
- error: 'msgpack.decode_unchecked: a Lua string or cdata expected' | ||
... | ||
msgpack.decode_unchecked('test', 0) | ||
--- | ||
- error: 'msgpack.decode_unchecked: offset is out of bounds' | ||
... | ||
msgpack.decode_unchecked('test', 5) | ||
--- | ||
- error: 'msgpack.decode_unchecked: offset is out of bounds' | ||
... | ||
-- Encode/decode a string. | ||
s = msgpack.encode({1, 2, 3}) .. msgpack.encode({4, 5, 6}) | ||
--- | ||
... | ||
obj, offset = msgpack.decode(s) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [1, 2, 3] | ||
... | ||
obj, offset = msgpack.decode(s, offset) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [4, 5, 6] | ||
... | ||
offset == #s + 1 | ||
--- | ||
- true | ||
... | ||
obj, offset = msgpack.decode_unchecked(s) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [1, 2, 3] | ||
... | ||
obj, offset = msgpack.decode_unchecked(s, offset) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [4, 5, 6] | ||
... | ||
offset == #s + 1 | ||
--- | ||
- true | ||
... | ||
-- Encode/decode a buffer. | ||
buf = buffer.ibuf() | ||
--- | ||
... | ||
len = msgpack.ibuf_encode(buf, {1, 2, 3}) | ||
--- | ||
... | ||
len = msgpack.ibuf_encode(buf, {4, 5, 6}) + len | ||
--- | ||
... | ||
buf:size() == len | ||
--- | ||
- true | ||
... | ||
orig_rpos = buf.rpos | ||
--- | ||
... | ||
obj, rpos = msgpack.decode(buf.rpos, buf:size()) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [1, 2, 3] | ||
... | ||
buf.rpos = rpos | ||
--- | ||
... | ||
obj, rpos = msgpack.decode(buf.rpos, buf:size()) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [4, 5, 6] | ||
... | ||
buf.rpos = rpos | ||
--- | ||
... | ||
buf:size() == 0 | ||
--- | ||
- true | ||
... | ||
buf.rpos = orig_rpos | ||
--- | ||
... | ||
obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size()) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [1, 2, 3] | ||
... | ||
buf.rpos = rpos | ||
--- | ||
... | ||
obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size()) | ||
--- | ||
... | ||
obj | ||
--- | ||
- [4, 5, 6] | ||
... | ||
buf.rpos = rpos | ||
--- | ||
... | ||
buf:size() == 0 | ||
--- | ||
- true | ||
... | ||
-- Invalid msgpack. | ||
s = msgpack.encode({1, 2, 3}) | ||
--- | ||
... | ||
s = s:sub(1, -2) | ||
--- | ||
... | ||
msgpack.decode(s) | ||
--- | ||
- null | ||
- null | ||
- 'msgpack.decode: invalid MsgPack' | ||
... | ||
buf = buffer.ibuf() | ||
--- | ||
... | ||
msgpack.ibuf_encode(buf, {1, 2, 3}) | ||
--- | ||
- 4 | ||
... | ||
msgpack.decode(buf.rpos, buf:size() - 1) | ||
--- | ||
- null | ||
- null | ||
- 'msgpack.decode: invalid MsgPack' | ||
... |
Oops, something went wrong.