Skip to content

Commit f0186d1

Browse files
committed
Provisional "fix" for #71. Requires nightly :(
1 parent 6b46e8a commit f0186d1

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ default = ["builtin-lua"]
2222
# * LUA_NUMBER as double
2323
# * LUA_EXTRASPACE is sizeof(void*)
2424
builtin-lua = ["gcc"]
25+
# Error handling is completely broken on windows with
26+
# https://github.com/rust-lang/rust/pull/46833 merged, and this includes stable
27+
# rustc 1.24.0+. This feature fixes error handling on windows, but requires
28+
# nightly! See https://github.com/rust-lang/rust/issues/48251 and
29+
# https://github.com/chucklefish/rlua/issues/71 for details.
30+
unwind = []
2531

2632
[dependencies]
2733
libc = { version = "0.2" }

src/function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<'lua> Function<'lua> {
122122
/// # }
123123
/// ```
124124
pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>> {
125+
#[cfg_attr(feature = "unwind", unwind)]
125126
unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int {
126127
let nargs = ffi::lua_gettop(state);
127128
let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(2)) as c_int;

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(feature = "unwind", feature(unwind_attributes))]
2+
13
//! # High-level bindings to Lua
24
//!
35
//! The `rlua` crate provides safe high-level bindings to the [Lua programming language].

src/lua.rs

+2
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ impl Lua {
759759
pub(crate) unsafe fn userdata_metatable<T: UserData>(&self) -> Result<c_int> {
760760
// Used if both an __index metamethod is set and regular methods, checks methods table
761761
// first, then __index metamethod.
762+
#[cfg_attr(feature = "unwind", unwind)]
762763
unsafe extern "C" fn meta_index_impl(state: *mut ffi::lua_State) -> c_int {
763764
ffi::luaL_checkstack(state, 2, ptr::null());
764765

@@ -994,6 +995,7 @@ impl Lua {
994995
&'lua self,
995996
func: Callback<'callback, 'static>,
996997
) -> Result<Function<'lua>> {
998+
#[cfg_attr(feature = "unwind", unwind)]
997999
unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int {
9981000
callback_error(state, || {
9991001
let lua = Lua {

src/util.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105
// limited lua stack. `nargs` and `nresults` are similar to the parameters of `lua_pcall`, but the
106106
// given function return type is not the return value count, instead the inner function return
107107
// values are assumed to match the `nresults` param. Internally uses 3 extra stack spaces, and does
108-
// not call checkstack.
108+
// not call checkstack. Provided function must *not* panic.
109109
pub unsafe fn protect_lua_call<F, R>(
110110
state: *mut ffi::lua_State,
111111
nargs: c_int,
@@ -121,6 +121,7 @@ where
121121
nresults: c_int,
122122
}
123123

124+
#[cfg_attr(feature = "unwind", unwind)]
124125
unsafe extern "C" fn do_call<F, R>(state: *mut ffi::lua_State) -> c_int
125126
where
126127
F: FnOnce(*mut ffi::lua_State) -> R,
@@ -271,6 +272,7 @@ pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
271272
ptr::read(ud)
272273
}
273274

275+
#[cfg_attr(feature = "unwind", unwind)]
274276
pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int {
275277
callback_error(state, || {
276278
take_userdata::<T>(state);
@@ -291,11 +293,13 @@ where
291293
Ok(Err(err)) => {
292294
ffi::luaL_checkstack(state, 2, ptr::null());
293295
push_wrapped_error(state, err);
296+
println!("erroring...");
294297
ffi::lua_error(state)
295298
}
296299
Err(p) => {
297300
ffi::luaL_checkstack(state, 2, ptr::null());
298301
push_wrapped_panic(state, p);
302+
println!("erroring...");
299303
ffi::lua_error(state)
300304
}
301305
}
@@ -304,6 +308,7 @@ where
304308
// Takes an error at the top of the stack, and if it is a WrappedError, converts it to an
305309
// Error::CallbackError with a traceback, if it is some lua type, prints the error along with a
306310
// traceback, and if it is a WrappedPanic, does not modify it.
311+
#[cfg_attr(feature = "unwind", unwind)]
307312
pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
308313
ffi::luaL_checkstack(state, 2, ptr::null());
309314

@@ -334,6 +339,7 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
334339
}
335340

336341
// A variant of pcall that does not allow lua to catch panic errors from callback_error
342+
#[cfg_attr(feature = "unwind", unwind)]
337343
pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int {
338344
ffi::luaL_checkstack(state, 2, ptr::null());
339345

@@ -356,7 +362,9 @@ pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int {
356362
}
357363

358364
// A variant of xpcall that does not allow lua to catch panic errors from callback_error
365+
#[cfg_attr(feature = "unwind", unwind)]
359366
pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int {
367+
#[cfg_attr(feature = "unwind", unwind)]
360368
unsafe extern "C" fn xpcall_msgh(state: *mut ffi::lua_State) -> c_int {
361369
ffi::luaL_checkstack(state, 2, ptr::null());
362370

@@ -504,6 +512,7 @@ unsafe fn is_wrapped_panic(state: *mut ffi::lua_State, index: c_int) -> bool {
504512
unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int {
505513
static ERROR_METATABLE_REGISTRY_KEY: u8 = 0;
506514

515+
#[cfg_attr(feature = "unwind", unwind)]
507516
unsafe extern "C" fn error_tostring(state: *mut ffi::lua_State) -> c_int {
508517
ffi::luaL_checkstack(state, 2, ptr::null());
509518

@@ -605,6 +614,7 @@ unsafe fn get_panic_metatable(state: *mut ffi::lua_State) -> c_int {
605614
unsafe fn get_destructed_userdata_metatable(state: *mut ffi::lua_State) -> c_int {
606615
static DESTRUCTED_USERDATA_METATABLE: u8 = 0;
607616

617+
#[cfg_attr(feature = "unwind", unwind)]
608618
unsafe extern "C" fn destructed_error(state: *mut ffi::lua_State) -> c_int {
609619
ffi::luaL_checkstack(state, 2, ptr::null());
610620
push_wrapped_error(state, Error::CallbackDestructed);

0 commit comments

Comments
 (0)