From 542f8c24e79b1ae6649d15caff1192e74d1ea96c Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sat, 3 Oct 2026 17:01:58 -0400 Subject: [PATCH] Bind LuaJIT's native luaL_traceback in mlua-sys Under the luajit features, luaL_traceback resolved to the lua-compat-5.3 port, whose compat53_findfield walks every global and every field of every global table to name a frame. mlua builds a traceback for every callback error, so a pcall'd error on a VM with large global tables cost hundreds of microseconds. LuaJIT exports luaL_traceback itself (since before 2.0.0) and names frames from the call site without searching. Declare the native symbol under the luajit feature and compile the port and its helpers only for Lua 5.1. --- mlua-sys/src/lua51/compat.rs | 12 ++++++++++-- mlua-sys/src/lua51/lauxlib.rs | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mlua-sys/src/lua51/compat.rs b/mlua-sys/src/lua51/compat.rs index 16cd81a9..b5912e18 100644 --- a/mlua-sys/src/lua51/compat.rs +++ b/mlua-sys/src/lua51/compat.rs @@ -2,9 +2,10 @@ //! //! Based on github.com/keplerproject/lua-compat-5.3 -use std::ffi::CStr; use std::os::raw::{c_char, c_int, c_void}; -use std::{mem, ptr}; +use std::ptr; +#[cfg(not(feature = "luajit"))] +use std::{ffi::CStr, mem}; use super::lauxlib::*; use super::lua::*; @@ -21,9 +22,12 @@ unsafe fn compat53_reverse(L: *mut lua_State, mut a: c_int, mut b: c_int) { } } +#[cfg(not(feature = "luajit"))] const COMPAT53_LEVELS1: c_int = 10; // size of the first part of the stack +#[cfg(not(feature = "luajit"))] const COMPAT53_LEVELS2: c_int = 11; // size of the second part of the stack +#[cfg(not(feature = "luajit"))] unsafe fn compat53_countlevels(L: *mut lua_State) -> c_int { let mut ar: lua_Debug = mem::zeroed(); let (mut li, mut le) = (1, 1); @@ -70,6 +74,7 @@ unsafe fn compat53_checkmode( LUA_OK } +#[cfg(not(feature = "luajit"))] unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) -> c_int { if level == 0 || lua_istable(L, -1) == 0 { return 0; // not found @@ -97,6 +102,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) -> 0 // not found } +#[cfg(not(feature = "luajit"))] unsafe fn compat53_pushglobalfuncname(L: *mut lua_State, L1: *mut lua_State, ar: *mut lua_Debug) -> c_int { let top = lua_gettop(L); lua_getinfo(L1, cstr!("f"), ar); // push function @@ -118,6 +124,7 @@ unsafe fn compat53_pushglobalfuncname(L: *mut lua_State, L1: *mut lua_State, ar: } } +#[cfg(not(feature = "luajit"))] unsafe fn compat53_pushfuncname(L: *mut lua_State, L1: *mut lua_State, ar: *mut lua_Debug) { // try first a global name if compat53_pushglobalfuncname(L, L1, ar) != 0 { @@ -458,6 +465,7 @@ pub unsafe fn luaL_len(L: *mut lua_State, idx: c_int) -> lua_Integer { res } +#[cfg(not(feature = "luajit"))] pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const c_char, mut level: c_int) { let mut ar: lua_Debug = mem::zeroed(); let top = lua_gettop(L); diff --git a/mlua-sys/src/lua51/lauxlib.rs b/mlua-sys/src/lua51/lauxlib.rs index 767d8fe0..63dae6a0 100644 --- a/mlua-sys/src/lua51/lauxlib.rs +++ b/mlua-sys/src/lua51/lauxlib.rs @@ -54,6 +54,9 @@ unsafe extern "C-unwind" { def: *const c_char, lst: *const *const c_char, ) -> c_int; + + #[cfg(feature = "luajit")] + pub fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const c_char, level: c_int); } // Pre-defined references