-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
- Loading branch information
Showing
20 changed files
with
764 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.