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

[Merged by Bors] - Implement optional chains #2390

Closed
wants to merge 4 commits into from
Closed

Conversation

jedel1043
Copy link
Member

@jedel1043 jedel1043 commented Oct 28, 2022

This Pull Request implements optional chains.

Example:

const adventurer = {
    name: 'Alice',
    cat: {
        name: 'Dinah'
    }
};

console.log(adventurer.cat?.name); // Dinah
console.log(adventurer.dog?.name); // undefined

Since I needed to implement Opcode::RotateLeft, and #2378 had an implementation for Opcode::RotateRight, I took the opportunity to integrate both ops into this PR (big thanks to @HalidOdat for the original implementation!).

This PR almost has 100% conformance for the optional-chaining test suite. However, there's this one test that can't be solved until we properly set function names for function expressions in object and class definitions.

@jedel1043 jedel1043 added enhancement New feature or request vm Issues and PRs related to the Boa Virtual Machine. ast Issue surrounding the abstract syntax tree labels Oct 28, 2022
@jedel1043 jedel1043 added this to the v0.17.0 milestone Oct 28, 2022
@github-actions
Copy link

Test262 conformance changes

Test result main count PR count difference
Total 93,789 93,789 0
Passed 69,215 69,271 +56
Ignored 18,352 18,352 0
Failed 6,222 6,166 -56
Panics 0 0 0
Conformance 73.80% 73.86% +0.06%
Fixed tests (56):
test/language/statements/class/elements/grammar-private-field-optional-chaining.js [strict mode] (previously Failed)
test/language/statements/class/elements/grammar-private-field-optional-chaining.js (previously Failed)
test/language/statements/class/elements/private-field-after-optional-chain.js [strict mode] (previously Failed)
test/language/statements/class/elements/private-field-after-optional-chain.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-async-square-brackets.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-async-square-brackets.js (previously Failed)
test/language/expressions/optional-chaining/call-expression.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/call-expression.js (previously Failed)
test/language/expressions/optional-chaining/new-target-optional-call.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/new-target-optional-call.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-of-type-error.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-of-type-error.js (previously Failed)
test/language/expressions/optional-chaining/optional-expression.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-expression.js (previously Failed)
test/language/expressions/optional-chaining/eval-optional-call.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/eval-optional-call.js (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-this.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-this.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-while.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-while.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-async-optional-chain-square-brackets.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-async-optional-chain-square-brackets.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-arguments.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-arguments.js (previously Failed)
test/language/expressions/optional-chaining/super-property-optional-call.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/super-property-optional-call.js (previously Failed)
test/language/expressions/optional-chaining/optional-call-preserves-this.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-call-preserves-this.js (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-identifier.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-identifier.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-in.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-in.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-identifiername.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-identifiername.js (previously Failed)
test/language/expressions/optional-chaining/runtime-semantics-evaluation.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/runtime-semantics-evaluation.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-await-of.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for-await-of.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-expression.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-prod-expression.js (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-literal.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/member-expression-async-literal.js (previously Failed)
test/language/expressions/optional-chaining/short-circuiting.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/short-circuiting.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-do.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-do.js (previously Failed)
test/language/expressions/optional-chaining/optional-chain-expression-optional-expression.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/optional-chain-expression-optional-expression.js (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for.js [strict mode] (previously Failed)
test/language/expressions/optional-chaining/iteration-statement-for.js (previously Failed)
test/language/expressions/class/elements/grammar-private-field-optional-chaining.js [strict mode] (previously Failed)
test/language/expressions/class/elements/grammar-private-field-optional-chaining.js (previously Failed)
test/language/expressions/class/elements/private-field-after-optional-chain.js [strict mode] (previously Failed)
test/language/expressions/class/elements/private-field-after-optional-chain.js (previously Failed)

@codecov
Copy link

codecov bot commented Oct 28, 2022

Codecov Report

Merging #2390 (7e0d354) into main (f446c09) will decrease coverage by 0.22%.
The diff coverage is 25.56%.

@@            Coverage Diff             @@
##             main    #2390      +/-   ##
==========================================
- Coverage   40.03%   39.81%   -0.23%     
==========================================
  Files         305      307       +2     
  Lines       23336    23547     +211     
==========================================
+ Hits         9343     9375      +32     
- Misses      13993    14172     +179     
Impacted Files Coverage Δ
boa_engine/src/syntax/ast/expression/mod.rs 38.70% <0.00%> (-1.30%) ⬇️
boa_engine/src/syntax/ast/punctuator.rs 11.11% <ø> (ø)
boa_engine/src/syntax/lexer/regex.rs 52.50% <0.00%> (-0.67%) ⬇️
boa_engine/src/vm/code_block.rs 32.56% <0.00%> (-0.17%) ⬇️
boa_engine/src/vm/mod.rs 56.20% <0.00%> (-0.94%) ⬇️
boa_engine/src/vm/opcode/jump/mod.rs 47.36% <0.00%> (-16.92%) ⬇️
boa_engine/src/vm/opcode/mod.rs 50.00% <ø> (ø)
boa_engine/src/vm/opcode/swap/mod.rs 20.00% <0.00%> (-80.00%) ⬇️
boa_engine/src/syntax/ast/expression/optional.rs 10.20% <10.20%> (ø)
boa_engine/src/bytecompiler/mod.rs 28.59% <12.14%> (-0.90%) ⬇️
... and 45 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

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

Looking very good! Thanks for the implementation :) I added some comments that could be interesting to check.

boa_engine/src/bytecompiler/mod.rs Show resolved Hide resolved
boa_engine/src/syntax/ast/expression/optional.rs Outdated Show resolved Hide resolved
boa_engine/src/syntax/ast/expression/optional.rs Outdated Show resolved Hide resolved
boa_engine/src/syntax/ast/expression/optional.rs Outdated Show resolved Hide resolved
@jedel1043 jedel1043 requested a review from Razican October 28, 2022 17:12
Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

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

Looks perfect to me :)

@HalidOdat
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Oct 29, 2022
This Pull Request implements optional chains.

Example:

```Javascript
const adventurer = {
    name: 'Alice',
    cat: {
        name: 'Dinah'
    }
};

console.log(adventurer.cat?.name); // Dinah
console.log(adventurer.dog?.name); // undefined
```

Since I needed to implement `Opcode::RotateLeft`, and #2378 had an implementation for `Opcode::RotateRight`, I took the opportunity to integrate both ops into this PR (big thanks to @HalidOdat for the original implementation!).

This PR almost has 100% conformance for the `optional-chaining` test suite. However, there's this one [test](https://github.com/tc39/test262/blob/85373b4ce12a908f8fc517093d95cf2ed2f5ee6a/test/language/expressions/optional-chaining/member-expression.js) that can't be solved until we properly set function names for function expressions in object and class definitions.
@bors
Copy link

bors bot commented Oct 29, 2022

Pull request successfully merged into main.

Build succeeded:

@bors bors bot changed the title Implement optional chains [Merged by Bors] - Implement optional chains Oct 29, 2022
@bors bors bot closed this Oct 29, 2022
@bors bors bot deleted the optional-chains branch October 29, 2022 05:23
bors bot pushed a commit that referenced this pull request Oct 30, 2022
Fixes #1917 (fixes the code example given with  the hashes)

Fixes the order dependent execution of assignment, so the following code works correctly:
```javascript
function f(x) { console.log(x) } // used to check the order of execution
let a = [[]]

(f(1), a)[(f(2), 0)][(f(3), 0)] = (f(4), 123) // 🤮
```
Prints out:
```bash
1
2
3
4
123
```
As expected

This introduces some opcodes:
 - ~~`Swap3`:  currently used only to keep some previous code working that needs refactoring.~~
 - ~~`RotateRight n`: Rotates the `n` top values from the top of the stack to the right by `1`.~~ Already added by #2390 

~~Besides the new opcodes,~~ Some opcodes pop and push order of values on the stack have been changed. To eliminate many swaps and to make this change easier.

~~This PR is still a WIP and needs more refactoring~~

This is now ready for review/merge :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ast Issue surrounding the abstract syntax tree enhancement New feature or request vm Issues and PRs related to the Boa Virtual Machine.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants