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 // TODO(neis): Test with proxies. 6 7 8 9 //////////////////////////////////////////////////////////////////////////////// 10 // (Auxiliaries) 11 12 13 "use strict"; 14 15 var global = this; 16 17 var sym = Symbol("gaga"); 18 19 var objects = [ 20 {}, 21 [], 22 function() {}, 23 function() { 24 return arguments; 25 }(), 26 function() { 27 'use strict'; 28 return arguments; 29 }(), 30 Object(1), 31 Object(true), 32 Object('bla'), 33 new Date, 34 new RegExp, 35 new Set, 36 new Map, 37 new WeakMap, 38 new WeakSet, 39 new ArrayBuffer(10), 40 new Int32Array(5), 41 Object, 42 Function, 43 Date, 44 RegExp, 45 global 46 ]; 47 48 function prepare(target) { 49 target["bla"] = true; 50 target[4] = 42; 51 target[sym] = "foo"; 52 target["noconf"] = 43; 53 Object.defineProperty(target, "noconf", 54 { configurable: false }); 55 Object.defineProperty(target, "nowrite", 56 { writable: false, configurable: true, value: 44 }); 57 Object.defineProperty(target, "getter", 58 { get: function () {return this.bla}, configurable: true }); 59 Object.defineProperty(target, "setter", 60 { set: function (x) {this.gaga = x}, configurable: true }); 61 Object.defineProperty(target, "setter2", 62 { set: function (x) {}, configurable: true }); 63 } 64 65 66 67 //////////////////////////////////////////////////////////////////////////////// 68 // Reflect.get 69 70 71 (function testReflectGetArity() { 72 assertEquals(2, Reflect.get.length); 73 })(); 74 75 76 (function testReflectGetOnNonObject() { 77 assertThrows(function() { Reflect.get(); }, TypeError); 78 assertThrows(function() { Reflect.get(42, "bla"); }, TypeError); 79 assertThrows(function() { Reflect.get(null, "bla"); }, TypeError); 80 })(); 81 82 83 (function testReflectGetKeyConversion() { 84 var target = {bla: 42}; 85 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 86 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 87 assertEquals(42, Reflect.get(target, a)); 88 assertThrowsEquals(function() { Reflect.get(target, b); }, "gaga"); 89 })(); 90 91 92 (function testReflectGetOnObject() { 93 var receiver = {bla: false}; 94 for (let target of objects) { 95 prepare(target); 96 assertEquals(true, Reflect.get(target, "bla")); 97 assertEquals(true, Reflect.get(target, "bla", target)); 98 assertEquals(true, Reflect.get(target, "bla", receiver)); 99 assertEquals(42, Reflect.get(target, 4)); 100 assertEquals(42, Reflect.get(target, 4, target)); 101 assertEquals(42, Reflect.get(target, 4, receiver)); 102 assertEquals(42, Reflect.get(target, "4")); 103 assertEquals(42, Reflect.get(target, "4", target)); 104 assertEquals(42, Reflect.get(target, "4", receiver)); 105 assertEquals("foo", Reflect.get(target, sym)); 106 assertEquals("foo", Reflect.get(target, sym, target)); 107 assertEquals("foo", Reflect.get(target, sym, receiver)); 108 assertEquals(43, Reflect.get(target, "noconf")); 109 assertEquals(43, Reflect.get(target, "noconf", target)); 110 assertEquals(43, Reflect.get(target, "noconf", receiver)); 111 assertEquals(true, Reflect.get(target, "getter")); 112 assertEquals(true, Reflect.get(target, "getter", target)); 113 assertEquals(false, Reflect.get(target, "getter", receiver)); 114 assertEquals(undefined, Reflect.get(target, "setter")); 115 assertEquals(undefined, Reflect.get(target, "setter", target)); 116 assertEquals(undefined, Reflect.get(target, "setter", receiver)); 117 assertEquals(undefined, Reflect.get(target, "foo")); 118 assertEquals(undefined, Reflect.get(target, "foo", target)); 119 assertEquals(undefined, Reflect.get(target, "foo", receiver)); 120 assertEquals(undefined, Reflect.get(target, 666)); 121 assertEquals(undefined, Reflect.get(target, 666, target)); 122 assertEquals(undefined, Reflect.get(target, 666, receiver)); 123 124 let proto = target.__proto__; 125 target.__proto__ = { get foo() {return this.bla} }; 126 assertEquals(true, Reflect.get(target, "foo")); 127 assertEquals(true, Reflect.get(target, "foo", target)); 128 assertEquals(false, Reflect.get(target, "foo", receiver)); 129 target.__proto__ = proto; 130 } 131 })(); 132 133 134 135 //////////////////////////////////////////////////////////////////////////////// 136 // Reflect.set 137 138 139 (function testReflectSetArity() { 140 assertEquals(3, Reflect.set.length); 141 })(); 142 143 144 (function testReflectSetOnNonObject() { 145 assertThrows(function() { Reflect.set(); }, TypeError); 146 assertThrows(function() { Reflect.set(42, "bla"); }, TypeError); 147 assertThrows(function() { Reflect.set(null, "bla"); }, TypeError); 148 })(); 149 150 151 (function testReflectSetKeyConversion() { 152 var target = {}; 153 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 154 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 155 assertTrue(Reflect.set(target, a, 42)); 156 assertEquals(42, target.bla); 157 assertThrowsEquals(function() { Reflect.set(target, b, 42); }, "gaga"); 158 })(); 159 160 161 (function testReflectSetOnObject() { 162 var receiver = {bla: false}; 163 var value = 34234; 164 for (let target of objects) { 165 prepare(target); 166 assertTrue(Reflect.set(target, "bla", value)); 167 assertEquals(value, target.bla); 168 169 prepare(target); 170 assertTrue(Reflect.set(target, "bla", value, target)); 171 assertEquals(value, target.bla); 172 173 prepare(target); 174 assertTrue(Reflect.set(target, "bla", value, receiver)); 175 assertEquals(true, target.bla); 176 assertEquals(value, receiver.bla); 177 receiver.bla = false; 178 179 prepare(target); 180 assertTrue(Reflect.set(target, 4, value)); 181 assertEquals(value, target[4]); 182 183 prepare(target); 184 assertTrue(Reflect.set(target, 4, value, target)); 185 assertEquals(value, target[4]); 186 187 prepare(target); 188 assertTrue(Reflect.set(target, 4, value, receiver)); 189 assertEquals(42, target[4]); 190 assertEquals(value, receiver[4]); 191 delete receiver[4]; 192 193 prepare(target); 194 assertTrue(Reflect.set(target, sym, value)); 195 assertEquals(value, target[sym]); 196 197 prepare(target); 198 assertTrue(Reflect.set(target, sym, value, target)); 199 assertEquals(value, target[sym]); 200 201 prepare(target); 202 assertTrue(Reflect.set(target, sym, value, receiver)); 203 assertEquals("foo", target[sym]); 204 assertEquals(value, receiver[sym]); 205 delete receiver[sym]; 206 207 prepare(target); 208 assertTrue(Reflect.set(target, "noconf", value)); 209 assertEquals(value, target.noconf); 210 211 prepare(target); 212 assertTrue(Reflect.set(target, "noconf", value, target)); 213 assertEquals(value, target.noconf); 214 215 prepare(target); 216 assertTrue(Reflect.set(target, "noconf", value, receiver)); 217 assertEquals(43, target.noconf); 218 assertEquals(value, receiver.noconf); 219 delete receiver.noconf; 220 221 assertTrue(Reflect.set(target, "setter", value)); 222 assertEquals(value, target.gaga) 223 delete target.gaga; 224 225 assertTrue(Reflect.set(target, "setter", value, target)); 226 assertEquals(value, target.gaga) 227 delete target.gaga; 228 229 assertTrue(Reflect.set(target, "setter", value, receiver)); 230 assertFalse("gaga" in target); 231 assertEquals(value, receiver.gaga); 232 delete receiver.gaga; 233 234 assertFalse(Reflect.set(target, "nowrite", value)); 235 assertEquals(44, target.nowrite); 236 237 assertFalse(Reflect.set(target, "nowrite", value, target)); 238 assertEquals(44, target.nowrite); 239 240 assertFalse(Reflect.set(target, "nowrite", value, receiver)); 241 assertEquals(44, target.nowrite); 242 assertFalse("nowrite" in receiver); 243 244 // Data vs Non-Writable 245 assertFalse(Reflect.set({}, "nowrite", value, target)); 246 247 // Data vs Accessor 248 assertFalse(Reflect.set({}, "unknown", 0, {set unknown(x) {}})); 249 assertFalse(Reflect.set(target, "unknown", value, {set unknown(x) {}})); 250 assertFalse(Reflect.set(target, "bla", value, {set bla(x) {}})); 251 assertFalse(Reflect.set(target, "bla", value, {get bla() {}})); 252 253 // Accessor vs Data 254 assertTrue(Reflect.set({set bla(x) {}}), "bla", value, target); 255 assertFalse(Reflect.set({get bla() {}}, "bla", value, target)); 256 257 // Data vs Non-Object 258 assertFalse(Reflect.set({}, "bla", value, null)); 259 assertFalse(Reflect.set({bla: 42}, "bla", value, null)); 260 261 // Accessor vs Non-Object 262 assertTrue(Reflect.set(target, "setter2", value, null)); 263 assertFalse(Reflect.set(target, "getter", value, null)); 264 265 let receiver2 = {}; 266 Object.defineProperty(receiver2, "bla", 267 {configurable: false, writable: true, value: true}); 268 Object.defineProperty(receiver2, "not_in_target", 269 {configurable: false, writable: true, value: true}); 270 assertTrue(Reflect.set(target, "bla", value, receiver2)); 271 assertTrue(Reflect.set(target, "not_in_target", value, receiver2)); 272 } 273 })(); 274 275 276 277 //////////////////////////////////////////////////////////////////////////////// 278 // Reflect.has 279 280 281 (function testReflectHasArity() { 282 assertEquals(2, Reflect.has.length); 283 })(); 284 285 286 (function testReflectHasOnNonObject() { 287 assertThrows(function() { Reflect.has(); }, TypeError); 288 assertThrows(function() { Reflect.has(42, "bla"); }, TypeError); 289 assertThrows(function() { Reflect.has(null, "bla"); }, TypeError); 290 })(); 291 292 293 (function testReflectHasKeyConversion() { 294 var target = {bla: 42}; 295 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 296 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 297 assertTrue(Reflect.has(target, a)); 298 assertThrowsEquals(function() { Reflect.has(target, b); }, "gaga"); 299 })(); 300 301 302 (function testReflectHasOnObject() { 303 for (let target of objects) { 304 prepare(target); 305 assertTrue(Reflect.has(target, "bla")); 306 assertTrue(Reflect.has(target, 4)); 307 assertTrue(Reflect.has(target, "4")); 308 assertTrue(Reflect.has(target, sym)); 309 assertTrue(Reflect.has(target, "noconf")); 310 assertTrue(Reflect.has(target, "getter")); 311 assertTrue(Reflect.has(target, "setter")); 312 assertFalse(Reflect.has(target, "foo")); 313 assertFalse(Reflect.has(target, 666)); 314 315 let proto = target.__proto__; 316 target.__proto__ = { get foo() {return this.bla} }; 317 assertEquals(true, Reflect.has(target, "foo")); 318 target.__proto__ = proto; 319 } 320 })(); 321 322 323 324 //////////////////////////////////////////////////////////////////////////////// 325 // Reflect.defineProperty 326 327 328 (function testReflectDefinePropertyArity() { 329 assertEquals(3, Reflect.defineProperty.length); 330 })(); 331 332 333 (function testReflectDefinePropertyOnNonObject() { 334 assertThrows(function() { Reflect.defineProperty(); }, TypeError); 335 assertThrows(function() { Reflect.defineProperty(42, "bla"); }, TypeError); 336 assertThrows(function() { Reflect.defineProperty(null, "bla"); }, TypeError); 337 assertThrows(function() { Reflect.defineProperty({}, "bla"); }, TypeError); 338 assertThrows(function() { Reflect.defineProperty({}, "bla", 42); }, 339 TypeError); 340 assertThrows(function() { Reflect.defineProperty({}, "bla", null); }, 341 TypeError); 342 })(); 343 344 345 (function testReflectDefinePropertyKeyConversion() { 346 var target = {}; 347 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 348 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 349 assertTrue(Reflect.defineProperty(target, a, {value: 42})); 350 assertEquals(target.bla, 42); 351 assertThrowsEquals(function() { Reflect.defineProperty(target, b); }, "gaga"); 352 })(); 353 354 355 // See reflect-define-property.js for further tests. 356 357 358 359 //////////////////////////////////////////////////////////////////////////////// 360 // Reflect.deleteProperty 361 362 363 (function testReflectDeletePropertyArity() { 364 assertEquals(2, Reflect.deleteProperty.length); 365 })(); 366 367 368 (function testReflectDeletePropertyOnNonObject() { 369 assertThrows(function() { Reflect.deleteProperty(); }, TypeError); 370 assertThrows(function() { Reflect.deleteProperty(42, "bla"); }, TypeError); 371 assertThrows(function() { Reflect.deleteProperty(null, "bla"); }, TypeError); 372 })(); 373 374 375 (function testReflectDeletePropertyKeyConversion() { 376 var target = {bla: 42}; 377 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 378 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 379 assertTrue(Reflect.deleteProperty(target, a)); 380 assertThrowsEquals(function() { Reflect.deleteProperty(target, b); }, "gaga"); 381 })(); 382 383 384 (function testReflectDeletePropertyOnObject() { 385 for (let target of objects) { 386 prepare(target); 387 assertTrue(Reflect.deleteProperty(target, "bla")); 388 assertEquals(undefined, Object.getOwnPropertyDescriptor(target, "bla")); 389 if (target instanceof Int32Array) { 390 assertFalse(Reflect.deleteProperty(target, 4)); 391 } else { 392 assertTrue(Reflect.deleteProperty(target, 4)); 393 assertEquals(undefined, Object.getOwnPropertyDescriptor(target, 4)); 394 } 395 assertTrue(Reflect.deleteProperty(target, sym)); 396 assertEquals(undefined, Object.getOwnPropertyDescriptor(target, sym)); 397 assertFalse(Reflect.deleteProperty(target, "noconf")); 398 assertEquals(43, target.noconf); 399 assertTrue(Reflect.deleteProperty(target, "getter")); 400 assertTrue(Reflect.deleteProperty(target, "setter")); 401 assertTrue(Reflect.deleteProperty(target, "foo")); 402 assertTrue(Reflect.deleteProperty(target, 666)); 403 404 let proto = target.__proto__; 405 target.__proto__ = { get foo() {return this.bla} }; 406 assertEquals(true, Reflect.deleteProperty(target, "foo")); 407 target.__proto__ = proto; 408 } 409 })(); 410 411 412 413 //////////////////////////////////////////////////////////////////////////////// 414 // Reflect.getPrototypeOf 415 416 417 (function testReflectGetPrototypeOfArity() { 418 assertEquals(1, Reflect.getPrototypeOf.length); 419 })(); 420 421 422 (function testReflectGetPrototypeOnNonObject() { 423 assertThrows(function() { Reflect.getPrototypeOf(); }, TypeError); 424 assertThrows(function() { Reflect.getPrototypeOf(42); }, TypeError); 425 assertThrows(function() { Reflect.getPrototypeOf(null); }, TypeError); 426 })(); 427 428 429 // See reflect-get-prototype-of.js for further tests. 430 431 432 433 //////////////////////////////////////////////////////////////////////////////// 434 // Reflect.setPrototypeOf 435 436 437 (function testReflectSetPrototypeOfArity() { 438 assertEquals(2, Reflect.setPrototypeOf.length); 439 })(); 440 441 442 (function testReflectSetPrototypeOfOnNonObject() { 443 assertThrows(function() { Reflect.setPrototypeOf(undefined, {}); }, 444 TypeError); 445 assertThrows(function() { Reflect.setPrototypeOf(42, {}); }, TypeError); 446 assertThrows(function() { Reflect.setPrototypeOf(null, {}); }, TypeError); 447 448 assertThrows(function() { Reflect.setPrototypeOf({}, undefined); }, 449 TypeError); 450 assertThrows(function() { Reflect.setPrototypeOf({}, 42); }, TypeError); 451 assertTrue(Reflect.setPrototypeOf({}, null)); 452 })(); 453 454 455 // See reflect-set-prototype-of.js for further tests. 456 457 458 459 //////////////////////////////////////////////////////////////////////////////// 460 // Reflect.isExtensible 461 462 463 (function testReflectIsExtensibleArity() { 464 assertEquals(1, Reflect.isExtensible.length); 465 })(); 466 467 468 (function testReflectIsExtensibleOnNonObject() { 469 assertThrows(function() { Reflect.isExtensible(); }, TypeError); 470 assertThrows(function() { Reflect.isExtensible(42); }, TypeError); 471 assertThrows(function() { Reflect.isExtensible(null); }, TypeError); 472 })(); 473 474 475 (function testReflectIsExtensibleOnObject() { 476 // This should be the last test on [objects] as it modifies them irreversibly. 477 for (let target of objects) { 478 prepare(target); 479 if (target instanceof Int32Array) continue; // issue v8:4460 480 assertTrue(Reflect.isExtensible(target)); 481 Object.preventExtensions(target); 482 assertFalse(Reflect.isExtensible(target)); 483 } 484 })(); 485 486 487 //////////////////////////////////////////////////////////////////////////////// 488 // Reflect.getOwnPropertyDescriptor 489 490 491 (function testReflectGetOwnPropertyDescriptorArity() { 492 assertEquals(2, Reflect.getOwnPropertyDescriptor.length); 493 })(); 494 495 496 (function testReflectGetOwnPropertyDescriptorOnNonObject() { 497 assertThrows(function() { Reflect.getOwnPropertyDescriptor(); }, TypeError); 498 assertThrows(function() { Reflect.getOwnPropertyDescriptor(42); }, 499 TypeError); 500 assertThrows(function() { Reflect.getOwnPropertyDescriptor(null); }, 501 TypeError); 502 })(); 503 504 505 (function testReflectGetOwnPropertyDescriptorKeyConversion() { 506 var target = {bla: 42}; 507 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; 508 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; 509 assertEquals(42, Reflect.getOwnPropertyDescriptor(target, a).value); 510 assertThrowsEquals(() => Reflect.getOwnPropertyDescriptor(target, b), "gaga"); 511 })(); 512 513 514 // See reflect-get-own-property-descriptor.js for further tests. 515 516 517 518 //////////////////////////////////////////////////////////////////////////////// 519 // Reflect.ownKeys 520 521 522 (function testReflectOwnKeysArity() { 523 assertEquals(1, Reflect.ownKeys.length); 524 })(); 525 526 527 (function testReflectOwnKeysOnNonObject() { 528 assertThrows(function() { Reflect.ownKeys(); }, TypeError); 529 assertThrows(function() { Reflect.ownKeys(42); }, TypeError); 530 assertThrows(function() { Reflect.ownKeys(null); }, TypeError); 531 })(); 532 533 534 (function testReflectOwnKeysOnObject(){ 535 assertEquals(["z", "y", "x"], Reflect.ownKeys({z: 3, y: 2, x: 1})); 536 assertEquals(["length"], Reflect.ownKeys([])); 537 538 var s1 = Symbol("foo"); 539 var s2 = Symbol("bar"); 540 var obj = { [s1]: 0, "bla": 0, 42: 0, "0": 0, 541 [s2]: 0, "-1": 0, "88": 0, "aaa": 0 }; 542 assertEquals(["0", "42", "88", "bla", "-1", "aaa", s1, s2], 543 Reflect.ownKeys(obj)); 544 // Force dict-mode elements. 545 delete obj[0]; 546 assertEquals(["42", "88", "bla", "-1", "aaa", s1, s2], 547 Reflect.ownKeys(obj)); 548 // Force dict-mode properties. 549 delete obj["bla"]; 550 assertEquals(["42", "88", "-1", "aaa", s1, s2], Reflect.ownKeys(obj)); 551 })(); 552 553 554 // See reflect-own-keys.js for further tests. 555 556 557 558 //////////////////////////////////////////////////////////////////////////////// 559 // Reflect.preventExtensions 560 561 562 (function testReflectPreventExtensionsArity() { 563 assertEquals(1, Reflect.preventExtensions.length); 564 })(); 565 566 567 (function testReflectPreventExtensionsOnNonObject() { 568 assertThrows(function() { Reflect.preventExtensions(); }, TypeError); 569 assertThrows(function() { Reflect.preventExtensions(42); }, TypeError); 570 assertThrows(function() { Reflect.preventExtensions(null); }, TypeError); 571 })(); 572 573 574 // See reflect-prevent-extensions.js for further tests. 575 576 // TODO(neis): Need proxies to test the situation where 577 // [[preventExtensions]] returns false. 578