| 1 |
// Copyright (C) 2016 The V8 Project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
es6id: 18.2.2 |
| 6 |
esid: sec-isfinite-number |
| 7 |
description: > |
| 8 |
Use non-object value returned from @@toPrimitive method |
| 9 |
info: | |
| 10 |
isFinite (number) |
| 11 |
|
| 12 |
1. Let num be ? ToNumber(number). |
| 13 |
|
| 14 |
ToPrimitive ( input [ , PreferredType ] ) |
| 15 |
|
| 16 |
[...] |
| 17 |
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). |
| 18 |
5. If exoticToPrim is not undefined, then |
| 19 |
a. Let result be ? Call(exoticToPrim, input, « hint »). |
| 20 |
b. If Type(result) is not Object, return result. |
| 21 |
features: [Symbol.toPrimitive] |
| 22 |
---*/ |
| 23 |
|
| 24 |
var called = 0; |
| 25 |
var obj = { |
| 26 |
valueOf: function() { |
| 27 |
called = NaN; |
| 28 |
return Infinity; |
| 29 |
}, |
| 30 |
toString: function() { |
| 31 |
called = NaN; |
| 32 |
return Infinity; |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
obj[Symbol.toPrimitive] = function() { |
| 37 |
called += 1; |
| 38 |
return 42; |
| 39 |
}; |
| 40 |
assert.sameValue(isFinite(obj), true, "use returned value - number"); |
| 41 |
assert.sameValue(called, 1, "toPrimitive is called - number"); |
| 42 |
|
| 43 |
called = 0; |
| 44 |
obj[Symbol.toPrimitive] = function() { |
| 45 |
called += 1; |
| 46 |
return "42"; |
| 47 |
}; |
| 48 |
assert.sameValue(isFinite(obj), true, "use returned value - string"); |
| 49 |
assert.sameValue(called, 1, "toPrimitive is called - string"); |
| 50 |
|
| 51 |
called = 0; |
| 52 |
obj[Symbol.toPrimitive] = function() { |
| 53 |
called += 1; |
| 54 |
return true; |
| 55 |
}; |
| 56 |
assert.sameValue(isFinite(obj), true, "use returned value - boolean true"); |
| 57 |
assert.sameValue(called, 1, "toPrimitive is called - boolean true"); |
| 58 |
|
| 59 |
called = 0; |
| 60 |
obj[Symbol.toPrimitive] = function() { |
| 61 |
called += 1; |
| 62 |
return false; |
| 63 |
}; |
| 64 |
assert.sameValue(isFinite(obj), true, "use returned value - boolean false"); |
| 65 |
assert.sameValue(called, 1, "toPrimitive is called - boolean false"); |
| 66 |
|
| 67 |
called = 0; |
| 68 |
obj[Symbol.toPrimitive] = function() { |
| 69 |
called += 1; |
| 70 |
return Infinity; |
| 71 |
}; |
| 72 |
assert.sameValue(isFinite(obj), false, "use returned value - Infinity"); |
| 73 |
assert.sameValue(called, 1, "toPrimitive is called - Infinity"); |
| 74 |
|
| 75 |
called = 0; |
| 76 |
obj[Symbol.toPrimitive] = function() { |
| 77 |
called += 1; |
| 78 |
return -Infinity; |
| 79 |
}; |
| 80 |
assert.sameValue(isFinite(obj), false, "use returned value - -Infinity"); |
| 81 |
assert.sameValue(called, 1, "toPrimitive is called - -Infinity"); |