-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
with
and object environments (#2692)
This Pull Request changes the following: - Implement `with` statement parsing, ast node, compilation and excution. - Implement object environments that are used in the `with` statement excution. The implementation of object environments can probably be optimized further by using more compile-time information about when object environments can exist. Maybe there could also be a separate environment stack for object environments to reduce the filtering and iteration that is needed with the current implementation. This does not fix all tests in the `test/language/statements/with` suite yet. But for most failing tests that I have looked at we are missing other features / have bugs elsewhere. As a note for the review: The functions in the `impl Context` block in `boa_engine/src/environments/runtime.rs` are mostly copied / moved from the existing functions. The only change there should be the addition of the object environment logic. They had to be moved to `Context` because of borrow semantics.
- Loading branch information
Showing
20 changed files
with
729 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::{ | ||
expression::Expression, | ||
statement::Statement, | ||
try_break, | ||
visitor::{VisitWith, Visitor, VisitorMut}, | ||
}; | ||
use boa_interner::{Interner, ToInternedString}; | ||
use core::ops::ControlFlow; | ||
|
||
/// The `with` statement extends the scope chain for a statement. | ||
/// | ||
/// More information: | ||
/// - [ECMAScript reference][spec] | ||
/// - [MDN documentation][mdn] | ||
/// | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with | ||
/// [spec]: https://tc39.es/ecma262/#prod-WithStatement | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct With { | ||
expression: Expression, | ||
statement: Box<Statement>, | ||
} | ||
|
||
impl With { | ||
/// Creates a `With` AST node. | ||
#[must_use] | ||
pub fn new(expression: Expression, statement: Statement) -> Self { | ||
Self { | ||
expression, | ||
statement: Box::new(statement), | ||
} | ||
} | ||
|
||
/// Gets the expression value of this `With` statement. | ||
#[must_use] | ||
pub const fn expression(&self) -> &Expression { | ||
&self.expression | ||
} | ||
|
||
/// Gets the statement value of this `With` statement. | ||
#[must_use] | ||
pub const fn statement(&self) -> &Statement { | ||
&self.statement | ||
} | ||
} | ||
|
||
impl From<With> for Statement { | ||
fn from(with: With) -> Self { | ||
Self::With(with) | ||
} | ||
} | ||
|
||
impl ToInternedString for With { | ||
fn to_interned_string(&self, interner: &Interner) -> String { | ||
format!( | ||
"with ({}) {{{}}}", | ||
self.expression.to_interned_string(interner), | ||
self.statement.to_interned_string(interner) | ||
) | ||
} | ||
} | ||
|
||
impl VisitWith for With { | ||
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy> | ||
where | ||
V: Visitor<'a>, | ||
{ | ||
try_break!(visitor.visit_expression(&self.expression)); | ||
visitor.visit_statement(&self.statement) | ||
} | ||
|
||
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy> | ||
where | ||
V: VisitorMut<'a>, | ||
{ | ||
try_break!(visitor.visit_expression_mut(&mut self.expression)); | ||
visitor.visit_statement_mut(&mut self.statement) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::{bytecompiler::ByteCompiler, vm::Opcode}; | ||
use boa_ast::statement::With; | ||
|
||
impl ByteCompiler<'_, '_> { | ||
/// Compile a [`With`] `boa_ast` node | ||
pub(crate) fn compile_with(&mut self, with: &With, configurable_globals: bool) { | ||
self.compile_expr(with.expression(), true); | ||
self.context.push_compile_time_environment(false); | ||
self.emit_opcode(Opcode::PushObjectEnvironment); | ||
self.compile_stmt(with.statement(), false, configurable_globals); | ||
self.context.pop_compile_time_environment(); | ||
self.emit_opcode(Opcode::PopEnvironment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.