Skip to content

Commit 553c30c

Browse files
committed
sproto.default returns array type
1 parent 415459e commit 553c30c

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

lualib-src/sproto/lsproto.c

+34-16
Original file line numberDiff line numberDiff line change
@@ -621,31 +621,49 @@ lloadproto(lua_State *L) {
621621
return 1;
622622
}
623623

624+
static void
625+
push_default(const struct sproto_arg *args, int array) {
626+
lua_State *L = args->ud;
627+
switch(args->type) {
628+
case SPROTO_TINTEGER:
629+
if (args->extra)
630+
lua_pushnumber(L, 0.0);
631+
else
632+
lua_pushinteger(L, 0);
633+
break;
634+
case SPROTO_TBOOLEAN:
635+
lua_pushboolean(L, 0);
636+
break;
637+
case SPROTO_TSTRING:
638+
lua_pushliteral(L, "");
639+
break;
640+
case SPROTO_TSTRUCT:
641+
if (array) {
642+
lua_pushstring(L, sproto_name(args->subtype));
643+
} else {
644+
lua_createtable(L, 0, 1);
645+
lua_pushstring(L, sproto_name(args->subtype));
646+
lua_setfield(L, -2, "__type");
647+
}
648+
break;
649+
default:
650+
luaL_error(L, "Invalid type %d", args->type);
651+
break;
652+
}
653+
}
654+
624655
static int
625656
encode_default(const struct sproto_arg *args) {
626657
lua_State *L = args->ud;
627658
lua_pushstring(L, args->tagname);
628659
if (args->index > 0) {
629660
lua_newtable(L);
661+
push_default(args, 1);
662+
lua_setfield(L, -2, "__array");
630663
lua_rawset(L, -3);
631664
return SPROTO_CB_NOARRAY;
632665
} else {
633-
switch(args->type) {
634-
case SPROTO_TINTEGER:
635-
lua_pushinteger(L, 0);
636-
break;
637-
case SPROTO_TBOOLEAN:
638-
lua_pushboolean(L, 0);
639-
break;
640-
case SPROTO_TSTRING:
641-
lua_pushliteral(L, "");
642-
break;
643-
case SPROTO_TSTRUCT:
644-
lua_createtable(L, 0, 1);
645-
lua_pushstring(L, sproto_name(args->subtype));
646-
lua_setfield(L, -2, "__type");
647-
break;
648-
}
666+
push_default(args, 0);
649667
lua_rawset(L, -3);
650668
return SPROTO_CB_NIL;
651669
}

0 commit comments

Comments
 (0)