From ff0d923aaec3335db93eda633bc99772ee9d481b Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Mon, 27 Jun 2022 13:04:32 +0100 Subject: [PATCH] Don't use custom allocator for non-vendored LuaJIT (fixes #176) --- src/lua.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 5efbe55..17951c6 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -126,7 +126,6 @@ pub(crate) struct ExtraData { sandboxed: bool, } -#[cfg_attr(any(feature = "lua51", feature = "luajit"), allow(dead_code))] struct MemoryInfo { used_memory: isize, memory_limit: isize, @@ -430,12 +429,19 @@ impl Lua { new_ptr } - let mem_info = Box::into_raw(Box::new(MemoryInfo { - used_memory: 0, - memory_limit: 0, - })); + // Skip Rust allocator for non-vendored LuaJIT (see https://github.com/khvzak/mlua/issues/176) + let use_rust_allocator = !(cfg!(feature = "luajit") && cfg!(not(feature = "vendored"))); - let state = ffi::lua_newstate(allocator, mem_info as *mut c_void); + let (state, mem_info) = if use_rust_allocator { + let mem_info = Box::into_raw(Box::new(MemoryInfo { + used_memory: 0, + memory_limit: 0, + })); + let state = ffi::lua_newstate(allocator, mem_info as *mut c_void); + (state, mem_info) + } else { + (ffi::luaL_newstate(), ptr::null_mut()) + }; ffi::luaL_requiref(state, cstr!("_G"), ffi::luaopen_base, 1); ffi::lua_pop(state, 1);