Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Parse private generator methods in classes #2092

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!();
Razican marked this conversation as resolved.
Show resolved Hide resolved
}
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