| 1312 | } |
| 1313 | |
| 1314 | static int lpb_pushdeffield(lua_State *L, lpb_State *LS, const pb_Field *f, int is_proto3) { |
| 1315 | int ret = 0, u = 0; |
| 1316 | const pb_Type *type; |
| 1317 | char *end; |
| 1318 | if (f == NULL) return 0; |
| 1319 | switch (f->type_id) { |
| 1320 | case PB_Tenum: |
| 1321 | if ((type = f ? f->type : NULL) == NULL) return 0; |
| 1322 | if ((f = pb_fname(type, f->default_value)) != NULL) |
| 1323 | ret = LS->enum_as_value ? |
| 1324 | (lpb_pushinteger(L, f->number, 1, LS->int64_mode), 1) : |
| 1325 | (lua_pushstring(L, (const char*)f->name), 1); |
| 1326 | else if (is_proto3) |
| 1327 | ret = (f = pb_field(type, 0)) == NULL || LS->enum_as_value ? |
| 1328 | (lua_pushinteger(L, 0), 1) : |
| 1329 | (lua_pushstring(L, (const char*)f->name), 1); |
| 1330 | break; |
| 1331 | case PB_Tmessage: |
| 1332 | ret = (lpb_pushtypetable(L, LS, f->type), 1); |
| 1333 | break; |
| 1334 | case PB_Tbytes: case PB_Tstring: |
| 1335 | if (f->default_value) |
| 1336 | ret = (lua_pushstring(L, (const char*)f->default_value), 1); |
| 1337 | else if (is_proto3) ret = (lua_pushliteral(L, ""), 1); |
| 1338 | break; |
| 1339 | case PB_Tbool: |
| 1340 | if (f->default_value) { |
| 1341 | if (f->default_value == lpb_name(LS, pb_slice("true"))) |
| 1342 | ret = (lua_pushboolean(L, 1), 1); |
| 1343 | else if (f->default_value == lpb_name(LS, pb_slice("false"))) |
| 1344 | ret = (lua_pushboolean(L, 0), 1); |
| 1345 | } else if (is_proto3) ret = (lua_pushboolean(L, 0), 1); |
| 1346 | break; |
| 1347 | case PB_Tdouble: case PB_Tfloat: |
| 1348 | if (f->default_value) { |
| 1349 | lua_Number ln = (lua_Number)strtod((const char*)f->default_value, &end); |
| 1350 | if ((const char*)f->default_value == end) return 0; |
| 1351 | ret = (lua_pushnumber(L, ln), 1); |
| 1352 | } else if (is_proto3) ret = (lua_pushnumber(L, 0.0), 1); |
| 1353 | break; |
| 1354 | |
| 1355 | case PB_Tuint64: case PB_Tfixed64: case PB_Tfixed32: case PB_Tuint32: |
| 1356 | u = 1; |
| 1357 | /* FALLTHROUGH */ |
| 1358 | default: |
| 1359 | if (f->default_value) { |
| 1360 | lua_Integer li = (lua_Integer)strtol((const char*)f->default_value, &end, 10); |
| 1361 | if ((const char*)f->default_value == end) return 0; |
| 1362 | ret = (lpb_pushinteger(L, li, u, LS->int64_mode), 1); |
| 1363 | } else if (is_proto3) ret = (lua_pushinteger(L, 0), 1); |
| 1364 | } |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | static void lpb_setdeffields(lua_State *L, lpb_State *LS, const pb_Type *t, lpb_DefFlags flags) { |
| 1369 | const pb_Field *f = NULL; |
no test coverage detected