Lua: Add lengths of (some) containers to their string representations

develop
lethosor 2016-08-13 19:52:14 -04:00
parent 18a8512242
commit 576174ea0b
1 changed files with 14 additions and 1 deletions

@ -996,9 +996,22 @@ static int meta_ptr_tostring(lua_State *state)
{ {
uint8_t *ptr = get_object_addr(state, 1, 0, "access"); uint8_t *ptr = get_object_addr(state, 1, 0, "access");
bool has_length = false;
uint64_t length = 0;
auto *cid = dynamic_cast<df::container_identity*>(get_object_identity(state, 1, "__tostring()", true, true));
if (cid && (cid->type() == IDTYPE_CONTAINER || cid->type() == IDTYPE_STL_PTR_VECTOR))
{
has_length = true;
length = cid->lua_item_count(state, ptr, container_identity::COUNT_LEN);
}
lua_getfield(state, UPVAL_METATABLE, "__metatable"); lua_getfield(state, UPVAL_METATABLE, "__metatable");
const char *cname = lua_tostring(state, -1); const char *cname = lua_tostring(state, -1);
if (has_length)
lua_pushstring(state, stl_sprintf("<%s[%llu]: %p>", cname, length, (void*)ptr).c_str());
else
lua_pushstring(state, stl_sprintf("<%s: %p>", cname, (void*)ptr).c_str()); lua_pushstring(state, stl_sprintf("<%s: %p>", cname, (void*)ptr).c_str());
return 1; return 1;
} }