Don't use custom allocator for non-vendored LuaJIT (fixes #176)

This commit is contained in:
Alex Orlenko 2022-06-27 13:04:32 +01:00
parent d9ac7abff2
commit ff0d923aae
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
1 changed files with 12 additions and 6 deletions

View File

@ -126,7 +126,6 @@ pub(crate) struct ExtraData {
sandboxed: bool, sandboxed: bool,
} }
#[cfg_attr(any(feature = "lua51", feature = "luajit"), allow(dead_code))]
struct MemoryInfo { struct MemoryInfo {
used_memory: isize, used_memory: isize,
memory_limit: isize, memory_limit: isize,
@ -430,12 +429,19 @@ impl Lua {
new_ptr new_ptr
} }
// 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, mem_info) = if use_rust_allocator {
let mem_info = Box::into_raw(Box::new(MemoryInfo { let mem_info = Box::into_raw(Box::new(MemoryInfo {
used_memory: 0, used_memory: 0,
memory_limit: 0, memory_limit: 0,
})); }));
let state = ffi::lua_newstate(allocator, mem_info as *mut c_void); 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::luaL_requiref(state, cstr!("_G"), ffi::luaopen_base, 1);
ffi::lua_pop(state, 1); ffi::lua_pop(state, 1);