OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-tostring | 5 // Flags: --harmony-tostring |
6 | 6 |
7 var global = this; | 7 var global = this; |
8 | 8 |
9 var funs = { | 9 var funs = { |
10 Object: [ Object ], | 10 Object: [ Object ], |
11 Function: [ Function ], | 11 Function: [ Function ], |
12 Array: [ Array ], | |
13 String: [ String ], | 12 String: [ String ], |
14 Boolean: [ Boolean ], | 13 Boolean: [ Boolean ], |
15 Number: [ Number ], | 14 Number: [ Number ], |
16 Date: [ Date ], | 15 Date: [ Date ], |
17 RegExp: [ RegExp ], | 16 RegExp: [ RegExp ], |
18 Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError, | 17 Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError, |
19 EvalError, URIError ] | 18 EvalError, URIError ] |
20 } | 19 } |
21 for (f in funs) { | 20 for (f in funs) { |
22 for (i in funs[f]) { | 21 for (i in funs[f]) { |
23 assertEquals("[object " + f + "]", | 22 assertEquals("[object " + f + "]", |
24 Object.prototype.toString.call(new funs[f][i]), | 23 Array.prototype.toString.call(new funs[f][i]), |
25 funs[f][i]); | 24 funs[f][i]); |
26 assertEquals("[object Function]", | 25 assertEquals("[object Function]", |
27 Object.prototype.toString.call(funs[f][i]), | 26 Array.prototype.toString.call(funs[f][i]), |
28 funs[f][i]); | 27 funs[f][i]); |
29 } | 28 } |
30 } | 29 } |
31 | 30 |
| 31 |
32 function testToStringTag(className) { | 32 function testToStringTag(className) { |
33 // Using builtin toStringTags | 33 // Using builtin toStringTags |
34 var obj = {}; | 34 var obj = {}; |
35 obj[Symbol.toStringTag] = className; | 35 obj[Symbol.toStringTag] = className; |
36 assertEquals("[object ~" + className + "]", | 36 assertEquals("[object ~" + className + "]", |
37 Object.prototype.toString.call(obj)); | 37 Array.prototype.toString.call(obj)); |
38 | 38 |
39 // Getter throws | 39 // Getter throws |
40 obj = {}; | 40 obj = {}; |
41 Object.defineProperty(obj, Symbol.toStringTag, { | 41 Object.defineProperty(obj, Symbol.toStringTag, { |
42 get: function() { throw className; } | 42 get: function() { throw className; } |
43 }); | 43 }); |
44 assertThrows(function() { | 44 assertThrows(function() { |
45 Object.prototype.toString.call(obj); | 45 Array.prototype.toString.call(obj); |
46 }, className); | 46 }, className); |
47 | 47 |
48 // Getter does not throw | 48 // Getter does not throw |
49 obj = {}; | 49 obj = {}; |
50 Object.defineProperty(obj, Symbol.toStringTag, { | 50 Object.defineProperty(obj, Symbol.toStringTag, { |
51 get: function() { return className; } | 51 get: function() { return className; } |
52 }); | 52 }); |
53 assertEquals("[object ~" + className + "]", | 53 assertEquals("[object ~" + className + "]", |
54 Object.prototype.toString.call(obj)); | 54 Array.prototype.toString.call(obj)); |
55 | 55 |
56 // Custom, non-builtin toStringTags | 56 // Custom, non-builtin toStringTags |
57 obj = {}; | 57 obj = {}; |
58 obj[Symbol.toStringTag] = "X" + className; | 58 obj[Symbol.toStringTag] = "X" + className; |
59 assertEquals("[object X" + className + "]", | 59 assertEquals("[object X" + className + "]", |
60 Object.prototype.toString.call(obj)); | 60 Array.prototype.toString.call(obj)); |
61 | 61 |
62 // With getter | 62 // With getter |
63 obj = {}; | 63 obj = {}; |
64 Object.defineProperty(obj, Symbol.toStringTag, { | 64 Object.defineProperty(obj, Symbol.toStringTag, { |
65 get: function() { return "X" + className; } | 65 get: function() { return "X" + className; } |
66 }); | 66 }); |
67 assertEquals("[object X" + className + "]", | 67 assertEquals("[object X" + className + "]", |
68 Object.prototype.toString.call(obj)); | 68 Array.prototype.toString.call(obj)); |
69 | 69 |
70 // Undefined toStringTag should return [object className] | 70 // Undefined toStringTag should return [object className] |
71 var obj = className === "Arguments" ? | 71 var obj = className === "Arguments" ? |
72 (function() { return arguments; })() : new global[className]; | 72 (function() { return arguments; })() : new global[className]; |
73 obj[Symbol.toStringTag] = undefined; | 73 obj[Symbol.toStringTag] = undefined; |
74 assertEquals("[object " + className + "]", | 74 assertEquals("[object " + className + "]", |
75 Object.prototype.toString.call(obj)); | 75 Array.prototype.toString.call(obj)); |
76 | 76 |
77 // With getter | 77 // With getter |
78 var obj = className === "Arguments" ? | 78 var obj = className === "Arguments" ? |
79 (function() { return arguments; })() : new global[className]; | 79 (function() { return arguments; })() : new global[className]; |
80 Object.defineProperty(obj, Symbol.toStringTag, { | 80 Object.defineProperty(obj, Symbol.toStringTag, { |
81 get: function() { return undefined; } | 81 get: function() { return undefined; } |
82 }); | 82 }); |
83 assertEquals("[object " + className + "]", | 83 assertEquals("[object " + className + "]", |
84 Object.prototype.toString.call(obj)); | 84 Array.prototype.toString.call(obj)); |
85 } | 85 } |
86 | 86 |
| 87 |
87 [ | 88 [ |
88 "Arguments", | 89 "Arguments", |
89 "Array", | |
90 "Boolean", | 90 "Boolean", |
91 "Date", | 91 "Date", |
92 "Error", | 92 "Error", |
93 "Function", | 93 "Function", |
94 "Number", | 94 "Number", |
95 "RegExp", | 95 "RegExp", |
96 "String" | 96 "String" |
97 ].forEach(testToStringTag); | 97 ].forEach(testToStringTag); |
98 | 98 |
| 99 |
99 function testToStringTagNonString(value) { | 100 function testToStringTagNonString(value) { |
100 var obj = {}; | 101 var obj = {}; |
101 obj[Symbol.toStringTag] = value; | 102 obj[Symbol.toStringTag] = value; |
102 assertEquals("[object ???]", Object.prototype.toString.call(obj)); | 103 assertEquals("[object ???]", Array.prototype.toString.call(obj)); |
103 | 104 |
104 // With getter | 105 // With getter |
105 obj = {}; | 106 obj = {}; |
106 Object.defineProperty(obj, Symbol.toStringTag, { | 107 Object.defineProperty(obj, Symbol.toStringTag, { |
107 get: function() { return value; } | 108 get: function() { return value; } |
108 }); | 109 }); |
109 assertEquals("[object ???]", Object.prototype.toString.call(obj)); | 110 assertEquals("[object ???]", Array.prototype.toString.call(obj)); |
110 } | 111 } |
111 | 112 |
| 113 |
112 [ | 114 [ |
113 null, | 115 null, |
114 function() {}, | 116 function() {}, |
115 [], | 117 [], |
116 {}, | 118 {}, |
117 /regexp/, | 119 /regexp/, |
118 42, | 120 42, |
119 Symbol("sym"), | 121 Symbol("sym"), |
120 new Date(), | 122 new Date(), |
121 (function() { return arguments; })(), | 123 (function() { return arguments; })(), |
122 true, | 124 true, |
123 new Error("oops"), | 125 new Error("oops"), |
124 new String("str") | 126 new String("str") |
125 ].forEach(testToStringTagNonString); | 127 ].forEach(testToStringTagNonString); |
126 | 128 |
127 function testObjectToStringPropertyDesc() { | 129 |
| 130 function testArrayToStringPropertyDesc() { |
128 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString"); | 131 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString"); |
129 assertTrue(desc.writable); | 132 assertTrue(desc.writable); |
130 assertFalse(desc.enumerable); | 133 assertFalse(desc.enumerable); |
131 assertTrue(desc.configurable); | 134 assertTrue(desc.configurable); |
132 } | 135 } |
133 testObjectToStringPropertyDesc(); | 136 testArrayToStringPropertyDesc(); |
| 137 |
| 138 |
| 139 function testArrayToStringOwnNonStringValue() { |
| 140 var obj = Object.defineProperty({}, Symbol.toStringTag, { value: 1 }); |
| 141 assertEquals("[object ???]", ([]).toString.call(obj)); |
| 142 } |
| 143 testArrayToStringOwnNonStringValue(); |
| 144 |
| 145 |
| 146 function testArrayToStringBasic() { |
| 147 assertEquals("1,2,3", [1,2,3].toString()); |
| 148 assertEquals(",,3", [,,3].toString()); |
| 149 } |
| 150 testArrayToStringBasic(); |
| 151 |
| 152 |
| 153 function testArrayToStringObjectWithCallableJoin() { |
| 154 var obj = { join: function() { return "CallableJoin"; } }; |
| 155 assertEquals("CallableJoin", Array.prototype.toString.call(obj)); |
| 156 } |
| 157 testArrayToStringObjectWithCallableJoin(); |
| 158 |
OLD | NEW |