Find the size of the array on the top of the Lua stack * -1 object (not a pure array) * >=0 elements in array */
| 670 | * >=0 elements in array |
| 671 | */ |
| 672 | static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) |
| 673 | { |
| 674 | double k; |
| 675 | int max; |
| 676 | int items; |
| 677 | |
| 678 | max = 0; |
| 679 | items = 0; |
| 680 | |
| 681 | lua_pushnil(l); |
| 682 | /* table, startkey */ |
| 683 | while (lua_next(l, -2) != 0) { |
| 684 | /* table, key, value */ |
| 685 | if (lua_type(l, -2) == LUA_TNUMBER && |
| 686 | (k = lua_tonumber(l, -2))) { |
| 687 | /* Integer >= 1 ? */ |
| 688 | if (floor(k) == k && k >= 1) { |
| 689 | if (k > max) |
| 690 | max = k; |
| 691 | items++; |
| 692 | lua_pop(l, 1); |
| 693 | continue; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* Must not be an array (non integer key) */ |
| 698 | lua_pop(l, 2); |
| 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | /* Encode excessively sparse arrays as objects (if enabled) */ |
| 703 | if (cfg->encode_sparse_ratio > 0 && |
| 704 | max > items * cfg->encode_sparse_ratio && |
| 705 | max > cfg->encode_sparse_safe) { |
| 706 | if (!cfg->encode_sparse_convert) |
| 707 | json_encode_exception(l, cfg, json, -1, "excessively sparse array"); |
| 708 | |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | return max; |
| 713 | } |
| 714 | |
| 715 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, |
| 716 | int current_depth, strbuf_t *json) |
no test coverage detected