Skip to content
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

Work around for building with webpack5 #2512

Merged
merged 6 commits into from
Apr 19, 2021
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
103 changes: 54 additions & 49 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,19 @@ impl<'a> Context<'a> {
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);
footer.push_str("\nexport const ");
footer.push_str(&import.name);
footer.push_str(" = ");
footer.push_str(js.trim());
footer.push_str(";\n");
if js.starts_with("function") {
let body = &js[8..];
footer.push_str("\nexport function ");
footer.push_str(&import.name);
footer.push_str(body.trim());
footer.push_str(";\n");
} else {
footer.push_str("\nexport const ");
footer.push_str(&import.name);
footer.push_str(" = ");
footer.push_str(js.trim());
footer.push_str(";\n");
}
}
if needs_manual_start {
start = Some("\nwasm.__wbindgen_start();\n".to_string());
Expand Down Expand Up @@ -1738,17 +1746,14 @@ impl<'a> Context<'a> {
(Some(table), Some(alloc)) => {
let add = self.expose_add_to_externref_table(table, alloc)?;
self.global(&format!(
"
function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);

}} catch (e) {{
const idx = {}(e);
wasm.{}(idx);
}}
}};
"\
function handleError(f, args) {{
try {{
return f.apply(this, args);
}} catch (e) {{
const idx = {}(e);
wasm.{}(idx);
}}
}}
",
add, store,
Expand All @@ -1757,16 +1762,13 @@ impl<'a> Context<'a> {
_ => {
self.expose_add_heap_object();
self.global(&format!(
"
function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);

}} catch (e) {{
wasm.{}(addHeapObject(e));
}}
}};
"\
function handleError(f, args) {{
try {{
return f.apply(this, args);
}} catch (e) {{
wasm.{}(addHeapObject(e));
}}
}}
",
store,
Expand All @@ -1782,27 +1784,24 @@ impl<'a> Context<'a> {
}
self.global(
"\
function logError(f) {
return function () {
try {
return f.apply(this, arguments);

} catch (e) {
let error = (function () {
try {
return e instanceof Error \
? `${e.message}\\n\\nStack:\\n${e.stack}` \
: e.toString();
} catch(_) {
return \"<failed to stringify thrown value>\";
}
}());
console.error(\"wasm-bindgen: imported JS function that \
was not marked as `catch` threw an error:\", \
error);
throw e;
}
};
function logError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
let error = (function () {
try {
return e instanceof Error \
? `${e.message}\\n\\nStack:\\n${e.stack}` \
: e.toString();
} catch(_) {
return \"<failed to stringify thrown value>\";
}
}());
console.error(\"wasm-bindgen: imported JS function that \
was not marked as `catch` threw an error:\", \
error);
throw e;
}
}
",
);
Expand Down Expand Up @@ -2404,9 +2403,15 @@ impl<'a> Context<'a> {
}
Kind::Import(core) => {
let code = if catch {
format!("handleError(function{})", code)
format!(
"function() {{ return handleError(function {}, arguments) }}",
code
)
} else if log_error {
format!("logError(function{})", code)
format!(
"function() {{ return logError(function {}, arguments) }}",
code
)
} else {
format!("function{}", code)
};
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-empty.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as wasm from './reference_test_bg.wasm';

export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
Expand Down
27 changes: 12 additions & 15 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,33 @@ function addToExternrefTable0(obj) {
return idx;
}

function handleError(f) {
return function () {
try {
return f.apply(this, arguments);

} catch (e) {
const idx = addToExternrefTable0(e);
wasm.__wbindgen_exn_store(idx);
}
};
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
const idx = addToExternrefTable0(e);
wasm.__wbindgen_exn_store(idx);
}
}
/**
*/
export function exported() {
wasm.exported();
}

export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
foo();
});
}, arguments) };

export const __wbindgen_throw = function(arg0, arg1) {
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

export const __wbindgen_rethrow = function(arg0) {
export function __wbindgen_rethrow(arg0) {
throw arg0;
};

export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-nop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function foo() {
wasm.foo();
}

export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
Expand Down
21 changes: 9 additions & 12 deletions crates/cli/tests/reference/import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,24 @@ function addHeapObject(obj) {
return idx;
}

function handleError(f) {
return function () {
try {
return f.apply(this, arguments);

} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
};
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
/**
*/
export function exported() {
wasm.exported();
}

export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
foo();
});
}, arguments) };

export const __wbindgen_rethrow = function(arg0) {
export function __wbindgen_rethrow(arg0) {
throw takeObject(arg0);
};

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function foo(a) {
wasm.foo(ptr0, len0);
}

export const __wbindgen_throw = function(arg0, arg1) {
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};