Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: bfab8953b9ed56d30b5921db9a1653e6d35c3f84
https://github.com/WebKit/WebKit/commit/bfab8953b9ed56d30b5921db9a1653e6d35c3f84
Author: Sosuke Suzuki <[email protected]>
Date: 2026-04-24 (Fri, 24 Apr 2026)
Changed paths:
A JSTests/stress/optional-chaining-in-condition-context.js
M Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp
M Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h
M Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
M Source/JavaScriptCore/parser/Nodes.h
Log Message:
-----------
[JSC] Add `emitBytecodeInConditionContext` for optional chaining
https://bugs.webkit.org/show_bug.cgi?id=310815
Reviewed by Yusuke Suzuki.
`if (a?.x)` emitted `mov Undefined` + `jfalse` on short-circuit, even
though undefined is always falsy. This wastes 2 instructions (8 -> 6)
and requires materializing the undefined constant.
Implement OptionalChainNode::emitBytecodeInConditionContext to route
the short-circuit bail-out directly to falseTarget instead of
materializing undefined. The optimization composes with existing
condition-context handling in LogicalNotNode, LogicalOpNode, and
ConditionalNode, so patterns like `if (!a?.x)`, `if (a?.x && b?.y)`,
`while (node?.next)`, and `a?.x ? 1 : 2` are optimized automatically.
`delete a?.x` is excluded since its short-circuit result is true, not
undefined.
Before:
[ 1] jundefined_or_null arg1, ->12
[ 4] get_by_id loc5, arg1, "x"
[10] jmp ->15
[12] mov loc5, Undefined
[15] jfalse loc5, ->20
[18] ret 1
[20] ret 2
After:
[ 1] jundefined_or_null arg1, ->15
[ 4] get_by_id loc5, arg1, "x"
[10] jfalse loc5, ->15
[13] ret 1
[15] ret 2
Test: JSTests/stress/optional-chaining-in-condition-context.js
* JSTests/stress/optional-chaining-in-condition-context.js: Added.
(shouldBe):
(testNested):
(testNot):
(testAnd):
(testOr):
(testTernary):
(testWhile):
(testDoWhile):
(testFor):
(testOptCall):
(testBracket):
(testDelete):
(testDeep5):
(testMixed):
(testPrimitive):
(testComplexLogic):
(testNestedTernary):
(testDoubleNot):
(testSideEffect):
(testBracketSideEffect):
(let.getterObj.get x):
(let.getterFalsy.get x):
(let.getterThrows.get x):
(testGetter):
(testGetterInTry):
(testProxy):
(testSymbol):
(testCompareUndef):
(testCompareNull):
(testCoalesce):
(testParenthesized):
(testIfElseChain):
(testSwitch):
(testPoly):
(testFinally):
(testGenerator):
(async testAsync):
(testThis):
(testInnerChain):
(testComma):
(get t):
(has):
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp:
(JSC::BytecodeGenerator::pushOptionalChainTarget):
(JSC::BytecodeGenerator::discardOptionalChainTarget):
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h:
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::OptionalChainNode::emitBytecodeInConditionContext):
* Source/JavaScriptCore/parser/Nodes.h:
Canonical link: https://commits.webkit.org/312001@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications