Repository navigation
Commit 63b7d62
committed
[JSC] Keep the call in
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(a?.b)(x) outside the optional chain1 parent 2730e16 commit 63b7d62
4 files changed
Lines changed: 186 additions & 6 deletions
File tree
- JSTests/stress
- Source/JavaScriptCore
- bytecompiler
- parser
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1399 | 1399 | | |
1400 | 1400 | | |
1401 | 1401 | | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
1402 | 1414 | | |
1403 | 1415 | | |
1404 | 1416 | | |
| |||
3599 | 3611 | | |
3600 | 3612 | | |
3601 | 3613 | | |
| 3614 | + | |
| 3615 | + | |
| 3616 | + | |
| 3617 | + | |
| 3618 | + | |
| 3619 | + | |
| 3620 | + | |
| 3621 | + | |
| 3622 | + | |
| 3623 | + | |
| 3624 | + | |
| 3625 | + | |
| 3626 | + | |
| 3627 | + | |
| 3628 | + | |
| 3629 | + | |
| 3630 | + | |
| 3631 | + | |
| 3632 | + | |
| 3633 | + | |
| 3634 | + | |
| 3635 | + | |
| 3636 | + | |
| 3637 | + | |
| 3638 | + | |
| 3639 | + | |
| 3640 | + | |
| 3641 | + | |
| 3642 | + | |
| 3643 | + | |
| 3644 | + | |
| 3645 | + | |
| 3646 | + | |
| 3647 | + | |
| 3648 | + | |
| 3649 | + | |
| 3650 | + | |
| 3651 | + | |
| 3652 | + | |
| 3653 | + | |
3602 | 3654 | | |
3603 | 3655 | | |
3604 | 3656 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1473 | 1473 | | |
1474 | 1474 | | |
1475 | 1475 | | |
1476 | | - | |
| 1476 | + | |
1477 | 1477 | | |
1478 | 1478 | | |
1479 | 1479 | | |
1480 | | - | |
1481 | | - | |
1482 | | - | |
1483 | | - | |
1484 | | - | |
| 1480 | + | |
| 1481 | + | |
1485 | 1482 | | |
1486 | 1483 | | |
1487 | 1484 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
| 1453 | + | |
| 1454 | + | |
1453 | 1455 | | |
1454 | 1456 | | |
1455 | 1457 | | |
| |||
0 commit comments