Skip to content

Commit 7272e40

Browse files
committed
Faster non-scoped callbacks
1 parent c6ef393 commit 7272e40

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/state/raw.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,27 +1028,16 @@ impl RawLua {
10281028

10291029
// Creates a Function out of a Callback containing a 'static Fn.
10301030
pub(crate) fn create_callback(&self, func: Callback) -> Result<Function> {
1031+
// This is non-scoped version of the callback (upvalue is always valid)
1032+
// TODO: add a scoped version
10311033
unsafe extern "C-unwind" fn call_callback(state: *mut ffi::lua_State) -> c_int {
1032-
// Normal functions can be scoped and therefore destroyed,
1033-
// so we need to check that the first upvalue is valid
1034-
let (upvalue, extra) = match ffi::lua_type(state, ffi::lua_upvalueindex(1)) {
1035-
ffi::LUA_TUSERDATA => {
1036-
let upvalue = get_userdata::<CallbackUpvalue>(state, ffi::lua_upvalueindex(1));
1037-
(upvalue, (*upvalue).extra.get())
1038-
}
1039-
_ => (ptr::null_mut(), ptr::null_mut()),
1040-
};
1041-
callback_error_ext(state, extra, |extra, nargs| {
1034+
let upvalue = get_userdata::<CallbackUpvalue>(state, ffi::lua_upvalueindex(1));
1035+
callback_error_ext(state, (*upvalue).extra.get(), |extra, nargs| {
10421036
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
1043-
if upvalue.is_null() {
1044-
return Err(Error::CallbackDestructed);
1045-
}
1046-
10471037
// The lock must be already held as the callback is executed
10481038
let rawlua = (*extra).raw_lua();
10491039
let _guard = StateGuard::new(rawlua, state);
10501040
let func = &*(*upvalue).data;
1051-
10521041
func(rawlua, nargs)
10531042
})
10541043
}

0 commit comments

Comments
 (0)