Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Allow to pass a C buffer to msgpack.decode(). Syntax: buf = buffer.ibuf() ... obj, rpos = 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. - Allow to store encoded objects in a user-supplied ibuf. Syntax: buf = buffer.ibuf() len = msgpack.encode(obj, buf) ('len' is the number of bytes stored in the buffer) - Add tests. Closes #2755
- Loading branch information
Showing
3 changed files
with
369 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
buffer = require 'buffer' | ||
--- | ||
... | ||
msgpack = require 'msgpack' | ||
--- | ||
... | ||
-- Arguments check. | ||
buf = buffer.ibuf() | ||
--- | ||
... | ||
msgpack.encode() | ||
--- | ||
- error: 'msgpack.encode: a Lua object expected' | ||
... | ||
msgpack.encode('test', 'str') | ||
--- | ||
- error: expected cdata as 2 argument | ||
... | ||
msgpack.encode('test', buf.buf) | ||
--- | ||
- error: 'msgpack.encode: argument 2 must be of type ''struct ibuf''' | ||
... | ||
msgpack.decode() | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode(123) | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode(buf) | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode(buf.buf, 'size') | ||
--- | ||
- error: 'bad argument #2 to ''?'' (number expected, got string)' | ||
... | ||
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('test', 'offset') | ||
--- | ||
- error: 'bad argument #2 to ''?'' (number expected, got string)' | ||
... | ||
msgpack.decode_unchecked() | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode_unchecked(123) | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode_unchecked(buf) | ||
--- | ||
- error: 'msgpack.decode: a Lua string or ''char *'' expected' | ||
... | ||
msgpack.decode_unchecked('test', 0) | ||
--- | ||
- error: 'msgpack.decode: offset is out of bounds' | ||
... | ||
msgpack.decode_unchecked('test', 5) | ||
--- | ||
- error: 'msgpack.decode: offset is out of bounds' | ||
... | ||
msgpack.decode_unchecked('test', 'offset') | ||
--- | ||
- error: 'bad argument #2 to ''?'' (number expected, got string)' | ||
... | ||
-- 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.encode({1, 2, 3}, buf) | ||
--- | ||
... | ||
len = msgpack.encode({4, 5, 6}, buf) + 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) | ||
--- | ||
- error: 'msgpack.decode: invalid MsgPack' | ||
... | ||
buf = buffer.ibuf() | ||
--- | ||
... | ||
msgpack.encode({1, 2, 3}, buf) | ||
--- | ||
- 4 | ||
... | ||
msgpack.decode(buf.rpos, buf:size() - 1) | ||
--- | ||
- error: 'msgpack.decode: invalid MsgPack' | ||
... |
Oops, something went wrong.