Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect userdata in lua

Tags:

c

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.

like image 760
guilin 桂林 Avatar asked Mar 02 '13 15:03

guilin 桂林


People also ask

What is Userdata Lua?

A userdata value is a pointer to a block of raw memory. There are two kinds of userdata: full userdata, where the block of memory is managed by Lua, and light userdata, where the block of memory is managed by the host. Userdata has no predefined operations in Lua, except assignment and identity test.

What does hashtag mean in Lua?

# is the lua length operator which works on strings or on table arrays.


1 Answers

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.

like image 112
fouronnes Avatar answered Sep 21 '22 19:09

fouronnes