Skip to content

Conversation

@vdinak240
Copy link

https://bugswebkit.org/show_bug.cgi?id=235085 rdar://99056882

Reviewed by Yusuke Suzuki.

Class static initialization block is a new feature of a class to perform additional static initialization during class definition evaluation.

class C {
    static { /* … */ }
}

TC39 Spec: https://tc39.es/proposal-class-static-block/
TC39 Proposal: https://github.com/tc39/proposal-class-static-block
MDN Web Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_static_initialization_blocks

In this patch, static blocks are implemented as functions which are evaluated along with the initialization of static class fields during class definition evaluation. This can be further optimized by inlining static block functions to the field initialization.

  • JSTests/stress/class-static-block.js: Added. (assert):
    (A):
    (assert.C):
    (assert.B):
    (assert.D):
    (assert.A):
    (assert.A.friendA.prototype.getX):
    (assert.A.friendA.prototype.setX):
    (assert.A.prototype.getX):
    (assert.inner):
    (catch.C.prototype.async inner):
    (catch.C):
    (catch):
    (async inner.C.prototype.async inner):
    (async inner.C):
    (async inner):
    (C.inner):
    (C):
    (await.C.inner):
    (await.C):
    (await):
    (arguments.C.inner):
    (arguments.C):
    (arguments):
  • Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp: (JSC::PropertyListNode::emitBytecode):
    (JSC::FunctionCallValueNode::emitBytecode):
    (JSC::FuncExprNode::emitBytecode):
  • Source/JavaScriptCore/parser/ASTBuilder.h: (JSC::ASTBuilder::createFunctionExpr):
    (JSC::ASTBuilder::createProperty):
    (JSC::ASTBuilder::makeFunctionCallNode):
  • Source/JavaScriptCore/parser/NodeConstructors.h: (JSC::PropertyNode::PropertyNode):
    (JSC::FunctionCallValueNode::FunctionCallValueNode): (JSC::FuncExprNode::FuncExprNode):
  • Source/JavaScriptCore/parser/Nodes.h: (JSC::FuncExprNode::isStaticBlockFunction const):
  • Source/JavaScriptCore/parser/Parser.cpp: (JSC::Parser::isArrowFunctionParameters): (JSC::Parser::parseStatementListItem):
    (JSC::Parser::parseVariableDeclarationList): (JSC::Parser::parseBreakStatement):
    (JSC::Parser::parseContinueStatement):
    (JSC::Parser::parseReturnStatement):
    (JSC::Parser::parseTryStatement):
    (JSC::Parser::parseBlockStatement):
    (JSC::stringArticleForFunctionMode):
    (JSC::stringForFunctionMode):
    (JSC::Parser::parseFunctionParameters): (JSC::Parser::parseFunctionInfo):
    (JSC::Parser::parseClass):
    (JSC::Parser::parseClassFieldInitializerSourceElements): (JSC::Parser::parseAssignmentExpression): (JSC::Parser::parsePrimaryExpression):
    (JSC::Parser::parseMemberExpression):
    (JSC::Parser::parseUnaryExpression):
  • Source/JavaScriptCore/parser/Parser.h: (JSC::Scope::setSourceParseMode):
    (JSC::Scope::setIsStaticBlockScope):
    (JSC::Scope::isStaticBlockScope):
    (JSC::Parser::canUseIdentifierAwait):
    (JSC::Parser::disallowedIdentifierAwaitReason):
    (JSC::Parser::findClosetFunctionScope):
    (JSC::Parser::findClosetAsyncFunctionScope):
    (JSC::Parser::findScopeUntilStaticBlock):
  • Source/JavaScriptCore/parser/ParserModes.h: (JSC::isFunctionParseMode):
    (JSC::isMethodParseMode):
  • Source/JavaScriptCore/parser/SyntaxChecker.h: (JSC::SyntaxChecker::makeFunctionCallNode):
    (JSC::SyntaxChecker::createFunctionExpr):
    (JSC::SyntaxChecker::createProperty):
  • Source/JavaScriptCore/runtime/FunctionExecutable.cpp: (JSC::FunctionExecutable::toStringSlow):

Canonical link: https://commits.webkit.org/255173@main

Pull Request Template

File a Bug

All changes should be associated with a bug. The WebKit project is currently using Bugzilla as our bug tracker. Note that multiple changes may be associated with a single bug.

Provided Tooling

The WebKit Project strongly recommends contributors use Tools/Scripts/git-webkit to generate pull requests. See Setup and Contributing Code for how to do this.

Template

If a contributor wishes to file a pull request manually, the template is below. Manually-filed pull requests should contain their commit message as the pull request description, and their commit message should be formatted like the template below.

