Fallback to Lua internal allocator if unable to create Lua VM with Rust one.

This should fix #194
This commit is contained in:
Alex Orlenko 2022-08-02 10:20:28 +01:00
parent 0cd724f63b
commit bf6708ba58
No known key found for this signature in database
GPG key ID: 4C150C250863B96D

View file

@ -437,8 +437,14 @@ impl Lua {
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::default()));
let state = ffi::lua_newstate(allocator, mem_info as *mut c_void);
let mut mem_info = Box::into_raw(Box::new(MemoryInfo::default()));
let mut state = ffi::lua_newstate(allocator, mem_info as *mut c_void);
// If state is null (it's possible for LuaJIT on non-x86 arch) then switch to Lua internal allocator
if state.is_null() {
drop(Box::from_raw(mem_info));
mem_info = ptr::null_mut();
state = ffi::luaL_newstate();
}
(state, mem_info)
} else {
(ffi::luaL_newstate(), ptr::null_mut())