Skip to content

Commit 143dfda

Browse files
committed
add Lua 5.5 support
- fix "out of memory" issue. - add `lua_pushexternalstring` support.
1 parent ee4beb3 commit 143dfda

3 files changed

Lines changed: 46 additions & 46 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
ROCKSPEC: rockspecs/lua-protobuf-scm-1.rockspec
2323
strategy:
2424
matrix:
25-
luaVersion: ["5.1", "5.2", "5.3", "5.4", "luajit-openresty"]
25+
luaVersion: ["5.1", "5.2", "5.3", "5.4.8", "5.5.0", "luajit-openresty"]
2626
os: ["ubuntu-latest"]
2727
steps:
2828
- name: Install LCov

pb.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ PB_NS_BEGIN
3131
#define check_slice(L,idx) ((pb_Slice*)luaL_checkudata(L,idx,PB_SLICE))
3232
#define test_slice(L,idx) ((pb_Slice*)luaL_testudata(L,idx,PB_SLICE))
3333
#define push_slice(L,s) lua_pushlstring((L), (s).p, pb_len((s)))
34-
#define lpb_returnself(L) { return lua_settop(L, 1), 1; }
3534

3635
static int lpb_relindex(int idx, int offset) {
3736
return idx < 0 && idx > LUA_REGISTRYINDEX ? idx - offset : idx;
@@ -523,8 +522,7 @@ static int lpbF_read(lua_State *L) {
523522
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, fp);
524523
luaL_addsize(&b, nr);
525524
} while (nr == LUAL_BUFFERSIZE);
526-
luaL_pushresult(&b); /* close buffer */
527-
return 1;
525+
return luaL_pushresult(&b), 1; /* close buffer */
528526
}
529527

530528
static int lpbF_write(lua_State *L, FILE *f, int idx) {
@@ -584,8 +582,7 @@ LUALIB_API int luaopen_pb_io(lua_State *L) {
584582
#undef ENTRY
585583
{ NULL, NULL }
586584
};
587-
luaL_newlib(L, libs);
588-
return 1;
585+
return luaL_newlib(L, libs), 1;
589586
}
590587

591588
/* protobuf integer conversion */
@@ -739,8 +736,7 @@ static int Lpb_tohex(lua_State *L) {
739736
if (r[0] == r[1]) hex[2] = '\0';
740737
luaL_addstring(&lb, hex);
741738
}
742-
luaL_pushresult(&lb);
743-
return 1;
739+
return luaL_pushresult(&lb), 1;
744740
}
745741

746742
static int Lpb_fromhex(lua_State *L) {
@@ -766,8 +762,7 @@ static int Lpb_fromhex(lua_State *L) {
766762
curr = curr<<4 | num;
767763
if (++idx % 2 == 0) luaL_addchar(&lb, curr), curr = 0;
768764
}
769-
luaL_pushresult(&lb);
770-
return 1;
765+
return luaL_pushresult(&lb), 1;
771766
}
772767

