MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / lua_array_length

Function lua_array_length

LuaSTGPlus/LuaExtensions/cjson4lua/lua_cjson.c:496–537  ·  view source on GitHub ↗

Find the size of the array on the top of the Lua stack * -1 object (not a pure array) * >=0 elements in array */

Source from the content-addressed store, hash-verified

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

Callers 1

json_append_dataFunction · 0.85

Calls 1

json_encode_exceptionFunction · 0.85

Tested by

no test coverage detected