1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --allow-natives-syntax 6 7assertEquals(1, %ToPrimitive(1)); 8assertEquals(1, %ToPrimitive_Number(1)); 9assertEquals(1, %ToPrimitive_String(1)); 10assertEquals(1, %_ToPrimitive(1)); 11assertEquals(1, %_ToPrimitive_Number(1)); 12assertEquals(1, %_ToPrimitive_String(1)); 13 14assertEquals(.5, %ToPrimitive(.5)); 15assertEquals(.5, %ToPrimitive_Number(.5)); 16assertEquals(.5, %ToPrimitive_String(.5)); 17assertEquals(.5, %_ToPrimitive(.5)); 18assertEquals(.5, %_ToPrimitive_Number(.5)); 19assertEquals(.5, %_ToPrimitive_String(.5)); 20 21assertEquals(null, %ToPrimitive(null)); 22assertEquals(null, %ToPrimitive_Number(null)); 23assertEquals(null, %ToPrimitive_String(null)); 24assertEquals(null, %_ToPrimitive(null)); 25assertEquals(null, %_ToPrimitive_Number(null)); 26assertEquals(null, %_ToPrimitive_String(null)); 27 28assertEquals(true, %ToPrimitive(true)); 29assertEquals(true, %ToPrimitive_Number(true)); 30assertEquals(true, %ToPrimitive_String(true)); 31assertEquals(true, %_ToPrimitive(true)); 32assertEquals(true, %_ToPrimitive_Number(true)); 33assertEquals(true, %_ToPrimitive_String(true)); 34 35assertEquals(false, %ToPrimitive(false)); 36assertEquals(false, %ToPrimitive_Number(false)); 37assertEquals(false, %ToPrimitive_String(false)); 38assertEquals(false, %_ToPrimitive(false)); 39assertEquals(false, %_ToPrimitive_Number(false)); 40assertEquals(false, %_ToPrimitive_String(false)); 41 42assertEquals(undefined, %ToPrimitive(undefined)); 43assertEquals(undefined, %ToPrimitive_Number(undefined)); 44assertEquals(undefined, %ToPrimitive_String(undefined)); 45assertEquals(undefined, %_ToPrimitive(undefined)); 46assertEquals(undefined, %_ToPrimitive_Number(undefined)); 47assertEquals(undefined, %_ToPrimitive_String(undefined)); 48 49assertEquals("random text", %ToPrimitive("random text")); 50assertEquals("random text", %ToPrimitive_Number("random text")); 51assertEquals("random text", %ToPrimitive_String("random text")); 52assertEquals("random text", %_ToPrimitive("random text")); 53assertEquals("random text", %_ToPrimitive_Number("random text")); 54assertEquals("random text", %_ToPrimitive_String("random text")); 55 56assertEquals(Symbol.toPrimitive, %ToPrimitive(Symbol.toPrimitive)); 57assertEquals(Symbol.toPrimitive, %ToPrimitive_Number(Symbol.toPrimitive)); 58assertEquals(Symbol.toPrimitive, %ToPrimitive_String(Symbol.toPrimitive)); 59assertEquals(Symbol.toPrimitive, %_ToPrimitive(Symbol.toPrimitive)); 60assertEquals(Symbol.toPrimitive, %_ToPrimitive_Number(Symbol.toPrimitive)); 61assertEquals(Symbol.toPrimitive, %_ToPrimitive_String(Symbol.toPrimitive)); 62 63var a = { toString: function() { return "xyz" }}; 64assertEquals("xyz", %ToPrimitive(a)); 65assertEquals("xyz", %ToPrimitive_Number(a)); 66assertEquals("xyz", %ToPrimitive_String(a)); 67assertEquals("xyz", %_ToPrimitive(a)); 68assertEquals("xyz", %_ToPrimitive_Number(a)); 69assertEquals("xyz", %_ToPrimitive_String(a)); 70 71var b = { valueOf: function() { return 42 }}; 72assertEquals(42, %ToPrimitive(b)); 73assertEquals(42, %ToPrimitive_Number(b)); 74assertEquals("[object Object]", %ToPrimitive_String(b)); 75assertEquals(42, %_ToPrimitive(b)); 76assertEquals(42, %_ToPrimitive_Number(b)); 77assertEquals("[object Object]", %_ToPrimitive_String(b)); 78 79var c = { 80 toString: function() { return "x"}, 81 valueOf: function() { return 123 } 82}; 83assertEquals(123, %ToPrimitive(c)); 84assertEquals(123, %ToPrimitive_Number(c)); 85assertEquals("x", %ToPrimitive_String(c)); 86assertEquals(123, %_ToPrimitive(c)); 87assertEquals(123, %_ToPrimitive_Number(c)); 88assertEquals("x", %_ToPrimitive_String(c)); 89 90var d = { 91 [Symbol.toPrimitive]: function(hint) { return hint } 92}; 93assertEquals("default", %ToPrimitive(d)); 94assertEquals("number", %ToPrimitive_Number(d)); 95assertEquals("string", %ToPrimitive_String(d)); 96assertEquals("default", %_ToPrimitive(d)); 97assertEquals("number", %_ToPrimitive_Number(d)); 98assertEquals("string", %_ToPrimitive_String(d)); 99 100var e = new Date(0); 101assertEquals(e.toString(), %ToPrimitive(e)); 102assertEquals(0, %ToPrimitive_Number(e)); 103assertEquals(e.toString(), %ToPrimitive_String(e)); 104assertEquals(e.toString(), %_ToPrimitive(e)); 105assertEquals(0, %_ToPrimitive_Number(e)); 106assertEquals(e.toString(), %_ToPrimitive_String(e)); 107