Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The simplified version of the evaluation of `a = a || b` is: > AssignmentExpression : LeftHandSideExpression = LogicalORExpression || LogicalANDExpression > 1. Let lRef be ? Evaluation of LeftHandSideExpression. > 2. Let llRef be ? Evaluation of LogicalORExpression. > 3. Let llVal be ? GetValue(llRef). > 4. If ToBoolean(llVal) is true > a. Perform ? PutValue(lRef, llVal). > b. return llVal. > 5. Let lrRef be ? Evaluation of LogicalANDExpression. > 6. Let rRef be ? GetValue(lrRef). > 7. Let rVal be ? GetValue(rRef). [Note GetValue(rRef) returns rRef itself] > 8. Perform ? PutValue(lRef, rVal). > 9. Return rVal. The simplified version of the evaluation of `a ||= b` is: > AssignmentExpression : LeftHandSideExpression ||= AssignmentExpression > 1. Let lRef be ? Evaluation of LeftHandSideExpression. > 2. Let lVal be ? GetValue(lRef). > 3. If ToBoolean(lVal) is true, return lVal. > 4. Let rRef be ? Evaluation of AssignmentExpression. > 5. Let rVal be ? GetValue(rRef). > 6. Perform ? PutValue(lRef, rVal). > 7. Return rVal. The difference of these is that - the evaluation of `a` is done twice for `a = a || b`, one with `1. Let lRef be ? Evaluation of LeftHandSideExpression` and one with `2. Let llRef be ? Evaluation of LogicalORExpression.`. This is same with #8366, #8367. - `PutValue(lRef, llVal)` is performed when `ToBoolean(lVal)` is `true`. So `x = x || 1` can be compressed to `x ||= 1` when the conditions written in #8366 are met and `PutValue(lRef, llVal)` does not have a side effect. When `a` is a non-global identifier (and not a reference created by a `with` statement), these conditions are met. **References** - [Spec of `||`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-binary-logical-operators-runtime-semantics-evaluation) - [Spec of `=` / `||=`](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-binary-logical-operators-runtime-semantics-evaluation)
- Loading branch information