Skip to content

Commit 0ee93f0

Browse files
committed
allow custom wasm binary name
1 parent 62a7381 commit 0ee93f0

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

godot-macros/src/gdextension.rs

+28-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn attribute_gdextension(item: venial::Item) -> ParseResult<TokenStream> {
2929
let mut parser = KvParser::parse_required(&drained_attributes, "gdextension", &impl_decl)?;
3030
let entry_point = parser.handle_ident("entry_point")?;
3131
let entry_symbol = parser.handle_ident("entry_symbol")?;
32+
let wasm_binary = parser.handle_expr("wasm_binary")?;
3233
parser.finish()?;
3334

3435
if entry_point.is_some() && entry_symbol.is_some() {
@@ -48,6 +49,13 @@ pub fn attribute_gdextension(item: venial::Item) -> ParseResult<TokenStream> {
4849
.or(entry_point)
4950
.unwrap_or_else(|| ident("gdext_rust_init"));
5051

52+
let wasm_binary = match wasm_binary {
53+
Some(wasm_binary) => {
54+
quote! { format!("'{}'", (&#wasm_binary as &str).replace("\\", "\\\\").replace("'", "\\'")) }
55+
}
56+
None => quote! { "snakePkgName + '.wasm'" },
57+
};
58+
5159
let impl_ty = &impl_decl.self_ty;
5260

5361
Ok(quote! {
@@ -69,34 +77,35 @@ pub fn attribute_gdextension(item: venial::Item) -> ParseResult<TokenStream> {
6977
// involved, but I don't know what guarantees we have here.
7078
//
7179
// We should keep an eye out for these sorts of failures!
72-
let script = std::ffi::CString::new(concat!(
73-
"var pkgName = '", env!("CARGO_PKG_NAME"), "';", r#"
74-
var libName = pkgName.replaceAll('-', '_') + '.wasm';
75-
if (!(libName in LDSO.loadedLibsByName)) {
80+
let script = std::ffi::CString::new(format!(
81+
r#"var pkgName = '{}';
82+
var snakePkgName = pkgName.replaceAll('-', '_');
83+
var libName = {};
84+
if (!(libName in LDSO.loadedLibsByName)) {{
7685
// Always print to console, even if the error is suppressed.
77-
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.`);
78-
throw new Error(`Wasm module '${libName}' not found. Check the console for more information.`);
79-
}
86+
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.`);
87+
throw new Error(`Wasm module '${{libName}}' not found. Check the console for more information.`);
88+
}}
8089
var dso = LDSO.loadedLibsByName[libName];
8190
// This property was renamed as of emscripten 3.1.34
8291
var dso_exports = "module" in dso ? dso["module"] : dso["exports"];
8392
var registrants = [];
84-
for (sym in dso_exports) {
85-
if (sym.startsWith("dynCall_")) {
86-
if (!(sym in Module)) {
87-
console.log(`Patching Module with ${sym}`);
93+
for (sym in dso_exports) {{
94+
if (sym.startsWith("dynCall_")) {{
95+
if (!(sym in Module)) {{
96+
console.log(`Patching Module with ${{sym}}`);
8897
Module[sym] = dso_exports[sym];
89-
}
90-
} else if (sym.startsWith("__godot_rust_registrant_")) {
98+
}}
99+
}} else if (sym.startsWith("__godot_rust_registrant_")) {{
91100
registrants.push(sym);
92-
}
93-
}
94-
for (sym of registrants) {
95-
console.log(`Running registrant ${sym}`);
101+
}}
102+
}}
103+
for (sym of registrants) {{
104+
console.log(`Running registrant ${{sym}}`);
96105
dso_exports[sym]();
97-
}
106+
}}
98107
console.log("Added", registrants.length, "plugins to registry!");
99-
"#)).expect("Unable to create CString from script");
108+
"#, env!("CARGO_PKG_NAME"), #wasm_binary)).expect("Unable to create CString from script");
100109

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

0 commit comments

Comments
 (0)