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] - Implement for await...of loops #2286

Closed
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions boa_engine/src/builtins/iterable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ pub struct IteratorResult {
}

impl IteratorResult {
/// Create a new `IteratorResult`.
pub(crate) fn new(object: JsObject) -> Self {
Self { object }
}

/// `IteratorComplete ( iterResult )`
///
/// The abstract operation `IteratorComplete` takes argument `iterResult` (an `Object`) and
Expand Down
16 changes: 14 additions & 2 deletions boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,11 @@ impl<'b> ByteCompiler<'b> {
self.emit_opcode(Opcode::PopEnvironment);
}

self.emit_opcode(Opcode::InitIterator);
if for_of_loop.r#await() {
self.emit_opcode(Opcode::InitIteratorAsync);
} else {
self.emit_opcode(Opcode::InitIterator);
}

self.emit_opcode(Opcode::LoopStart);
let start_address = self.next_opcode_location();
Expand All @@ -1557,7 +1561,15 @@ impl<'b> ByteCompiler<'b> {
self.context.push_compile_time_environment(false);
let push_env =
self.emit_opcode_with_two_operands(Opcode::PushDeclarativeEnvironment);
let exit = self.emit_opcode_with_operand(Opcode::ForInLoopNext);

let exit = if for_of_loop.r#await() {
self.emit_opcode(Opcode::ForAwaitOfLoopIterate);
self.emit_opcode(Opcode::Await);
self.emit_opcode(Opcode::GeneratorNext);
self.emit_opcode_with_operand(Opcode::ForAwaitOfLoopNext)
} else {
self.emit_opcode_with_operand(Opcode::ForInLoopNext)
};

