Handle catch parameters in regenerator (#854)

This commit is contained in:
강동윤 2020-06-18 23:11:06 +09:00 committed by GitHub
parent ff440157a0
commit 92ce023171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -1071,7 +1071,7 @@ impl CaseHandler<'_> {
let after = self.loc();
let TryStmt {
handler,
mut handler,
block,
finalizer,
..
@ -1121,12 +1121,21 @@ impl CaseHandler<'_> {
try_entry.catch_entry.as_mut().unwrap().first_loc = loc;
let safe_param = folder.make_var();
folder.clear_pending_exception(try_entry.first_loc, Some(safe_param));
folder
.clear_pending_exception(try_entry.first_loc, Some(safe_param.clone()));
//bodyPath.traverse(catchParamVisitor, {
// getSafeParam: () => t.cloneDeep(safeParam),
// catchParamName: handler.param.name
//});
handler = handler.map(|handler| {
let body = handler.body.fold_with(&mut CatchParamHandler {
safe_param: &safe_param,
param: handler.param.as_ref(),
});
CatchClause { body, ..handler }
});
folder.with_entry(
Entry::Catch(try_entry.catch_entry.clone().unwrap()),
@ -1439,3 +1448,29 @@ impl Fold<Expr> for InvalidToLit<'_> {
}
}
}
struct CatchParamHandler<'a> {
safe_param: &'a Expr,
param: Option<&'a Pat>,
}
noop_fold_type!(CatchParamHandler<'_>);
impl Fold<Expr> for CatchParamHandler<'_> {
fn fold(&mut self, node: Expr) -> Expr {
match self.param {
None => return node,
Some(Pat::Ident(i)) => match &node {
Expr::Ident(r) => {
if r.sym == i.sym && i.span.ctxt() == r.span.ctxt() {
return self.safe_param.clone();
}
}
_ => {}
},
_ => {}
}
node.fold_children(self)
}
}

View File

@ -1127,3 +1127,16 @@ const v = genFactory()();
expect(v.next()).toEqual({ value: 1, done: false })
expect(v.next()).toEqual({ done: true })"
);
test_exec!(
syntax(),
|_| es2015::regenerator(Mark::fresh(Mark::root())),
issue_853_1,
"function throwingFn() { throw 'Error' }
function* gen() {
try { yield throwingFn() } catch (e) { yield e }
};
const v = gen();
expect(v.next()).toEqual({ done: false, value: 'Error'});
"
);