From bf6708ba58ab5358346501436e0569302ff14199 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 2 Aug 2022 10:20:28 +0100 Subject: [PATCH] Fallback to Lua internal allocator if unable to create Lua VM with Rust one. This should fix #194 --- src/lua.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index bfd3374..fc631d9 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -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())