@@ -621,31 +621,49 @@ lloadproto(lua_State *L) {
621
621
return 1 ;
622
622
}
623
623
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
+
624
655
static int
625
656
encode_default (const struct sproto_arg * args ) {
626
657
lua_State * L = args -> ud ;
627
658
lua_pushstring (L , args -> tagname );
628
659
if (args -> index > 0 ) {
629
660
lua_newtable (L );
661
+ push_default (args , 1 );
662
+ lua_setfield (L , -2 , "__array" );
630
663
lua_rawset (L , -3 );
631
664
return SPROTO_CB_NOARRAY ;
632
665
} 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 );
649
667
lua_rawset (L , -3 );
650
668
return SPROTO_CB_NIL ;
651
669
}
0 commit comments