Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: 63b7d629dd1cac21611e16eb4d3e2ef80b7a86ca
https://github.com/WebKit/WebKit/commit/63b7d629dd1cac21611e16eb4d3e2ef80b7a86ca
Author: Sosuke Suzuki <[email protected]>
Date: 2026-09-24 (Thu, 24 Sep 2026)
Changed paths:
A JSTests/stress/optional-chaining-parenthesized-call.js
M Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
M Source/JavaScriptCore/parser/ASTBuilder.h
M Source/JavaScriptCore/parser/Nodes.h
Log Message:
-----------
[JSC] Keep the call in `(a?.b)(x)` outside the optional chain
https://bugs.webkit.org/show_bug.cgi?id=325104
Reviewed by Keith Miller.
JSC parses both `(a?.b)(x)` and `a?.b(x)` into
OptionalChain(Call(a.b, x))
The parentheses end the optional chain, so the call in `(a?.b)(x)` is not part
of it [1]. When `a` is null or undefined, `(a?.b)` evaluates to undefined, and
the outer call still has to evaluate `x` and throw a TypeError. JSC instead
skipped both the argument evaluation and the call, and returned undefined.
The call was pulled into the chain to keep `this`: an OptionalChainNode is not
a location, so calling it directly would use an undefined `this` even when `a`
is not nullish.
This patch parses `(a?.b)(x)` into
Call(OptionalChain(a.b), x)
and adds OptionalChainNode::emitCallee, which loads the callee and `this`
inside the chain. `this` is still `a` when the chain does not short-circuit.
When it does, the callee is undefined and evaluation continues with the
arguments and the call.
Two other behavior changes follow from the new shape. `(a?.b)() = 1` in sloppy
mode is now a runtime ReferenceError like `(a.b)() = 1`, as specified for
function call assignment targets [2], instead of a SyntaxError. And
`x = (o?.f)()` in a class field initializer no longer drops the call.
[1]: https://tc39.es/ecma262/#sec-optional-chains
[2]:
https://tc39.es/ecma262/#sec-runtime-errors-for-function-call-assignment-targets
Test: JSTests/stress/optional-chaining-parenthesized-call.js
* JSTests/stress/optional-chaining-parenthesized-call.js: Added.
(shouldBe):
(const.arg):
(const.key):
(shouldThrowTypeError):
(shouldThrowReferenceError):
(Private.prototype.method):
(Private.call):
(Private):
(f):
(const.receiver.b):
(const.receiver.c.d):
(Field):
(testConditionContext):
(testNullish):
(testNonNullish):
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::FunctionCallValueNode::emitBytecode):
(JSC::OptionalChainNode::emitCallee):
* Source/JavaScriptCore/parser/ASTBuilder.h:
(JSC::ASTBuilder::makeFunctionCallNode):
* Source/JavaScriptCore/parser/Nodes.h:
Canonical link: https://commits.webkit.org/321867@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications