Try different approach for errors handling.
Instead of convering error to CallbackError in error message handler, do it earlier at callback_error stage. Better fix for #44.
This commit is contained in:
parent
01714d2510
commit
fe39ae09bf
|
@ -32,8 +32,7 @@ const void *MLUA_WRAPPED_ERROR_KEY = NULL;
|
||||||
const void *MLUA_WRAPPED_PANIC_KEY = NULL;
|
const void *MLUA_WRAPPED_PANIC_KEY = NULL;
|
||||||
|
|
||||||
extern void wrapped_error_traceback(lua_State *L, int error_idx,
|
extern void wrapped_error_traceback(lua_State *L, int error_idx,
|
||||||
int traceback_idx,
|
int traceback_idx);
|
||||||
int convert_to_callback_error);
|
|
||||||
|
|
||||||
extern int mlua_hook_proc(lua_State *L, lua_Debug *ar);
|
extern int mlua_hook_proc(lua_State *L, lua_Debug *ar);
|
||||||
|
|
||||||
|
@ -76,9 +75,12 @@ static int lua_call_rust(lua_State *L) {
|
||||||
if (ret == -1 /* WrappedError */) {
|
if (ret == -1 /* WrappedError */) {
|
||||||
if (lua_checkstack(L, LUA_TRACEBACK_STACK) != 0) {
|
if (lua_checkstack(L, LUA_TRACEBACK_STACK) != 0) {
|
||||||
luaL_traceback(L, L, NULL, 0);
|
luaL_traceback(L, L, NULL, 0);
|
||||||
// Attach traceback
|
// Convert to CallbackError and attach traceback
|
||||||
wrapped_error_traceback(L, -2, -1, 0);
|
wrapped_error_traceback(L, -2, -1);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
} else {
|
||||||
|
// Convert to CallbackError with error message as a traceback
|
||||||
|
wrapped_error_traceback(L, -1, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_error(L);
|
lua_error(L);
|
||||||
|
@ -92,7 +94,18 @@ void lua_call_mlua_hook_proc(lua_State *L, lua_Debug *ar) {
|
||||||
lua_newuserdata(L, max(MLUA_WRAPPED_ERROR_SIZE, MLUA_WRAPPED_PANIC_SIZE));
|
lua_newuserdata(L, max(MLUA_WRAPPED_ERROR_SIZE, MLUA_WRAPPED_PANIC_SIZE));
|
||||||
lua_rotate(L, 1, 1);
|
lua_rotate(L, 1, 1);
|
||||||
int ret = mlua_hook_proc(L, ar);
|
int ret = mlua_hook_proc(L, ar);
|
||||||
if (ret == -1) {
|
if (ret < 0) {
|
||||||
|
if (ret == -1 /* WrappedError */) {
|
||||||
|
if (lua_checkstack(L, LUA_TRACEBACK_STACK) != 0) {
|
||||||
|
luaL_traceback(L, L, NULL, 0);
|
||||||
|
// Convert to CallbackError and attach traceback
|
||||||
|
wrapped_error_traceback(L, -2, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
} else {
|
||||||
|
// Convert to CallbackError with error message as a traceback
|
||||||
|
wrapped_error_traceback(L, -1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
lua_error(L);
|
lua_error(L);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -420,19 +433,16 @@ int error_traceback(lua_State *state) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_wrapped_struct(state, -1, MLUA_WRAPPED_ERROR_KEY) != 0) {
|
if (MLUA_WRAPPED_PANIC_KEY == NULL ||
|
||||||
// Convert to CallbackError
|
is_wrapped_struct(state, -1, MLUA_WRAPPED_PANIC_KEY) ||
|
||||||
wrapped_error_traceback(state, -1, 0, 1);
|
is_wrapped_struct(state, -1, MLUA_WRAPPED_ERROR_KEY)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MLUA_WRAPPED_PANIC_KEY != NULL &&
|
const char *s = luaL_tolstring(state, -1, NULL);
|
||||||
!is_wrapped_struct(state, -1, MLUA_WRAPPED_PANIC_KEY)) {
|
if (lua_checkstack(state, LUA_TRACEBACK_STACK) != 0) {
|
||||||
const char *s = luaL_tolstring(state, -1, NULL);
|
luaL_traceback(state, state, s, 1);
|
||||||
if (lua_checkstack(state, LUA_TRACEBACK_STACK) != 0) {
|
lua_remove(state, -2);
|
||||||
luaL_traceback(state, state, s, 1);
|
|
||||||
lua_remove(state, -2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
44
src/util.rs
44
src/util.rs
|
@ -313,7 +313,7 @@ where
|
||||||
Ok(Err(err)) => {
|
Ok(Err(err)) => {
|
||||||
ffi::lua_settop(state, 1);
|
ffi::lua_settop(state, 1);
|
||||||
let error_ud = ffi::lua_touserdata(state, 1);
|
let error_ud = ffi::lua_touserdata(state, 1);
|
||||||
ptr::write(error_ud as *mut WrappedError, WrappedError(err, None));
|
ptr::write(error_ud as *mut WrappedError, WrappedError(err));
|
||||||
get_gc_metatable_for::<WrappedError>(state);
|
get_gc_metatable_for::<WrappedError>(state);
|
||||||
ffi::lua_setmetatable(state, -2);
|
ffi::lua_setmetatable(state, -2);
|
||||||
-1
|
-1
|
||||||
|
@ -338,23 +338,17 @@ pub unsafe extern "C" fn wrapped_error_traceback(
|
||||||
state: *mut ffi::lua_State,
|
state: *mut ffi::lua_State,
|
||||||
error_idx: c_int,
|
error_idx: c_int,
|
||||||
traceback_idx: c_int,
|
traceback_idx: c_int,
|
||||||
convert_to_callback_error: c_int,
|
|
||||||
) {
|
) {
|
||||||
let wrapped_error = mlua_expect!(
|
let wrapped_error = mlua_expect!(
|
||||||
get_gc_userdata::<WrappedError>(state, error_idx).as_mut(),
|
get_gc_userdata::<WrappedError>(state, error_idx).as_mut(),
|
||||||
"cannot get <WrappedError>"
|
"cannot get <WrappedError>"
|
||||||
);
|
);
|
||||||
|
let traceback = match traceback_idx {
|
||||||
if convert_to_callback_error != 0 {
|
0 => "<not enough stack space for traceback>".to_string(),
|
||||||
let traceback = wrapped_error
|
_ => to_string(state, traceback_idx),
|
||||||
.1
|
};
|
||||||
.take()
|
let cause = Arc::new(wrapped_error.0.clone());
|
||||||
.unwrap_or_else(|| "<not enough stack space for traceback>".into());
|
wrapped_error.0 = Error::CallbackError { traceback, cause };
|
||||||
let cause = Arc::new(wrapped_error.0.clone());
|
|
||||||
wrapped_error.0 = Error::CallbackError { traceback, cause };
|
|
||||||
} else {
|
|
||||||
wrapped_error.1 = Some(to_string(state, traceback_idx));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns Lua main thread for Lua >= 5.2 or checks that the passed thread is main for Lua 5.1.
|
// Returns Lua main thread for Lua >= 5.2 or checks that the passed thread is main for Lua 5.1.
|
||||||
|
@ -384,7 +378,7 @@ pub unsafe fn get_main_state(state: *mut ffi::lua_State) -> Option<*mut ffi::lua
|
||||||
// Uses 2 stack spaces and does not call checkstack.
|
// Uses 2 stack spaces and does not call checkstack.
|
||||||
pub unsafe fn push_wrapped_error(state: *mut ffi::lua_State, err: Error) -> Result<()> {
|
pub unsafe fn push_wrapped_error(state: *mut ffi::lua_State, err: Error) -> Result<()> {
|
||||||
let error_ud = ffi::safe::lua_newwrappederror(state)? as *mut WrappedError;
|
let error_ud = ffi::safe::lua_newwrappederror(state)? as *mut WrappedError;
|
||||||
ptr::write(error_ud, WrappedError(err, None));
|
ptr::write(error_ud, WrappedError(err));
|
||||||
get_gc_metatable_for::<WrappedError>(state);
|
get_gc_metatable_for::<WrappedError>(state);
|
||||||
ffi::lua_setmetatable(state, -2);
|
ffi::lua_setmetatable(state, -2);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -457,8 +451,7 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(*const
|
||||||
callback_error(state, |_| {
|
callback_error(state, |_| {
|
||||||
check_stack(state, 3)?;
|
check_stack(state, 3)?;
|
||||||
|
|
||||||
let wrapped_error = get_gc_userdata::<WrappedError>(state, -1).as_ref();
|
let err_buf = if let Some(error) = get_wrapped_error(state, -1).as_ref() {
|
||||||
let err_buf = if let Some(error) = wrapped_error {
|
|
||||||
let err_buf_key = &ERROR_PRINT_BUFFER_KEY as *const u8 as *const c_void;
|
let err_buf_key = &ERROR_PRINT_BUFFER_KEY as *const u8 as *const c_void;
|
||||||
ffi::lua_rawgetp(state, ffi::LUA_REGISTRYINDEX, err_buf_key);
|
ffi::lua_rawgetp(state, ffi::LUA_REGISTRYINDEX, err_buf_key);
|
||||||
let err_buf = ffi::lua_touserdata(state, -1) as *mut String;
|
let err_buf = ffi::lua_touserdata(state, -1) as *mut String;
|
||||||
|
@ -468,20 +461,9 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(*const
|
||||||
// Depending on how the API is used and what error types scripts are given, it may
|
// Depending on how the API is used and what error types scripts are given, it may
|
||||||
// be possible to make this consume arbitrary amounts of memory (for example, some
|
// be possible to make this consume arbitrary amounts of memory (for example, some
|
||||||
// kind of recursive error structure?)
|
// kind of recursive error structure?)
|
||||||
let _ = write!(&mut (*err_buf), "{}", error.0);
|
let _ = write!(&mut (*err_buf), "{}", error);
|
||||||
match error {
|
|
||||||
WrappedError(Error::CallbackError { .. }, _) => {}
|
|
||||||
// This is a workaround to avoid having duplicate traceback for string errors
|
|
||||||
// with attached traceback at the `error_traceback` (message handler) stage.
|
|
||||||
WrappedError(Error::RuntimeError(ref err), _)
|
|
||||||
if err.contains("\nstack traceback:\n") => {}
|
|
||||||
WrappedError(_, Some(ref traceback)) => {
|
|
||||||
let _ = write!(&mut (*err_buf), "\n{}", traceback);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
// Find first two sources that caused the error
|
// Find first two sources that caused the error
|
||||||
let mut source1 = error.0.source();
|
let mut source1 = error.source();
|
||||||
let mut source0 = source1.and_then(|s| s.source());
|
let mut source0 = source1.and_then(|s| s.source());
|
||||||
while let Some(source) = source0.and_then(|s| s.source()) {
|
while let Some(source) = source0.and_then(|s| s.source()) {
|
||||||
source1 = source0;
|
source1 = source0;
|
||||||
|
@ -557,7 +539,7 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(*const
|
||||||
callback_error(state, |_| {
|
callback_error(state, |_| {
|
||||||
check_stack(state, 2)?;
|
check_stack(state, 2)?;
|
||||||
let error_ud = ffi::safe::lua_newwrappederror(state)? as *mut WrappedError;
|
let error_ud = ffi::safe::lua_newwrappederror(state)? as *mut WrappedError;
|
||||||
ptr::write(error_ud, WrappedError(Error::CallbackDestructed, None));
|
ptr::write(error_ud, WrappedError(Error::CallbackDestructed));
|
||||||
get_gc_metatable_for::<WrappedError>(state);
|
get_gc_metatable_for::<WrappedError>(state);
|
||||||
ffi::lua_setmetatable(state, -2);
|
ffi::lua_setmetatable(state, -2);
|
||||||
Ok(-1) // to trigger lua_error
|
Ok(-1) // to trigger lua_error
|
||||||
|
@ -621,7 +603,7 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(*const
|
||||||
Ok((wrapped_error_key, wrapped_panic_key))
|
Ok((wrapped_error_key, wrapped_panic_key))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct WrappedError(pub Error, pub Option<String>);
|
pub(crate) struct WrappedError(pub Error);
|
||||||
pub(crate) struct WrappedPanic(pub Option<Box<dyn Any + Send + 'static>>);
|
pub(crate) struct WrappedPanic(pub Option<Box<dyn Any + Send + 'static>>);
|
||||||
|
|
||||||
// Converts the given lua value to a string in a reasonable format without causing a Lua error or
|
// Converts the given lua value to a string in a reasonable format without causing a Lua error or
|
||||||
|
|
Loading…
Reference in New Issue