From dec360f78f156e54a4b4a348e781aedac310b299 Mon Sep 17 00:00:00 2001 From: kyren Date: Fri, 16 Feb 2018 22:00:58 -0500 Subject: [PATCH] Can.. can I do this? Is this a thing that actually works? Drastic times and all that. --- Cargo.toml | 6 ------ build.rs | 23 ++++++++++++++++++----- src/function.rs | 2 +- src/lib.rs | 2 +- src/lua.rs | 4 ++-- src/util.rs | 16 ++++++++-------- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 374fa4c..4182d1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,12 +22,6 @@ default = ["builtin-lua"] # * LUA_NUMBER as double # * LUA_EXTRASPACE is sizeof(void*) builtin-lua = ["gcc"] -# Error handling is completely broken on windows with -# https://github.com/rust-lang/rust/pull/46833 merged, and this includes stable -# rustc 1.24.0+. This feature fixes error handling on windows, but requires -# nightly! See https://github.com/rust-lang/rust/issues/48251 and -# https://github.com/chucklefish/rlua/issues/71 for details. -unwind = [] [dependencies] libc = { version = "0.2" } diff --git a/build.rs b/build.rs index 6aa2863..43e5b19 100644 --- a/build.rs +++ b/build.rs @@ -1,16 +1,29 @@ #[cfg(feature = "builtin-lua")] extern crate gcc; +use std::env; + fn main() { + let target_os = env::var("CARGO_CFG_TARGET_OS"); + let target_family = env::var("CARGO_CFG_TARGET_FAMILY"); + + if target_family == Ok("windows".to_string()) { + // Error handling is completely broken on windows with + // https://github.com/rust-lang/rust/pull/46833 merged, and this includes stable rustc + // 1.24.0+. `#[unwind]` fixes error handling on windows, but requires nightly! This + // HORRIBLE HACK enables `#[unwind]` on stable rust by setting RUSTC_BOOTSTRAP=1 during + // build. This is very evil, don't do this kids. + // + // See https://github.com/rust-lang/rust/issues/48251 and + // https://github.com/chucklefish/rlua/issues/71 for more details. + println!("cargo:rustc-env=RUSTC_BOOTSTRAP=1"); + println!("cargo:rustc-cfg=unwind"); + } + #[cfg(feature = "builtin-lua")] { - use std::env; - let mut config = gcc::Build::new(); - let target_os = env::var("CARGO_CFG_TARGET_OS"); - let target_family = env::var("CARGO_CFG_TARGET_FAMILY"); - if target_os == Ok("linux".to_string()) { config.define("LUA_USE_LINUX", None); } else if target_os == Ok("macos".to_string()) { diff --git a/src/function.rs b/src/function.rs index 485a415..be4f396 100644 --- a/src/function.rs +++ b/src/function.rs @@ -122,7 +122,7 @@ impl<'lua> Function<'lua> { /// # } /// ``` pub fn bind>(&self, args: A) -> Result> { - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int { let nargs = ffi::lua_gettop(state); let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(2)) as c_int; diff --git a/src/lib.rs b/src/lib.rs index 0300bdd..e7e29bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "unwind", feature(unwind_attributes))] +#![cfg_attr(unwind, feature(unwind_attributes))] //! # High-level bindings to Lua //! diff --git a/src/lua.rs b/src/lua.rs index a044caf..16f18d0 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -759,7 +759,7 @@ impl Lua { pub(crate) unsafe fn userdata_metatable(&self) -> Result { // Used if both an __index metamethod is set and regular methods, checks methods table // first, then __index metamethod. - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn meta_index_impl(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); @@ -995,7 +995,7 @@ impl Lua { &'lua self, func: Callback<'callback, 'static>, ) -> Result> { - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int { callback_error(state, || { let lua = Lua { diff --git a/src/util.rs b/src/util.rs index af6a50d..99377fb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -121,7 +121,7 @@ where nresults: c_int, } - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn do_call(state: *mut ffi::lua_State) -> c_int where F: FnOnce(*mut ffi::lua_State) -> R, @@ -272,7 +272,7 @@ pub unsafe fn take_userdata(state: *mut ffi::lua_State) -> T { ptr::read(ud) } -#[cfg_attr(feature = "unwind", unwind)] +#[cfg_attr(unwind, unwind)] pub unsafe extern "C" fn userdata_destructor(state: *mut ffi::lua_State) -> c_int { callback_error(state, || { take_userdata::(state); @@ -306,7 +306,7 @@ where // Takes an error at the top of the stack, and if it is a WrappedError, converts it to an // Error::CallbackError with a traceback, if it is some lua type, prints the error along with a // traceback, and if it is a WrappedPanic, does not modify it. -#[cfg_attr(feature = "unwind", unwind)] +#[cfg_attr(unwind, unwind)] pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); @@ -337,7 +337,7 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int { } // A variant of pcall that does not allow lua to catch panic errors from callback_error -#[cfg_attr(feature = "unwind", unwind)] +#[cfg_attr(unwind, unwind)] pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); @@ -360,9 +360,9 @@ pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int { } // A variant of xpcall that does not allow lua to catch panic errors from callback_error -#[cfg_attr(feature = "unwind", unwind)] +#[cfg_attr(unwind, unwind)] pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int { - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn xpcall_msgh(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); @@ -510,7 +510,7 @@ unsafe fn is_wrapped_panic(state: *mut ffi::lua_State, index: c_int) -> bool { unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int { static ERROR_METATABLE_REGISTRY_KEY: u8 = 0; - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn error_tostring(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); @@ -612,7 +612,7 @@ unsafe fn get_panic_metatable(state: *mut ffi::lua_State) -> c_int { unsafe fn get_destructed_userdata_metatable(state: *mut ffi::lua_State) -> c_int { static DESTRUCTED_USERDATA_METATABLE: u8 = 0; - #[cfg_attr(feature = "unwind", unwind)] + #[cfg_attr(unwind, unwind)] unsafe extern "C" fn destructed_error(state: *mut ffi::lua_State) -> c_int { ffi::luaL_checkstack(state, 2, ptr::null()); push_wrapped_error(state, Error::CallbackDestructed);