Skip to content

Commit

Permalink
regenerator: Handle yield* correctly (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jun 10, 2020
1 parent 54b3692 commit 5022999
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
19 changes: 11 additions & 8 deletions ecmascript/transforms/src/compat/es2015/regenerator/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,7 @@ impl CaseHandler<'_> {
.as_callee(),
args: vec![
arg.unwrap().as_arg(),
match result {
Expr::Ident(ref i) => i.clone().as_arg(),
_ => unreachable!(
"make_var() returned something other than ident: {:?}",
result
),
},
result.clone().as_arg(),
after.to_stmt_index().as_arg(),
],
type_args: Default::default(),
Expand All @@ -680,7 +674,16 @@ impl CaseHandler<'_> {
.into();

self.emit(ret);
self.mark(after);
let after = self.mark(after);
match self.listing.last_mut().unwrap() {
Stmt::Return(ReturnStmt {
arg: Some(box Expr::Call(CallExpr { args, .. })),
..
}) => {
*args.last_mut().unwrap() = after.to_stmt_index().as_arg();
}
_ => unreachable!(),
}

return result;
}
Expand Down
91 changes: 91 additions & 0 deletions ecmascript/transforms/tests/es2015_regenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,94 @@ test_exec!(
expect(() => v.next()).toThrow('2')
"
);

test_exec!(
syntax(),
|_| es2015::regenerator(Mark::fresh(Mark::root())),
issue_831_1,
"function* myGenerator() {
yield* [1,2,3];
}
const v = myGenerator();
expect(v.next()).toEqual({ value: 1, done: false });
expect(v.next()).toEqual({ value: 2, done: false });
expect(v.next()).toEqual({ value: 3, done: false });
expect(v.next()).toEqual({ done: true });
"
);

// test interop between cjs module and regenerator
test!(
syntax(),
|_| {
let mark = Mark::fresh(Mark::root());

chain!(
es2015::regenerator(mark),
common_js(mark, Default::default()),
)
},
issue_831_2,
"export function* myGenerator() {
yield* [1,2,3];
}",
"'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.myGenerator = myGenerator;
var regeneratorRuntime = require('regenerator-runtime');
var _marked = regeneratorRuntime.mark(myGenerator);
function myGenerator() {
return regeneratorRuntime.wrap(function myGenerator$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
return _ctx.delegateYield([
1,
2,
3
], _ctx.t0, 1);
case 1:
_ctx.t0;
case 2:
case 'end':
return _ctx.stop();
}
}, _marked);
}"
);

// test interop between export and regenerator
test!(
syntax(),
|_| {
let mark = Mark::fresh(Mark::root());

es2015::regenerator(mark)
},
issue_831_3,
"export function* myGenerator() {
yield* [1,2,3];
}",
"var regeneratorRuntime = require('regenerator-runtime');
var _marked = regeneratorRuntime.mark(myGenerator);
export function myGenerator() {
return regeneratorRuntime.wrap(function myGenerator$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
return _ctx.delegateYield([
1,
2,
3
], _ctx.t0, 1);
case 1:
_ctx.t0;
case 2:
case 'end':
return _ctx.stop();
}
}, _marked);
}
"
);

0 comments on commit 5022999

Please sign in to comment.