Completely rewrite ToPrimitive method priority testing

This commit is contained in:
Valerie R Young 2017-10-04 16:14:02 -04:00 committed by Rick Waldron
parent fc86cef984
commit 232eec1f3b
4 changed files with 259 additions and 95 deletions

View File

@ -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.trimStart
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.trimStart.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.'
);

View File

@ -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.trimStart
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.trimStart.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.'
);

View File

@ -0,0 +1,92 @@
// 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.trimStart
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.trimStart.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.'
);

View File

@ -1,95 +0,0 @@
// Copyright (C) 2017 the Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.trimStart
description: >
ToString perfers Symbol.toPrimitive to toString to valueOf
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 ])
...
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" ».
...
features: [string-trimming, Symbol.toPrimitive]
---*/
var trimStart = String.prototype.trimStart;
var called = 0;
var thisVal = {
get [Symbol.toPrimitive]() {
called += 1;
return function() { return '' };
},
toString: function() {
throw new Test262Error(
'this.toString called before this[Symbol.toPrimitive]'
);
},
valueOf: function() {
throw new Test262Error(
'this.valueOf called before this[Symbol.toPrimitive]'
);
},
};
// Test that thisVal[Symbol.toPrimitive] is called before toString or valueOf
trimStart.call(thisVal);
assert.sameValue(called, 1, '[Symbol.toPrimitive] expected to have been called');
var called = 0;
var thisVal = {
[Symbol.toPrimitive]: undefined,
get toString() {
called += 1;
return function() { return '' };
},
valueOf: function() {
throw new Test262Error(
'this.valueOf called before this[Symbol.toPrimitive]'
);
},
};
// Test that toString is called before valueOf
trimStart.call(thisVal);
assert.sameValue(called, 1, 'this.toString expected to have been called');
var called = 0;
var thisVal = {
[Symbol.toPrimitive]: undefined,
toString: undefined,
get valueOf() {
called += 1;
return function() { return '' };
},
};
// Test that valueOf is called when neither [Symbol.toPrimitive] nor toString
// are defined.
trimStart.call(thisVal);
assert.sameValue(called, 1, 'this.valueOf expected to have been called');