Skip to content

Commit

Permalink
Parse private generator methods in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed May 28, 2022
1 parent 55060c6 commit 9435973
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 16 deletions.
18 changes: 18 additions & 0 deletions boa_engine/src/syntax/ast/node/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ pub enum PropertyName {
///
/// [spec]: https://tc39.es/ecma262/#prod-LiteralPropertyName
Literal(Sym),

/// A `Computed` property name is an expression that gets evaluated and converted into a property name.
///
/// More information:
Expand Down Expand Up @@ -389,3 +390,20 @@ impl From<Node> for PropertyName {
unsafe impl Trace for PropertyName {
unsafe_empty_trace!();
}

/// `ClassElementName` can be either a property name or a private identifier.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-ClassElementName
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Finalize)]
pub(crate) enum ClassElementName {
PropertyName(PropertyName),
PrivateIdentifier(Sym),
}

unsafe impl Trace for ClassElementName {
unsafe_empty_trace!();
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,28 @@ where
.kind()
== &TokenKind::Punctuator(Punctuator::Mul)
{
let (property_name, method) =
let position = cursor
.peek(0, interner)?
.ok_or(ParseError::AbruptEnd)?
.span()
.start();
let (class_element_name, method) =
GeneratorMethod::new(self.allow_yield, self.allow_await).parse(cursor, interner)?;
return Ok(object::PropertyDefinition::method_definition(
method,
property_name,
));

match class_element_name {
object::ClassElementName::PropertyName(property_name) => {
return Ok(object::PropertyDefinition::method_definition(
method,
property_name,
))
}
object::ClassElementName::PrivateIdentifier(_) => {
return Err(ParseError::general(
"private identifier not allowed in object literal",
position,
))
}
}
}

let mut property_name =
Expand Down Expand Up @@ -455,6 +471,62 @@ where
}
}

/// `ClassElementName` can be either a property name or a private identifier.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-ClassElementName
#[derive(Debug, Clone)]
pub(in crate::syntax::parser) struct ClassElementName {
allow_yield: AllowYield,
allow_await: AllowAwait,
}

impl ClassElementName {
/// Creates a new `ClassElementName` parser.
pub(in crate::syntax::parser) fn new<Y, A>(allow_yield: Y, allow_await: A) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
{
Self {
allow_yield: allow_yield.into(),
allow_await: allow_await.into(),
}
}
}

impl<R> TokenParser<R> for ClassElementName
where
R: Read,
{
type Output = object::ClassElementName;

fn parse(
self,
cursor: &mut Cursor<R>,
interner: &mut Interner,
) -> Result<Self::Output, ParseError> {
let _timer = Profiler::global().start_event("ClassElementName", "Parsing");

match cursor
.peek(0, interner)?
.ok_or(ParseError::AbruptEnd)?
.kind()
{
TokenKind::PrivateIdentifier(ident) => {
let ident = *ident;
cursor.next(interner).expect("token disappeared");
Ok(object::ClassElementName::PrivateIdentifier(ident))
}
_ => Ok(object::ClassElementName::PropertyName(
PropertyName::new(self.allow_yield, self.allow_await).parse(cursor, interner)?,
)),
}
}
}

/// Initializer parsing.
///
/// More information:
Expand Down Expand Up @@ -537,7 +609,7 @@ impl<R> TokenParser<R> for GeneratorMethod
where
R: Read,
{
type Output = (object::PropertyName, MethodDefinition);
type Output = (object::ClassElementName, MethodDefinition);

fn parse(
self,
Expand All @@ -547,8 +619,8 @@ where
let _timer = Profiler::global().start_event("GeneratorMethod", "Parsing");
cursor.expect(Punctuator::Mul, "generator method definition", interner)?;

let property_name =
PropertyName::new(self.allow_yield, self.allow_await).parse(cursor, interner)?;
let class_element_name =
ClassElementName::new(self.allow_yield, self.allow_await).parse(cursor, interner)?;

let params = UniqueFormalParameters::new(true, false).parse(cursor, interner)?;

Expand Down Expand Up @@ -584,7 +656,7 @@ where
)?;

Ok((
property_name,
class_element_name,
MethodDefinition::Generator(GeneratorExpr::new(None, params, body)),
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,22 +617,38 @@ where
}
let strict = cursor.strict_mode();
cursor.set_strict_mode(true);
let (property_name, method) =
let (class_element_name, method) =
GeneratorMethod::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
cursor.set_strict_mode(strict);
if r#static {
if let Some(name) = property_name.prop_name() {
if name == Sym::PROTOTYPE {

match class_element_name {
node::object::ClassElementName::PropertyName(property_name) if r#static => {
if let Some(Sym::PROTOTYPE) = property_name.prop_name() {
return Err(ParseError::general(
"class may not have static method definitions named 'prototype'",
name_position,
));
}
ClassElementNode::StaticMethodDefinition(property_name, method)
}
node::object::ClassElementName::PropertyName(property_name) => {
ClassElementNode::MethodDefinition(property_name, method)
}
node::object::ClassElementName::PrivateIdentifier(Sym::CONSTRUCTOR) => {
return Err(ParseError::general(
"class constructor may not be a private method",
name_position,
))
}
node::object::ClassElementName::PrivateIdentifier(private_ident)
if r#static =>
{
ClassElementNode::PrivateStaticMethodDefinition(private_ident, method)
}
node::object::ClassElementName::PrivateIdentifier(private_ident) => {
ClassElementNode::PrivateMethodDefinition(private_ident, method)
}
ClassElementNode::StaticMethodDefinition(property_name, method)
} else {
ClassElementNode::MethodDefinition(property_name, method)
}
}
TokenKind::Keyword((Keyword::Async, true)) => {
Expand Down

0 comments on commit 9435973

Please sign in to comment.