C语言 如何在lua中检查用户数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15175859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:32:25  来源:igfitidea点击:

How to inspect userdata in lua

clua

提问by u443966

I am using inspect.luato inspect table to string.

我正在使用inspect.lua来检查表到字符串。

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

但是,如果值是一个用户数据,它只返回 <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.

我真的需要知道 userdata 类型是什么,userdata 值是什么,这对调试非常重要,我不想在任何 IDE 中进行,我只是希望打印人员可以帮助我调试。

回答by fouronnes

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.

提供 userdata 类型以允许将任意 C 数据存储在 Lua 变量中。用户数据值是指向原始内存块的指针。[...] Userdata 在 Lua 中没有预定义的操作,除了赋值和身份测试。

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

正如@Eric 所指出的,你可以从 Lua 做的唯一一件事就是检查元表:

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.

如果您使用的是 C API,您应该能够注册一个自定义函数来打印块所持有的任何内容。

回答by Oliver

If the userdata metatable was created with luaL_newmetatableit contains an entry __namecontaining 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.

如果创建的 userdata 元表luaL_newmetatable包含一个__name包含 userdatas 类型的条目(自 Lua 5.3 版起),请参阅https://www.lua.org/manual/5.3/manual.html#luaL_newmetatable。不幸的是,这个功能仍然没有被广泛使用。