Improving the code generation for catch (#2098)

* Improving the code generation for catch

* Fixing newlines

* Running rustfmt

* Updating unit tests

* Fixing build error
This commit is contained in:
Pauan 2020-04-22 16:14:00 +02:00 committed by GitHub
parent a93b778b5c
commit 7bc9147258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 40 deletions

View File

@ -65,6 +65,8 @@ pub struct JsFunction {
pub ts_arg_tys: Vec<String>, pub ts_arg_tys: Vec<String>,
pub ts_ret_ty: Option<String>, pub ts_ret_ty: Option<String>,
pub might_be_optional_field: bool, pub might_be_optional_field: bool,
pub catch: bool,
pub log_error: bool,
} }
impl<'a, 'b> Builder<'a, 'b> { impl<'a, 'b> Builder<'a, 'b> {
@ -201,7 +203,6 @@ impl<'a, 'b> Builder<'a, 'b> {
if self.catch { if self.catch {
js.cx.expose_handle_error()?; js.cx.expose_handle_error()?;
call = format!("try {{\n{}}} catch (e) {{\n handleError(e)\n}}\n", call);
} }
// Generate a try/catch block in debug mode which handles unexpected and // Generate a try/catch block in debug mode which handles unexpected and
@ -210,7 +211,6 @@ impl<'a, 'b> Builder<'a, 'b> {
// elsewhere. // elsewhere.
if self.log_error { if self.log_error {
js.cx.expose_log_error(); js.cx.expose_log_error();
call = format!("try {{\n{}}} catch (e) {{\n logError(e)\n}}\n", call);
} }
code.push_str(&call); code.push_str(&call);
@ -235,6 +235,8 @@ impl<'a, 'b> Builder<'a, 'b> {
ts_arg_tys, ts_arg_tys,
ts_ret_ty, ts_ret_ty,
might_be_optional_field, might_be_optional_field,
catch: self.catch,
log_error: self.log_error,
}) })
} }

View File

@ -1666,10 +1666,17 @@ impl<'a> Context<'a> {
let add = self.expose_add_to_anyref_table(table, alloc)?; let add = self.expose_add_to_anyref_table(table, alloc)?;
self.global(&format!( self.global(&format!(
" "
function handleError(e) {{ function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);
}} catch (e) {{
const idx = {}(e); const idx = {}(e);
wasm.{}(idx); wasm.{}(idx);
}} }}
}};
}}
", ",
add, store, add, store,
)); ));
@ -1678,9 +1685,16 @@ impl<'a> Context<'a> {
self.expose_add_heap_object(); self.expose_add_heap_object();
self.global(&format!( self.global(&format!(
" "
function handleError(e) {{ function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);
}} catch (e) {{
wasm.{}(addHeapObject(e)); wasm.{}(addHeapObject(e));
}} }}
}};
}}
", ",
store, store,
)); ));
@ -1695,7 +1709,12 @@ impl<'a> Context<'a> {
} }
self.global( self.global(
"\ "\
function logError(e) { function logError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
let error = (function () { let error = (function () {
try { try {
return e instanceof Error \ return e instanceof Error \
@ -1710,6 +1729,8 @@ impl<'a> Context<'a> {
error); error);
throw e; throw e;
} }
};
}
", ",
); );
} }
@ -2183,6 +2204,8 @@ impl<'a> Context<'a> {
js_doc, js_doc,
code, code,
might_be_optional_field, might_be_optional_field,
catch,
log_error,
} = builder } = builder
.process(&adapter, instrs, arg_names) .process(&adapter, instrs, arg_names)
.with_context(|| match kind { .with_context(|| match kind {
@ -2201,6 +2224,9 @@ impl<'a> Context<'a> {
// on what's being exported. // on what's being exported.
match kind { match kind {
Kind::Export(export) => { Kind::Export(export) => {
assert_eq!(catch, false);
assert_eq!(log_error, false);
let ts_sig = match export.generate_typescript { let ts_sig = match export.generate_typescript {
true => Some(ts_sig.as_str()), true => Some(ts_sig.as_str()),
false => None, false => None,
@ -2257,10 +2283,20 @@ impl<'a> Context<'a> {
} }
} }
Kind::Import(core) => { Kind::Import(core) => {
self.wasm_import_definitions let code = if catch {
.insert(core, format!("function{}", code)); format!("handleError(function{})", code)
} else if log_error {
format!("logError(function{})", code)
} else {
format!("function{}", code)
};
self.wasm_import_definitions.insert(core, code);
} }
Kind::Adapter => { Kind::Adapter => {
assert_eq!(catch, false);
assert_eq!(log_error, false);
self.globals.push_str("function "); self.globals.push_str("function ");
self.globals.push_str(&self.adapter_name(id)); self.globals.push_str(&self.adapter_name(id));
self.globals.push_str(&code); self.globals.push_str(&code);

View File

@ -24,9 +24,16 @@ function addToAnyrefTable0(obj) {
return idx; return idx;
} }
function handleError(e) { function handleError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
const idx = addToAnyrefTable0(e); const idx = addToAnyrefTable0(e);
wasm.__wbindgen_exn_store(idx); wasm.__wbindgen_exn_store(idx);
}
};
} }
/** /**
*/ */
@ -34,13 +41,9 @@ export function exported() {
wasm.exported(); wasm.exported();
} }
export const __wbg_foo_8d66ddef0ff279d6 = function() { export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
try {
foo(); foo();
} catch (e) { });
handleError(e)
}
};
export const __wbindgen_throw = function(arg0, arg1) { export const __wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1)); throw new Error(getStringFromWasm0(arg0, arg1));

View File

@ -29,8 +29,15 @@ function addHeapObject(obj) {
return idx; return idx;
} }
function handleError(e) { function handleError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e)); wasm.__wbindgen_exn_store(addHeapObject(e));
}
};
} }
/** /**
*/ */
@ -42,13 +49,9 @@ export const __wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0); takeObject(arg0);
}; };
export const __wbg_foo_8d66ddef0ff279d6 = function() { export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
try {
foo(); foo();
} catch (e) { });
handleError(e)
}
};
export const __wbindgen_rethrow = function(arg0) { export const __wbindgen_rethrow = function(arg0) {
throw takeObject(arg0); throw takeObject(arg0);