From 7e0d354a7239e83952d4292b0d14c04a2383ce54 Mon Sep 17 00:00:00 2001 From: jedel1043 Date: Fri, 28 Oct 2022 12:12:44 -0500 Subject: [PATCH] Rename `OptionalItem` to `OptionalOperation` --- boa_engine/src/bytecompiler/mod.rs | 10 ++-- boa_engine/src/syntax/ast/expression/mod.rs | 2 +- .../src/syntax/ast/expression/optional.rs | 53 ++++++++++--------- .../expression/left_hand_side/optional/mod.rs | 26 ++++----- .../left_hand_side/optional/tests.rs | 20 +++---- 5 files changed, 57 insertions(+), 54 deletions(-) diff --git a/boa_engine/src/bytecompiler/mod.rs b/boa_engine/src/bytecompiler/mod.rs index af361759efa..28ad4eff429 100644 --- a/boa_engine/src/bytecompiler/mod.rs +++ b/boa_engine/src/bytecompiler/mod.rs @@ -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, @@ -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) => { @@ -1610,7 +1610,7 @@ 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]); @@ -1618,7 +1618,7 @@ impl<'b> ByteCompiler<'b> { 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(_))); diff --git a/boa_engine/src/syntax/ast/expression/mod.rs b/boa_engine/src/syntax/ast/expression/mod.rs index cdbdb93da59..5e833abfd3b 100644 --- a/boa_engine/src/syntax/ast/expression/mod.rs +++ b/boa_engine/src/syntax/ast/expression/mod.rs @@ -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; diff --git a/boa_engine/src/syntax/ast/expression/optional.rs b/boa_engine/src/syntax/ast/expression/optional.rs index d9d0034dd19..11a56b1e8f6 100644 --- a/boa_engine/src/syntax/ast/expression/optional.rs +++ b/boa_engine/src/syntax/ast/expression/optional.rs @@ -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. @@ -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)), } } } @@ -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 { @@ -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 } @@ -147,13 +147,13 @@ impl ToInternedString for OptionalItem { #[derive(Clone, Debug, PartialEq)] pub struct Optional { target: Box, - 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, @@ -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 { diff --git a/boa_engine/src/syntax/parser/expression/left_hand_side/optional/mod.rs b/boa_engine/src/syntax/parser/expression/left_hand_side/optional/mod.rs index d12fc8a83f1..ce78197969f 100644 --- a/boa_engine/src/syntax/parser/expression/left_hand_side/optional/mod.rs +++ b/boa_engine/src/syntax/parser/expression/left_hand_side/optional/mod.rs @@ -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}, @@ -66,26 +68,26 @@ where cursor: &mut Cursor, token: &Token, interner: &mut Interner, - ) -> ParseResult { + ) -> ParseResult { 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( @@ -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(_) => { @@ -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 @@ -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)), } } @@ -158,7 +160,7 @@ where } }; - items.push(OptionalItem::new(item, shorted)); + items.push(OptionalOperation::new(item, shorted)); } Ok(Optional::new(self.target, items.into())) diff --git a/boa_engine/src/syntax/parser/expression/left_hand_side/optional/tests.rs b/boa_engine/src/syntax/parser/expression/left_hand_side/optional/tests.rs index 42a7b3d1b15..b7a59888080 100644 --- a/boa_engine/src/syntax/parser/expression/left_hand_side/optional/tests.rs +++ b/boa_engine/src/syntax/parser/expression/left_hand_side/optional/tests.rs @@ -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, }, @@ -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")), ), @@ -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(),