MCPcopy Create free account
hub / github.com/DFHack/dfhack / meta_sizeof

Function meta_sizeof

library/LuaWrapper.cpp:582–621  ·  view source on GitHub ↗

* Method: sizeof for DF object references. * * Returns: size[, address] */

Source from the content-addressed store, hash-verified

580 * Returns: size[, address]
581 */
582static int meta_sizeof(lua_State *state)
583{
584 int argc = lua_gettop(state);
585
586 if (argc != 1)
587 luaL_error(state, "Usage: object:sizeof() or df.sizeof(object)");
588
589 // Two special cases: nil and lightuserdata for NULL and void*
590 if (lua_isnil(state, 1) || lua_islightuserdata(state, 1))
591 {
592 lua_pushnil(state);
593 lua_pushinteger(state, (size_t)lua_touserdata(state, 1));
594 return 2;
595 }
596
597 const type_identity *id = get_object_identity(state, 1, "df.sizeof()", true, true);
598
599 // Static arrays need special handling
600 if (id->type() == IDTYPE_BUFFER)
601 {
602 auto buf = (const df::buffer_container_identity*)id;
603 const type_identity *item = buf->getItemType();
604 int count = buf->getSize();
605
606 fetch_container_details(state, lua_gettop(state), &item, &count);
607
608 lua_pushinteger(state, item->byte_size() * count);
609 }
610 else
611 lua_pushinteger(state, id->byte_size());
612
613 // Add the address
614 if (lua_isuserdata(state, 1))
615 {
616 lua_pushinteger(state, (size_t)get_object_ref(state, 1));
617 return 2;
618 }
619 else
620 return 1;
621}
622
623/**
624 * Method: displace for DF object references.

Callers

nothing calls this directly

Calls 10

lua_gettopFunction · 0.85
luaL_errorFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushintegerFunction · 0.85
lua_touserdataFunction · 0.85
fetch_container_detailsFunction · 0.85
lua_isuserdataFunction · 0.85
byte_sizeMethod · 0.80
typeMethod · 0.45
getSizeMethod · 0.45

Tested by

no test coverage detected