LuaJIT, how to convert cdata to userdata?

不打扰是莪最后的温柔 提交于 2020-01-04 14:09:03

问题


I want to use LuaJIT for its ability to create structs and arrays in Lua. But my functions which use the data require userdata or a string (not a string representation, just used as a container) that stores the data.

But looking through the api I don't see if this is even possible. Is it?

Thanks.


回答1:


LuaJIT FFI should not be mixed with classic C/API. While there are mechanisms to convert a const char* pointer to Lua string (ffi.string), there is no way to convert an FFI struct to Lua userdata.

FFI functions have no knowledge of a lua_State, which is needed to create userdata on the C side.

I don't know how large your C/API binding base is at the moment, but one solution would be to rewrite your C/API functions to FFI, if you're set on using FFI. You can do it gradually, making sure that there is a clear line between FFI and C/API.



来源:https://stackoverflow.com/questions/18820577/luajit-how-to-convert-cdata-to-userdata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!