match for_of_loop.init() {
IterableLoopInitializer::Identifier(ref ident) => {
Expand Down
9 changes: 8 additions & 1 deletion boa_engine/src/syntax/ast/node/iteration/for_of_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ pub struct ForOfLoop {
iterable: Box<Node>,
body: Box<Node>,
label: Option<Sym>,
r#await: bool,
}

impl ForOfLoop {
/// Creates a new "for of" loop AST node.
pub fn new<I, B>(init: IterableLoopInitializer, iterable: I, body: B) -> Self
pub fn new<I, B>(init: IterableLoopInitializer, iterable: I, body: B, r#await: bool) -> Self
where
I: Into<Node>,
B: Into<Node>,
Expand All @@ -25,6 +26,7 @@ impl ForOfLoop {
iterable: Box::new(iterable.into()),
body: Box::new(body.into()),
label: None,
r#await,
}
}

Expand All @@ -48,6 +50,11 @@ impl ForOfLoop {
self.label = Some(label);
}

/// Returns true if this "for...of" loop is an "for await...of" loop.
pub(crate) fn r#await(&self) -> bool {
self.r#await
}

/// Converts the "for of" loop to a string with the given indentation.
pub(in crate::syntax::ast::node) fn to_indented_string(
&self,
Expand Down
34 changes: 29 additions & 5 deletions boa_engine/src/syntax/parser/statement/iteration/for_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,34 @@ where
) -> Result<Self::Output, ParseError> {
let _timer = Profiler::global().start_event("ForStatement", "Parsing");
cursor.expect((Keyword::For, false), "for statement", interner)?;
let init_position = cursor
.expect(Punctuator::OpenParen, "for statement", interner)?
.span()
.end();

let mut r#await = false;

let next = cursor.next(interner)?.ok_or(ParseError::AbruptEnd)?;
let init_position = match next.kind() {
TokenKind::Punctuator(Punctuator::OpenParen) => next.span().end(),
TokenKind::Keyword((Keyword::Await, _)) if !self.allow_await.0 => {
return Err(ParseError::unexpected(
next.to_string(interner),
next.span(),
"for await...of is only valid in async functions or async generators",
));
}
TokenKind::Keyword((Keyword::Await, _)) => {
r#await = true;
cursor
.expect(Punctuator::OpenParen, "for await...of", interner)?
.span()
.end()
}
_ => {
return Err(ParseError::unexpected(
next.to_string(interner),
next.span(),
"for statement",
));
}
};

let init = match cursor
.peek(0, interner)?
Expand Down Expand Up @@ -233,7 +257,7 @@ where
}
}

return Ok(ForOfLoop::new(init, iterable, body).into());
return Ok(ForOfLoop::new(init, iterable, body, r#await).into());
}
(Some(Node::ConstDeclList(list)), _) => {
// Reject const declarations without initializers inside for loops
Expand Down
2 changes: 2 additions & 0 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl CodeBlock {
| Opcode::SuperCall
| Opcode::ForInLoopInitIterator
| Opcode::ForInLoopNext
| Opcode::ForAwaitOfLoopNext
| Opcode::ConcatToString
| Opcode::GeneratorNextDelegate => {
let result = self.read::<u32>(*pc).to_string();
Expand Down Expand Up @@ -374,6 +375,7 @@ impl CodeBlock {
| Opcode::CallSpread
| Opcode::NewSpread
| Opcode::SuperCallSpread
| Opcode::ForAwaitOfLoopIterate
| Opcode::Nop => String::new(),
}
}
Expand Down
42 changes: 41 additions & 1 deletion boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
builtins::{
async_generator::{AsyncGenerator, AsyncGeneratorState},
function::{ConstructorKind, Function},
iterable::{IteratorHint, IteratorRecord},
iterable::{IteratorHint, IteratorRecord, IteratorResult},
Array, ForInIterator, JsArgs, Number, Promise,
},
environments::EnvironmentSlots,
Expand Down Expand Up @@ -2129,6 +2129,46 @@ impl Context {
self.vm.push(done);
}
}
Opcode::ForAwaitOfLoopIterate => {
let _done = self
.vm
.pop()
.as_boolean()
.expect("iterator [[Done]] was not a boolean");
let next_method = self.vm.pop();
let next_method_object = if let Some(object) = next_method.as_callable() {
object
} else {
return self.throw_type_error("iterable next method not a function");
};
let iterator = self.vm.pop();
let next_result = next_method_object.call(&iterator, &[], self)?;
self.vm.push(iterator);
self.vm.push(next_method);
self.vm.push(next_result);
}
Opcode::ForAwaitOfLoopNext => {
let address = self.vm.read::<u32>();

let next_result = self.vm.pop();
let next_result = if let Some(next_result) = next_result.as_object() {
IteratorResult::new(next_result.clone())
} else {
return self.throw_type_error("next value should be an object");
};

if next_result.complete(self)? {
self.vm.frame_mut().pc = address as usize;
self.vm.frame_mut().loop_env_stack_dec();
self.vm.frame_mut().try_env_stack_dec();
self.realm.environments.pop();
self.vm.push(true);
} else {
self.vm.push(false);
let value = next_result.value(self)?;
self.vm.push(value);
}
}
Opcode::ConcatToString => {
let value_count = self.vm.read::<u32>();
let mut strings = Vec::with_capacity(value_count as usize);
Expand Down
18 changes: 18 additions & 0 deletions boa_engine/src/vm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,20 @@ pub enum Opcode {
/// Stack: iterator, next_method, done **=>** iterator, next_method, done, next_result
ForInLoopNext,

/// Move to the next value in a for await..of loop.
///
/// Operands:
///
/// Stack: iterator, next_method, done **=>** iterator, next_method, next_result
ForAwaitOfLoopIterate,

/// Get the value from a for await..of loop next result.
///
/// Operands: address: `u32`
///
/// Stack: next_result **=>** done, value
ForAwaitOfLoopNext,

/// Concat multiple stack objects into a string.
///
/// Operands: value_count: `u32`
Expand Down Expand Up @@ -1337,6 +1351,8 @@ impl Opcode {
Self::IteratorClose => "IteratorClose",
Self::IteratorToArray => "IteratorToArray",
Self::ForInLoopNext => "ForInLoopNext",
Self::ForAwaitOfLoopNext => "ForAwaitOfLoopNext",
Self::ForAwaitOfLoopIterate => "ForAwaitOfLoopIterate",
Self::ConcatToString => "ConcatToString",
Self::RequireObjectCoercible => "RequireObjectCoercible",
Self::ValueNotNullOrUndefined => "ValueNotNullOrUndefined",
Expand Down Expand Up @@ -1481,6 +1497,8 @@ impl Opcode {
Self::IteratorClose => "INST - IteratorClose",
Self::IteratorToArray => "INST - IteratorToArray",
Self::ForInLoopNext => "INST - ForInLoopNext",
Self::ForAwaitOfLoopIterate => "INST - ForAwaitOfLoopIterate",
Self::ForAwaitOfLoopNext => "INST - ForAwaitOfLoopNext",
Self::ConcatToString => "INST - ConcatToString",
Self::RequireObjectCoercible => "INST - RequireObjectCoercible",
Self::ValueNotNullOrUndefined => "INST - ValueNotNullOrUndefined",
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn run_super_method_in_object() {
assert_eq!(
Context::default().eval(source.as_bytes()),
Ok(JsValue::from("super"))
)
);
}

#[test]
Expand All @@ -180,5 +180,5 @@ fn get_reference_by_super() {
assert_eq!(
Context::default().eval(source.as_bytes()),
Ok(JsValue::from("ab"))
)
);
}