From a3c21f41a2210a1f2a7072ff4a09bf43dd8bc7dd Mon Sep 17 00:00:00 2001 From: Leo Balter Date: Wed, 9 Oct 2019 18:11:34 -0400 Subject: [PATCH 1/4] Add tests for Nullish Coalesce Expression --- .../coalesce/abrupt-is-a-short-circuit.js | 58 ++++++++++++ .../cannot-chain-head-with-logical-and.js | 29 ++++++ .../cannot-chain-head-with-logical-or.js | 29 ++++++ .../cannot-chain-tail-with-logical-and.js | 30 +++++++ .../cannot-chain-tail-with-logical-or.js | 30 +++++++ .../coalesce/chainable-with-bitwise-and.js | 49 ++++++++++ .../coalesce/chainable-with-bitwise-or.js | 49 ++++++++++ .../coalesce/chainable-with-bitwise-xor.js | 49 ++++++++++ .../expressions/coalesce/chainable.js | 50 +++++++++++ .../expressions/coalesce/follows-null.js | 49 ++++++++++ .../expressions/coalesce/follows-undefined.js | 49 ++++++++++ .../coalesce/short-circuit-number-0.js | 81 +++++++++++++++++ .../coalesce/short-circuit-number-42.js | 81 +++++++++++++++++ .../short-circuit-number-empty-string.js | 82 +++++++++++++++++ .../coalesce/short-circuit-number-false.js | 81 +++++++++++++++++ .../coalesce/short-circuit-number-object.js | 89 +++++++++++++++++++ .../coalesce/short-circuit-number-string.js | 82 +++++++++++++++++ .../coalesce/short-circuit-number-symbol.js | 82 +++++++++++++++++ .../coalesce/short-circuit-number-true.js | 81 +++++++++++++++++ .../short-circuit-prevents-evaluation.js | 56 ++++++++++++ .../language/expressions/coalesce/tco-cond.js | 19 ++++ .../expressions/coalesce/tco-pos-null.js | 19 ++++ .../expressions/coalesce/tco-pos-undefined.js | 19 ++++ .../conditional/coalesce-expr-ternary.js | 73 +++++++++++++++ 24 files changed, 1316 insertions(+) create mode 100644 test/language/expressions/coalesce/abrupt-is-a-short-circuit.js create mode 100644 test/language/expressions/coalesce/cannot-chain-head-with-logical-and.js create mode 100644 test/language/expressions/coalesce/cannot-chain-head-with-logical-or.js create mode 100644 test/language/expressions/coalesce/cannot-chain-tail-with-logical-and.js create mode 100644 test/language/expressions/coalesce/cannot-chain-tail-with-logical-or.js create mode 100644 test/language/expressions/coalesce/chainable-with-bitwise-and.js create mode 100644 test/language/expressions/coalesce/chainable-with-bitwise-or.js create mode 100644 test/language/expressions/coalesce/chainable-with-bitwise-xor.js create mode 100644 test/language/expressions/coalesce/chainable.js create mode 100644 test/language/expressions/coalesce/follows-null.js create mode 100644 test/language/expressions/coalesce/follows-undefined.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-0.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-42.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-empty-string.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-false.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-object.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-string.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-symbol.js create mode 100644 test/language/expressions/coalesce/short-circuit-number-true.js create mode 100644 test/language/expressions/coalesce/short-circuit-prevents-evaluation.js create mode 100644 test/language/expressions/coalesce/tco-cond.js create mode 100644 test/language/expressions/coalesce/tco-pos-null.js create mode 100644 test/language/expressions/coalesce/tco-pos-undefined.js create mode 100644 test/language/expressions/conditional/coalesce-expr-ternary.js diff --git a/test/language/expressions/coalesce/abrupt-is-a-short-circuit.js b/test/language/expressions/coalesce/abrupt-is-a-short-circuit.js new file mode 100644 index 00000000000..94dd038372b --- /dev/null +++ b/test/language/expressions/coalesce/abrupt-is-a-short-circuit.js @@ -0,0 +1,58 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Abrupt completions are also a Short circuit and prevent evaluation of the right-side expressions +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +function poison() { + throw new Test262Error('poison handled'); +} + +function morePoison() { + throw 'poison!!!!'; +} + +x = undefined; +assert.throws(Test262Error, function() { + undefined ?? poison() ?? morePoison(); +}, 'undefined ?? poison() ?? morePoison();'); + +x = undefined; +assert.throws(Test262Error, function() { + null ?? poison() ?? morePoison(); +}, 'null ?? poison() ?? morePoison();'); + +assert.throws(Test262Error, function() { + poison() ?? morePoison(); +}, 'poison() ?? morePoison();'); diff --git a/test/language/expressions/coalesce/cannot-chain-head-with-logical-and.js b/test/language/expressions/coalesce/cannot-chain-head-with-logical-and.js new file mode 100644 index 00000000000..fc9d27d464e --- /dev/null +++ b/test/language/expressions/coalesce/cannot-chain-head-with-logical-and.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Cannot immediately contain, or be contained within, an && or || operation. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression +features: [coalesce-expression] +negative: + phase: early + type: SyntaxError +---*/ + +0 && 0 ?? true; \ No newline at end of file diff --git a/test/language/expressions/coalesce/cannot-chain-head-with-logical-or.js b/test/language/expressions/coalesce/cannot-chain-head-with-logical-or.js new file mode 100644 index 00000000000..658d15555d7 --- /dev/null +++ b/test/language/expressions/coalesce/cannot-chain-head-with-logical-or.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Cannot immediately contain, or be contained within, an && or || operation. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression +features: [coalesce-expression] +negative: + phase: early + type: SyntaxError +---*/ + +0 || 0 ?? true; \ No newline at end of file diff --git a/test/language/expressions/coalesce/cannot-chain-tail-with-logical-and.js b/test/language/expressions/coalesce/cannot-chain-tail-with-logical-and.js new file mode 100644 index 00000000000..f0c2dd77f74 --- /dev/null +++ b/test/language/expressions/coalesce/cannot-chain-tail-with-logical-and.js @@ -0,0 +1,30 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + If the CoalesceExpressionHead is undefined or null, follow return the right-side value. + Otherwise, return the left-side value. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression +features: [coalesce-expression] +negative: + phase: early + type: SyntaxError +---*/ + +0 ?? 0 && true; \ No newline at end of file diff --git a/test/language/expressions/coalesce/cannot-chain-tail-with-logical-or.js b/test/language/expressions/coalesce/cannot-chain-tail-with-logical-or.js new file mode 100644 index 00000000000..00028c3e1db --- /dev/null +++ b/test/language/expressions/coalesce/cannot-chain-tail-with-logical-or.js @@ -0,0 +1,30 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + If the CoalesceExpressionHead is undefined or null, follow return the right-side value. + Otherwise, return the left-side value. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression +features: [coalesce-expression] +negative: + phase: early + type: SyntaxError +---*/ + +0 ?? 0 || true; \ No newline at end of file diff --git a/test/language/expressions/coalesce/chainable-with-bitwise-and.js b/test/language/expressions/coalesce/chainable-with-bitwise-and.js new file mode 100644 index 00000000000..f88b4155d52 --- /dev/null +++ b/test/language/expressions/coalesce/chainable-with-bitwise-and.js @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + CoalesceExpression is chainable with the BitwiseANDExpression +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = null ?? 42 & 43; +assert.sameValue(x, 42, 'null ?? 42 & 43'); + +x = undefined ?? 42 & 43; +assert.sameValue(x, 42, 'null ?? 42 & 43'); + +x = false ?? 42 & 43; +assert.sameValue(x, false, 'false ?? 42 & 43'); + +x = true ?? 42 & 43; +assert.sameValue(x, true, 'true ?? 42 & 43'); diff --git a/test/language/expressions/coalesce/chainable-with-bitwise-or.js b/test/language/expressions/coalesce/chainable-with-bitwise-or.js new file mode 100644 index 00000000000..e6441bca363 --- /dev/null +++ b/test/language/expressions/coalesce/chainable-with-bitwise-or.js @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + CoalesceExpression is chainable with the BitwiseORExpression +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = null ?? 1 | 42; +assert.sameValue(x, 43, 'null ?? 1 | 42'); + +x = undefined ?? 1 | 42; +assert.sameValue(x, 43, 'null ?? 1 | 42'); + +x = false ?? 1 | 42; +assert.sameValue(x, false, 'false ?? 1 | 42'); + +x = true ?? 1 | 42; +assert.sameValue(x, true, 'true ?? 1 | 42'); diff --git a/test/language/expressions/coalesce/chainable-with-bitwise-xor.js b/test/language/expressions/coalesce/chainable-with-bitwise-xor.js new file mode 100644 index 00000000000..230c590a4cc --- /dev/null +++ b/test/language/expressions/coalesce/chainable-with-bitwise-xor.js @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + CoalesceExpression is chainable with the BitwiseXORExpression +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = null ?? 1 ^ 42; +assert.sameValue(x, 43, 'null ?? 1 ^ 42'); + +x = undefined ?? 1 ^ 42; +assert.sameValue(x, 43, 'null ?? 1 ^ 42'); + +x = false ?? 1 ^ 42; +assert.sameValue(x, false, 'false ?? 1 ^ 42'); + +x = true ?? 1 ^ 42; +assert.sameValue(x, true, 'true ?? 1 ^ 42'); diff --git a/test/language/expressions/coalesce/chainable.js b/test/language/expressions/coalesce/chainable.js new file mode 100644 index 00000000000..948645f9a12 --- /dev/null +++ b/test/language/expressions/coalesce/chainable.js @@ -0,0 +1,50 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + If the CoalesceExpressionHead is undefined or null, follow return the right-side value. + Otherwise, return the left-side value. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = null ?? undefined ?? 42; +assert.sameValue(x, 42, 'null ?? undefined ?? 42'); + +x = undefined ?? null ?? 42; +assert.sameValue(x, 42, 'undefined ?? null ?? 42'); + +x = null ?? null ?? 42; +assert.sameValue(x, 42, 'null ?? null ?? 42'); + +x = undefined ?? undefined ?? 42; +assert.sameValue(x, 42, 'null ?? null ?? 42'); diff --git a/test/language/expressions/coalesce/follows-null.js b/test/language/expressions/coalesce/follows-null.js new file mode 100644 index 00000000000..64068a5a559 --- /dev/null +++ b/test/language/expressions/coalesce/follows-null.js @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + If the CoalesceExpressionHead is null, follow return the right-side eval. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = null ?? 42; +assert.sameValue(x, 42, 'null ?? 42'); + +x = null ?? undefined; +assert.sameValue(x, undefined, 'null ?? undefined'); + +x = null ?? null; +assert.sameValue(x, null, 'null ?? null'); + +x = null ?? false; +assert.sameValue(x, false, 'null ?? false'); diff --git a/test/language/expressions/coalesce/follows-undefined.js b/test/language/expressions/coalesce/follows-undefined.js new file mode 100644 index 00000000000..050a91775ff --- /dev/null +++ b/test/language/expressions/coalesce/follows-undefined.js @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + If the CoalesceExpressionHead is undefined, follow return the right-side eval. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined ?? 42; +assert.sameValue(x, 42, 'undefined ?? 42'); + +x = undefined ?? undefined; +assert.sameValue(x, undefined, 'undefined ?? undefined'); + +x = undefined ?? null; +assert.sameValue(x, null, 'undefined ?? null'); + +x = undefined ?? false; +assert.sameValue(x, false, 'undefined ?? false'); diff --git a/test/language/expressions/coalesce/short-circuit-number-0.js b/test/language/expressions/coalesce/short-circuit-number-0.js new file mode 100644 index 00000000000..54ec3a54f1e --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-0.js @@ -0,0 +1,81 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (0) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = 0 ?? 1; +assert.sameValue(x, 0, '0 ?? 1'); + +x = undefined; +x = 0 ?? null; +assert.sameValue(x, 0, '0 ?? null'); + +x = undefined; +x = 0 ?? undefined; +assert.sameValue(x, 0, '0 ?? undefined'); + +x = undefined; +x = 0 ?? null ?? undefined; +assert.sameValue(x, 0, '0 ?? null ?? undefined'); + +x = undefined; +x = 0 ?? undefined ?? null; +assert.sameValue(x, 0, '0 ?? undefined ?? null'); + +x = undefined; +x = 0 ?? null ?? null; +assert.sameValue(x, 0, '0 ?? null ?? null'); + +x = undefined; +x = 0 ?? undefined ?? undefined; +assert.sameValue(x, 0, '0 ?? null ?? null'); + +x = undefined; +x = null ?? 0 ?? null; +assert.sameValue(x, 0, 'null ?? 0 ?? null'); + +x = undefined; +x = null ?? 0 ?? undefined; +assert.sameValue(x, 0, 'null ?? 0 ?? undefined'); + +x = undefined; +x = undefined ?? 0 ?? null; +assert.sameValue(x, 0, 'undefined ?? 0 ?? null'); + +x = undefined; +x = undefined ?? 0 ?? undefined; +assert.sameValue(x, 0, 'undefined ?? 0 ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-42.js b/test/language/expressions/coalesce/short-circuit-number-42.js new file mode 100644 index 00000000000..b3c58b54278 --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-42.js @@ -0,0 +1,81 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (42) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = 42 ?? 1; +assert.sameValue(x, 42, '42 ?? 1'); + +x = undefined; +x = 42 ?? null; +assert.sameValue(x, 42, '42 ?? null'); + +x = undefined; +x = 42 ?? undefined; +assert.sameValue(x, 42, '42 ?? undefined'); + +x = undefined; +x = 42 ?? null ?? undefined; +assert.sameValue(x, 42, '42 ?? null ?? undefined'); + +x = undefined; +x = 42 ?? undefined ?? null; +assert.sameValue(x, 42, '42 ?? undefined ?? null'); + +x = undefined; +x = 42 ?? null ?? null; +assert.sameValue(x, 42, '42 ?? null ?? null'); + +x = undefined; +x = 42 ?? undefined ?? undefined; +assert.sameValue(x, 42, '42 ?? null ?? null'); + +x = undefined; +x = null ?? 42 ?? null; +assert.sameValue(x, 42, 'null ?? 42 ?? null'); + +x = undefined; +x = null ?? 42 ?? undefined; +assert.sameValue(x, 42, 'null ?? 42 ?? undefined'); + +x = undefined; +x = undefined ?? 42 ?? null; +assert.sameValue(x, 42, 'undefined ?? 42 ?? null'); + +x = undefined; +x = undefined ?? 42 ?? undefined; +assert.sameValue(x, 42, 'undefined ?? 42 ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-empty-string.js b/test/language/expressions/coalesce/short-circuit-number-empty-string.js new file mode 100644 index 00000000000..0868fc800ed --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-empty-string.js @@ -0,0 +1,82 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (the empty string) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +var str = ''; + +x = undefined; +x = str ?? 1; +assert.sameValue(x, str, 'str ?? 1'); + +x = undefined; +x = str ?? null; +assert.sameValue(x, str, 'str ?? null'); + +x = undefined; +x = str ?? undefined; +assert.sameValue(x, str, 'str ?? undefined'); + +x = undefined; +x = str ?? null ?? undefined; +assert.sameValue(x, str, 'str ?? null ?? undefined'); + +x = undefined; +x = str ?? undefined ?? null; +assert.sameValue(x, str, 'str ?? undefined ?? null'); + +x = undefined; +x = str ?? null ?? null; +assert.sameValue(x, str, 'str ?? null ?? null'); + +x = undefined; +x = str ?? undefined ?? undefined; +assert.sameValue(x, str, 'str ?? null ?? null'); + +x = undefined; +x = null ?? str ?? null; +assert.sameValue(x, str, 'null ?? str ?? null'); + +x = undefined; +x = null ?? str ?? undefined; +assert.sameValue(x, str, 'null ?? str ?? undefined'); + +x = undefined; +x = undefined ?? str ?? null; +assert.sameValue(x, str, 'undefined ?? str ?? null'); + +x = undefined; +x = undefined ?? str ?? undefined; +assert.sameValue(x, str, 'undefined ?? str ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-false.js b/test/language/expressions/coalesce/short-circuit-number-false.js new file mode 100644 index 00000000000..d11733f9e43 --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-false.js @@ -0,0 +1,81 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (false) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = false ?? 1; +assert.sameValue(x, false, 'false ?? 1'); + +x = undefined; +x = false ?? null; +assert.sameValue(x, false, 'false ?? null'); + +x = undefined; +x = false ?? undefined; +assert.sameValue(x, false, 'false ?? undefined'); + +x = undefined; +x = false ?? null ?? undefined; +assert.sameValue(x, false, 'false ?? null ?? undefined'); + +x = undefined; +x = false ?? undefined ?? null; +assert.sameValue(x, false, 'false ?? undefined ?? null'); + +x = undefined; +x = false ?? null ?? null; +assert.sameValue(x, false, 'false ?? null ?? null'); + +x = undefined; +x = false ?? undefined ?? undefined; +assert.sameValue(x, false, 'false ?? null ?? null'); + +x = undefined; +x = null ?? false ?? null; +assert.sameValue(x, false, 'null ?? false ?? null'); + +x = undefined; +x = null ?? false ?? undefined; +assert.sameValue(x, false, 'null ?? false ?? undefined'); + +x = undefined; +x = undefined ?? false ?? null; +assert.sameValue(x, false, 'undefined ?? false ?? null'); + +x = undefined; +x = undefined ?? false ?? undefined; +assert.sameValue(x, false, 'undefined ?? false ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-object.js b/test/language/expressions/coalesce/short-circuit-number-object.js new file mode 100644 index 00000000000..dce37d765ef --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-object.js @@ -0,0 +1,89 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (object) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +var obj = { + toString() { + return null; + }, + valueOf() { + return null; + } +}; + +x = undefined; +x = obj ?? 1; +assert.sameValue(x, obj, 'obj ?? 1'); + +x = undefined; +x = obj ?? null; +assert.sameValue(x, obj, 'obj ?? null'); + +x = undefined; +x = obj ?? undefined; +assert.sameValue(x, obj, 'obj ?? undefined'); + +x = undefined; +x = obj ?? null ?? undefined; +assert.sameValue(x, obj, 'obj ?? null ?? undefined'); + +x = undefined; +x = obj ?? undefined ?? null; +assert.sameValue(x, obj, 'obj ?? undefined ?? null'); + +x = undefined; +x = obj ?? null ?? null; +assert.sameValue(x, obj, 'obj ?? null ?? null'); + +x = undefined; +x = obj ?? undefined ?? undefined; +assert.sameValue(x, obj, 'obj ?? null ?? null'); + +x = undefined; +x = null ?? obj ?? null; +assert.sameValue(x, obj, 'null ?? obj ?? null'); + +x = undefined; +x = null ?? obj ?? undefined; +assert.sameValue(x, obj, 'null ?? obj ?? undefined'); + +x = undefined; +x = undefined ?? obj ?? null; +assert.sameValue(x, obj, 'undefined ?? obj ?? null'); + +x = undefined; +x = undefined ?? obj ?? undefined; +assert.sameValue(x, obj, 'undefined ?? obj ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-string.js b/test/language/expressions/coalesce/short-circuit-number-string.js new file mode 100644 index 00000000000..38c60eafe46 --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-string.js @@ -0,0 +1,82 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (string) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +var str = 'undefined'; + +x = undefined; +x = str ?? 1; +assert.sameValue(x, str, 'str ?? 1'); + +x = undefined; +x = str ?? null; +assert.sameValue(x, str, 'str ?? null'); + +x = undefined; +x = str ?? undefined; +assert.sameValue(x, str, 'str ?? undefined'); + +x = undefined; +x = str ?? null ?? undefined; +assert.sameValue(x, str, 'str ?? null ?? undefined'); + +x = undefined; +x = str ?? undefined ?? null; +assert.sameValue(x, str, 'str ?? undefined ?? null'); + +x = undefined; +x = str ?? null ?? null; +assert.sameValue(x, str, 'str ?? null ?? null'); + +x = undefined; +x = str ?? undefined ?? undefined; +assert.sameValue(x, str, 'str ?? null ?? null'); + +x = undefined; +x = null ?? str ?? null; +assert.sameValue(x, str, 'null ?? str ?? null'); + +x = undefined; +x = null ?? str ?? undefined; +assert.sameValue(x, str, 'null ?? str ?? undefined'); + +x = undefined; +x = undefined ?? str ?? null; +assert.sameValue(x, str, 'undefined ?? str ?? null'); + +x = undefined; +x = undefined ?? str ?? undefined; +assert.sameValue(x, str, 'undefined ?? str ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-symbol.js b/test/language/expressions/coalesce/short-circuit-number-symbol.js new file mode 100644 index 00000000000..fccfded6815 --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-symbol.js @@ -0,0 +1,82 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (Symbol) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +var s = Symbol(); + +x = undefined; +x = s ?? 1; +assert.sameValue(x, s, 's ?? null'); + +x = undefined; +x = s ?? null; +assert.sameValue(x, s, 's ?? null'); + +x = undefined; +x = s ?? undefined; +assert.sameValue(x, s, 's ?? undefined'); + +x = undefined; +x = s ?? null ?? undefined; +assert.sameValue(x, s, 's ?? null ?? undefined'); + +x = undefined; +x = s ?? undefined ?? null; +assert.sameValue(x, s, 's ?? undefined ?? null'); + +x = undefined; +x = s ?? null ?? null; +assert.sameValue(x, s, 's ?? null ?? null'); + +x = undefined; +x = s ?? undefined ?? undefined; +assert.sameValue(x, s, 's ?? null ?? null'); + +x = undefined; +x = null ?? s ?? null; +assert.sameValue(x, s, 'null ?? s ?? null'); + +x = undefined; +x = null ?? s ?? undefined; +assert.sameValue(x, s, 'null ?? s ?? undefined'); + +x = undefined; +x = undefined ?? s ?? null; +assert.sameValue(x, s, 'undefined ?? s ?? null'); + +x = undefined; +x = undefined ?? s ?? undefined; +assert.sameValue(x, s, 'undefined ?? s ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-number-true.js b/test/language/expressions/coalesce/short-circuit-number-true.js new file mode 100644 index 00000000000..778f8087fde --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-number-true.js @@ -0,0 +1,81 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit if the CoalesceExpressionHead is not undefined or null (true) +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = true ?? 1; +assert.sameValue(x, true, 'true ?? null'); + +x = undefined; +x = true ?? null; +assert.sameValue(x, true, 'true ?? null'); + +x = undefined; +x = true ?? undefined; +assert.sameValue(x, true, 'true ?? undefined'); + +x = undefined; +x = true ?? null ?? undefined; +assert.sameValue(x, true, 'true ?? null ?? undefined'); + +x = undefined; +x = true ?? undefined ?? null; +assert.sameValue(x, true, 'true ?? undefined ?? null'); + +x = undefined; +x = true ?? null ?? null; +assert.sameValue(x, true, 'true ?? null ?? null'); + +x = undefined; +x = true ?? undefined ?? undefined; +assert.sameValue(x, true, 'true ?? null ?? null'); + +x = undefined; +x = null ?? true ?? null; +assert.sameValue(x, true, 'null ?? true ?? null'); + +x = undefined; +x = null ?? true ?? undefined; +assert.sameValue(x, true, 'null ?? true ?? undefined'); + +x = undefined; +x = undefined ?? true ?? null; +assert.sameValue(x, true, 'undefined ?? true ?? null'); + +x = undefined; +x = undefined ?? true ?? undefined; +assert.sameValue(x, true, 'undefined ?? true ?? undefined'); diff --git a/test/language/expressions/coalesce/short-circuit-prevents-evaluation.js b/test/language/expressions/coalesce/short-circuit-prevents-evaluation.js new file mode 100644 index 00000000000..b064f5a2a6b --- /dev/null +++ b/test/language/expressions/coalesce/short-circuit-prevents-evaluation.js @@ -0,0 +1,56 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + Short circuit can prevent evaluation of the right-side expressions +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; +function poison() { + throw new Test262Error('should not evaluate poison'); +} + +x = undefined; +x = undefined ?? 42 ?? undefined ?? poison(); +assert.sameValue(x, 42); + +x = undefined; +x = 42 ?? undefined ?? poison(); +assert.sameValue(x, 42); + +x = undefined; +x = undefined ?? 42 ?? poison(); +assert.sameValue(x, 42); + +x = undefined; +x = 42 ?? poison(); +assert.sameValue(x, 42); diff --git a/test/language/expressions/coalesce/tco-cond.js b/test/language/expressions/coalesce/tco-cond.js new file mode 100644 index 00000000000..9852200e336 --- /dev/null +++ b/test/language/expressions/coalesce/tco-cond.js @@ -0,0 +1,19 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: Expression is a candidate for tail-call optimization. +esid: sec-static-semantics-hascallintailposition +flags: [onlyStrict] +features: [tail-call-optimization, coalesce-expression] +includes: [tcoHelper.js] +---*/ + +var callCount = 0; +(function f(n) { + if (n === 0) { + callCount += 1 + return; + } + return f(n - 1) ?? false; +}($MAX_ITERATIONS)); +assert.sameValue(callCount, 1); diff --git a/test/language/expressions/coalesce/tco-pos-null.js b/test/language/expressions/coalesce/tco-pos-null.js new file mode 100644 index 00000000000..cf706133a54 --- /dev/null +++ b/test/language/expressions/coalesce/tco-pos-null.js @@ -0,0 +1,19 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: Expression is a candidate for tail-call optimization. +esid: sec-static-semantics-hascallintailposition +flags: [onlyStrict] +features: [tail-call-optimization, coalesce-expression] +includes: [tcoHelper.js] +---*/ + +var callCount = 0; +(function f(n) { + if (n === 0) { + callCount += 1 + return; + } + return null ?? f(n - 1); +}($MAX_ITERATIONS)); +assert.sameValue(callCount, 1); diff --git a/test/language/expressions/coalesce/tco-pos-undefined.js b/test/language/expressions/coalesce/tco-pos-undefined.js new file mode 100644 index 00000000000..53a2d4f7cbf --- /dev/null +++ b/test/language/expressions/coalesce/tco-pos-undefined.js @@ -0,0 +1,19 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: Expression is a candidate for tail-call optimization. +esid: sec-static-semantics-hascallintailposition +flags: [onlyStrict] +features: [tail-call-optimization, coalesce-expression] +includes: [tcoHelper.js] +---*/ + +var callCount = 0; +(function f(n) { + if (n === 0) { + callCount += 1 + return; + } + return undefined ?? f(n - 1); +}($MAX_ITERATIONS)); +assert.sameValue(callCount, 1); diff --git a/test/language/expressions/conditional/coalesce-expr-ternary.js b/test/language/expressions/conditional/coalesce-expr-ternary.js new file mode 100644 index 00000000000..0d319143785 --- /dev/null +++ b/test/language/expressions/conditional/coalesce-expr-ternary.js @@ -0,0 +1,73 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + ShortCircuitExpression in the Conditional Expression (? :) +esid: sec-conditional-operator +info: | + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression +features: [coalesce-expression] +---*/ + +var x; + +x = undefined ?? true ? 0 : 42; +assert.sameValue(x, 0, 'undefined ?? true ? 0 : 42'); + +x = undefined; +x = null ?? true ? 0 : 42; +assert.sameValue(x, 0, 'null ?? true ? 0 : 42'); + +x = undefined; +x = undefined ?? false ? 0 : 42; +assert.sameValue(x, 42, 'undefined ?? false ? 0 : 42'); + +x = undefined; +x = null ?? false ? 0 : 42; +assert.sameValue(x, 42, 'null ?? false ? 0 : 42'); + +x = undefined; +x = false ?? true ? 0 : 42; +assert.sameValue(x, 42, 'false ?? true ? 0 : 42'); + +x = undefined; +x = 0 ?? true ? 0 : 42; +assert.sameValue(x, 42, '0 ?? true ? 0 : 42'); + +x = undefined; +x = 1 ?? false ? 0 : 42; +assert.sameValue(x, 0, '1 ?? false ? 0 : 42'); + +x = undefined; +x = true ?? false ? 0 : 42; +assert.sameValue(x, 0, 'true ?? false ? 0 : 42'); + +x = undefined; +x = true ?? true ? 0 : 42; +assert.sameValue(x, 0, 'true ?? true ? 0 : 42'); + +x = undefined; +x = '' ?? true ? 0 : 42; +assert.sameValue(x, 42, '"" ?? true ? 0 : 42'); + +x = undefined; +x = Symbol() ?? false ? 0 : 42; +assert.sameValue(x, 0, 'Symbol() ?? false ? 0 : 42'); + +x = undefined; +x = {} ?? false ? 0 : 42; +assert.sameValue(x, 0, 'object ?? false ? 0 : 42'); From 1c26f3f434f5be3bbc4d5d7d17eda7e131fa3f6e Mon Sep 17 00:00:00 2001 From: Leo Balter Date: Wed, 9 Oct 2019 18:14:51 -0400 Subject: [PATCH 2/4] Add feature --- features.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/features.txt b/features.txt index 9e32bb4f40d..5081f6c2272 100644 --- a/features.txt +++ b/features.txt @@ -145,6 +145,10 @@ top-level-await # https://github.com/tc39/proposal-regexp-match-indices regexp-match-indices +# Nullish Coalesce Expression +# https://github.com/tc39/proposal-nullish-coalescing +coalesce-expression + ## Standard language features # # Language features that have been included in a published version of the From 5c35ce9d9c5e01496897cc0ebb3e86238be97e81 Mon Sep 17 00:00:00 2001 From: Leo Balter Date: Wed, 9 Oct 2019 18:20:45 -0400 Subject: [PATCH 3/4] Improve metadata --- .../language/expressions/coalesce/tco-cond.js | 19 ------------------- .../expressions/coalesce/tco-pos-null.js | 7 +++++++ .../expressions/coalesce/tco-pos-undefined.js | 7 +++++++ 3 files changed, 14 insertions(+), 19 deletions(-) delete mode 100644 test/language/expressions/coalesce/tco-cond.js diff --git a/test/language/expressions/coalesce/tco-cond.js b/test/language/expressions/coalesce/tco-cond.js deleted file mode 100644 index 9852200e336..00000000000 --- a/test/language/expressions/coalesce/tco-cond.js +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (C) 2019 Leo Balter. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. -/*--- -description: Expression is a candidate for tail-call optimization. -esid: sec-static-semantics-hascallintailposition -flags: [onlyStrict] -features: [tail-call-optimization, coalesce-expression] -includes: [tcoHelper.js] ----*/ - -var callCount = 0; -(function f(n) { - if (n === 0) { - callCount += 1 - return; - } - return f(n - 1) ?? false; -}($MAX_ITERATIONS)); -assert.sameValue(callCount, 1); diff --git a/test/language/expressions/coalesce/tco-pos-null.js b/test/language/expressions/coalesce/tco-pos-null.js index cf706133a54..93c24453a32 100644 --- a/test/language/expressions/coalesce/tco-pos-null.js +++ b/test/language/expressions/coalesce/tco-pos-null.js @@ -1,8 +1,15 @@ // Copyright (C) 2019 Leo Balter. All rights reserved. // This code is governed by the BSD license found in the LICENSE file. + /*--- description: Expression is a candidate for tail-call optimization. esid: sec-static-semantics-hascallintailposition +info: | + Expression Rules + + CoalesceExpression : CoalesceExpressionHead ?? BitwiseORExpression + + 1. Return HasCallInTailPosition of BitwiseORExpression with argument call. flags: [onlyStrict] features: [tail-call-optimization, coalesce-expression] includes: [tcoHelper.js] diff --git a/test/language/expressions/coalesce/tco-pos-undefined.js b/test/language/expressions/coalesce/tco-pos-undefined.js index 53a2d4f7cbf..0065123eb19 100644 --- a/test/language/expressions/coalesce/tco-pos-undefined.js +++ b/test/language/expressions/coalesce/tco-pos-undefined.js @@ -1,8 +1,15 @@ // Copyright (C) 2019 Leo Balter. All rights reserved. // This code is governed by the BSD license found in the LICENSE file. + /*--- description: Expression is a candidate for tail-call optimization. esid: sec-static-semantics-hascallintailposition +info: | + Expression Rules + + CoalesceExpression : CoalesceExpressionHead ?? BitwiseORExpression + + 1. Return HasCallInTailPosition of BitwiseORExpression with argument call. flags: [onlyStrict] features: [tail-call-optimization, coalesce-expression] includes: [tcoHelper.js] From 276f514246b1b56b755cb6a7ad9da0a509598a8e Mon Sep 17 00:00:00 2001 From: Leo Balter Date: Wed, 9 Oct 2019 18:38:39 -0400 Subject: [PATCH 4/4] Add cases for matching paren covered logical expressions --- ...able-if-parenthesis-covered-logical-and.js | 53 ++++++++++++++++ ...nable-if-parenthesis-covered-logical-or.js | 61 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-and.js create mode 100644 test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-or.js diff --git a/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-and.js b/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-and.js new file mode 100644 index 00000000000..1787895536b --- /dev/null +++ b/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-and.js @@ -0,0 +1,53 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + CoalesceExpression is chainable with the LogicalANDExpression is any is covered. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = (null ?? 41) && 42; +assert.sameValue(x, 42, '(null ?? 41) && 42'); + +x = undefined; +x = null ?? (41 && 42); +assert.sameValue(x, 42, 'null ?? (41 && 42)`'); + +x = undefined; +x = (41 && 42) ?? null; +assert.sameValue(x, 42, '(41 && 42) ?? null'); + +x = undefined; +x = 41 && (null ?? 42); +assert.sameValue(x, 42, '41 && (null ?? 42)`'); diff --git a/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-or.js b/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-or.js new file mode 100644 index 00000000000..24061320593 --- /dev/null +++ b/test/language/expressions/coalesce/chainable-if-parenthesis-covered-logical-or.js @@ -0,0 +1,61 @@ +// Copyright (C) 2019 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: > + CoalesceExpression is chainable with the LogicalORExpression is any is covered. +esid: sec-conditional-operator +info: | + ConditionalExpression : + ShortCircuitExpression + ShortCircuitExpression ? AssignmentExpression : AssignmentExpression + + ShortCircuitExpression : + LogicalORExpression + CoalesceExpression + + CoalesceExpression : + CoalesceExpressionHead ?? BitwiseORExpression + + CoalesceExpressionHead : + CoalesceExpression + BitwiseORExpression + + Runtime Semantics: Evaluation + + CoalesceExpression:CoalesceExpressionHead??BitwiseORExpression + + 1. Let lref be the result of evaluating CoalesceExpressionHead. + 2. Let lval be ? GetValue(lref). + 3. If lval is undefined or null, + a. Let rref be the result of evaluating BitwiseORExpression. + b. Return ? GetValue(rref). + 4. Otherwise, return lval. +features: [coalesce-expression] +---*/ + +var x; + +x = undefined; +x = (null ?? 42) || 43; +assert.sameValue(x, 42, '(null ?? 42) || 43'); + +x = undefined; +x = null ?? (42 || 43); +assert.sameValue(x, 42, 'null ?? (42 || 43)`'); + +x = undefined; +x = (null || 42) ?? 43; +assert.sameValue(x, 42, '(null || 42) ?? 43'); + +x = undefined; +x = null || (42 ?? 43); +assert.sameValue(x, 42, 'null || (42 ?? 43)`'); + +x = undefined; +x = (42 || 43) ?? null; +assert.sameValue(x, 42, '(42 || 43) ?? null'); + +x = undefined; +x = 42 || (null ?? 43); +assert.sameValue(x, 42, '42 || (null ?? 43)');