wea.inspect: pull metatable names from item.__name

this is completely arbitrary, but will assist in discovering types of 
tables etc.
This commit is contained in:
Starbeamrainbowlabs 2021-12-31 01:38:24 +00:00
parent 70d6f36444
commit dd8cd78d6b
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 13 additions and 1 deletions

View file

@ -1,5 +1,10 @@
--- Serialises an arbitrary value to a string. --- Serialises an arbitrary value to a string.
-- Note that although the resulting table *looks* like valid Lua, it isn't. -- Note that although the resulting table *looks* like valid Lua, it isn't.
-- Completely arbitrarily, if a table (or it's associated metatable) has the
-- key __name then it is conidered the name of the parent metatable. This can
-- be useful for identifying custom table-based types.
-- Should anyone come across a 'proper' way to obtain the name of a metatable
-- in pure vanilla Lua, I will update this to follow that standard instead.
-- @param item any Input item to serialise. -- @param item any Input item to serialise.
-- @param sep string key value seperator -- @param sep string key value seperator
-- @param new_line string key value pair delimiter -- @param new_line string key value pair delimiter
@ -12,7 +17,13 @@ local function inspect(item, maxdepth)
end end
if maxdepth < 1 then return "[truncated]" end if maxdepth < 1 then return "[truncated]" end
local result = { "{\n" } local result = { }
-- Consider our (arbitrarily decided) property __name to the type of this item
-- Remember that this implicitly checks the metatable so long as __index is set.
if type(item.__name) == "string" then
table.insert(result, "("..item.__name..") ")
end
table.insert(result, "{\n")
for key,value in pairs(item) do for key,value in pairs(item) do
local value_text = inspect(value, maxdepth - 1) local value_text = inspect(value, maxdepth - 1)
:gsub("\n", "\n\t") :gsub("\n", "\n\t")

View file

@ -2,6 +2,7 @@
-- @class -- @class
local Vector3 = {} local Vector3 = {}
Vector3.__index = Vector3 Vector3.__index = Vector3
Vector3.__name = "Vector3" -- A hack to allow identification in wea.inspect
--- Creates a new Vector3 instance. --- Creates a new Vector3 instance.
-- @param x number The x co-ordinate value. -- @param x number The x co-ordinate value.