2025-06-30 17:22:12 +10:00

154 lines
5.3 KiB
Rust

//! Raw bindings for LuaJIT generated using [bindgen](https://docs.rs/bindgen/).
//!
//! ## Feature flags
//!
//! Features flags are used to control certain features of LuaJIT. They are not enabled by default
//! unless otherwise specified below.
//!
//! - `runtime`: links the target with `libluajit`. This should only be enabled by binary targets.
//! - `jit`: enables the JIT compiler. *This is enabled by default.*
//! - `ffi`: enables the FFI library. *This is enabled by default.*
//! - `unwind`: configures LuaJIT to use stack unwinding instead of `longjmp` for error handling.
//! **This MUST be enabled if `panic = "unwind"` is set.**
//! - `bundled-alloc`: configures LuaJIT to include its own bundled allocator. If this is not
//! enabled, LuaJIT will use the system allocator by default.
//! - `lua52`: enables Lua 5.2 compatibility mode. *This is enabled by default.*
#![allow(nonstandard_style)]
use std::{ffi::*, ptr};
// #[cfg(all(panic = "abort", feature = "unwind"))]
// compile_error!(r#"feature "unwind" cannot be enabled if panic = "abort""#);
// #[cfg(all(panic = "unwind", not(feature = "unwind")))]
// compile_error!(r#"feature "unwind" must be enabled if panic = "unwind""#);
include!(env!("LUAJIT_SYS_BINDGEN"));
pub unsafe extern "C" fn luaJIT_openlibs(L: *mut lua_State) {
unsafe {
lua_getglobal(L, c"package".as_ptr());
lua_getfield(L, -1, c"preload".as_ptr());
lua_replace(L, -2);
macro_rules! load {
($n:literal, $f:literal) => {{
let n: &'static CStr = $n;
let f = include_bytes!(concat!(env!("LUAJIT_SYS_JITLIB"), "/", $f));
if luaL_loadbuffer(L, f.as_ptr().cast(), f.len(), n.as_ptr()) == 0 {
lua_setfield(L, -2, n.as_ptr());
} else {
lua_error(L);
}
}};
}
load!(c"jit.vmdef", "vmdef.lua");
load!(c"jit.dis_x86", "dis_x86.lua");
load!(c"jit.dis_x64", "dis_x64.lua");
load!(c"jit.dis_arm", "dis_arm.lua");
load!(c"jit.dis_arm64", "dis_arm64.lua");
load!(c"jit.dis_arm64be", "dis_arm64be.lua");
load!(c"jit.dis_ppc", "dis_ppc.lua");
load!(c"jit.dis_mips", "dis_mips.lua");
load!(c"jit.dis_mipsel", "dis_mipsel.lua");
load!(c"jit.dis_mips64", "dis_mips64.lua");
load!(c"jit.dis_mips64el", "dis_mips64el.lua");
load!(c"jit.dis_mips64r6", "dis_mips64r6.lua");
load!(c"jit.dis_mips64r6el", "dis_mips64r6el.lua");
load!(c"jit.bc", "bc.lua");
load!(c"jit.bcsave", "bcsave.lua");
load!(c"jit.v", "v.lua");
load!(c"jit.p", "p.lua");
load!(c"jit.dump", "dump.lua");
load!(c"jit.zone", "zone.lua");
lua_pop(L, 1);
}
}
// constants not exposed by lua.h
pub const LUA_TPROTO: c_int = LUA_TTHREAD + 1;
pub const LUA_TCDATA: c_int = LUA_TTHREAD + 2;
// macros not translated by bindgen
pub unsafe fn lua_upvalueindex(i: c_int) -> c_int {
LUA_GLOBALSINDEX - i
}
pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
unsafe { lua_settop(L, -n - 1) }
}
pub unsafe fn lua_newtable(L: *mut lua_State) {
unsafe { lua_createtable(L, 0, 0) }
}
pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
unsafe { (lua_pushcfunction(L, f), lua_setglobal(L, n)) };
}
pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
unsafe { lua_pushcclosure(L, f, 0) }
}
pub unsafe fn lua_strlen(L: *mut lua_State, i: c_int) -> usize {
unsafe { lua_objlen(L, i) }
}
pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TFUNCTION) as c_int }
}
pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TTABLE) as c_int }
}
pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int }
}
pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TNIL) as c_int }
}
pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TBOOLEAN) as c_int }
}
pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TTHREAD) as c_int }
}
pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) == LUA_TNONE) as c_int }
}
pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
unsafe { (lua_type(L, n) <= LUA_TNIL) as c_int }
}
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: impl AsRef<[u8]>) {
unsafe { lua_pushlstring(L, s.as_ref().as_ptr().cast(), s.as_ref().len()) }
}
pub unsafe fn lua_setglobal(L: *mut lua_State, s: *const c_char) {
unsafe { lua_setfield(L, LUA_GLOBALSINDEX, s) }
}
pub unsafe fn lua_getglobal(L: *mut lua_State, s: *const c_char) {
unsafe { lua_getfield(L, LUA_GLOBALSINDEX, s) }
}
pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
unsafe { lua_tolstring(L, i, ptr::null_mut()) }
}
pub unsafe fn lua_open() -> *mut lua_State {
unsafe { luaL_newstate() }
}
pub unsafe fn lua_getregistry(L: *mut lua_State) {
unsafe { lua_pushvalue(L, LUA_REGISTRYINDEX) }
}
pub unsafe fn lua_getgccount(L: *mut lua_State) -> c_int {
unsafe { lua_gc(L, LUA_GCCOUNT, 0) }
}