Skip to content

Commit 69ef08d

Browse files
committed
Switch back to Luau userdata destructors defined in mlua-sys v0.6
1 parent f7f04d5 commit 69ef08d

File tree

7 files changed

+14
-40
lines changed

7 files changed

+14
-40
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ parking_lot = { version = "0.12", features = ["arc_lock"] }
5959
anyhow = { version = "1.0", optional = true }
6060
rustversion = "1.0"
6161

62-
ffi = { package = "mlua-sys", version = "0.7.0", path = "mlua-sys" }
62+
ffi = { package = "mlua-sys", version = "0.6.8", path = "mlua-sys" }
6363

6464
[target.'cfg(unix)'.dependencies]
6565
libloading = { version = "0.8", optional = true }

src/luau/package.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,16 @@ unsafe extern "C-unwind" fn lua_require(state: *mut ffi::lua_State) -> c_int {
124124
ffi::lua_pop(state, 1); // remove nil
125125

126126
// load the module
127-
let err_buf = ffi::lua_newuserdata_t(state, StdString::new());
127+
let err_buf = ffi::lua_newuserdata_t::<StdString>(state);
128+
err_buf.write(StdString::new());
128129
ffi::luaL_getsubtable(state, ffi::LUA_REGISTRYINDEX, cstr!("_LOADERS")); // _LOADERS is at index 3
129130
for i in 1.. {
130131
if ffi::lua_rawgeti(state, -1, i) == ffi::LUA_TNIL {
131132
// no more loaders?
132-
if (*err_buf).is_empty() {
133+
if (&*err_buf).is_empty() {
133134
ffi::luaL_error(state, cstr!("module '%s' not found"), name);
134135
} else {
135-
let bytes = (*err_buf).as_bytes();
136+
let bytes = (&*err_buf).as_bytes();
136137
let extra = ffi::lua_pushlstring(state, bytes.as_ptr() as *const _, bytes.len());
137138
ffi::luaL_error(state, cstr!("module '%s' not found:%s"), name, extra);
138139
}

src/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl MemoryState {
9797
}
9898
}
9999

100-
unsafe extern "C" fn allocator(
100+
unsafe extern "C-unwind" fn allocator(
101101
extra: *mut c_void,
102102
ptr: *mut c_void,
103103
osize: usize,

src/state.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,12 +1966,7 @@ impl Lua {
19661966

19671967
#[inline(always)]
19681968
pub(crate) fn lock(&self) -> ReentrantMutexGuard<RawLua> {
1969-
let rawlua = self.raw.lock();
1970-
#[cfg(feature = "luau")]
1971-
if unsafe { (*rawlua.extra.get()).running_userdata_gc } {
1972-
panic!("Luau VM is suspended while userdata destructor is running");
1973-
}
1974-
rawlua
1969+
self.raw.lock()
19751970
}
19761971

19771972
#[inline(always)]
@@ -1993,12 +1988,7 @@ impl WeakLua {
19931988
#[track_caller]
19941989
#[inline(always)]
19951990
pub(crate) fn lock(&self) -> LuaGuard {
1996-
let guard = LuaGuard::new(self.0.upgrade().expect("Lua instance is destroyed"));
1997-
#[cfg(feature = "luau")]
1998-
if unsafe { (*guard.extra.get()).running_userdata_gc } {
1999-
panic!("Luau VM is suspended while userdata destructor is running");
2000-
}
2001-
guard
1991+
LuaGuard::new(self.0.upgrade().expect("Lua instance is destroyed"))
20021992
}
20031993

20041994
#[inline(always)]

src/state/extra.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ pub(crate) struct ExtraData {
8181
#[cfg(feature = "luau")]
8282
pub(super) interrupt_callback: Option<crate::types::InterruptCallback>,
8383

84-
#[cfg(feature = "luau")]
85-
pub(crate) running_userdata_gc: bool,
8684
#[cfg(feature = "luau")]
8785
pub(super) sandboxed: bool,
8886
#[cfg(feature = "luau")]
@@ -184,8 +182,6 @@ impl ExtraData {
184182
compiler: None,
185183
#[cfg(feature = "luau-jit")]
186184
enable_jit: true,
187-
#[cfg(feature = "luau")]
188-
running_userdata_gc: false,
189185
}));
190186

191187
// Store it in the registry

src/userdata/util.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -437,18 +437,8 @@ pub(crate) unsafe extern "C-unwind" fn collect_userdata<T>(state: *mut ffi::lua_
437437

438438
// This method is called by Luau GC when it's time to collect the userdata.
439439
#[cfg(feature = "luau")]
440-
pub(crate) unsafe extern "C" fn collect_userdata<T>(
441-
state: *mut ffi::lua_State,
442-
ud: *mut std::os::raw::c_void,
443-
) {
444-
// Almost none Lua operations are allowed when destructor is running,
445-
// so we need to set a flag to prevent calling any Lua functions
446-
let extra = (*ffi::lua_callbacks(state)).userdata as *mut crate::state::ExtraData;
447-
(*extra).running_userdata_gc = true;
448-
// Luau does not support _any_ panics in destructors (they are declared as "C", NOT as "C-unwind"),
449-
// so any panics will trigger `abort()`.
440+
pub(crate) unsafe extern "C-unwind" fn collect_userdata<T>(ud: *mut std::os::raw::c_void) {
450441
ptr::drop_in_place(ud as *mut T);
451-
(*extra).running_userdata_gc = false;
452442
}
453443

454444
// This method can be called by user or Lua GC to destroy the userdata.

src/util/userdata.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ pub(crate) unsafe fn push_internal_userdata<T: TypeKey>(
1515
#[cfg(not(feature = "luau"))]
1616
let ud_ptr = if protect {
1717
protect_lua!(state, 0, 1, move |state| {
18-
let ud_ptr = ffi::lua_newuserdata(state, const { mem::size_of::<T>() }) as *mut T;
19-
ptr::write(ud_ptr, t);
20-
ud_ptr
18+
ffi::lua_newuserdata(state, const { mem::size_of::<T>() }) as *mut T
2119
})?
2220
} else {
23-
let ud_ptr = ffi::lua_newuserdata(state, const { mem::size_of::<T>() }) as *mut T;
24-
ptr::write(ud_ptr, t);
25-
ud_ptr
21+
ffi::lua_newuserdata(state, const { mem::size_of::<T>() }) as *mut T
2622
};
2723

2824
#[cfg(feature = "luau")]
2925
let ud_ptr = if protect {
30-
protect_lua!(state, 0, 1, move |state| ffi::lua_newuserdata_t::<T>(state, t))?
26+
protect_lua!(state, 0, 1, move |state| ffi::lua_newuserdata_t::<T>(state))?
3127
} else {
32-
ffi::lua_newuserdata_t::<T>(state, t)
28+
ffi::lua_newuserdata_t::<T>(state)
3329
};
3430

31+
ptr::write(ud_ptr, t);
3532
get_internal_metatable::<T>(state);
3633
ffi::lua_setmetatable(state, -2);
3734
Ok(ud_ptr)

0 commit comments

Comments
 (0)