Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5 | var global = this; |
| 6 | |
| 7 | var funs = { |
| 8 | Object: [ Object ], |
| 9 | Function: [ Function ], |
| 10 | Array: [ Array ], |
| 11 | String: [ String ], |
| 12 | Boolean: [ Boolean ], |
| 13 | Number: [ Number ], |
| 14 | Date: [ Date ], |
| 15 | RegExp: [ RegExp ], |
| 16 | Error: [ Error, TypeError, RangeError, SyntaxError, ReferenceError, |
| 17 | EvalError, URIError ] |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 18 | }; |
| 19 | for (var f in funs) { |
| 20 | for (var i in funs[f]) { |
| 21 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 22 | assertEquals("[object " + f + "]", |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 23 | Object.prototype.toString.call(new funs[f][i]), |
| 24 | funs[f][i]); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 25 | assertEquals("[object Function]", |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 26 | Object.prototype.toString.call(funs[f][i]), |
| 27 | funs[f][i]); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | function testToStringTag(className) { |
| 32 | // Using builtin toStringTags |
| 33 | var obj = {}; |
| 34 | obj[Symbol.toStringTag] = className; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | assertEquals("[object " + className + "]", |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 36 | Object.prototype.toString.call(obj)); |
| 37 | |
| 38 | // Getter throws |
| 39 | obj = {}; |
| 40 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 41 | get: function() { throw className; } |
| 42 | }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 43 | assertThrowsEquals(function() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 44 | Object.prototype.toString.call(obj); |
| 45 | }, className); |
| 46 | |
| 47 | // Getter does not throw |
| 48 | obj = {}; |
| 49 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 50 | get: function() { return className; } |
| 51 | }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | assertEquals("[object " + className + "]", |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 53 | Object.prototype.toString.call(obj)); |
| 54 | |
| 55 | // Custom, non-builtin toStringTags |
| 56 | obj = {}; |
| 57 | obj[Symbol.toStringTag] = "X" + className; |
| 58 | assertEquals("[object X" + className + "]", |
| 59 | Object.prototype.toString.call(obj)); |
| 60 | |
| 61 | // With getter |
| 62 | obj = {}; |
| 63 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 64 | get: function() { return "X" + className; } |
| 65 | }); |
| 66 | assertEquals("[object X" + className + "]", |
| 67 | Object.prototype.toString.call(obj)); |
| 68 | |
| 69 | // Undefined toStringTag should return [object className] |
| 70 | var obj = className === "Arguments" ? |
| 71 | (function() { return arguments; })() : new global[className]; |
| 72 | obj[Symbol.toStringTag] = undefined; |
| 73 | assertEquals("[object " + className + "]", |
| 74 | Object.prototype.toString.call(obj)); |
| 75 | |
| 76 | // With getter |
| 77 | var obj = className === "Arguments" ? |
| 78 | (function() { return arguments; })() : new global[className]; |
| 79 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 80 | get: function() { return undefined; } |
| 81 | }); |
| 82 | assertEquals("[object " + className + "]", |
| 83 | Object.prototype.toString.call(obj)); |
| 84 | } |
| 85 | |
| 86 | [ |
| 87 | "Arguments", |
| 88 | "Array", |
| 89 | "Boolean", |
| 90 | "Date", |
| 91 | "Error", |
| 92 | "Function", |
| 93 | "Number", |
| 94 | "RegExp", |
| 95 | "String" |
| 96 | ].forEach(testToStringTag); |
| 97 | |
| 98 | function testToStringTagNonString(value) { |
| 99 | var obj = {}; |
| 100 | obj[Symbol.toStringTag] = value; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 101 | assertEquals("[object Object]", Object.prototype.toString.call(obj)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 102 | |
| 103 | // With getter |
| 104 | obj = {}; |
| 105 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 106 | get: function() { return value; } |
| 107 | }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | assertEquals("[object Object]", Object.prototype.toString.call(obj)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | [ |
| 112 | null, |
| 113 | function() {}, |
| 114 | [], |
| 115 | {}, |
| 116 | /regexp/, |
| 117 | 42, |
| 118 | Symbol("sym"), |
| 119 | new Date(), |
| 120 | (function() { return arguments; })(), |
| 121 | true, |
| 122 | new Error("oops"), |
| 123 | new String("str") |
| 124 | ].forEach(testToStringTagNonString); |
| 125 | |
| 126 | function testObjectToStringPropertyDesc() { |
| 127 | var desc = Object.getOwnPropertyDescriptor(Object.prototype, "toString"); |
| 128 | assertTrue(desc.writable); |
| 129 | assertFalse(desc.enumerable); |
| 130 | assertTrue(desc.configurable); |
| 131 | } |
| 132 | testObjectToStringPropertyDesc(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 134 | function testObjectToStringOnNonStringValue(obj) { |
| 135 | Object.defineProperty(obj, Symbol.toStringTag, { value: 1 }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 | assertEquals("[object Object]", ({}).toString.call(obj)); |
| 137 | } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 138 | testObjectToStringOnNonStringValue({}); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | // Proxies |
| 142 | |
| 143 | function assertTag(tag, obj) { |
| 144 | assertEquals("[object " + tag + "]", Object.prototype.toString.call(obj)); |
| 145 | } |
| 146 | |
| 147 | assertTag("Object", new Proxy({}, {})); |
| 148 | assertTag("Array", new Proxy([], {})); |
| 149 | assertTag("Function", new Proxy(() => 42, {})); |
| 150 | assertTag("Foo", new Proxy(() => 42, {get() {return "Foo"}})); |
| 151 | assertTag("Function", new Proxy(() => 42, {get() {return 666}})); |
| 152 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 153 | var revocable = Proxy.revocable([], {}); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 154 | revocable.revoke(); |
| 155 | assertThrows(() => Object.prototype.toString.call(revocable.proxy), TypeError); |
| 156 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 157 | var handler = {}; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 158 | revocable = Proxy.revocable([], handler); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 159 | // The first get() call, i.e., toString() revokes the proxy |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 160 | handler.get = () => revocable.revoke(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 161 | assertEquals("[object Array]", Object.prototype.toString.call(revocable.proxy)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | assertThrows(() => Object.prototype.toString.call(revocable.proxy), TypeError); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 163 | |
| 164 | revocable = Proxy.revocable([], handler); |
| 165 | handler.get = () => {revocable.revoke(); return "value";}; |
| 166 | assertEquals("[object value]", Object.prototype.toString.call(revocable.proxy)); |
| 167 | assertThrows(() => Object.prototype.toString.call(revocable.proxy), TypeError); |
| 168 | |
| 169 | |
| 170 | revocable = Proxy.revocable(function() {}, handler); |
| 171 | handler.get = () => revocable.revoke(); |
| 172 | assertEquals("[object Function]", Object.prototype.toString.call(revocable.proxy)); |
| 173 | assertThrows(() => Object.prototype.toString.call(revocable.proxy), TypeError); |
| 174 | |
| 175 | function* gen() { yield 1; } |
| 176 | |
| 177 | assertTag("GeneratorFunction", gen); |
| 178 | Object.defineProperty(gen, Symbol.toStringTag, {writable: true}); |
| 179 | gen[Symbol.toStringTag] = "different string"; |
| 180 | assertTag("different string", gen); |
| 181 | gen[Symbol.toStringTag] = 1; |
| 182 | assertTag("Function", gen); |
| 183 | |
| 184 | function overwriteToStringTagWithNonStringValue(tag, obj) { |
| 185 | assertTag(tag, obj); |
| 186 | |
| 187 | Object.defineProperty(obj, Symbol.toStringTag, { |
| 188 | configurable: true, |
| 189 | value: "different string" |
| 190 | }); |
| 191 | assertTag("different string", obj); |
| 192 | |
| 193 | testObjectToStringOnNonStringValue(obj); |
| 194 | } |
| 195 | |
| 196 | overwriteToStringTagWithNonStringValue("global", global); |
| 197 | overwriteToStringTagWithNonStringValue("Generator", gen()); |
| 198 | |
| 199 | var arrayBuffer = new ArrayBuffer(); |
| 200 | overwriteToStringTagWithNonStringValue("ArrayBuffer", arrayBuffer); |
| 201 | overwriteToStringTagWithNonStringValue("DataView", new DataView(arrayBuffer)); |
| 202 | |
| 203 | overwriteToStringTagWithNonStringValue("Int8Array", new Int8Array()); |
| 204 | overwriteToStringTagWithNonStringValue("Uint8Array", new Uint8Array()); |
| 205 | overwriteToStringTagWithNonStringValue("Uint8ClampedArray", |
| 206 | new Uint8ClampedArray()); |
| 207 | overwriteToStringTagWithNonStringValue("Int16Array", new Int16Array()); |
| 208 | overwriteToStringTagWithNonStringValue("Uint16Array", new Uint16Array()); |
| 209 | overwriteToStringTagWithNonStringValue("Int32Array", new Int32Array()); |
| 210 | overwriteToStringTagWithNonStringValue("Uint32Array", new Uint32Array()); |
| 211 | overwriteToStringTagWithNonStringValue("Float32Array", new Float32Array()); |
| 212 | overwriteToStringTagWithNonStringValue("Float64Array", new Float64Array()); |
| 213 | |
| 214 | var set = new Set(); |
| 215 | var map = new Map(); |
| 216 | |
| 217 | overwriteToStringTagWithNonStringValue("Set", set); |
| 218 | overwriteToStringTagWithNonStringValue("Map", map); |
| 219 | |
| 220 | overwriteToStringTagWithNonStringValue("Set Iterator", set[Symbol.iterator]()); |
| 221 | overwriteToStringTagWithNonStringValue("Map Iterator", map[Symbol.iterator]()); |
| 222 | |
| 223 | overwriteToStringTagWithNonStringValue("WeakSet", new WeakSet()); |
| 224 | overwriteToStringTagWithNonStringValue("WeakMap", new WeakMap()); |
| 225 | |
| 226 | overwriteToStringTagWithNonStringValue("Promise", new Promise(function() {})); |