Returns true if the Lua table on top of the stack is exclusively composed * of keys from numerical keys from 1 up to N, with N being the total number * of elements, without any hole in the middle. */
| 423 | * of keys from numerical keys from 1 up to N, with N being the total number |
| 424 | * of elements, without any hole in the middle. */ |
| 425 | int table_is_an_array(lua_State *L) { |
| 426 | int count = 0, max = 0; |
| 427 | #if LUA_VERSION_NUM < 503 |
| 428 | lua_Number n; |
| 429 | #else |
| 430 | lua_Integer n; |
| 431 | #endif |
| 432 | |
| 433 | /* Stack top on function entry */ |
| 434 | int stacktop; |
| 435 | |
| 436 | stacktop = lua_gettop(L); |
| 437 | |
| 438 | lua_pushnil(L); |
| 439 | while(lua_next(L,-2)) { |
| 440 | /* Stack: ... key value */ |
| 441 | lua_pop(L,1); /* Stack: ... key */ |
| 442 | /* The <= 0 check is valid here because we're comparing indexes. */ |
| 443 | #if LUA_VERSION_NUM < 503 |
| 444 | if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 || |
| 445 | !IS_INT_EQUIVALENT(n)) |
| 446 | #else |
| 447 | if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0) |
| 448 | #endif |
| 449 | { |
| 450 | lua_settop(L, stacktop); |
| 451 | return 0; |
| 452 | } |
| 453 | max = (n > max ? n : max); |
| 454 | count++; |
| 455 | } |
| 456 | /* We have the total number of elements in "count". Also we have |
| 457 | * the max index encountered in "max". We can't reach this code |
| 458 | * if there are indexes <= 0. If you also note that there can not be |
| 459 | * repeated keys into a table, you have that if max==count you are sure |
| 460 | * that there are all the keys form 1 to count (both included). */ |
| 461 | lua_settop(L, stacktop); |
| 462 | return max == count; |
| 463 | } |
| 464 | |
| 465 | /* If the length operator returns non-zero, that is, there is at least |
| 466 | * an object at key '1', we serialize to message pack list. Otherwise |
no test coverage detected