guilin 桂林
guilin 桂林

Reputation: 17422

How to inspect userdata in lua

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.

Upvotes: 20

Views: 39326

Answers (2)

Oliver
Oliver

Reputation: 740

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.

Upvotes: 0

fouronnes
fouronnes

Reputation: 4028

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.

Upvotes: 17

Related Questions