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