18

I am using inspect.lua to inspect table to string.

But, if the value is a userdata, it returns just <userdata 1>

I really need to know what the userdata type is, what the userdata value is, it's very important for debuging, I don't want do it in any IDE, I just want something can help me debug by print staffs.

guilin 桂林
  • 17,050
  • 29
  • 92
  • 146
  • 6
    The only info that you can get is from `getmetatable(someuserdata)`. What exactly do you want to inspect? – Eric Mar 02 '13 at 15:12
  • 1
    thanks a lot, there are something very useful, `print(inspect(getmetatable(someuserdata)))` – guilin 桂林 Mar 02 '13 at 15:17
  • where "someuserdata" is what, exactly?. And the return value of getmetatable is what exactly? The manual is useless. – CashCow Jan 12 '15 at 15:51

2 Answers2

16

You cannot.

From the manual :

The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value is a pointer to a block of raw memory. [...] Userdata has no predefined operations in Lua, except assignment and identity test.

As indicated by @Eric, the only thing you can do from Lua is inspect the metatable :

print(inspect(getmetatable(someuserdata)))

If you are using the C API, you should be able to register a custom function that prints whatever is held by the block.

fouronnes
  • 3,838
  • 23
  • 41
0

If the userdata metatable was created with luaL_newmetatable it contains an entry __name containing the userdatas type (since Lua version 5.3), see https://www.lua.org/manual/5.3/manual.html#luaL_newmetatable. Unfortunately this feature is still not widely used.

Oliver
  • 740
  • 4
  • 6