Skip to content
Permalink
Browse files
[JSC] Implement support for class static initialization blocks
https://bugs.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
  • Loading branch information
hyjorc1 authored and Yijia Huang committed Oct 5, 2022
1 parent 3870776 commit c33391001b54c44edeaac63efff2b07c0e57e7be
Show file tree
Hide file tree
Showing 16 changed files with 891 additions and 44 deletions.
@@ -1,2 +1,2 @@
Exception: SyntaxError: new.target is only valid inside functions.
Exception: SyntaxError: new.target is only valid inside functions or static blocks.
at globalCatchNewTargetSyntaxError.js:6
@@ -1,2 +1,2 @@
Exception: SyntaxError: new.target is only valid inside functions.
Exception: SyntaxError: new.target is only valid inside functions or static blocks.
at globalNewTargetSyntaxError.js:6
@@ -1,2 +1,2 @@
Exception: SyntaxError: new.target is only valid inside functions.
Exception: SyntaxError: new.target is only valid inside functions or static blocks.
at globalParamCatchNewTargetSyntaxError.js:7

0 comments on commit c333910

Please sign in to comment.