Additionally, the pull request should be mentioned on Bugzilla, labels applied to the pull request matching the component and version of the Bugzilla associated with the pull request and the pull request assigned to its author.

< bug title >
https://bugs.webkit.org/show_bug.cgi?id=#####

Reviewed by NOBODY (OOPS!).

Explanation of why this fixes the bug (OOPS!).

* path/changed.ext:
(function):
(class.function):

….webkit.org/show_bug.cgi?id=235085 rdar://99056882

Reviewed by Yusuke Suzuki.

Class static initialization block is a new feature of a class to perform
additional static initialization during class definition evaluation.

```
class C {
    static { /* … */ }
}
```

TC39 Spec: https://tc39.es/proposal-class-static-block/
TC39 Proposal: https://github.com/tc39/proposal-class-static-block
MDN Web Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_static_initialization_blocks

In this patch, static blocks are implemented as functions which are
evaluated along with the initialization of static class fields during
class definition evaluation. This can be further optimized by inlining
static block functions to the field initialization.

* JSTests/stress/class-static-block.js: Added.
(assert):
(A):
(assert.C):
(assert.B):
(assert.D):
(assert.A):
(assert.A.friendA.prototype.getX):
(assert.A.friendA.prototype.setX):
(assert.A.prototype.getX):
(assert.inner):
(catch.C.prototype.async inner):
(catch.C):
(catch):
(async inner.C.prototype.async inner):
(async inner.C):
(async inner):
(C.inner):
(C):
(await.C.inner):
(await.C):
(await):
(arguments.C.inner):
(arguments.C):
(arguments):
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::PropertyListNode::emitBytecode):
(JSC::FunctionCallValueNode::emitBytecode):
(JSC::FuncExprNode::emitBytecode):
* Source/JavaScriptCore/parser/ASTBuilder.h:
(JSC::ASTBuilder::createFunctionExpr):
(JSC::ASTBuilder::createProperty):
(JSC::ASTBuilder::makeFunctionCallNode):
* Source/JavaScriptCore/parser/NodeConstructors.h:
(JSC::PropertyNode::PropertyNode):
(JSC::FunctionCallValueNode::FunctionCallValueNode):
(JSC::FuncExprNode::FuncExprNode):
* Source/JavaScriptCore/parser/Nodes.h:
(JSC::FuncExprNode::isStaticBlockFunction const):
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::isArrowFunctionParameters):
(JSC::Parser<LexerType>::parseStatementListItem):
(JSC::Parser<LexerType>::parseVariableDeclarationList):
(JSC::Parser<LexerType>::parseBreakStatement):
(JSC::Parser<LexerType>::parseContinueStatement):
(JSC::Parser<LexerType>::parseReturnStatement):
(JSC::Parser<LexerType>::parseTryStatement):
(JSC::Parser<LexerType>::parseBlockStatement):
(JSC::stringArticleForFunctionMode):
(JSC::stringForFunctionMode):
(JSC::Parser<LexerType>::parseFunctionParameters):
(JSC::Parser<LexerType>::parseFunctionInfo):
(JSC::Parser<LexerType>::parseClass):
(JSC::Parser<LexerType>::parseClassFieldInitializerSourceElements):
(JSC::Parser<LexerType>::parseAssignmentExpression):
(JSC::Parser<LexerType>::parsePrimaryExpression):
(JSC::Parser<LexerType>::parseMemberExpression):
(JSC::Parser<LexerType>::parseUnaryExpression):
* Source/JavaScriptCore/parser/Parser.h:
(JSC::Scope::setSourceParseMode):
(JSC::Scope::setIsStaticBlockScope):
(JSC::Scope::isStaticBlockScope):
(JSC::Parser::canUseIdentifierAwait):
(JSC::Parser::disallowedIdentifierAwaitReason):
(JSC::Parser::findClosetFunctionScope):
(JSC::Parser::findClosetAsyncFunctionScope):
(JSC::Parser::findScopeUntilStaticBlock):
* Source/JavaScriptCore/parser/ParserModes.h:
(JSC::isFunctionParseMode):
(JSC::isMethodParseMode):
* Source/JavaScriptCore/parser/SyntaxChecker.h:
(JSC::SyntaxChecker::makeFunctionCallNode):
(JSC::SyntaxChecker::createFunctionExpr):
(JSC::SyntaxChecker::createProperty):
* Source/JavaScriptCore/runtime/FunctionExecutable.cpp:
(JSC::FunctionExecutable::toStringSlow):

Canonical link: https://commits.webkit.org/255173@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants