Skip to content

Wasm threading fixes #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions godot-core/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,48 @@ pub unsafe trait ExtensionLibrary {
// Nothing by default.
}

/// Whether to override the Wasm binary filename used by your GDExtension which the library should expect at runtime. Return `None`
/// to use the default where gdext expects either `{YourCrate}.wasm` (default binary name emitted by Rust) or
/// `{YourCrate}.threads.wasm` (for builds producing separate single-threaded and multi-threaded binaries).
///
/// Upon exporting a game to the web, the library has to know at runtime the exact name of the `.wasm` binary file being used to load
/// each GDExtension. By default, Rust exports the binary as `cratename.wasm`, so that is the name checked by godot-rust by default.
///
/// However, if you need to rename that binary, you can make the library aware of the new binary name by returning
/// `Some("newname.wasm")` (don't forget to **include the `.wasm` extension**).
///
/// For example, to have two simultaneous versions, one supporting multi-threading and the other not, you could add a suffix to the
/// filename of the Wasm binary of the multi-threaded version in your build process. If you choose the suffix `.threads.wasm`,
/// you're in luck as godot-rust already accepts this suffix by default, but let's say you want to use a different suffix, such as
/// `-with-threads.wasm`. For this, you can have a `"nothreads"` feature which, when absent, should produce a suffixed binary,
/// which can be informed to gdext as follows:
///
/// ```no_run
/// # use godot::init::*;
/// struct MyExtension;
///
/// #[gdextension]
/// unsafe impl ExtensionLibrary for MyExtension {
/// fn override_wasm_binary() -> Option<&'static str> {
/// // Binary name unchanged ("mycrate.wasm") without thread support.
/// #[cfg(feature = "nothreads")]
/// return None;
///
/// // Tell gdext we add a custom suffix to the binary with thread support.
/// // Please note that this is not needed if "mycrate.threads.wasm" is used.
/// // (You could return `None` as well in that particular case.)
/// #[cfg(not(feature = "nothreads"))]
/// Some("mycrate-with-threads.wasm")
/// }
/// }
/// ```
/// Note that simply overriding this method won't change the name of the Wasm binary produced by Rust automatically: you'll still
/// have to rename it by yourself in your build process, as well as specify the updated binary name in your `.gdextension` file.
/// This is just to ensure gdext is aware of the new name given to the binary, avoiding runtime errors.
fn override_wasm_binary() -> Option<&'static str> {
None
}

