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