Skip to content

Commit 87cfa39

Browse files
authored
Merge pull request #1 from cloudflare/marek/serialize-without-interning
serialization: add method to serialize without ffi.string conversion
2 parents 4f09730 + cff13e2 commit 87cfa39

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION:=0.1.3-1
1+
VERSION:=0.1.3-2
22
CXXFLAGS:=-std=gnu++11 -g -Iproto -I/usr/local/include
33
LDFLAGS:=-L/usr/local/lib -lcapnp -lkj -pthread
44
CAPNP_TEST:=../capnp_test

capnp/compile.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,9 @@ end
524524
function comp_serialize(res, name)
525525
insert(res, format([[
526526
527-
serialize = function(data, p8, size)
528-
if not p8 then
527+
-- Serialize and return pointer to char[] and size
528+
serialize_cdata = function(data, p8, size)
529+
if p8 == nil then
529530
size = _M.%s.calc_size(data)
530531
531532
p8 = get_str_buf(size)
@@ -543,9 +544,14 @@ function comp_serialize(res, name)
543544
-- skip header & struct pointer
544545
_M.%s.flat_serialize(data, p32 + 4)
545546
547+
return p8, size
548+
end,
549+
550+
serialize = function(data, p8, size)
551+
p8, size = _M.%s.serialize_cdata(data, p8, size)
546552
return ffi_string(p8, size)
547553
end,
548-
]], name, name, name))
554+
]], name, name, name, name))
549555
end
550556

551557
function comp_flat_serialize(res, nodes, struct, fields, size, name)

lua-capnproto.rockspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package = "lua-capnproto"
2-
version = "0.1.3-1"
2+
version = "0.1.3-2"
33
source = {
44
url = "git://github.com/cloudflare/lua-capnproto",
5-
tag = "v0.1.3-1",
5+
tag = "v0.1.3-2",
66
}
77
description = {
88
summary = "Lua-capnproto is a pure lua implementation of capnproto based on LuaJIT.",

tests/10-encode-decode.lua

+12
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,16 @@ function test_list_uint16_size()
741741
assert_equal(16 + 8 * 6 + 72, hw_capnp.T3.calc_size(data))
742742
end
743743

744+
function test_serialize_cdata()
745+
local data = {
746+
i0 = 32,
747+
}
748+
749+
assert_equal(128, hw_capnp.T1.calc_size(data))
750+
local bin = hw_capnp.T1.serialize(data)
751+
local arr, len = hw_capnp.T1.serialize_cdata(data)
752+
assert_equal(#bin, len)
753+
assert_equal(bin, ffi.string(arr, len))
754+
end
755+
744756
return _G

0 commit comments

Comments
 (0)