Skip to content

Commit fda6bb9

Browse files
authored
Work around for building with webpack5 (#2512)
* Use instead of * rewrite js files in wasm-bindgen-cli reference tests * fix formatting * fix typo * ensure function return * run cargo fmt
1 parent 0d911ea commit fda6bb9

File tree

6 files changed

+78
-79
lines changed

6 files changed

+78
-79
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,19 @@ impl<'a> Context<'a> {
419419
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
420420
let import = self.module.imports.get_mut(*id);
421421
import.module = format!("./{}_bg.js", module_name);
422-
footer.push_str("\nexport const ");
423-
footer.push_str(&import.name);
424-
footer.push_str(" = ");
425-
footer.push_str(js.trim());
426-
footer.push_str(";\n");
422+
if js.starts_with("function") {
423+
let body = &js[8..];
424+
footer.push_str("\nexport function ");
425+
footer.push_str(&import.name);
426+
footer.push_str(body.trim());
427+
footer.push_str(";\n");
428+
} else {
429+
footer.push_str("\nexport const ");
430+
footer.push_str(&import.name);
431+
footer.push_str(" = ");
432+
footer.push_str(js.trim());
433+
footer.push_str(";\n");
434+
}
427435
}
428436
if needs_manual_start {
429437
start = Some("\nwasm.__wbindgen_start();\n".to_string());
@@ -1738,17 +1746,14 @@ impl<'a> Context<'a> {
17381746
(Some(table), Some(alloc)) => {
17391747
let add = self.expose_add_to_externref_table(table, alloc)?;
17401748
self.global(&format!(
1741-
"
1742-
function handleError(f) {{
1743-
return function () {{
1744-
try {{
1745-
return f.apply(this, arguments);
1746-
1747-
}} catch (e) {{
1748-
const idx = {}(e);
1749-
wasm.{}(idx);
1750-
}}
1751-
}};
1749+
"\
1750+
function handleError(f, args) {{
1751+
try {{
1752+
return f.apply(this, args);
1753+
}} catch (e) {{
1754+
const idx = {}(e);
1755+
wasm.{}(idx);
1756+
}}
17521757
}}
17531758
",
17541759
add, store,
@@ -1757,16 +1762,13 @@ impl<'a> Context<'a> {
17571762
_ => {
17581763
self.expose_add_heap_object();
17591764
self.global(&format!(
1760-
"
1761-
function handleError(f) {{
1762-
return function () {{
1763-
try {{
1764-
return f.apply(this, arguments);
1765-
1766-
}} catch (e) {{
1767-
wasm.{}(addHeapObject(e));
1768-
}}
1769-
}};
1765+
"\
1766+
function handleError(f, args) {{
1767+
try {{
1768+
return f.apply(this, args);
1769+
}} catch (e) {{
1770+
wasm.{}(addHeapObject(e));
1771+
}}
17701772
}}
17711773
",
17721774
store,
@@ -1782,27 +1784,24 @@ impl<'a> Context<'a> {
17821784
}
17831785
self.global(
17841786
"\
1785-
function logError(f) {
1786-
return function () {
1787-
try {
1788-
return f.apply(this, arguments);
1789-
1790-
} catch (e) {
1791-
let error = (function () {
1792-
try {
1793-
return e instanceof Error \
1794-
? `${e.message}\\n\\nStack:\\n${e.stack}` \
1795-
: e.toString();
1796-
} catch(_) {
1797-
return \"<failed to stringify thrown value>\";
1798-
}
1799-
}());
1800-
console.error(\"wasm-bindgen: imported JS function that \
1801-
was not marked as `catch` threw an error:\", \
1802-
error);
1803-
throw e;
1804-
}
1805-
};
1787+
function logError(f, args) {
1788+
try {
1789+
return f.apply(this, args);
1790+
} catch (e) {
1791+
let error = (function () {
1792+
try {
1793+
return e instanceof Error \
1794+
? `${e.message}\\n\\nStack:\\n${e.stack}` \
1795+
: e.toString();
1796+
} catch(_) {
1797+
return \"<failed to stringify thrown value>\";
1798+
}
1799+
}());
1800+
console.error(\"wasm-bindgen: imported JS function that \
1801+
was not marked as `catch` threw an error:\", \
1802+
error);
1803+
throw e;
1804+
}
18061805
}
18071806
",
18081807
);
@@ -2404,9 +2403,15 @@ impl<'a> Context<'a> {
24042403
}
24052404
Kind::Import(core) => {
24062405
let code = if catch {
2407-
format!("handleError(function{})", code)
2406+
format!(
2407+
"function() {{ return handleError(function {}, arguments) }}",
2408+
code
2409+
)
24082410
} else if log_error {
2409-
format!("logError(function{})", code)
2411+
format!(
2412+
"function() {{ return logError(function {}, arguments) }}",
2413+
code
2414+
)
24102415
} else {
24112416
format!("function{}", code)
24122417
};

crates/cli/tests/reference/anyref-empty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as wasm from './reference_test_bg.wasm';
22

3-
export const __wbindgen_init_externref_table = function() {
3+
export function __wbindgen_init_externref_table() {
44
const table = wasm.__wbindgen_export_0;
55
const offset = table.grow(4);
66
table.set(0, undefined);

crates/cli/tests/reference/anyref-import-catch.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,33 @@ function addToExternrefTable0(obj) {
2424
return idx;
2525
}
2626

27-
function handleError(f) {
28-
return function () {
29-
try {
30-
return f.apply(this, arguments);
31-
32-
} catch (e) {
33-
const idx = addToExternrefTable0(e);
34-
wasm.__wbindgen_exn_store(idx);
35-
}
36-
};
27+
function handleError(f, args) {
28+
try {
29+
return f.apply(this, args);
30+
} catch (e) {
31+
const idx = addToExternrefTable0(e);
32+
wasm.__wbindgen_exn_store(idx);
33+
}
3734
}
3835
/**
3936
*/
4037
export function exported() {
4138
wasm.exported();
4239
}
4340

44-
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
41+
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
4542
foo();
46-
});
43+
}, arguments) };
4744

48-
export const __wbindgen_throw = function(arg0, arg1) {
45+
export function __wbindgen_throw(arg0, arg1) {
4946
throw new Error(getStringFromWasm0(arg0, arg1));
5047
};
5148

52-
export const __wbindgen_rethrow = function(arg0) {
49+
export function __wbindgen_rethrow(arg0) {
5350
throw arg0;
5451
};
5552

56-
export const __wbindgen_init_externref_table = function() {
53+
export function __wbindgen_init_externref_table() {
5754
const table = wasm.__wbindgen_export_0;
5855
const offset = table.grow(4);
5956
table.set(0, undefined);

crates/cli/tests/reference/anyref-nop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function foo() {
66
wasm.foo();
77
}
88

9-
export const __wbindgen_init_externref_table = function() {
9+
export function __wbindgen_init_externref_table() {
1010
const table = wasm.__wbindgen_export_0;
1111
const offset = table.grow(4);
1212
table.set(0, undefined);

crates/cli/tests/reference/import-catch.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,24 @@ function addHeapObject(obj) {
2929
return idx;
3030
}
3131

32-
function handleError(f) {
33-
return function () {
34-
try {
35-
return f.apply(this, arguments);
36-
37-
} catch (e) {
38-
wasm.__wbindgen_exn_store(addHeapObject(e));
39-
}
40-
};
32+
function handleError(f, args) {
33+
try {
34+
return f.apply(this, args);
35+
} catch (e) {
36+
wasm.__wbindgen_exn_store(addHeapObject(e));
37+
}
4138
}
4239
/**
4340
*/
4441
export function exported() {
4542
wasm.exported();
4643
}
4744

48-
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
45+
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
4946
foo();
50-
});
47+
}, arguments) };
5148

52-
export const __wbindgen_rethrow = function(arg0) {
49+
export function __wbindgen_rethrow(arg0) {
5350
throw takeObject(arg0);
5451
};
5552

crates/cli/tests/reference/string-arg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function foo(a) {
8383
wasm.foo(ptr0, len0);
8484
}
8585

86-
export const __wbindgen_throw = function(arg0, arg1) {
86+
export function __wbindgen_throw(arg0, arg1) {
8787
throw new Error(getStringFromWasm0(arg0, arg1));
8888
};
8989

0 commit comments

Comments
 (0)