From bd55a58515db02ae1112a22604356d5a7f630aa1 Mon Sep 17 00:00:00 2001 From: cxj Date: Wed, 1 Jul 2015 16:54:20 +0800 Subject: [PATCH 1/4] support sharedata serialization --- lualib-src/lua-seri.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 18b90d0d..6e118230 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -260,8 +260,28 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { if (index < 0) { index = lua_gettop(L) + index + 1; } - int array_size = wb_table_array(L, wb, index, depth); - wb_table_hash(L, wb, index, depth, array_size); + if (luaL_getmetafield(L, index, "__pairs")) { + uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); + wb_push(wb, &n, 1); + lua_pushvalue(L, index); + lua_call(L, 1, 3); + while (1) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_copy(L, -5, -3); + lua_call(L, 2, 2); + int type = lua_type(L, -2); + if (type == LUA_TNIL) + break; + pack_one(L, wb, -2, depth); + pack_one(L, wb, -1, depth); + lua_pop(L, 1); + } + wb_nil(wb); + } else { + int array_size = wb_table_array(L, wb, index, depth); + wb_table_hash(L, wb, index, depth, array_size); + } } static void From 172356be190bbf8233d6ee722b0d4621172190a9 Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 13:51:03 +0800 Subject: [PATCH 2/4] fix lua-seric.c wb_table --- lualib-src/lua-seri.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 6e118230..1d12e263 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -260,7 +260,7 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { if (index < 0) { index = lua_gettop(L) + index + 1; } - if (luaL_getmetafield(L, index, "__pairs")) { + if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); wb_push(wb, &n, 1); lua_pushvalue(L, index); From a77d36911dd33f22dc523dac33ba79e5a46ce43d Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 14:47:50 +0800 Subject: [PATCH 3/4] change wb_table to call wb_table_metapairs for keeping consistent style --- lualib-src/lua-seri.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lualib-src/lua-seri.c b/lualib-src/lua-seri.c index 1d12e263..0db2267d 100644 --- a/lualib-src/lua-seri.c +++ b/lualib-src/lua-seri.c @@ -254,6 +254,27 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a wb_nil(wb); } +static void +wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { + uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); + wb_push(wb, &n, 1); + lua_pushvalue(L, index); + lua_call(L, 1, 3); + for(;;) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_copy(L, -5, -3); + lua_call(L, 2, 2); + int type = lua_type(L, -2); + if (type == LUA_TNIL) + break; + pack_one(L, wb, -2, depth); + pack_one(L, wb, -1, depth); + lua_pop(L, 1); + } + wb_nil(wb); +} + static void wb_table(lua_State *L, struct write_block *wb, int index, int depth) { luaL_checkstack(L, LUA_MINSTACK, NULL); @@ -261,23 +282,7 @@ wb_table(lua_State *L, struct write_block *wb, int index, int depth) { index = lua_gettop(L) + index + 1; } if (luaL_getmetafield(L, index, "__pairs") != LUA_TNIL) { - uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); - wb_push(wb, &n, 1); - lua_pushvalue(L, index); - lua_call(L, 1, 3); - while (1) { - lua_pushvalue(L, -2); - lua_pushvalue(L, -2); - lua_copy(L, -5, -3); - lua_call(L, 2, 2); - int type = lua_type(L, -2); - if (type == LUA_TNIL) - break; - pack_one(L, wb, -2, depth); - pack_one(L, wb, -1, depth); - lua_pop(L, 1); - } - wb_nil(wb); + wb_table_metapairs(L, wb, index, depth); } else { int array_size = wb_table_array(L, wb, index, depth); wb_table_hash(L, wb, index, depth, array_size); From d17e6f59fef44510957521842ff8c7ceff4dc4aa Mon Sep 17 00:00:00 2001 From: cxj Date: Thu, 2 Jul 2015 15:02:11 +0800 Subject: [PATCH 4/4] add sharedata lua serialization test --- examples/share.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/share.lua b/examples/share.lua index 21c3bf54..2721d480 100644 --- a/examples/share.lua +++ b/examples/share.lua @@ -37,6 +37,16 @@ skynet.start(function() skynet.error(string.format("b[%d]=%s", k,v)) end + -- test lua serialization + local s = skynet.packstring(obj) + local nobj = skynet.unpack(s) + for k,v in pairs(nobj) do + skynet.error(string.format("nobj[%s]=%s", k,v)) + end + for k,v in ipairs(nobj) do + skynet.error(string.format("nobj.b[%d]=%s", k,v)) + end + for i = 1, 5 do skynet.sleep(100) skynet.error("second " ..i)