diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index cb332027..16d36554 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -55,6 +55,13 @@ static int64_t lua_tointegerx(lua_State *L, int idx, int *isnum) { return 0; } } + +static int lua_absindex (lua_State *L, int idx) { + if (idx > 0 || idx <= LUA_REGISTRYINDEX) + return idx; + return lua_gettop(L) + idx + 1; +} + #endif static void @@ -235,6 +242,11 @@ encode(const struct sproto_arg *args) { return 8; } } + case SPROTO_DOUBLE: { + lua_Number v = lua_tonumber(L, -1); + *(double*)args->value = (double)v; + return 8; + } case SPROTO_TBOOLEAN: { int v = lua_toboolean(L, -1); if (!lua_isboolean(L,-1)) { @@ -395,6 +407,11 @@ decode(const struct sproto_arg *args) { } break; } + case SPROTO_DOUBLE: { + double v = *(double*)args->value; + lua_pushnumber(L, v); + break; + } case SPROTO_TBOOLEAN: { int v = *(uint64_t*)args->value; lua_pushboolean(L,v); @@ -671,6 +688,9 @@ push_default(const struct sproto_arg *args, int array) { else lua_pushinteger(L, 0); break; + case SPROTO_DOUBLE: + lua_pushnumber(L, 0.0); + break; case SPROTO_TBOOLEAN: lua_pushboolean(L, 0); break; diff --git a/lualib-src/sproto/sproto.c b/lualib-src/sproto/sproto.c index 2c36ad9f..4d38d51c 100644 --- a/lualib-src/sproto/sproto.c +++ b/lualib-src/sproto/sproto.c @@ -944,6 +944,7 @@ sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_call args.type = type; args.index = 0; switch(type) { + case SPROTO_DOUBLE: case SPROTO_TINTEGER: case SPROTO_TBOOLEAN: { union { @@ -1177,6 +1178,7 @@ sproto_decode(const struct sproto_type *st, const void * data, int size, sproto_ } } else { switch (f->type) { + case SPROTO_DOUBLE: case SPROTO_TINTEGER: { uint32_t sz = todword(currentdata); if (sz == SIZEOF_INT32) { diff --git a/lualib-src/sproto/sproto.h b/lualib-src/sproto/sproto.h index f70309ec..1fb9252e 100644 --- a/lualib-src/sproto/sproto.h +++ b/lualib-src/sproto/sproto.h @@ -13,7 +13,8 @@ struct sproto_type; #define SPROTO_TINTEGER 0 #define SPROTO_TBOOLEAN 1 #define SPROTO_TSTRING 2 -#define SPROTO_TSTRUCT 3 +#define SPROTO_DOUBLE 3 +#define SPROTO_TSTRUCT 4 // sub type of string (sproto_arg.extra) #define SPROTO_TSTRING_STRING 0 diff --git a/lualib/sprotoparser.lua b/lualib/sprotoparser.lua index 4c9a7d07..afac92f7 100644 --- a/lualib/sprotoparser.lua +++ b/lualib/sprotoparser.lua @@ -97,7 +97,10 @@ local convert = {} function convert.protocol(all, obj) local result = { tag = obj[2] } for _, p in ipairs(obj[3]) do - assert(result[p[1]] == nil) + local pt = p[1] + if result[pt] ~= nil then + error(string.format("redefine %s in protocol %s", pt, obj[1])) + end local typename = p[2] if type(typename) == "table" then local struct = typename @@ -179,6 +182,7 @@ local buildin_types = { boolean = 1, string = 2, binary = 2, -- binary is a sub type of string + double = 3, } local function checktype(types, ptype, t)