Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix async generators #2853

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boa_ast/src/function/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a> arbitrary::Arbitrary<'a> for FormalParameterList {

bitflags! {
/// Flags for a [`FormalParameterList`].
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FormalParameterListFlags: u8 {
/// Has only identifier parameters with no initialization expressions.
Expand Down
6 changes: 3 additions & 3 deletions boa_ast/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ mod tests {
let a = Position::new(10, 30);
let b = Position::new(10, 50);

let _ = Span::new(a, b);
let _ = Span::new(a, a);
let _ = Span::from(a);
Span::new(a, b);
Span::new(a, a);
Span::from(a);
}

/// Checks that the `PartialEq` implementation of `Span` is consistent.
Expand Down
51 changes: 42 additions & 9 deletions boa_engine/src/bytecompiler/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod object_literal;
mod unary;
mod update;

use super::{Access, Callable, NodeKind};
use super::{Access, Callable, Label, NodeKind};
use crate::{
bytecompiler::{ByteCompiler, Literal},
vm::Opcode,
Expand Down Expand Up @@ -162,12 +162,42 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::PushUndefined);
}

if r#yield.delegate() {
if self.in_async_generator {
self.emit_opcode(Opcode::GetAsyncIterator);
} else {
self.emit_opcode(Opcode::GetIterator);
}
if r#yield.delegate() && self.in_async_generator {
self.emit_opcode(Opcode::GetAsyncIterator);
self.emit_opcode(Opcode::PushUndefined);

let start_address = self.next_opcode_location();
let (throw_method_undefined, return_method_undefined) =
self.emit_opcode_with_two_operands(Opcode::GeneratorAsyncDelegateNext);
self.emit_opcode(Opcode::Await);

let skip_yield = Label {
index: self.next_opcode_location(),
};
let exit = Label {
index: self.next_opcode_location() + 8,
};
self.emit(
Opcode::GeneratorAsyncDelegateResume,
&[Self::DUMMY_ADDRESS, start_address, Self::DUMMY_ADDRESS],
);

self.emit_opcode(Opcode::PushUndefined);
self.emit_opcode(Opcode::Yield);
self.emit(Opcode::Jump, &[start_address]);

self.patch_jump(skip_yield);
self.patch_jump(return_method_undefined);
self.emit_opcode(Opcode::Await);
self.emit_opcode(Opcode::GeneratorResumeReturn);

self.patch_jump(throw_method_undefined);
self.iterator_close(true);
self.emit_opcode(Opcode::Throw);

self.patch_jump(exit);
} else if r#yield.delegate() {
self.emit_opcode(Opcode::GetIterator);
self.emit_opcode(Opcode::PushUndefined);
let start_address = self.next_opcode_location();
let start = self.emit_opcode_with_operand(Opcode::GeneratorNextDelegate);
Expand All @@ -179,11 +209,14 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode_with_two_operands(Opcode::AsyncGeneratorNext);
self.emit_opcode(Opcode::PushUndefined);
self.emit_opcode(Opcode::Yield);
self.emit_opcode(Opcode::GeneratorNext);
let normal_completion =
self.emit_opcode_with_operand(Opcode::GeneratorAsyncResumeYield);
self.patch_jump(skip_yield);
self.emit_opcode(Opcode::Await);
self.emit_opcode(Opcode::GeneratorNext);
self.emit_opcode(Opcode::GeneratorResumeReturn);

