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.
5 // Flags: --allow-natives-syntax
7 assertEquals(1, %ToPrimitive(1));
8 assertEquals(1, %ToPrimitive_Number(1));
9 assertEquals(1, %ToPrimitive_String(1));
10 assertEquals(1, %_ToPrimitive(1));
11 assertEquals(1, %_ToPrimitive_Number(1));
12 assertEquals(1, %_ToPrimitive_String(1));
14 assertEquals(.5, %ToPrimitive(.5));
15 assertEquals(.5, %ToPrimitive_Number(.5));
16 assertEquals(.5, %ToPrimitive_String(.5));
17 assertEquals(.5, %_ToPrimitive(.5));
18 assertEquals(.5, %_ToPrimitive_Number(.5));
19 assertEquals(.5, %_ToPrimitive_String(.5));
21 assertEquals(null, %ToPrimitive(null));
22 assertEquals(null, %ToPrimitive_Number(null));
23 assertEquals(null, %ToPrimitive_String(null));
24 assertEquals(null, %_ToPrimitive(null));
25 assertEquals(null, %_ToPrimitive_Number(null));
26 assertEquals(null, %_ToPrimitive_String(null));
28 assertEquals(true, %ToPrimitive(true));
29 assertEquals(true, %ToPrimitive_Number(true));
30 assertEquals(true, %ToPrimitive_String(true));
31 assertEquals(true, %_ToPrimitive(true));
32 assertEquals(true, %_ToPrimitive_Number(true));
33 assertEquals(true, %_ToPrimitive_String(true));
35 assertEquals(false, %ToPrimitive(false));
36 assertEquals(false, %ToPrimitive_Number(false));
37 assertEquals(false, %ToPrimitive_String(false));
38 assertEquals(false, %_ToPrimitive(false));
39 assertEquals(false, %_ToPrimitive_Number(false));
40 assertEquals(false, %_ToPrimitive_String(false));
42 assertEquals(undefined, %ToPrimitive(undefined));
43 assertEquals(undefined, %ToPrimitive_Number(undefined));
44 assertEquals(undefined, %ToPrimitive_String(undefined));
45 assertEquals(undefined, %_ToPrimitive(undefined));
46 assertEquals(undefined, %_ToPrimitive_Number(undefined));
47 assertEquals(undefined, %_ToPrimitive_String(undefined));
49 assertEquals("random text", %ToPrimitive("random text"));
50 assertEquals("random text", %ToPrimitive_Number("random text"));
51 assertEquals("random text", %ToPrimitive_String("random text"));
52 assertEquals("random text", %_ToPrimitive("random text"));
53 assertEquals("random text", %_ToPrimitive_Number("random text"));
54 assertEquals("random text", %_ToPrimitive_String("random text"));
56 assertEquals(Symbol.toPrimitive, %ToPrimitive(Symbol.toPrimitive));
57 assertEquals(Symbol.toPrimitive, %ToPrimitive_Number(Symbol.toPrimitive));
58 assertEquals(Symbol.toPrimitive, %ToPrimitive_String(Symbol.toPrimitive));
59 assertEquals(Symbol.toPrimitive, %_ToPrimitive(Symbol.toPrimitive));
60 assertEquals(Symbol.toPrimitive, %_ToPrimitive_Number(Symbol.toPrimitive));
61 assertEquals(Symbol.toPrimitive, %_ToPrimitive_String(Symbol.toPrimitive));
63 var a = { toString: function() { return "xyz" }};
64 assertEquals("xyz", %ToPrimitive(a));
65 assertEquals("xyz", %ToPrimitive_Number(a));
66 assertEquals("xyz", %ToPrimitive_String(a));
67 assertEquals("xyz", %_ToPrimitive(a));
68 assertEquals("xyz", %_ToPrimitive_Number(a));
69 assertEquals("xyz", %_ToPrimitive_String(a));
71 var b = { valueOf: function() { return 42 }};
72 assertEquals(42, %ToPrimitive(b));
73 assertEquals(42, %ToPrimitive_Number(b));
74 assertEquals("[object Object]", %ToPrimitive_String(b));
75 assertEquals(42, %_ToPrimitive(b));
76 assertEquals(42, %_ToPrimitive_Number(b));
77 assertEquals("[object Object]", %_ToPrimitive_String(b));
80 toString: function() { return "x"},
81 valueOf: function() { return 123 }
83 assertEquals(123, %ToPrimitive(c));
84 assertEquals(123, %ToPrimitive_Number(c));
85 assertEquals("x", %ToPrimitive_String(c));
86 assertEquals(123, %_ToPrimitive(c));
87 assertEquals(123, %_ToPrimitive_Number(c));
88 assertEquals("x", %_ToPrimitive_String(c));
91 [Symbol.toPrimitive]: function(hint) { return hint }
93 assertEquals("default", %ToPrimitive(d));
94 assertEquals("number", %ToPrimitive_Number(d));
95 assertEquals("string", %ToPrimitive_String(d));
96 assertEquals("default", %_ToPrimitive(d));
97 assertEquals("number", %_ToPrimitive_Number(d));
98 assertEquals("string", %_ToPrimitive_String(d));
101 assertEquals(e.toString(), %ToPrimitive(e));
102 assertEquals(0, %ToPrimitive_Number(e));
103 assertEquals(e.toString(), %ToPrimitive_String(e));
104 assertEquals(e.toString(), %_ToPrimitive(e));
105 assertEquals(0, %_ToPrimitive_Number(e));
106 assertEquals(e.toString(), %_ToPrimitive_String(e));