pName associates Lua objects (functions, tables, threads, userdata) with string names. An object may have up to one name; the same name may be used among multiple objects.
Dependencies: None.
local pName = require("p_name")
local function _assertMetatable(v, mt)
if getmetatable(v) ~= mt then
error("invalid " .. pName.safeGet(mt))
end
end
local _mt_point = {}
_mt_point.__index = _mt_point
pName.set(_mt_point, "Point")
local function newPoint(x, y)
return setmetatable({x=x, y=y}, _mt_point)
end
local my_point = newPoint(0, 5)
_assertMetatable(my_point, _mt_point) -- OK
local not_my_point = {}
_assertMetatable(not_my_point, _mt_point) --> invalid Point
API
pName.get
Gets the name for a Lua object.
local name = pName.get(o)
Returns: The Lua object’s registered name, or nil if there is no name.
pName.safeGet
Gets the name for a Lua object, falling back to a default name if it’s not registered.
local name = pName.safeGet(o, [fallback_name])
-
o: The Lua object (function, table, thread or userdata). -
[fallback_name]: ("Unknown") The name to use if no name is registered.
Returns: The registered name or the fallback name.
pName.set
Assigns, overwrites or deletes a name for a Lua object.
retval = pName.set(o, [name])
-
o: The Lua object (function, table, thread or userdata). -
[name]The name (string), orfalse/nilto erase any existing name.
Returns: The Lua object, to support one-liners like local tbl = pName.set({}, "SomeName").
VERSION: 2.106