Skip to content

Commit 7bc9147

Browse files
authored
Improving the code generation for catch (#2098)
* Improving the code generation for catch * Fixing newlines * Running rustfmt * Updating unit tests * Fixing build error
1 parent a93b778 commit 7bc9147

File tree

4 files changed

+84
-40
lines changed

4 files changed

+84
-40
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub struct JsFunction {
6565
pub ts_arg_tys: Vec<String>,
6666
pub ts_ret_ty: Option<String>,
6767
pub might_be_optional_field: bool,
68+
pub catch: bool,
69+
pub log_error: bool,
6870
}
6971

7072
impl<'a, 'b> Builder<'a, 'b> {
@@ -201,7 +203,6 @@ impl<'a, 'b> Builder<'a, 'b> {
201203

202204
if self.catch {
203205
js.cx.expose_handle_error()?;
204-
call = format!("try {{\n{}}} catch (e) {{\n handleError(e)\n}}\n", call);
205206
}
206207

207208
// Generate a try/catch block in debug mode which handles unexpected and
@@ -210,7 +211,6 @@ impl<'a, 'b> Builder<'a, 'b> {
210211
// elsewhere.
211212
if self.log_error {
212213
js.cx.expose_log_error();
213-
call = format!("try {{\n{}}} catch (e) {{\n logError(e)\n}}\n", call);
214214
}
215215

216216
code.push_str(&call);
@@ -235,6 +235,8 @@ impl<'a, 'b> Builder<'a, 'b> {
235235
ts_arg_tys,
236236
ts_ret_ty,
237237
might_be_optional_field,
238+
catch: self.catch,
239+
log_error: self.log_error,
238240
})
239241
}
240242

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

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,9 +1666,16 @@ impl<'a> Context<'a> {
16661666
let add = self.expose_add_to_anyref_table(table, alloc)?;
16671667
self.global(&format!(
16681668
"
1669-
function handleError(e) {{
1670-
const idx = {}(e);
1671-
wasm.{}(idx);
1669+
function handleError(f) {{
1670+
return function () {{
1671+
try {{
1672+
return f.apply(this, arguments);
1673+
1674+
}} catch (e) {{
1675+
const idx = {}(e);
1676+
wasm.{}(idx);
1677+
}}
1678+
}};
16721679
}}
16731680
",
16741681
add, store,
@@ -1678,8 +1685,15 @@ impl<'a> Context<'a> {
16781685
self.expose_add_heap_object();
16791686
self.global(&format!(
16801687
"
1681-
function handleError(e) {{
1682-
wasm.{}(addHeapObject(e));
1688+
function handleError(f) {{
1689+
return function () {{
1690+
try {{
1691+
return f.apply(this, arguments);
1692+
1693+
}} catch (e) {{
1694+
wasm.{}(addHeapObject(e));
1695+
}}
1696+
}};
16831697
}}
16841698
",
16851699
store,
@@ -1695,20 +1709,27 @@ impl<'a> Context<'a> {
16951709
}
16961710
self.global(
16971711
"\
1698-
function logError(e) {
1699-
let error = (function () {
1712+
function logError(f) {
1713+
return function () {
17001714
try {
1701-
return e instanceof Error \
1702-
? `${e.message}\\n\\nStack:\\n${e.stack}` \
1703-
: e.toString();
1704-
} catch(_) {
1705-
return \"<failed to stringify thrown value>\";
1715+
return f.apply(this, arguments);
1716+
1717+
} catch (e) {
1718+
let error = (function () {
1719+
try {
1720+
return e instanceof Error \
1721+
? `${e.message}\\n\\nStack:\\n${e.stack}` \
1722+
: e.toString();
1723+
} catch(_) {
1724+
return \"<failed to stringify thrown value>\";
1725+
}
1726+
}());
1727+
console.error(\"wasm-bindgen: imported JS function that \
1728+
was not marked as `catch` threw an error:\", \
1729+
error);
1730+
throw e;
17061731
}
1707-
}());
1708-
console.error(\"wasm-bindgen: imported JS function that \
1709-
was not marked as `catch` threw an error:\", \
1710-
error);
1711-
throw e;
1732+
};
17121733
}
17131734
",
17141735
);
@@ -2183,6 +2204,8 @@ impl<'a> Context<'a> {
21832204
js_doc,
21842205
code,
21852206
might_be_optional_field,
2207+
catch,
2208+
log_error,
21862209
} = builder
21872210
.process(&adapter, instrs, arg_names)
21882211
.with_context(|| match kind {
@@ -2201,6 +2224,9 @@ impl<'a> Context<'a> {
22012224
// on what's being exported.
22022225
match kind {
22032226
Kind::Export(export) => {
2227+
assert_eq!(catch, false);
2228+
assert_eq!(log_error, false);
2229+
22042230
let ts_sig = match export.generate_typescript {
22052231
true => Some(ts_sig.as_str()),
22062232
false => None,
@@ -2257,10 +2283,20 @@ impl<'a> Context<'a> {
22572283
}
22582284
}
22592285
Kind::Import(core) => {
2260-
self.wasm_import_definitions
2261-
.insert(core, format!("function{}", code));
2286+
let code = if catch {
2287+
format!("handleError(function{})", code)
2288+
} else if log_error {
2289+
format!("logError(function{})", code)
2290+
} else {
2291+
format!("function{}", code)
2292+
};
2293+
2294+
self.wasm_import_definitions.insert(core, code);
22622295
}
22632296
Kind::Adapter => {
2297+
assert_eq!(catch, false);
2298+
assert_eq!(log_error, false);
2299+
22642300
self.globals.push_str("function ");
22652301
self.globals.push_str(&self.adapter_name(id));
22662302
self.globals.push_str(&code);

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ function addToAnyrefTable0(obj) {
2424
return idx;
2525
}
2626

27-
function handleError(e) {
28-
const idx = addToAnyrefTable0(e);
29-
wasm.__wbindgen_exn_store(idx);
27+
function handleError(f) {
28+
return function () {
29+
try {
30+
return f.apply(this, arguments);
31+
32+
} catch (e) {
33+
const idx = addToAnyrefTable0(e);
34+
wasm.__wbindgen_exn_store(idx);
35+
}
36+
};
3037
}
3138
/**
3239
*/
3340
export function exported() {
3441
wasm.exported();
3542
}
3643

37-
export const __wbg_foo_8d66ddef0ff279d6 = function() {
38-
try {
39-
foo();
40-
} catch (e) {
41-
handleError(e)
42-
}
43-
};
44+
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
45+
foo();
46+
});
4447

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

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

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

32-
function handleError(e) {
33-
wasm.__wbindgen_exn_store(addHeapObject(e));
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+
};
3441
}
3542
/**
3643
*/
@@ -42,13 +49,9 @@ export const __wbindgen_object_drop_ref = function(arg0) {
4249
takeObject(arg0);
4350
};
4451

45-
export const __wbg_foo_8d66ddef0ff279d6 = function() {
46-
try {
47-
foo();
48-
} catch (e) {
49-
handleError(e)
50-
}
51-
};
52+
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
53+
foo();
54+
});
5255

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

0 commit comments

Comments
 (0)