/// Whether to enable hot reloading of this library. Return `None` to use the default behavior.
///
/// Enabling this will ensure that the library can be hot reloaded. If this is disabled then hot reloading may still work, but there is no
Expand Down
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(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
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(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
struct ScopedFunctionStack {
functions: Vec<*const dyn Fn() -> String>,
}

#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
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(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
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(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
return ERROR_CONTEXT_STACK.with(|cell| cell.borrow().get_last());

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

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

#[cfg(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
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(debug_assertions)]
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
ERROR_CONTEXT_STACK.with(|cell| cell.borrow_mut().pop_function());
result
}
Expand Down
1 change: 1 addition & 0 deletions godot-core/src/task/async_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn spawn(future: impl Future<Output = ()> + 'static) -> TaskHandle {
// By limiting async tasks to the main thread we can redirect all signal callbacks back to the main thread via `call_deferred`.
//
// Once thread-safe futures are possible the restriction can be lifted.
#[cfg(not(wasm_nothreads))]
assert!(
crate::init::is_main_thread(),
"godot_task() can only be used on the main thread"
Expand Down
66 changes: 46 additions & 20 deletions godot-macros/src/gdextension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pub fn attribute_gdextension(item: venial::Item) -> ParseResult<TokenStream> {
// host. See: https://github.com/rust-lang/rust/issues/42587
#[cfg(target_os = "emscripten")]
fn emscripten_preregistration() {
let pkg_name = env!("CARGO_PKG_NAME");
let wasm_binary = <#impl_ty as ::godot::init::ExtensionLibrary>::override_wasm_binary()
.map_or_else(
|| std::string::String::from("null"),
|bin| format!("'{}'", bin.replace("\\", "\\\\").replace("'", "\\'"))
);

// Module is documented here[1] by emscripten, so perhaps we can consider it a part
// of its public API? In any case for now we mutate global state directly in order
// to get things working.
Expand All @@ -69,34 +76,53 @@ pub fn attribute_gdextension(item: venial::Item) -> ParseResult<TokenStream> {
// involved, but I don't know what guarantees we have here.
//
// We should keep an eye out for these sorts of failures!
let script = std::ffi::CString::new(concat!(
"var pkgName = '", env!("CARGO_PKG_NAME"), "';", r#"
var libName = pkgName.replaceAll('-', '_') + '.wasm';
if (!(libName in LDSO.loadedLibsByName)) {
// Always print to console, even if the error is suppressed.
console.error(`godot-rust could not find the Wasm module '${libName}', needed to load the '${pkgName}' crate. Please ensure a file named '${libName}' exists in the game's web export files. This may require updating Wasm paths in the crate's corresponding '.gdextension' file, or just renaming the Wasm file to the correct name otherwise.`);
throw new Error(`Wasm module '${libName}' not found. Check the console for more information.`);
}
let script = format!(
r#"var pkgName = '{pkg_name}';
var wasmBinary = {wasm_binary};
if (wasmBinary === null) {{
var snakePkgName = pkgName.replaceAll('-', '_');
var normalLibName = snakePkgName + '.wasm';
var threadedLibName = snakePkgName + '.threads.wasm';
if (normalLibName in LDSO.loadedLibsByName) {{
var libName = normalLibName;
}} else if (threadedLibName in LDSO.loadedLibsByName) {{
var libName = threadedLibName;
}} else {{
// Always print to console, even if the error is suppressed.
console.error(`godot-rust could not find the Wasm module '${{normalLibName}}' nor '${{threadedLibName}}', one of which is needed by default to load the '${{pkgName}}' crate. This indicates its '.wasm' binary file was renamed to an unexpected name.\n\nPlease ensure its Wasm binary file has one of those names in the game's web export files. This may require updating Wasm paths in the crate's corresponding '.gdextension' file, or just renaming the Wasm file to one of the expected names otherwise.\n\nIf that GDExtension uses a different Wasm filename, please ensure it informs this new name to godot-rust by returning 'Some("newname.wasm")' from 'ExtensionLibrary::override_wasm_binary'.`);
throw new Error(`Wasm module '${{normalLibName}}' not found. Check the console for more information.`);
}}
}} else if (!wasmBinary.endsWith(".wasm")) {{
console.error(`godot-rust received an invalid Wasm binary name ('${{wasmBinary}}') from crate '${{pkgName}}', as the '.wasm' extension was missing.\n\nPlease ensure the 'ExtensionLibrary::override_wasm_binary' function for that GDExtension always returns a filename with the '.wasm' extension and try again.`);
throw new Error(`Invalid Wasm module '${{wasmBinary}}' (missing '.wasm' extension). Check the console for more information.`);
}} else if (wasmBinary in LDSO.loadedLibsByName) {{
var libName = wasmBinary;
}} else {{
console.error(`godot-rust could not find the Wasm module '${{wasmBinary}}', needed to load the '${{pkgName}}' crate. This indicates its '.wasm' binary file was renamed to an unexpected name.\n\nPlease ensure its Wasm binary file is named '${{wasmBinary}}' in the game's web export files. This may require updating Wasm paths in the crate's corresponding '.gdextension' file, or just renaming the Wasm file to the expected name otherwise.`);
throw new Error(`Wasm module '${{wasmBinary}}' not found. Check the console for more information.`);
}}
var dso = LDSO.loadedLibsByName[libName];
// This property was renamed as of emscripten 3.1.34
var dso_exports = "module" in dso ? dso["module"] : dso["exports"];
var registrants = [];
for (sym in dso_exports) {
if (sym.startsWith("dynCall_")) {
if (!(sym in Module)) {
console.log(`Patching Module with ${sym}`);
for (sym in dso_exports) {{
if (sym.startsWith("dynCall_")) {{
if (!(sym in Module)) {{
console.log(`Patching Module with ${{sym}}`);
Module[sym] = dso_exports[sym];
}
} else if (sym.startsWith("__godot_rust_registrant_")) {
}}
}} else if (sym.startsWith("__godot_rust_registrant_")) {{
registrants.push(sym);
}
}
for (sym of registrants) {
console.log(`Running registrant ${sym}`);
}}
}}
for (sym of registrants) {{
console.log(`Running registrant ${{sym}}`);
dso_exports[sym]();
}
}}
console.log("Added", registrants.length, "plugins to registry!");
"#)).expect("Unable to create CString from script");
"#);

let script = std::ffi::CString::new(script).expect("Unable to create CString from script");

extern "C" { fn emscripten_run_script(script: *const std::ffi::c_char); }
unsafe { emscripten_run_script(script.as_ptr()); }
Expand Down
Loading