Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: make optional chaining expression work #1684

Conversation

andreabergia
Copy link
Contributor

@andreabergia andreabergia commented Oct 7, 2024

I.e. makes this work:

o = {a: true};
o?.['a']

Note that the spec says that the expression on the right-hand side should not be evaluated if the left-hand side object is null or undefined.

Follow-up from #1593

I.e. code such as:

```js
o = {a: true};
o?.['a']
```

Note that the spec says that the expression on the right-hand side
should _not_ be evaluated if the left-hand side object is null or
undefined.
@andreabergia andreabergia force-pushed the optional-chaining-expressions-2 branch from 28c7c84 to 52a1ce2 Compare October 7, 2024 11:52
@rbri
Copy link
Collaborator

rbri commented Oct 7, 2024

Great!

@p-bakker
Copy link
Collaborator

p-bakker commented Oct 7, 2024

I guess you're saying that the previously merged optional chaining operator support didn't properly supported computed properties?

Wrt to your 'note': think you filled #1600 for that already, no?

@andreabergia
Copy link
Contributor Author

I guess you're saying that the previously merged optional chaining operator support didn't properly supported computed properties?

It didn't support them at all - it didn't even parse the syntax 🙂

Wrt to your 'note': think you filled #1600 for that already, no?

Yeah, but I have done something smaller in scope in this PR: I've just made this new implementation work correctly per the spec. I haven't fixed the general problem because, honestly, I can't imagine how to do it without having a big performance impact.

cfw.addALoad(contextLocal);
cfw.addALoad(variableObjectLocal);
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) {
addDynamicInvoke("PROP:GETINDEX", Signatures.PROP_GET_INDEX);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome -- using the new instructions!

I do see from the coverage report that we don't get to this specific case, and given that we just changed it, I'd feel better if we did -- I have a suggestion below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I copy&pasted the code from the existing GETELEM path here:

            case Token.GETELEM:
                generateExpression(child, node); // object
                generateExpression(child.getNext(), node); // id
                cfw.addALoad(contextLocal);
                cfw.addALoad(variableObjectLocal);
                if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) {
                    addDynamicInvoke("PROP:GETINDEX", Signatures.PROP_GET_INDEX);
                } else {
                    addDynamicInvoke("PROP:GETELEMENT", Signatures.PROP_GET_ELEMENT);
                }
                break;

However, after trying to add a simple test case for this, I've actually realized that the original code I copied does not work either. I.e. if I do this:

        Utils.assertWithAllOptimizationLevelsES6(true, "o = {[0]: true}; o[0]");

then the code path for PROP:GETINDEX is not followed. So, this looks like a problem in #1645

@gbrail
Copy link
Collaborator

gbrail commented Oct 10, 2024

I see one place where I think that the code coverage could be improved. Can you please take a look? Otherwise this looks good!

@andreabergia
Copy link
Contributor Author

Going to mark this as draft after the problems I've summarized in #1688

@andreabergia
Copy link
Contributor Author

Closed in favour of #1694

@andreabergia andreabergia deleted the optional-chaining-expressions-2 branch November 28, 2024 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants