MCPcopy Create free account
hub / github.com/defold/defold / DoCheckTableSize

Function DoCheckTableSize

engine/script/src/script_table.cpp:341–477  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 }

Callers 1

CheckTableSizeFunction · 0.85

Calls 15

lua_gettopFunction · 0.85
luaL_checktypeFunction · 0.85
lua_topointerFunction · 0.85
StackContainsFunction · 0.85
luaL_errorFunction · 0.85
StackPushFunction · 0.85
lua_pushvalueFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
lua_typeFunction · 0.85
IsHashFunction · 0.85
lua_typenameFunction · 0.85

Tested by

no test coverage detected