Merge pull request #1226 from Pauan/optimize-catch

Simplifying the error handling code
This commit is contained in:
Alex Crichton 2019-02-04 08:23:57 +01:00 committed by GitHub
commit bf8a380264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 10 deletions

View File

@ -1546,6 +1546,23 @@ impl<'a> Context<'a> {
)); ));
} }
fn expose_handle_error(&mut self) {
if !self.should_write_global("handle_error") {
return;
}
self.expose_uint32_memory();
self.expose_add_heap_object();
self.global(
"
function handleError(exnptr, e) {
const view = getUint32Memory();
view[exnptr / 4] = 1;
view[exnptr / 4 + 1] = addHeapObject(e);
}
",
);
}
fn wasm_import_needed(&self, name: &str) -> bool { fn wasm_import_needed(&self, name: &str) -> bool {
let imports = match self.module.import_section() { let imports = match self.module.import_section() {
Some(s) => s, Some(s) => s,

View File

@ -643,23 +643,16 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
}; };
if self.catch { if self.catch {
self.cx.expose_uint32_memory(); self.cx.expose_handle_error();
self.cx.expose_add_heap_object();
let catch = "\
const view = getUint32Memory();\n\
view[exnptr / 4] = 1;\n\
view[exnptr / 4 + 1] = addHeapObject(e);\n\
";
invoc = format!( invoc = format!(
"\ "\
try {{\n\ try {{\n\
{} {}
}} catch (e) {{\n\ }} catch (e) {{\n\
{} handleError(exnptr, e);\n\
}}\ }}\
", ",
&invoc, catch &invoc
); );
} else if self.catch_and_rethrow { } else if self.catch_and_rethrow {
invoc = format!( invoc = format!(