Skip to content

Commit

Permalink
Expand emscripten support to importing JS closures
Browse files Browse the repository at this point in the history
Notably, to work with emscripten, the emitted js has explicit
dependencies listed for the imports.
  • Loading branch information
google-yfyang committed Feb 21, 2025
1 parent 31e00db commit 92e51fd
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 83 deletions.
8 changes: 8 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
[target.'cfg(target_arch = "wasm32")']
runner = 'cargo run -p wasm-bindgen-cli --bin wasm-bindgen-test-runner --'

[build]
[target.'cfg(all(target_arch = "wasm32", target_os = "emscripten"))']
rustflags = [
"-Cllvm-args=-enable-emscripten-cxx-exceptions=0",
"-Clink-arg=-Wno-undefined",
"-Crelocation-model=static",
]
1 change: 1 addition & 0 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tempfile = "3.0"
walrus = { version = "0.23", features = ['parallel'] }
regex = "1"
wasm-bindgen-externref-xform = { path = '../externref-xform', version = '=0.2.100' }
wasm-bindgen-multi-value-xform = { path = '../multi-value-xform', version = '=0.2.100' }
wasm-bindgen-shared = { path = "../shared", version = '=0.2.100' }
Expand Down
9 changes: 7 additions & 2 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl<'a, 'b> Builder<'a, 'b> {
debug_name: &str,
ret_ty_override: &Option<String>,
ret_desc: &Option<String>,
import_deps: &mut Vec<String>,
) -> Result<JsFunction, Error> {
if self
.cx
Expand Down Expand Up @@ -194,6 +195,7 @@ impl<'a, 'b> Builder<'a, 'b> {
&instr.instr,
&mut self.log_error,
&self.constructor,
import_deps,
)?;
}

Expand Down Expand Up @@ -727,6 +729,7 @@ fn instruction(
instr: &Instruction,
log_error: &mut bool,
constructor: &Option<String>,
import_deps: &mut Vec<String>,
) -> Result<(), Error> {
fn wasm_to_string_enum(name: &str, index: &str) -> String {
// e.g. ["a","b","c"][someIndex]
Expand Down Expand Up @@ -807,7 +810,7 @@ fn instruction(
}

// Call the function through an export of the underlying module.
let call = invoc.invoke(js.cx, &args, &mut js.prelude, log_error)?;
let call = invoc.invoke(js.cx, &args, &mut js.prelude, log_error, import_deps)?;

// And then figure out how to actually handle where the call
// happens. This is pretty conditional depending on the number of
Expand Down Expand Up @@ -1637,6 +1640,7 @@ impl Invocation {
args: &[String],
prelude: &mut String,
log_error: &mut bool,
import_deps: &mut Vec<String>,
) -> Result<String, Error> {
match self {
Invocation::Core { id, .. } => {
Expand All @@ -1656,7 +1660,8 @@ impl Invocation {
if cx.import_never_log_error(import) {
*log_error = false;
}
cx.invoke_import(import, kind, args, variadic, prelude)
let ret = cx.invoke_import(import, kind, args, variadic, prelude, import_deps);
return ret
}
}
}
Expand Down
Loading

0 comments on commit 92e51fd

Please sign in to comment.