Can.. can I do this? Is this a thing that actually works?

Drastic times and all that.
This commit is contained in:
kyren 2018-02-16 22:00:58 -05:00
parent 73de52dcce
commit dec360f78f
6 changed files with 30 additions and 23 deletions

View File

@ -22,12 +22,6 @@ default = ["builtin-lua"]
# * LUA_NUMBER as double # * LUA_NUMBER as double
# * LUA_EXTRASPACE is sizeof(void*) # * LUA_EXTRASPACE is sizeof(void*)
builtin-lua = ["gcc"] 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] [dependencies]
libc = { version = "0.2" } libc = { version = "0.2" }

View File

@ -1,16 +1,29 @@
#[cfg(feature = "builtin-lua")] #[cfg(feature = "builtin-lua")]
extern crate gcc; extern crate gcc;
fn main() {
#[cfg(feature = "builtin-lua")]
{
use std::env; use std::env;
let mut config = gcc::Build::new(); fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS"); let target_os = env::var("CARGO_CFG_TARGET_OS");
let target_family = env::var("CARGO_CFG_TARGET_FAMILY"); 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")]
{
let mut config = gcc::Build::new();
if target_os == Ok("linux".to_string()) { if target_os == Ok("linux".to_string()) {
config.define("LUA_USE_LINUX", None); config.define("LUA_USE_LINUX", None);
} else if target_os == Ok("macos".to_string()) { } else if target_os == Ok("macos".to_string()) {

View File

@ -122,7 +122,7 @@ impl<'lua> Function<'lua> {
/// # } /// # }
/// ``` /// ```
pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>> { pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>> {
#[cfg_attr(feature = "unwind", unwind)] #[cfg_attr(unwind, unwind)]
unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int { unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int {
let nargs = ffi::lua_gettop(state); let nargs = ffi::lua_gettop(state);
let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(2)) as c_int; let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(2)) as c_int;

View File

@ -1,4 +1,4 @@
#![cfg_attr(feature = "unwind", feature(unwind_attributes))] #![cfg_attr(unwind, feature(unwind_attributes))]
//! # High-level bindings to Lua //! # High-level bindings to Lua
//! //!

View File

@ -759,7 +759,7 @@ impl Lua {
pub(crate) unsafe fn userdata_metatable<T: UserData>(&self) -> Result<c_int> { pub(crate) unsafe fn userdata_metatable<T: UserData>(&self) -> Result<c_int> {
// Used if both an __index metamethod is set and regular methods, checks methods table // Used if both an __index metamethod is set and regular methods, checks methods table
// first, then __index metamethod. // 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 { unsafe extern "C" fn meta_index_impl(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); ffi::luaL_checkstack(state, 2, ptr::null());
@ -995,7 +995,7 @@ impl Lua {
&'lua self, &'lua self,
func: Callback<'callback, 'static>, func: Callback<'callback, 'static>,
) -> Result<Function<'lua>> { ) -> Result<Function<'lua>> {
#[cfg_attr(feature = "unwind", unwind)] #[cfg_attr(unwind, unwind)]
unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int { unsafe extern "C" fn callback_call_impl(state: *mut ffi::lua_State) -> c_int {
callback_error(state, || { callback_error(state, || {
let lua = Lua { let lua = Lua {

View File

@ -121,7 +121,7 @@ where
nresults: c_int, nresults: c_int,
} }
#[cfg_attr(feature = "unwind", unwind)] #[cfg_attr(unwind, unwind)]
unsafe extern "C" fn do_call<F, R>(state: *mut ffi::lua_State) -> c_int unsafe extern "C" fn do_call<F, R>(state: *mut ffi::lua_State) -> c_int
where where
F: FnOnce(*mut ffi::lua_State) -> R, F: FnOnce(*mut ffi::lua_State) -> R,
@ -272,7 +272,7 @@ pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
ptr::read(ud) ptr::read(ud)
} }
#[cfg_attr(feature = "unwind", unwind)] #[cfg_attr(unwind, unwind)]
pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int { pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int {
callback_error(state, || { callback_error(state, || {
take_userdata::<T>(state); take_userdata::<T>(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 // 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 // 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. // 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 { pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); 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 // 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 { pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); 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 // 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 { 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 { unsafe extern "C" fn xpcall_msgh(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); 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 { unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int {
static ERROR_METATABLE_REGISTRY_KEY: u8 = 0; 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 { unsafe extern "C" fn error_tostring(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); 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 { unsafe fn get_destructed_userdata_metatable(state: *mut ffi::lua_State) -> c_int {
static DESTRUCTED_USERDATA_METATABLE: u8 = 0; 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 { unsafe extern "C" fn destructed_error(state: *mut ffi::lua_State) -> c_int {
ffi::luaL_checkstack(state, 2, ptr::null()); ffi::luaL_checkstack(state, 2, ptr::null());
push_wrapped_error(state, Error::CallbackDestructed); push_wrapped_error(state, Error::CallbackDestructed);