sproto.default returns array type

This commit is contained in:
Cloud Wu
2017-09-01 11:28:22 +08:00
parent 415459e656
commit 553c30c6d1

View File

@@ -621,17 +621,14 @@ lloadproto(lua_State *L) {
return 1; return 1;
} }
static int static void
encode_default(const struct sproto_arg *args) { push_default(const struct sproto_arg *args, int array) {
lua_State *L = args->ud; lua_State *L = args->ud;
lua_pushstring(L, args->tagname);
if (args->index > 0) {
lua_newtable(L);
lua_rawset(L, -3);
return SPROTO_CB_NOARRAY;
} else {
switch(args->type) { switch(args->type) {
case SPROTO_TINTEGER: case SPROTO_TINTEGER:
if (args->extra)
lua_pushnumber(L, 0.0);
else
lua_pushinteger(L, 0); lua_pushinteger(L, 0);
break; break;
case SPROTO_TBOOLEAN: case SPROTO_TBOOLEAN:
@@ -641,11 +638,32 @@ encode_default(const struct sproto_arg *args) {
lua_pushliteral(L, ""); lua_pushliteral(L, "");
break; break;
case SPROTO_TSTRUCT: case SPROTO_TSTRUCT:
if (array) {
lua_pushstring(L, sproto_name(args->subtype));
} else {
lua_createtable(L, 0, 1); lua_createtable(L, 0, 1);
lua_pushstring(L, sproto_name(args->subtype)); lua_pushstring(L, sproto_name(args->subtype));
lua_setfield(L, -2, "__type"); lua_setfield(L, -2, "__type");
}
break;
default:
luaL_error(L, "Invalid type %d", args->type);
break; break;
} }
}
static int
encode_default(const struct sproto_arg *args) {
lua_State *L = args->ud;
lua_pushstring(L, args->tagname);
if (args->index > 0) {
lua_newtable(L);
push_default(args, 1);
lua_setfield(L, -2, "__array");
lua_rawset(L, -3);
return SPROTO_CB_NOARRAY;
} else {
push_default(args, 0);
lua_rawset(L, -3); lua_rawset(L, -3);
return SPROTO_CB_NIL; return SPROTO_CB_NIL;
} }