Jon*_*ope 3 lua ffi luajit rust
我想让一个解释语言(特别是LuaJIT)调用一个返回字符串的Rust函数.
在我甚至取消引用指针之前我崩溃了什么.
我读到Rust字符串不是以null结尾的,所以我使用该to_c_str()函数来创建一个字符串,但是我认为生命周期在某种程度上会在整个事情中抛出一个扳手,因为我对它们仍然有些模糊.
Rust代码:
#![crate_type = "dylib"]
extern crate libc;
#[no_mangle]
pub extern "C" fn hello_world() -> std::c_str::CString {
"Hello World".to_c_str()
}
Run Code Online (Sandbox Code Playgroud)
Lua代码:
local ffi = require("ffi")
ffi.cdef[[
char *hello_world();
]]
local hello_world = ffi.load("hello_world")
local hw = hello_world.hello_world()
Run Code Online (Sandbox Code Playgroud)
A CString不仅是一个指针; 它是一个指针加上一个布尔值,指示是否CString拥有C字符串.因此,Lua代码中的声明与Rust代码中的定义不匹配.
使用方法返回a *const c_char或a .如果你的函数返回一个动态分配的字符串,你还需要提供一个函数来释放Lua代码需要手动调用的字符串,否则会导致内存泄漏.*mut c_charunwrapCString