| 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 |
esid: sec-date-value |
| 5 |
es6id: 20.3.2.2 |
| 6 |
description: > |
| 7 |
Exotic `Symbol.toPrimitive` method returns a primitive value other than a |
| 8 |
string |
| 9 |
info: | |
| 10 |
3. If NewTarget is not undefined, then |
| 11 |
a. If Type(value) is Object and value has a [[DateValue]] internal slot, then |
| 12 |
i. Let tv be thisTimeValue(value). |
| 13 |
b. Else, |
| 14 |
i. Let v be ? ToPrimitive(value). |
| 15 |
ii. If Type(v) is String, then |
| 16 |
1. Let tv be the result of parsing v as a date, in exactly the same |
| 17 |
manner as for the parse method (20.3.3.2). If the parse resulted |
| 18 |
in an abrupt completion, tv is the Completion Record. |
| 19 |
|
| 20 |
ToPrimitive ( input [ , PreferredType ] ) |
| 21 |
|
| 22 |
1. If PreferredType was not passed, let hint be "default". |
| 23 |
2. Else if PreferredType is hint String, let hint be "string". |
| 24 |
3. Else PreferredType is hint Number, let hint be "number". |
| 25 |
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). |
| 26 |
5. If exoticToPrim is not undefined, then |
| 27 |
a. Let result be ? Call(exoticToPrim, input, « hint »). |
| 28 |
b. If Type(result) is not Object, return result. |
| 29 |
features: [Symbol.toPrimitive] |
| 30 |
---*/ |
| 31 |
|
| 32 |
var toPrimitive = {}; |
| 33 |
var returnValue; |
| 34 |
toPrimitive[Symbol.toPrimitive] = function() { |
| 35 |
return returnValue; |
| 36 |
}; |
| 37 |
|
| 38 |
returnValue = 8; |
| 39 |
assert.sameValue(new Date(toPrimitive).getTime(), 8, 'number'); |
| 40 |
|
| 41 |
returnValue = undefined; |
| 42 |
assert.sameValue(new Date(toPrimitive).getTime(), NaN, 'undefined'); |
| 43 |
|
| 44 |
returnValue = true; |
| 45 |
assert.sameValue(new Date(toPrimitive).getTime(), 1, 'boolean `true`'); |
| 46 |
|
| 47 |
returnValue = false; |
| 48 |
assert.sameValue(new Date(toPrimitive).getTime(), 0, 'boolean `false`'); |
| 49 |
|
| 50 |
returnValue = null; |
| 51 |
assert.sameValue(new Date(toPrimitive).getTime(), 0, 'null'); |
| 52 |
|
| 53 |
returnValue = Symbol.toPrimitive; |
| 54 |
assert.throws(TypeError, function() { |
| 55 |
new Date(toPrimitive); |
| 56 |
}, 'symbol'); |