| 339 | } |
| 340 | |
| 341 | uint32_t DoCheckTableSize(lua_State* L, int index, int parent_offset, dmArray<const void*>& table_stack) |
| 342 | { |
| 343 | int top = lua_gettop(L); |
| 344 | (void)top; |
| 345 | |
| 346 | luaL_checktype(L, index, LUA_TTABLE); |
| 347 | |
| 348 | const void* table_data = (const void*)lua_topointer(L, index); |
| 349 | if (StackContains(table_stack, table_data)) |
| 350 | { |
| 351 | table_stack.SetCapacity(0); |
| 352 | return luaL_error(L, "Save table is recursive!"); |
| 353 | } |
| 354 | StackPush(table_stack, table_data); |
| 355 | |
| 356 | lua_pushvalue(L, index); |
| 357 | lua_pushnil(L); |
| 358 | |
| 359 | uint32_t size = 0; |
| 360 | |
| 361 | // count |
| 362 | size += 4; |
| 363 | while (lua_next(L, -2) != 0) |
| 364 | { |
| 365 | int key_type = lua_type(L, -2); |
| 366 | int value_type = lua_type(L, -1); |
| 367 | |
| 368 | if (IsHash(L, -2)) |
| 369 | { |
| 370 | key_type = LUA_THASH; |
| 371 | } |
| 372 | |
| 373 | if (key_type != LUA_TSTRING && key_type != LUA_TNUMBER && key_type != LUA_THASH) |
| 374 | { |
| 375 | luaL_error(L, "keys in table must be of type number, string or hash (found %s)", lua_typename(L, key_type)); |
| 376 | } |
| 377 | |
| 378 | // key + value type |
| 379 | size += 2; |
| 380 | if (key_type == LUA_TSTRING) |
| 381 | { |
| 382 | size += sizeof(uint32_t) + lua_objlen(L, -2); |
| 383 | } |
| 384 | else if (key_type == LUA_TNUMBER) |
| 385 | { |
| 386 | size += 4; |
| 387 | } |
| 388 | else if (key_type == LUA_THASH) |
| 389 | { |
| 390 | size += sizeof(dmhash_t); |
| 391 | } |
| 392 | |
| 393 | switch (value_type) |
| 394 | { |
| 395 | case LUA_TBOOLEAN: |
| 396 | { |
| 397 | size += 1; |
| 398 | } |
no test coverage detected