Skip to content

Commit

Permalink
Work around for building with webpack5 (#2512)
Browse files Browse the repository at this point in the history
* Use  instead of

* rewrite js files in wasm-bindgen-cli reference tests

* fix formatting

* fix typo

* ensure function return

* run cargo fmt
  • Loading branch information
nokotan committed Apr 19, 2021
1 parent 0d911ea commit fda6bb9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 79 deletions.
103 changes: 54 additions & 49 deletions crates/cli-support/src/js/mod.rs
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
@@ -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
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
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
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
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));
};

0 comments on commit fda6bb9

Please sign in to comment.