Find the size of the array on the top of the Lua stack * -1 object (not a pure array) * >=0 elements in array */
| 502 | * >=0 elements in array |
| 503 | */ |
| 504 | static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) |
| 505 | { |
| 506 | double k; |
| 507 | int max; |
| 508 | int items; |
| 509 | |
| 510 | max = 0; |
| 511 | items = 0; |
| 512 | |
| 513 | lua_pushnil(l); |
| 514 | /* table, startkey */ |
| 515 | while (lua_next(l, -2) != 0) { |
| 516 | /* table, key, value */ |
| 517 | if (lua_type(l, -2) == LUA_TNUMBER && |
| 518 | (k = lua_tonumber(l, -2))) { |
| 519 | /* Integer >= 1 ? */ |
| 520 | if (floor(k) == k && k >= 1) { |
| 521 | if (k > max) |
| 522 | max = k; |
| 523 | items++; |
| 524 | lua_pop(l, 1); |
| 525 | continue; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /* Must not be an array (non integer key) */ |
| 530 | lua_pop(l, 2); |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /* Encode excessively sparse arrays as objects (if enabled) */ |
| 535 | if (cfg->encode_sparse_ratio > 0 && |
| 536 | max > items * cfg->encode_sparse_ratio && |
| 537 | max > cfg->encode_sparse_safe) { |
| 538 | if (!cfg->encode_sparse_convert) |
| 539 | json_encode_exception(l, cfg, json, -1, "excessively sparse array"); |
| 540 | |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | return max; |
| 545 | } |
| 546 | |
| 547 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, |
| 548 | int current_depth, strbuf_t *json) |
no test coverage detected