773768
static int Lpb_result(lua_State *L) {
@@ -805,8 +800,7 @@ static int Lbuf_libcall(lua_State *L) {
805800

806801
static int Lbuf_tostring(lua_State *L) {
807802
pb_Buffer *buf = check_buffer(L, 1);
808-
lua_pushfstring(L, "pb.Buffer: %p", buf);
809-
return 1;
803+
return (void)lua_pushfstring(L, "pb.Buffer: %p", buf), 1;
810804
}
811805

812806
static int Lbuf_reset(lua_State *L) {
@@ -815,7 +809,7 @@ static int Lbuf_reset(lua_State *L) {
815809
pb_bufflen(buf) = 0;
816810
for (i = 2; i <= top; ++i)
817811
lpb_checkmem(L, pb_addslice(buf, lpb_checkslice(L, i)));
818-
lpb_returnself(L);
812+
return lua_settop(L, 1), 1;
819813
}
820814

821815
static int Lbuf_len(lua_State *L) {
@@ -1042,14 +1036,13 @@ static int Lslice_reset(lua_State *L) {
10421036
lpb_resetslice(L, s, size);
10431037
if (!lua_isnoneornil(L, 2))
10441038
lpb_initslice(L, 2, s, size);
1045-
lpb_returnself(L);
1039+
return lua_settop(L, 1), 1;
10461040
}
10471041

10481042
static int Lslice_tostring(lua_State *L) {
10491043
pb_Slice *s = check_slice(L, 1);
1050-
lua_pushfstring(L, "pb.Slice: %p%s", s,
1051-
lua_rawlen(L, 1) == sizeof(lpb_Slice) ? "" : " (raw)");
1052-
return 1;
1044+
return (void)lua_pushfstring(L, "pb.Slice: %p%s", s,
1045+
lua_rawlen(L, 1) == sizeof(lpb_Slice) ? "" : " (raw)"), 1;
10531046
}
10541047

10551048
static int Lslice_len(lua_State *L) {
@@ -1082,8 +1075,7 @@ static int Lslice_level(lua_State *L) {
10821075
lua_pushinteger(L, (lua_Integer)(se->end - s->buff[0].start));
10831076
return 3;
10841077
}
1085-
lua_pushinteger(L, s->used);
1086-
return 1;
1078+
return lua_pushinteger(L, s->used), 1;
10871079
}
10881080

10891081
static int Lslice_enter(lua_State *L) {
@@ -1102,7 +1094,7 @@ static int Lslice_enter(lua_State *L) {
11021094
view.start = s->curr.p;
11031095
lpb_enterview(L, s, view);
11041096
}
1105-
lpb_returnself(L);
1097+
return lua_settop(L, 1), 1;
11061098
}
11071099

11081100
static int Lslice_leave(lua_State *L) {
@@ -1593,9 +1585,8 @@ static void lpb_useenchooks(lpb_Env *e, int idx, const pb_Type *t) {
15931585
static uint64_t lpbE_readenum(lpb_Env *e, int idx, const pb_Field *f) {
15941586
lua_State *L = e->L;
15951587
int type = lua_type(L, idx);
1596-
if (type == LUA_TNUMBER)
1597-
return (uint64_t)lua_tonumber(L, idx);
1598-
else if (type == LUA_TSTRING) {
1588+
if (type == LUA_TNUMBER) return (uint64_t)lua_tonumber(L, idx);
1589+
if (type == LUA_TSTRING) {
15991590
size_t len;
16001591
const char *s = lua_tolstring(L, idx, &len);
16011592
const pb_Field *ev = pb_fname(f->type,
@@ -1607,9 +1598,9 @@ static uint64_t lpbE_readenum(lpb_Env *e, int idx, const pb_Field *f) {
16071598
"can not encode unknown enum '%s' at field '%s'",
16081599
lua_tostring(L, -1), (const char*)f->name);
16091600
return v;
1610-
} else
1611-
return argcheck(L, 0, 2, "number/string expected at enum field '%s', got %s",
1612-
(const char*)f->name, luaL_typename(L, idx));
1601+
}
1602+
return argcheck(L, 0, 2, "number/string expected at enum field '%s', got %s",
1603+
(const char*)f->name, luaL_typename(L, idx));
16131604
}
16141605

16151606
static void lpbE_field(lpb_Env *e, int idx, const pb_Field *f, lpbE_Mode m) {
@@ -1725,21 +1716,36 @@ static void lpbE_encode(lpb_Env *e, int idx, const pb_Type *t) {
17251716
}
17261717
}
17271718

1719+
#if LUA_VERSION_NUM < 505
1720+
static void lpb_pushbuffer(lua_State *L, pb_Buffer *B)
1721+
{ lua_pushlstring(L, pb_buffer(B), pb_bufflen(B)); }
1722+
#else
1723+
static void *lpb_freebuf(void *ud, void *ptr, size_t osize, size_t nsize) {
1724+
(void)ud, (void)osize, (void)nsize;
1725+
assert(nsize == 0);
1726+
return free(ptr), NULL;
1727+
}
1728+
1729+
static void lpb_pushbuffer(lua_State *L, pb_Buffer *B) {
1730+
size_t len = pb_bufflen(B);
1731+
char *s = (*pb_prepbuffsize(B, 1) = 0, pb_buffer(B));
1732+
B->buff = NULL, B->size = 0;
1733+
lua_pushexternalstring(L, s, len, lpb_freebuf, NULL);
1734+
}
1735+
#endif
1736+
17281737
static int Lpb_encode(lua_State *L) {
17291738
lpb_State *LS = lpb_lstate(L);
17301739
const pb_Type *t = lpb_type(L, LS, lpb_checkslice(L, 1));
17311740
lpb_Env e;
17321741
argcheck(L, t!=NULL, 1, "type '%s' does not exists", lua_tostring(L, 1));
17331742
luaL_checktype(L, 2, LUA_TTABLE);
17341743
e.L = L, e.LS = LS, e.b = test_buffer(L, 3);
1735-
if (e.b == NULL) e.b = &LS->buffer, pb_bufflen(e.b) = 0;
1744+
if (e.b == NULL) e.b = &LS->buffer, pb_resetbuffer(e.b);
17361745
if (e.LS->use_enc_hooks) lpb_useenchooks(&e, 2, t);
17371746
lpbE_encode(&e, 2, t);
1738-
if (e.b != &LS->buffer)
1739-
lua_settop(L, 3);
1740-
else
1741-
lua_pushlstring(L, pb_buffer(e.b), pb_bufflen(e.b));
1742-
return 1;
1747+
if (e.b != &LS->buffer) return lua_settop(L, 3), 1;
1748+
return lpb_pushbuffer(L, &LS->buffer), 1;
17431749
}
17441750

17451751
static int lpbE_pack(lpb_Env* e, int idx, const pb_Type* t) {
@@ -1763,14 +1769,10 @@ static int Lpb_pack(lua_State* L) {
17631769
int idx = 3;
17641770
argcheck(L, t!=NULL, 1, "type '%s' does not exists", lua_tostring(L, 1));
17651771
e.L = L, e.LS = LS, e.b = test_buffer(L, 2);
1766-
if (e.b == NULL)
1767-
idx = 2, e.b = &LS->buffer, pb_bufflen(e.b) = 0;
1772+
if (e.b == NULL) idx = 2, e.b = &LS->buffer, pb_resetbuffer(e.b);
17681773
lpbE_pack(&e, idx, t);
1769-
if (e.b != &LS->buffer)
1770-
lua_settop(L, 3);
1771-
else
1772-
lua_pushlstring(L, pb_buffer(e.b), pb_bufflen(e.b));
1773-
return 1;
1774+
if (e.b != &LS->buffer) return lua_settop(L, 3), 1;
1775+
return lpb_pushbuffer(L, &LS->buffer), 1;
17741776
}
17751777

17761778
/* protobuf decode */
@@ -2093,8 +2095,8 @@ LUALIB_API int luaopen_pb(lua_State *L) {
20932095
lua_pushvalue(L, -1);
20942096
lua_setfield(L, -2, "__index");
20952097
}
2096-
luaL_newlib(L, libs);
2097-
return 1;
2098+
lua_pop(L, 1);
2099+
return luaL_newlib(L, libs), 1;
20982100
}
20992101

21002102
static int Lpb_decode_unsafe(lua_State *L) {
@@ -2126,8 +2128,7 @@ static int Lpb_use(lua_State *L) {
21262128
case 0: if (GS) LS->state = GS; break;
21272129
case 1: LS->state = &LS->local; break;
21282130
}
2129-
lua_pushboolean(L, GS != NULL);
2130-
return 1;
2131+
return lua_pushboolean(L, GS != NULL), 1;
21312132
}
21322133

21332134
LUALIB_API int luaopen_pb_unsafe(lua_State *L) {
@@ -2139,8 +2140,7 @@ LUALIB_API int luaopen_pb_unsafe(lua_State *L) {
21392140
{ "use", Lpb_use },
21402141
{ NULL, NULL }
21412142
};
2142-
luaL_newlib(L, libs);
2143-
return 1;
2143+
return luaL_newlib(L, libs), 1;
21442144
}
21452145

21462146

test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ function _G.test_buffer()
962962
eq(#b, 6)
963963

964964
fail("integer format error: 'foo'", function() buffer.pack("v", "foo") end)
965-
if _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" then
965+
if _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" or _VERSION == "Lua 5.5" then
966966
fail("integer format error", function() buffer.pack("v", 1e308) end)
967967
else
968968
fail("number has no integer representation", function() buffer.pack("v", 1e308) end)

0 commit comments

Comments
 (0)