Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions godot-core/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use sys::out;

#[cfg(feature = "trace")]
pub use crate::meta::trace;
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
use std::cell::RefCell;

use crate::global::godot_error;
Expand Down Expand Up @@ -321,12 +321,12 @@ pub(crate) fn has_error_print_level(level: u8) -> bool {
/// Internal type used to store context information for debug purposes. Debug context is stored on the thread-local
/// ERROR_CONTEXT_STACK, which can later be used to retrieve the current context in the event of a panic. This value
/// probably shouldn't be used directly; use ['get_gdext_panic_context()'](get_gdext_panic_context) instead.
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
struct ScopedFunctionStack {
functions: Vec<*const dyn Fn() -> String>,
}

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
impl ScopedFunctionStack {
/// # Safety
/// Function must be removed (using [`pop_function()`](Self::pop_function)) before lifetime is invalidated.
Expand All @@ -351,7 +351,7 @@ impl ScopedFunctionStack {
}
}

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
thread_local! {
static ERROR_CONTEXT_STACK: RefCell<ScopedFunctionStack> = const {
RefCell::new(ScopedFunctionStack { functions: Vec::new() })
Expand All @@ -360,10 +360,10 @@ thread_local! {

// Value may return `None`, even from panic hook, if called from a non-Godot thread.
pub fn get_gdext_panic_context() -> Option<String> {
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
return ERROR_CONTEXT_STACK.with(|cell| cell.borrow().get_last());

#[cfg(not(all(debug_assertions, not(wasm_nothreads))))]
#[cfg(not(debug_assertions))]
None
}

Expand All @@ -378,10 +378,10 @@ where
E: Fn() -> String,
F: FnOnce() -> R + std::panic::UnwindSafe,
{
#[cfg(not(all(debug_assertions, not(wasm_nothreads))))]
let _ = error_context; // Unused in Release or `wasm_nothreads` builds.
#[cfg(not(debug_assertions))]
let _ = error_context; // Unused in Release.

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
ERROR_CONTEXT_STACK.with(|cell| unsafe {
// SAFETY: &error_context is valid for lifetime of function, and is removed from LAST_ERROR_CONTEXT before end of function.
cell.borrow_mut().push_function(&error_context)
Expand All @@ -390,7 +390,7 @@ where
let result =
std::panic::catch_unwind(code).map_err(|payload| extract_panic_message(payload.as_ref()));

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
ERROR_CONTEXT_STACK.with(|cell| cell.borrow_mut().pop_function());
result
}
Expand Down
Loading