LuaJIT,如何将cdata转换为userdata? [英] LuaJIT, how to convert cdata to userdata?

查看:523
本文介绍了LuaJIT,如何将cdata转换为userdata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LuaJIT来在Lua中创建结构和数组.但是我使用数据的函数需要userdata或存储数据的字符串(不是字符串表示形式,只是用作容器).

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.

但是通过api查看,我什至不知道这是否有可能.是吗?

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

谢谢.

推荐答案

LuaJIT FFI不应与经典C/API混合使用.虽然有将const char*指针转换为Lua字符串(ffi.string)的机制,但无法将FFI结构转换为Lua用户数据.

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函数不知道lua_State,这是在C端创建用户数据所必需的.

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

我不知道您的C/API绑定库目前有多大,但是一种解决方案是将C/API函数重写为FFI(如果您打算使用FFI).您可以逐步进行操作,确保FFI和C/API之间没有分界线.

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.

这篇关于LuaJIT,如何将cdata转换为userdata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