diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js new file mode 100644 index 0000000000000000000000000000000000000000..4aebe84585970dde6283d7073fe5103c5e4523ed --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js @@ -0,0 +1,57 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + This value is an object which cannot be converted to a primitive +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: undefined, +}; + +// If trimEnd is called on an object with neither Symbol.toPrimitive, toString +// nor valueOf defined, then a TypeError exception should be thrown. +assert.throws( + TypeError, + function() { String.prototype.trimEnd.call(thisVal); }, +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js new file mode 100644 index 0000000000000000000000000000000000000000..7ef1a2c1fbc2f0507e28cd92521cd0847658063b --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js @@ -0,0 +1,34 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when getting Symbol.toPrimitive method +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + get [Symbol.toPrimitive]() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js new file mode 100644 index 0000000000000000000000000000000000000000..f0bb5bf48be870fcf22f09b971109548b6281716 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js @@ -0,0 +1,36 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when Symbol.toPrimitive abrupt completes. +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-priority.js new file mode 100644 index 0000000000000000000000000000000000000000..df52f8a248149ae9b1d71a953ef883e7ab75b8ab --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-priority.js @@ -0,0 +1,74 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Priority of Symbol[toPrimitive] when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed += 1; + return function() { return '42 '; }; + }, + get toString() { + toStringAccessed += 1; + return function() { return ''; }; + }, + get valueOf() { + valueOfAccessed += 1; + return function() { return ''; }; + }, +}; + +// Test that thisVal[Symbol.toPrimitive] has been called. + +var result = String.prototype.trimEnd.call(thisVal); + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive] expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal[Symbol.toPrimitive] expected to have been called.', +); + +// Test that thisVal.toString and thisVal.valueOf have not been accessedo + +assert.sameValue( + toStringAccessed, + 0, + 'thisVal.toString should not have been accessed.' +); +assert.sameValue( + valueOfAccessed, + 0, + 'thisVal.valueOf should not have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js new file mode 100644 index 0000000000000000000000000000000000000000..b67fe117a45f604016e40da3e7a6aa1b342f8c59 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js @@ -0,0 +1,38 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when Symbol.toPrimitive returns an object +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If arguement is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js new file mode 100644 index 0000000000000000000000000000000000000000..f7ed44d1011aa998fbec4b7a6a5b5be74976fe30 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js @@ -0,0 +1,51 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when getting toString method +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + get toString() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js new file mode 100644 index 0000000000000000000000000000000000000000..40c2b9bcce2b1b4dd6d48ab45f325d7c892190b0 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js @@ -0,0 +1,53 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when toString called and abrupt completes. +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-priority.js new file mode 100644 index 0000000000000000000000000000000000000000..06d82538fe809ea304fa3027c813c98bb67886e9 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-priority.js @@ -0,0 +1,93 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Priority of toString when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed +=1; + return undefined; + }, + get toString() { + toStringAccessed += 1; + return function() { return '42 '; }; + }, + get valueOf() { + valueOfAccessed += 1; + return function() { return ''; }; + }, +}; + +// Test that toString is called when Symbol.toPrimitive is undefined. + +var result = String.prototype.trimEnd.call(thisVal) + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal.toString expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal.toString expected to have been called.', +); + +// Test that thisVal[toPrimitive] has been accessed. + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive should have been accessed.' +); + +// Test that thisVal.valueOf has not been accessed. + +assert.sameValue( + valueOfAccessed, + 0, + 'thisVal.valueOf should not have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-returns-object-err.js new file mode 100644 index 0000000000000000000000000000000000000000..e47539fadf221ae6d8a1e7ab974e288c19c3282d --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-returns-object-err.js @@ -0,0 +1,56 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when toString called and returns an object +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js new file mode 100644 index 0000000000000000000000000000000000000000..ccd4516745256872ea95c027dce375da88174a69 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js @@ -0,0 +1,52 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when getting valueOf method +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + get valueOf() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js new file mode 100644 index 0000000000000000000000000000000000000000..386398aebbd14c31f2e3147b1ac7f34feb761322 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js @@ -0,0 +1,54 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when valueOf called and abrupt completes. +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js new file mode 100644 index 0000000000000000000000000000000000000000..778846c07918816fbee2f22204d18275453ee16d --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js @@ -0,0 +1,91 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Priority of valueOf when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed += 1; + return undefined; + }, + get toString() { + toStringAccessed += 1; + return undefined; + }, + get valueOf() { + valueOfAccessed += 1; + return function() { return '42 '; }; + }, +}; + +// Test that valueOf is called when Symbol.toPrimitive and toString are both +// undefined. + +var result = String.prototype.trimEnd.call(thisVal); + +assert.sameValue( + valueOfAccessed, + 1, + 'thisVal.toString expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal.valueOf expected to have been called.', +); + +// Test that thisVal[toPrimitive] and thisVal.toString has been accessed. + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive should have been accessed.' +); +assert.sameValue( + toStringAccessed, + 1, + 'thisVal[Symbol.toString should have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-returns-object-err.js new file mode 100644 index 0000000000000000000000000000000000000000..c1666660749b3af4c27dab28cde90bda0d4c2d6f --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-returns-object-err.js @@ -0,0 +1,57 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimEnd +description: > + Abrupt completion when valueOf called and returns an object +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + b. Else if PreferredType is hint String, let hint be "string". + ... + d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) + e. If exoticToPrim is not undefined, then + i. Let result be ? Call(exoticToPrim, input, « hint »). + ii. If Type(result) is not Object, return result. + iii. Throw a TypeError exception. + f. If hint is "default", set hint to "number". + g. Return ? OrdinaryToPrimitive(input, hint). + ... + + OrdinaryToPrimitive( O, hint ) + ... + 3. If hint is "string", then + a. Let methodNames be « "toString", "valueOf" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +});