Skip to content

Commit

Permalink
Rename OptionalItem to OptionalOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Oct 28, 2022
1 parent c1f4cfc commit 7e0d354
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 54 deletions.
10 changes: 5 additions & 5 deletions boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
binary::{ArithmeticOp, BinaryOp, BitwiseOp, LogicalOp, RelationalOp},
unary::UnaryOp,
},
Call, Identifier, New, Optional, OptionalItemKind,
Call, Identifier, New, Optional, OptionalOperationKind,
},
function::{
ArrowFunction, AsyncFunction, AsyncGenerator, Class, ClassElement, FormalParameterList,
Expand Down Expand Up @@ -1591,9 +1591,9 @@ impl<'b> ByteCompiler<'b> {
/// is not null or undefined (if the operator `?.` was used).
/// - This assumes that the state of the stack before compiling is `...rest, this, value`,
/// since the operation compiled by this function could be a call.
fn compile_optional_item_kind(&mut self, kind: &OptionalItemKind) -> JsResult<()> {
fn compile_optional_item_kind(&mut self, kind: &OptionalOperationKind) -> JsResult<()> {
match kind {
OptionalItemKind::SimplePropertyAccess { field } => {
OptionalOperationKind::SimplePropertyAccess { field } => {
self.emit_opcode(Opcode::Dup);
match field {
PropertyAccessField::Const(name) => {
Expand All @@ -1610,15 +1610,15 @@ impl<'b> ByteCompiler<'b> {
self.emit_u8(3);
self.emit_opcode(Opcode::Pop);
}
OptionalItemKind::PrivatePropertyAccess { field } => {
OptionalOperationKind::PrivatePropertyAccess { field } => {
self.emit_opcode(Opcode::Dup);
let index = self.get_or_insert_name((*field).into());
self.emit(Opcode::GetPrivateField, &[index]);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Pop);
}
OptionalItemKind::Call { args } => {
OptionalOperationKind::Call { args } => {
let args = &**args;
let contains_spread = args.iter().any(|arg| matches!(arg, Expression::Spread(_)));

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/syntax/ast/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod r#yield;
pub use call::{Call, SuperCall};
pub use identifier::Identifier;
pub use new::New;
pub use optional::{Optional, OptionalItem, OptionalItemKind};
pub use optional::{Optional, OptionalOperation, OptionalOperationKind};
pub use r#await::Await;
pub use r#yield::Yield;
pub use spread::Spread;
Expand Down
53 changes: 27 additions & 26 deletions boa_engine/src/syntax/ast/expression/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{access::PropertyAccessField, Expression};
/// List of valid operations in an [`Optional`] chain.
#[cfg_attr(feature = "deser", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub enum OptionalItemKind {
pub enum OptionalOperationKind {
/// A property access (`a?.prop`).
SimplePropertyAccess {
/// The field accessed.
Expand All @@ -25,21 +25,21 @@ pub enum OptionalItemKind {
},
}

impl OptionalItemKind {
impl OptionalOperationKind {
#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
match self {
OptionalItemKind::SimplePropertyAccess { field } => field.contains_arguments(),
OptionalItemKind::PrivatePropertyAccess { .. } => false,
OptionalItemKind::Call { args } => args.iter().any(Expression::contains_arguments),
OptionalOperationKind::SimplePropertyAccess { field } => field.contains_arguments(),
OptionalOperationKind::PrivatePropertyAccess { .. } => false,
OptionalOperationKind::Call { args } => args.iter().any(Expression::contains_arguments),
}
}
#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
match self {
OptionalItemKind::SimplePropertyAccess { field } => field.contains(symbol),
OptionalItemKind::PrivatePropertyAccess { .. } => false,
OptionalItemKind::Call { args } => args.iter().any(|e| e.contains(symbol)),
OptionalOperationKind::SimplePropertyAccess { field } => field.contains(symbol),
OptionalOperationKind::PrivatePropertyAccess { .. } => false,
OptionalOperationKind::Call { args } => args.iter().any(|e| e.contains(symbol)),
}
}
}
Expand All @@ -52,24 +52,24 @@ impl OptionalItemKind {
/// is `undefined` or `null`.
#[cfg_attr(feature = "deser", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct OptionalItem {
kind: OptionalItemKind,
pub struct OptionalOperation {
kind: OptionalOperationKind,
shorted: bool,
}

impl OptionalItem {
/// Creates a new `OptionalItem`.
impl OptionalOperation {
/// Creates a new `OptionalOperation`.
#[inline]
pub fn new(kind: OptionalItemKind, shorted: bool) -> Self {
pub fn new(kind: OptionalOperationKind, shorted: bool) -> Self {
Self { kind, shorted }
}
/// Gets the kind of optional chain item.
/// Gets the kind of operation.
#[inline]
pub fn kind(&self) -> &OptionalItemKind {
pub fn kind(&self) -> &OptionalOperationKind {
&self.kind
}

/// Returns `true` if the item short-circuits the [`Optional`] chain when the target is
/// Returns `true` if the operation short-circuits the [`Optional`] chain when the target is
/// `undefined` or `null`.
#[inline]
pub fn shorted(&self) -> bool {
Expand All @@ -87,35 +87,35 @@ impl OptionalItem {
}
}

impl ToInternedString for OptionalItem {
impl ToInternedString for OptionalOperation {
fn to_interned_string(&self, interner: &Interner) -> String {
let mut buf = if self.shorted {
String::from("?.")
} else {
if let OptionalItemKind::SimplePropertyAccess {
if let OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(name),
} = &self.kind
{
return format!(".{}", interner.resolve_expect(*name));
}

if let OptionalItemKind::PrivatePropertyAccess { field } = &self.kind {
if let OptionalOperationKind::PrivatePropertyAccess { field } = &self.kind {
return format!(".#{}", interner.resolve_expect(*field));
}

String::new()
};
buf.push_str(&match &self.kind {
OptionalItemKind::SimplePropertyAccess { field } => match field {
OptionalOperationKind::SimplePropertyAccess { field } => match field {
PropertyAccessField::Const(name) => interner.resolve_expect(*name).to_string(),
PropertyAccessField::Expr(expr) => {
format!("[{}]", expr.to_interned_string(interner))
}
},
OptionalItemKind::PrivatePropertyAccess { field } => {
OptionalOperationKind::PrivatePropertyAccess { field } => {
format!("#{}", interner.resolve_expect(*field))
}
OptionalItemKind::Call { args } => format!("({})", join_nodes(interner, args)),
OptionalOperationKind::Call { args } => format!("({})", join_nodes(interner, args)),
});
buf
}
Expand Down Expand Up @@ -147,13 +147,13 @@ impl ToInternedString for OptionalItem {
#[derive(Clone, Debug, PartialEq)]
pub struct Optional {
target: Box<Expression>,
chain: Box<[OptionalItem]>,
chain: Box<[OptionalOperation]>,
}

impl Optional {
/// Creates a new `Optional` expression.
#[inline]
pub fn new(target: Expression, chain: Box<[OptionalItem]>) -> Self {
pub fn new(target: Expression, chain: Box<[OptionalOperation]>) -> Self {
Self {
target: Box::new(target),
chain,
Expand All @@ -168,13 +168,14 @@ impl Optional {

/// Gets the chain of accesses and calls that will be applied to the target at runtime.
#[inline]
pub fn chain(&self) -> &[OptionalItem] {
pub fn chain(&self) -> &[OptionalOperation] {
self.chain.as_ref()
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.target.contains_arguments() || self.chain.iter().any(OptionalItem::contains_arguments)
self.target.contains_arguments()
|| self.chain.iter().any(OptionalOperation::contains_arguments)
}
#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use boa_profiler::Profiler;
use crate::syntax::{
ast::{
self,
expression::{access::PropertyAccessField, Optional, OptionalItem, OptionalItemKind},
expression::{
access::PropertyAccessField, Optional, OptionalOperation, OptionalOperationKind,
},
Punctuator,
},
lexer::{Token, TokenKind},
Expand Down Expand Up @@ -66,26 +68,26 @@ where
cursor: &mut Cursor<R>,
token: &Token,
interner: &mut Interner,
) -> ParseResult<OptionalItemKind> {
) -> ParseResult<OptionalOperationKind> {
let item = match token.kind() {
TokenKind::Identifier(name) => OptionalItemKind::SimplePropertyAccess {
TokenKind::Identifier(name) => OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(*name),
},
TokenKind::Keyword((kw, _)) => OptionalItemKind::SimplePropertyAccess {
TokenKind::Keyword((kw, _)) => OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(kw.to_sym(interner)),
},
TokenKind::BooleanLiteral(true) => OptionalItemKind::SimplePropertyAccess {
TokenKind::BooleanLiteral(true) => OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(Sym::TRUE),
},
TokenKind::BooleanLiteral(false) => OptionalItemKind::SimplePropertyAccess {
TokenKind::BooleanLiteral(false) => OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(Sym::FALSE),
},
TokenKind::NullLiteral => OptionalItemKind::SimplePropertyAccess {
TokenKind::NullLiteral => OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(Sym::NULL),
},
TokenKind::PrivateIdentifier(name) => {
cursor.push_used_private_identifier(*name, token.span().start())?;
OptionalItemKind::PrivatePropertyAccess { field: *name }
OptionalOperationKind::PrivatePropertyAccess { field: *name }
}
_ => {
return Err(ParseError::expected(
Expand Down Expand Up @@ -115,7 +117,7 @@ where

let item = parse_const_access(cursor, &field, interner)?;

items.push(OptionalItem::new(item, false));
items.push(OptionalOperation::new(item, false));
continue;
}
TokenKind::TemplateMiddle(_) | TokenKind::TemplateNoSubstitution(_) => {
Expand All @@ -133,7 +135,7 @@ where
TokenKind::Punctuator(Punctuator::OpenParen) => {
let args = Arguments::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
OptionalItemKind::Call { args }
OptionalOperationKind::Call { args }
}
TokenKind::Punctuator(Punctuator::OpenBracket) => {
cursor
Expand All @@ -142,7 +144,7 @@ where
let idx = Expression::new(None, true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
cursor.expect(Punctuator::CloseBracket, "optional chain", interner)?;
OptionalItemKind::SimplePropertyAccess {
OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Expr(Box::new(idx)),
}
}
Expand All @@ -158,7 +160,7 @@ where
}
};

items.push(OptionalItem::new(item, shorted));
items.push(OptionalOperation::new(item, shorted));
}

Ok(Optional::new(self.target, items.into()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use boa_macros::utf16;
use crate::syntax::{
ast::{
expression::{
access::PropertyAccessField, literal::Literal, Identifier, Optional, OptionalItem,
OptionalItemKind,
access::PropertyAccessField, literal::Literal, Identifier, Optional, OptionalOperation,
OptionalOperationKind,
},
Expression, Statement,
},
Expand All @@ -21,8 +21,8 @@ fn simple() {
vec![Statement::Expression(
Optional::new(
Literal::Int(5).into(),
vec![OptionalItem::new(
OptionalItemKind::SimplePropertyAccess {
vec![OptionalOperation::new(
OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(
interner.get_or_intern_static("name", utf16!("name")),
),
Expand All @@ -48,22 +48,22 @@ fn complex_chain() {
Optional::new(
Identifier::new(interner.get_or_intern_static("a", utf16!("a"))).into(),
vec![
OptionalItem::new(
OptionalItemKind::SimplePropertyAccess {
OptionalOperation::new(
OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(
interner.get_or_intern_static("b", utf16!("b")),
),
},
true,
),
OptionalItem::new(
OptionalItemKind::Call {
OptionalOperation::new(
OptionalOperationKind::Call {
args: vec![Expression::Literal(Literal::Bool(true))].into(),
},
false,
),
OptionalItem::new(
OptionalItemKind::SimplePropertyAccess {
OptionalOperation::new(
OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Expr(Box::new(
Literal::String(interner.get_or_intern_static("c", utf16!("c")))
.into(),
Expand Down

0 comments on commit 7e0d354

Please sign in to comment.