self.patch_jump(skip_yield_await);
self.patch_jump(normal_completion);
} else {
self.emit_opcode(Opcode::Yield);
self.emit_opcode(Opcode::GeneratorNext);
Expand Down
15 changes: 13 additions & 2 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ impl CodeBlock {
| Opcode::SuperCall
| Opcode::IteratorUnwrapNextOrJump
| Opcode::ConcatToString
| Opcode::GeneratorAsyncResumeYield
| Opcode::GeneratorNextDelegate => {
let result = self.read::<u32>(*pc).to_string();
*pc += size_of::<u32>();
result
}

Opcode::PushDeclarativeEnvironment
| Opcode::PushFunctionEnvironment
| Opcode::CopyDataProperties
Expand All @@ -291,13 +291,23 @@ impl CodeBlock {
| Opcode::LoopContinue
| Opcode::LoopStart
| Opcode::TryStart
| Opcode::AsyncGeneratorNext => {
| Opcode::AsyncGeneratorNext
| Opcode::GeneratorAsyncDelegateNext => {
let operand1 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
let operand2 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
format!("{operand1}, {operand2}")
}
Opcode::GeneratorAsyncDelegateResume => {
let operand1 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
let operand2 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
let operand3 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
format!("{operand1}, {operand2}, {operand3}")
}
Opcode::GetArrowFunction
| Opcode::GetAsyncArrowFunction
| Opcode::GetFunction
Expand Down Expand Up @@ -447,6 +457,7 @@ impl CodeBlock {
| Opcode::CreateForInIterator
| Opcode::GetIterator
| Opcode::GetAsyncIterator
| Opcode::GeneratorResumeReturn
| Opcode::IteratorNext
| Opcode::IteratorNextSetDone
| Opcode::IteratorUnwrapNext
Expand Down
39 changes: 37 additions & 2 deletions boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(previous_pc, address, None, Color::None, EdgeStyle::Line);
}
Opcode::IteratorUnwrapNextOrJump | Opcode::GeneratorNextDelegate => {
Opcode::IteratorUnwrapNextOrJump
| Opcode::GeneratorAsyncResumeYield
| Opcode::GeneratorNextDelegate => {
let address = self.read::<u32>(pc) as usize;
pc += size_of::<u32>();
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
Expand All @@ -232,6 +234,39 @@ impl CodeBlock {
EdgeStyle::Line,
);
}
Opcode::GeneratorAsyncDelegateNext => {
let throw_method_undefined = self.read::<u32>(pc) as usize;
let return_method_undefined = self.read::<u32>(pc) as usize;
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(
previous_pc,
throw_method_undefined,
Some("throw method undefined".into()),
Color::None,
EdgeStyle::Line,
);
graph.add_edge(
previous_pc,
return_method_undefined,
Some("return method undefined".into()),
Color::None,
EdgeStyle::Line,
);
}
Opcode::GeneratorAsyncDelegateResume => {
self.read::<u32>(pc);
self.read::<u32>(pc);
let exit = self.read::<u32>(pc) as usize;
pc += size_of::<u32>();
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(
previous_pc,
exit,
Some("DONE".into()),
Color::None,
EdgeStyle::Line,
);
}
Opcode::CatchStart
| Opcode::CallEval
| Opcode::Call
Expand Down Expand Up @@ -544,7 +579,7 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::Return => {
Opcode::Return | Opcode::GeneratorResumeReturn => {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
if let Some((_try_pc, _next, Some(finally))) = try_entries.last() {
graph.add_edge(
Expand Down
25 changes: 25 additions & 0 deletions boa_engine/src/vm/opcode/await_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ impl Operation for Await {
GeneratorResumeKind::Normal,
context,
);

if let Some(async_generator) = gen
.call_frame
.as_ref()
.and_then(|f| f.async_generator.clone())
{
async_generator
.borrow_mut()
.as_async_generator_mut()
.expect("must be async generator")
.context = Some(gen);
}

// e. Assert: When we reach this step, asyncContext has already been removed from the execution context stack and prevContext is the currently running execution context.
// f. Return undefined.
Ok(JsValue::undefined())
Expand Down Expand Up @@ -82,6 +95,18 @@ impl Operation for Await {
context,
);

if let Some(async_generator) = gen
.call_frame
.as_ref()
.and_then(|f| f.async_generator.clone())
{
async_generator
.borrow_mut()
.as_async_generator_mut()
.expect("must be async generator")
.context = Some(gen);
}

Ok(JsValue::undefined())
},
captures,
Expand Down
Loading