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

Function meta_ptr_tostring

library/LuaWrapper.cpp:1020–1044  ·  view source on GitHub ↗

* Metamethod: represent a DF object reference as string. */

Source from the content-addressed store, hash-verified

1018 * Metamethod: represent a DF object reference as string.
1019 */
1020static int meta_ptr_tostring(lua_State *state)
1021{
1022 uint8_t *ptr = get_object_addr(state, 1, 0, "access");
1023
1024 bool has_length = false;
1025 uint64_t length = 0;
1026 auto *cid = dynamic_cast<const df::container_identity*>(get_object_identity(state, 1, "__tostring()", true, true));
1027
1028 if (cid && (cid->type() == IDTYPE_CONTAINER || cid->type() == IDTYPE_STL_PTR_VECTOR))
1029 {
1030 has_length = true;
1031 length = cid->lua_item_count(state, ptr, container_identity::COUNT_LEN);
1032 }
1033
1034 lua_getfield(state, UPVAL_METATABLE, "__metatable");
1035 const char *cname = lua_tostring(state, -1);
1036
1037 auto str = has_length ?
1038 fmt::format("<{}[{}]: {}>", cname, length, static_cast<void*>(ptr)) :
1039 fmt::format("<{}: {}>", cname, static_cast<void*>(ptr));
1040
1041 lua_pushlstring(state, str.data(), str.size());
1042
1043 return 1;
1044}
1045
1046/**
1047 * Metamethod: __index for enum.attrs

Callers

nothing calls this directly

Calls 6

lua_getfieldFunction · 0.85
lua_pushlstringFunction · 0.85
lua_item_countMethod · 0.80
typeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected