From f5004017fc89f18104746abe9720866078373c0e Mon Sep 17 00:00:00 2001 From: hanjingo Date: Sat, 25 Dec 2021 21:55:12 +0800 Subject: [PATCH 1/3] bug fix example: test.lua ```lua addressbook = { name = "Alice", id = 12345, phone = { { number = "1301234567" }, { number = "87654321", type = "WORK" }, } } ``` root@ef956f202572:/usr/src/pbc/binding/lua53# lua test.lua test/addressbook.proto tutorial Profile nick_name [1] LABEL_OPTIONAL icon [2] LABEL_OPTIONAL Person name [1] LABEL_REQUIRED id [2] LABEL_REQUIRED email [3] LABEL_OPTIONAL phone [4] LABEL_REPEATED test [5] LABEL_REPEATED profile [6] LABEL_OPTIONAL Ext AddressBook person [1] LABEL_REPEATED Alice 12345 1301234567 HOME 87654321 WORK Alice 123 table: 0xaaaaf4c2eed0 ---------------------split line----------------------------- modify test.lua with: ```lua addressbook = { name = "Alice", id = 12345, phone = { { number = "1301234567" }, { number = "87654321", type = "WORK" }, }, c = 3, -- <-i'm here } ``` root@ef956f202572:/usr/src/pbc/binding/lua53# lua test.lua test/addressbook.proto tutorial Profile nick_name [1] LABEL_OPTIONAL icon [2] LABEL_OPTIONAL Person name [1] LABEL_REQUIRED id [2] LABEL_REQUIRED email [3] LABEL_OPTIONAL phone [4] LABEL_REPEATED test [5] LABEL_REPEATED profile [6] LABEL_OPTIONAL Ext AddressBook person [1] LABEL_REPEATED lua: ./protobuf.lua:308: c stack traceback: [C]: in function 'assert' ./protobuf.lua:308: in metamethod '__index' ./protobuf.lua:217: in upvalue 'encode_message' ./protobuf.lua:324: in function 'protobuf.encode' test.lua:35: in main chunk [C]: in ? --- binding/lua53/protobuf.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/binding/lua53/protobuf.lua b/binding/lua53/protobuf.lua index 40ca42b..b53b0d5 100644 --- a/binding/lua53/protobuf.lua +++ b/binding/lua53/protobuf.lua @@ -215,7 +215,9 @@ local function encode_message(CObj, message_type, t) local type = encode_type_cache[message_type] for k,v in pairs(t) do local func = type[k] - func(CObj, k , v) + if func then + func(CObj, k , v) + end end end @@ -305,8 +307,13 @@ local _encode_type_meta = {} function _encode_type_meta:__index(key) local t, msg = c._env_type(P, self._CType, key) - local func = assert(_writer[t],key)(msg) - self[key] = func + -- local func = assert(_writer[t],key)(msg) + -- self[key] = func + local func = _writer[t] + if func then + func = func(msg) + self[key] = func + end return func end From ce26438bdffddb5f56a21a6e663cc59034b6dbe6 Mon Sep 17 00:00:00 2001 From: hanjingo Date: Sat, 25 Dec 2021 22:05:46 +0800 Subject: [PATCH 2/3] remove annotation --- binding/lua53/protobuf.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/binding/lua53/protobuf.lua b/binding/lua53/protobuf.lua index b53b0d5..293df3e 100644 --- a/binding/lua53/protobuf.lua +++ b/binding/lua53/protobuf.lua @@ -307,8 +307,6 @@ local _encode_type_meta = {} function _encode_type_meta:__index(key) local t, msg = c._env_type(P, self._CType, key) - -- local func = assert(_writer[t],key)(msg) - -- self[key] = func local func = _writer[t] if func then func = func(msg) From c4e7b73cad166f0789c57d64e27c29545abb2e65 Mon Sep 17 00:00:00 2001 From: hanjingo Date: Fri, 31 Dec 2021 23:31:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pbc.h | 2 +- src/alloc.c | 6 +++--- src/wmessage.c | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pbc.h b/pbc.h index 26312af..2f36c35 100644 --- a/pbc.h +++ b/pbc.h @@ -71,7 +71,7 @@ const char * pbc_rmessage_string(struct pbc_rmessage * , const char *key , int i struct pbc_rmessage * pbc_rmessage_message(struct pbc_rmessage *, const char *key, int index); int pbc_rmessage_size(struct pbc_rmessage *, const char *key); int pbc_rmessage_next(struct pbc_rmessage *, const char **key); - +// 新建pbc_wmessage struct pbc_wmessage * pbc_wmessage_new(struct pbc_env * env, const char *type_name); void pbc_wmessage_delete(struct pbc_wmessage *); diff --git a/src/alloc.c b/src/alloc.c index 96590ed..8b5448d 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -2,7 +2,7 @@ #include static int _g = 0; - +// 分配内存,并记录到_g void * _pbcM_malloc(size_t sz) { ++ _g; return malloc(sz); @@ -60,11 +60,11 @@ _pbcH_delete(struct heap *h) { } _pbcM_free(h); } - +// 创建一个堆;h:全局堆, size:堆大小 void* _pbcH_alloc(struct heap *h, int size) { size = (size + 3) & ~3; - if (h->size - h->used < size) { + if (h->size - h->used < size) { // 内存不够 struct heap_page * p; if (size < h->size) { p = (struct heap_page *)_pbcM_malloc(sizeof(struct heap_page) + h->size); diff --git a/src/wmessage.c b/src/wmessage.c index 8c10165..ca1d758 100644 --- a/src/wmessage.c +++ b/src/wmessage.c @@ -14,15 +14,15 @@ #endif #define WMESSAGE_SIZE 64 - +// pbc编码结构体 struct pbc_wmessage { - struct _message *type; - uint8_t * buffer; - uint8_t * ptr; - uint8_t * endptr; - pbc_array sub; - struct map_sp *packed; - struct heap * heap; + struct _message *type; // + uint8_t * buffer; // + uint8_t * ptr; // + uint8_t * endptr; // + pbc_array sub; // + struct map_sp *packed; // + struct heap * heap; // }; struct _packed { @@ -30,7 +30,7 @@ struct _packed { int ptype; pbc_array data; }; - +// 新建pbc_wmessage static struct pbc_wmessage * _wmessage_new(struct heap *h, struct _message *msg) { struct pbc_wmessage * m = (struct pbc_wmessage *)_pbcH_alloc(h, sizeof(*m));