Skip to content

Commit

Permalink
Get rid of the circular dependency when targeting ES modules (#3152)
Browse files Browse the repository at this point in the history
* Get rid of the circular dependency when targeting ES modules

Fixes #3102
Fixes #3149

I've changed the `*_bg.js` file to not import from the wasm module, eliminating the circular dependency between them.

It begins with an undefined `let wasm` which gets initialized by the user-facing JS file before calling `__wbindgen_start`, by calling an exported function `__wbg_set_wasm` that sets `wasm`.

* fmt

* Tweak formatting & update reference tests
  • Loading branch information
Liamolucko authored Nov 17, 2022
1 parent bc4f1c3 commit 721c86c
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 25 deletions.
36 changes: 22 additions & 14 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,6 @@ impl<'a> Context<'a> {
| OutputMode::Node {
experimental_modules: true,
} => {
imports.push_str(&format!(
"import * as wasm from './{}_bg.wasm';\n",
module_name
));
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
let import = self.module.imports.get_mut(*id);
import.module = format!("./{}_bg.js", module_name);
Expand All @@ -459,6 +455,15 @@ impl<'a> Context<'a> {
}
}

self.imports_post.push_str(
"\
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
",
);

if needs_manual_start {
start = Some("\nwasm.__wbindgen_start();\n".to_string());
}
Expand Down Expand Up @@ -495,20 +500,23 @@ impl<'a> Context<'a> {
!self.config.mode.uses_es_modules() || js.is_empty(),
"ES modules require imports to be at the start of the file"
);
js.push_str(&imports);
js.push_str("\n");
js.push_str(&self.imports_post);
js.push_str("\n");

let mut push_with_newline = |s| {
js.push_str(s);
if !s.is_empty() {
js.push('\n');
}
};

push_with_newline(&imports);
push_with_newline(&self.imports_post);

// Emit all our exports from this module
js.push_str(&self.globals);
js.push_str("\n");
push_with_newline(&self.globals);

// Generate the initialization glue, if there was any
js.push_str(&init_js);
js.push_str("\n");
js.push_str(&footer);
js.push_str("\n");
push_with_newline(&init_js);
push_with_newline(&footer);
if self.config.mode.no_modules() {
js.push_str("})();\n");
}
Expand Down
7 changes: 5 additions & 2 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,11 @@ impl Output {
write(
&js_path,
format!(
"import * as wasm from \"./{}.wasm\";\nexport * from \"./{}\";{}",
wasm_name, js_name, start
"import * as wasm from \"./{wasm_name}.wasm\";
import {{ __wbg_set_wasm }} from \"./{js_name}\";
__wbg_set_wasm(wasm);
export * from \"./{js_name}\";
{start}"
),
)?;

Expand Down
5 changes: 4 additions & 1 deletion crates/cli/tests/reference/add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}

/**
* @param {number} a
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/reference/anyref-empty.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

Expand Down
5 changes: 4 additions & 1 deletion crates/cli/tests/reference/anyref-nop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}

/**
*/
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/tests/reference/empty.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}

6 changes: 5 additions & 1 deletion crates/cli/tests/reference/import-catch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


const heap = new Array(32).fill(undefined);

Expand Down
5 changes: 4 additions & 1 deletion crates/cli/tests/reference/nop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}

/**
*/
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/reference/result-string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


const heap = new Array(32).fill(undefined);

Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as wasm from './reference_test_bg.wasm';
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

Expand Down

0 comments on commit 721c86c

Please sign in to comment.