Skip to content

Implement support for class static initialization blocks …#1615

Merged
pgorszkowski-igalia merged 1 commit intoWebPlatformForEmbedded:wpe-2.38from
vdinak240:wpe-2.38
Mar 5, 2026
Merged

Implement support for class static initialization blocks …#1615
pgorszkowski-igalia merged 1 commit intoWebPlatformForEmbedded:wpe-2.38from
vdinak240:wpe-2.38

Conversation

@vdinak240
Copy link

@vdinak240 vdinak240 commented Feb 10, 2026

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
5cc281d

Build-Tests Layout-Tests
✅ 🛠 wpe-238-amd64-build ✅ 🧪 wpe-238-amd64-layout
✅ 🛠 wpe-238-arm32-build ✅ 🧪 wpe-238-arm32-layout

….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
Copy link

@aoikonomopoulos aoikonomopoulos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, as long as the test262 tests pass.

@pgorszkowski-igalia
Copy link

Before I merge it I have to run test262.

@pgorszkowski-igalia
Copy link

The test262 results without this change:

[47110/47190]
80943 tests run
4826 test files skipped
1307 tests failed in total
2 tests newly fail
60 tests newly pass

and the test262 results with this change:

[47110/47190]
80943 tests run
4826 test files skipped
1307 tests failed in total
2 tests newly fail
60 tests newly pass

Both are the same. No regressions.

I built(Tools/Scripts/build-webkit --jsc-only) and run(Tools/Scripts/test262-runner --release --jsc ./WebKitBuild/Release/bin/jsc) wpe-2.38 on armv8l.

@aoikonomopoulos : so I will merge it.

@pgorszkowski-igalia pgorszkowski-igalia merged commit cd5fe9d into WebPlatformForEmbedded:wpe-2.38 Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

6 participants