#lua #lua-script #script #lua-jit

sys luajit2-sys

LuaJIT-2.1 FFI 绑定

2 个版本

0.0.2 2020年1月19日
0.0.1 2020年1月19日

#12 in #lua-jit

MIT/Apache

2MB
61K SLoC

C 47K SLoC // 0.1% comments Lua 13K SLoC // 0.1% comments Rust 1K SLoC // 0.0% comments Batch 469 SLoC Shell 21 SLoC

Rust LuaJIT 绑定

crates.io docs.rs build

[dependencies]
luajit2-sys = "0.0.1"

导出的 Cargo 环境变量

DEP_LUAJIT_INCLUDE LuaJIT 源代码和头文件的路径
DEP_LUAJIT_LIB_NAME 平台特定的库文件名(Windows 上为 lua51,其他地方为 luajit

示例

use luajit2_sys as sys;
use std::ffi::CStr;

fn main() {
    unsafe {
        let lua = sys::luaL_newstate();
        sys::luaL_openlibs(lua);
        let script_data = b"return 1 + 2";
        let script_name = b"run_script\0";
        sys::luaL_loadbuffer(
            lua,
            script_data.as_ptr() as _,
            script_data.len() as _,
            script_name.as_ptr() as _,
        );
        sys::lua_pcall(lua, 0, 1, 0);
        let idx = sys::lua_gettop(lua);
        let s = sys::lua_tostring(lua, idx);
        let result = CStr::from_ptr(s).to_string_lossy().to_string();
        sys::lua_close(lua);

        println!("result: {}", result);
    }
}

依赖项