Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 5 | // Flags: --allow-natives-syntax --expose-gc |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | |
| 10 | function checkPrototypeChain(object, constructors) { |
| 11 | var proto = object.__proto__; |
| 12 | for (var i = 0; i < constructors.length; i++) { |
| 13 | assertEquals(constructors[i].prototype, proto); |
| 14 | assertEquals(constructors[i], proto.constructor); |
| 15 | proto = proto.__proto__; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | |
| 20 | (function() { |
| 21 | class A extends Object { |
| 22 | constructor(...args) { |
| 23 | assertFalse(new.target === undefined); |
| 24 | super(...args); |
| 25 | this.a = 42; |
| 26 | this.d = 4.2; |
| 27 | this.o = {foo:153}; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | var s = new A("foo"); |
| 32 | assertTrue(s instanceof Object); |
| 33 | assertTrue(s instanceof A); |
| 34 | assertEquals("object", typeof s); |
| 35 | checkPrototypeChain(s, [A, Object]); |
| 36 | assertEquals(42, s.a); |
| 37 | assertEquals(4.2, s.d); |
| 38 | assertEquals(153, s.o.foo); |
| 39 | |
| 40 | var s1 = new A("bar"); |
| 41 | assertTrue(%HaveSameMap(s, s1)); |
| 42 | |
| 43 | |
| 44 | var n = new A(153); |
| 45 | assertTrue(n instanceof Object); |
| 46 | assertTrue(n instanceof A); |
| 47 | assertEquals("object", typeof s); |
| 48 | checkPrototypeChain(s, [A, Object]); |
| 49 | assertEquals(42, n.a); |
| 50 | assertEquals(4.2, n.d); |
| 51 | assertEquals(153, n.o.foo); |
| 52 | |
| 53 | var n1 = new A(312); |
| 54 | assertTrue(%HaveSameMap(n, n1)); |
| 55 | assertTrue(%HaveSameMap(n, s)); |
| 56 | |
| 57 | |
| 58 | var b = new A(true); |
| 59 | assertTrue(b instanceof Object); |
| 60 | assertTrue(b instanceof A); |
| 61 | assertEquals("object", typeof s); |
| 62 | checkPrototypeChain(s, [A, Object]); |
| 63 | assertEquals(42, b.a); |
| 64 | assertEquals(4.2, b.d); |
| 65 | assertEquals(153, b.o.foo); |
| 66 | |
| 67 | var b1 = new A(true); |
| 68 | assertTrue(%HaveSameMap(b, b1)); |
| 69 | assertTrue(%HaveSameMap(b, s)); |
| 70 | |
| 71 | gc(); |
| 72 | })(); |
| 73 | |
| 74 | |
| 75 | (function() { |
| 76 | class A extends Function { |
| 77 | constructor(...args) { |
| 78 | assertFalse(new.target === undefined); |
| 79 | super(...args); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | this.a = 42; |
| 81 | this.d = 4.2; |
| 82 | this.o = {foo:153}; |
| 83 | } |
| 84 | } |
| 85 | var sloppy_func = new A(""); |
| 86 | var strict_func = new A("'use strict';"); |
| 87 | assertNull(sloppy_func.caller); |
| 88 | assertThrows("strict_f.caller"); |
| 89 | assertNull(Object.getOwnPropertyDescriptor(sloppy_func, "caller").value); |
| 90 | assertEquals(undefined, Object.getOwnPropertyDescriptor(strict_func, "caller")); |
| 91 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 92 | function CheckFunction(func) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 93 | assertEquals("function", typeof func); |
| 94 | assertTrue(func instanceof Object); |
| 95 | assertTrue(func instanceof Function); |
| 96 | assertTrue(func instanceof A); |
| 97 | checkPrototypeChain(func, [A, Function, Object]); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 98 | assertEquals(42, func.a); |
| 99 | assertEquals(4.2, func.d); |
| 100 | assertEquals(153, func.o.foo); |
| 101 | assertTrue(undefined !== func.prototype); |
| 102 | func.prototype.bar = "func.bar"; |
| 103 | var obj = new func(); |
| 104 | assertTrue(obj instanceof Object); |
| 105 | assertTrue(obj instanceof func); |
| 106 | assertEquals("object", typeof obj); |
| 107 | assertEquals(113, obj.foo); |
| 108 | assertEquals("func.bar", obj.bar); |
| 109 | delete func.prototype.bar; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | var source = "this.foo = 113;"; |
| 113 | |
| 114 | // Sloppy function |
| 115 | var sloppy_func = new A(source); |
| 116 | assertTrue(undefined !== sloppy_func.prototype); |
| 117 | CheckFunction(sloppy_func, false); |
| 118 | |
| 119 | var sloppy_func1 = new A("return 312;"); |
| 120 | assertTrue(%HaveSameMap(sloppy_func, sloppy_func1)); |
| 121 | |
| 122 | // Strict function |
| 123 | var strict_func = new A("'use strict'; " + source); |
| 124 | assertFalse(%HaveSameMap(strict_func, sloppy_func)); |
| 125 | CheckFunction(strict_func, false); |
| 126 | |
| 127 | var strict_func1 = new A("'use strict'; return 312;"); |
| 128 | assertTrue(%HaveSameMap(strict_func, strict_func1)); |
| 129 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | gc(); |
| 131 | })(); |
| 132 | |
| 133 | |
| 134 | (function() { |
| 135 | class A extends Boolean { |
| 136 | constructor(...args) { |
| 137 | assertFalse(new.target === undefined); |
| 138 | super(...args); |
| 139 | this.a = 42; |
| 140 | this.d = 4.2; |
| 141 | this.o = {foo:153}; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | var o = new A(true); |
| 146 | assertTrue(o instanceof Object); |
| 147 | assertTrue(o instanceof Boolean); |
| 148 | assertTrue(o instanceof A); |
| 149 | assertEquals("object", typeof o); |
| 150 | checkPrototypeChain(o, [A, Boolean]); |
| 151 | assertTrue(o.valueOf()); |
| 152 | assertEquals(42, o.a); |
| 153 | assertEquals(4.2, o.d); |
| 154 | assertEquals(153, o.o.foo); |
| 155 | |
| 156 | var o1 = new A(false); |
| 157 | assertTrue(%HaveSameMap(o, o1)); |
| 158 | |
| 159 | gc(); |
| 160 | })(); |
| 161 | |
| 162 | |
| 163 | function TestErrorSubclassing(error) { |
| 164 | class A extends error { |
| 165 | constructor(...args) { |
| 166 | assertFalse(new.target === undefined); |
| 167 | super(...args); |
| 168 | this.a = 42; |
| 169 | this.d = 4.2; |
| 170 | this.o = {foo:153}; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | var o = new A("message"); |
| 175 | assertTrue(o instanceof Object); |
| 176 | assertTrue(o instanceof error); |
| 177 | assertTrue(o instanceof Error); |
| 178 | assertTrue(o instanceof A); |
| 179 | assertEquals("object", typeof o); |
| 180 | if (error == Error) { |
| 181 | checkPrototypeChain(o, [A, Error, Object]); |
| 182 | } else { |
| 183 | checkPrototypeChain(o, [A, error, Error, Object]); |
| 184 | } |
| 185 | assertEquals("message", o.message); |
| 186 | assertEquals(error.name + ": message", o.toString()); |
| 187 | assertEquals(42, o.a); |
| 188 | assertEquals(4.2, o.d); |
| 189 | assertEquals(153, o.o.foo); |
| 190 | |
| 191 | var o1 = new A("achtung!"); |
| 192 | assertTrue(%HaveSameMap(o, o1)); |
| 193 | |
| 194 | gc(); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | (function() { |
| 199 | TestErrorSubclassing(Error); |
| 200 | TestErrorSubclassing(EvalError); |
| 201 | TestErrorSubclassing(RangeError); |
| 202 | TestErrorSubclassing(ReferenceError); |
| 203 | TestErrorSubclassing(SyntaxError); |
| 204 | TestErrorSubclassing(TypeError); |
| 205 | TestErrorSubclassing(URIError); |
| 206 | })(); |
| 207 | |
| 208 | |
| 209 | (function() { |
| 210 | class A extends Number { |
| 211 | constructor(...args) { |
| 212 | assertFalse(new.target === undefined); |
| 213 | super(...args); |
| 214 | this.a = 42; |
| 215 | this.d = 4.2; |
| 216 | this.o = {foo:153}; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | var o = new A(153); |
| 221 | assertTrue(o instanceof Object); |
| 222 | assertTrue(o instanceof Number); |
| 223 | assertTrue(o instanceof A); |
| 224 | assertEquals("object", typeof o); |
| 225 | checkPrototypeChain(o, [A, Number, Object]); |
| 226 | assertEquals(153, o.valueOf()); |
| 227 | assertEquals(42, o.a); |
| 228 | assertEquals(4.2, o.d); |
| 229 | assertEquals(153, o.o.foo); |
| 230 | |
| 231 | var o1 = new A(312); |
| 232 | assertTrue(%HaveSameMap(o, o1)); |
| 233 | |
| 234 | gc(); |
| 235 | })(); |
| 236 | |
| 237 | |
| 238 | (function() { |
| 239 | class A extends Date { |
| 240 | constructor(...args) { |
| 241 | assertFalse(new.target === undefined); |
| 242 | super(...args); |
| 243 | this.a = 42; |
| 244 | this.d = 4.2; |
| 245 | this.o = {foo:153}; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | var o = new A(1234567890); |
| 250 | assertTrue(o instanceof Object); |
| 251 | assertTrue(o instanceof Date); |
| 252 | assertTrue(o instanceof A); |
| 253 | assertEquals("object", typeof o); |
| 254 | checkPrototypeChain(o, [A, Date, Object]); |
| 255 | assertEquals(1234567890, o.getTime()); |
| 256 | assertEquals(42, o.a); |
| 257 | assertEquals(4.2, o.d); |
| 258 | assertEquals(153, o.o.foo); |
| 259 | |
| 260 | var o1 = new A(2015, 10, 29); |
| 261 | assertEquals(2015, o1.getFullYear()); |
| 262 | assertEquals(10, o1.getMonth()); |
| 263 | assertEquals(29, o1.getDate()); |
| 264 | assertTrue(%HaveSameMap(o, o1)); |
| 265 | |
| 266 | gc(); |
| 267 | })(); |
| 268 | |
| 269 | |
| 270 | (function() { |
| 271 | class A extends String { |
| 272 | constructor(...args) { |
| 273 | assertFalse(new.target === undefined); |
| 274 | super(...args); |
| 275 | this.a = 42; |
| 276 | this.d = 4.2; |
| 277 | this.o = {foo:153}; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | var o = new A("foo"); |
| 282 | assertTrue(o instanceof Object); |
| 283 | assertTrue(o instanceof String); |
| 284 | assertTrue(o instanceof A); |
| 285 | assertEquals("object", typeof o); |
| 286 | checkPrototypeChain(o, [A, String, Object]); |
| 287 | |
| 288 | assertEquals("foo", o.valueOf()); |
| 289 | assertEquals(42, o.a); |
| 290 | assertEquals(4.2, o.d); |
| 291 | assertEquals(153, o.o.foo); |
| 292 | |
| 293 | var o1 = new A("bar"); |
| 294 | assertTrue(%HaveSameMap(o, o1)); |
| 295 | |
| 296 | gc(); |
| 297 | })(); |
| 298 | |
| 299 | |
| 300 | (function() { |
| 301 | class A extends RegExp { |
| 302 | constructor(...args) { |
| 303 | assertFalse(new.target === undefined); |
| 304 | super(...args); |
| 305 | this.a = 42; |
| 306 | this.d = 4.2; |
| 307 | this.o = {foo:153}; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | var o = new A("o(..)h", "g"); |
| 312 | assertTrue(o instanceof Object); |
| 313 | assertTrue(o instanceof RegExp); |
| 314 | assertTrue(o instanceof A); |
| 315 | assertEquals("object", typeof o); |
| 316 | checkPrototypeChain(o, [A, RegExp, Object]); |
| 317 | assertTrue(o.test("ouch")); |
| 318 | assertArrayEquals(["ouch", "uc"], o.exec("boom! ouch! bam!")); |
| 319 | assertEquals("o(..)h", o.source); |
| 320 | assertTrue(o.global); |
| 321 | assertFalse(o.ignoreCase); |
| 322 | assertFalse(o.multiline); |
| 323 | assertEquals(10, o.lastIndex); |
| 324 | assertEquals(42, o.a); |
| 325 | assertEquals(4.2, o.d); |
| 326 | assertEquals(153, o.o.foo); |
| 327 | |
| 328 | var o1 = new A(7); |
| 329 | assertTrue(%HaveSameMap(o, o1)); |
| 330 | |
| 331 | gc(); |
| 332 | })(); |
| 333 | |
| 334 | |
| 335 | (function TestArraySubclassing() { |
| 336 | class A extends Array { |
| 337 | constructor(...args) { |
| 338 | assertFalse(new.target === undefined); |
| 339 | super(...args); |
| 340 | this.a = 42; |
| 341 | this.d = 4.2; |
| 342 | this.o = {foo:153}; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | var o = new Array(13); |
| 347 | assertTrue(o instanceof Object); |
| 348 | assertTrue(o instanceof Array); |
| 349 | assertEquals("object", typeof o); |
| 350 | checkPrototypeChain(o, [Array, Object]); |
| 351 | assertEquals(13, o.length); |
| 352 | |
| 353 | var o = new A(10); |
| 354 | assertTrue(o instanceof Object); |
| 355 | assertTrue(o instanceof Array); |
| 356 | assertTrue(o instanceof A); |
| 357 | assertEquals("object", typeof o); |
| 358 | checkPrototypeChain(o, [A, Array, Object]); |
| 359 | assertEquals(10, o.length); |
| 360 | assertEquals(42, o.a); |
| 361 | assertEquals(4.2, o.d); |
| 362 | assertEquals(153, o.o.foo); |
| 363 | |
| 364 | var o1 = new A(7); |
| 365 | assertTrue(%HaveSameMap(o, o1)); |
| 366 | })(); |
| 367 | |
| 368 | |
| 369 | var TypedArray = Uint8Array.__proto__; |
| 370 | |
| 371 | function TestTypedArraySubclassing(array) { |
| 372 | class A extends array { |
| 373 | constructor(...args) { |
| 374 | assertFalse(new.target === undefined); |
| 375 | super(...args); |
| 376 | this.a = 42; |
| 377 | this.d = 4.2; |
| 378 | this.o = {foo:153}; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | var o = new array(13); |
| 383 | assertTrue(o instanceof Object); |
| 384 | assertTrue(o instanceof TypedArray); |
| 385 | assertTrue(o instanceof array); |
| 386 | assertEquals("object", typeof o); |
| 387 | checkPrototypeChain(o, [array, TypedArray, Object]); |
| 388 | assertEquals(13, o.length); |
| 389 | |
| 390 | var o = new A(10); |
| 391 | assertTrue(o instanceof Object); |
| 392 | assertTrue(o instanceof TypedArray); |
| 393 | assertTrue(o instanceof array); |
| 394 | assertTrue(o instanceof A); |
| 395 | assertEquals("object", typeof o); |
| 396 | checkPrototypeChain(o, [A, array, TypedArray, Object]); |
| 397 | assertEquals(10, o.length); |
| 398 | assertEquals(42, o.a); |
| 399 | assertEquals(4.2, o.d); |
| 400 | assertEquals(153, o.o.foo); |
| 401 | |
| 402 | var o1 = new A(7); |
| 403 | assertTrue(%HaveSameMap(o, o1)); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | (function() { |
| 408 | TestTypedArraySubclassing(Int8Array); |
| 409 | TestTypedArraySubclassing(Uint8Array); |
| 410 | TestTypedArraySubclassing(Uint8ClampedArray); |
| 411 | TestTypedArraySubclassing(Int16Array); |
| 412 | TestTypedArraySubclassing(Uint16Array); |
| 413 | TestTypedArraySubclassing(Int32Array); |
| 414 | TestTypedArraySubclassing(Uint32Array); |
| 415 | TestTypedArraySubclassing(Float32Array); |
| 416 | TestTypedArraySubclassing(Float64Array); |
| 417 | })(); |
| 418 | |
| 419 | |
| 420 | function TestMapSetSubclassing(container, is_map) { |
| 421 | var keys = [{name: "banana"}, {name: "cow"}, {name: "orange"}, {name: "chicken"}, {name: "apple"}]; |
| 422 | |
| 423 | class A extends container { |
| 424 | constructor(...args) { |
| 425 | assertFalse(new.target === undefined); |
| 426 | super(...args); |
| 427 | this.a = 42; |
| 428 | this.d = 4.2; |
| 429 | this.o = {foo:153}; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | var o = new A(); |
| 434 | assertTrue(o instanceof Object); |
| 435 | assertTrue(o instanceof container); |
| 436 | assertTrue(o instanceof A); |
| 437 | assertEquals("object", typeof o); |
| 438 | checkPrototypeChain(o, [A, container, Object]); |
| 439 | |
| 440 | for (var i = 0; i < keys.length; i++) { |
| 441 | if (is_map) { |
| 442 | o.set(keys[i], (i + 1) * 11); |
| 443 | } else { |
| 444 | o.add(keys[i]); |
| 445 | } |
| 446 | } |
| 447 | o.delete(keys[1]); |
| 448 | o.delete(keys[3]); |
| 449 | |
| 450 | assertTrue(o.has(keys[0])); |
| 451 | assertFalse(o.has(keys[1])); |
| 452 | assertTrue(o.has(keys[2])); |
| 453 | assertFalse(o.has(keys[1])); |
| 454 | assertTrue(o.has(keys[4])); |
| 455 | if (is_map) { |
| 456 | assertEquals(11, o.get(keys[0])); |
| 457 | assertEquals(undefined, o.get(keys[1])); |
| 458 | assertEquals(33, o.get(keys[2])); |
| 459 | assertEquals(undefined, o.get(keys[3])); |
| 460 | assertEquals(55, o.get(keys[4])); |
| 461 | } |
| 462 | assertEquals(42, o.a); |
| 463 | assertEquals(4.2, o.d); |
| 464 | assertEquals(153, o.o.foo); |
| 465 | |
| 466 | var o1 = new A(); |
| 467 | assertTrue(%HaveSameMap(o, o1)); |
| 468 | |
| 469 | gc(); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | (function() { |
| 474 | TestMapSetSubclassing(Map, true); |
| 475 | TestMapSetSubclassing(WeakMap, true); |
| 476 | TestMapSetSubclassing(Set, false); |
| 477 | TestMapSetSubclassing(WeakSet, false); |
| 478 | })(); |
| 479 | |
| 480 | |
| 481 | (function() { |
| 482 | class A extends ArrayBuffer { |
| 483 | constructor(...args) { |
| 484 | assertFalse(new.target === undefined); |
| 485 | super(...args); |
| 486 | this.a = 42; |
| 487 | this.d = 4.2; |
| 488 | this.o = {foo:153}; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | var o = new A(16); |
| 493 | assertTrue(o instanceof Object); |
| 494 | assertTrue(o instanceof ArrayBuffer); |
| 495 | assertTrue(o instanceof A); |
| 496 | assertEquals("object", typeof o); |
| 497 | checkPrototypeChain(o, [A, ArrayBuffer, Object]); |
| 498 | |
| 499 | assertEquals(16, o.byteLength); |
| 500 | assertEquals(42, o.a); |
| 501 | assertEquals(4.2, o.d); |
| 502 | assertEquals(153, o.o.foo); |
| 503 | |
| 504 | var o1 = new A("bar"); |
| 505 | assertTrue(%HaveSameMap(o, o1)); |
| 506 | |
| 507 | |
| 508 | class MyInt32Array extends Int32Array { |
| 509 | constructor(v, name) { |
| 510 | super(v); |
| 511 | this.name = name; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | class MyUint32Array extends Uint32Array { |
| 516 | constructor(v, name) { |
| 517 | super(v); |
| 518 | this.name = name; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | var int32view = new MyInt32Array(o, "cats"); |
| 523 | var uint32view = new MyUint32Array(o, "dogs"); |
| 524 | |
| 525 | int32view[0] = -2; |
| 526 | uint32view[1] = 0xffffffff; |
| 527 | |
| 528 | assertEquals("cats", int32view.name); |
| 529 | assertEquals("dogs", uint32view.name); |
| 530 | assertEquals(-2, int32view[0]); |
| 531 | assertEquals(-1, int32view[1]); |
| 532 | assertEquals(0xfffffffe, uint32view[0]); |
| 533 | assertEquals(0xffffffff, uint32view[1]); |
| 534 | |
| 535 | gc(); |
| 536 | })(); |
| 537 | |
| 538 | |
| 539 | (function() { |
| 540 | class A extends DataView { |
| 541 | constructor(...args) { |
| 542 | assertFalse(new.target === undefined); |
| 543 | super(...args); |
| 544 | this.a = 42; |
| 545 | this.d = 4.2; |
| 546 | this.o = {foo:153}; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | var buffer = new ArrayBuffer(16); |
| 551 | var o = new A(buffer); |
| 552 | assertTrue(o instanceof Object); |
| 553 | assertTrue(o instanceof DataView); |
| 554 | assertTrue(o instanceof A); |
| 555 | assertEquals("object", typeof o); |
| 556 | checkPrototypeChain(o, [A, DataView, Object]); |
| 557 | |
| 558 | o.setUint32(0, 0xcafebabe, false); |
| 559 | assertEquals(0xcafebabe, o.getUint32(0, false)); |
| 560 | assertEquals(0xbebafeca, o.getUint32(0, true)); |
| 561 | assertEquals(42, o.a); |
| 562 | assertEquals(4.2, o.d); |
| 563 | assertEquals(153, o.o.foo); |
| 564 | |
| 565 | var o1 = new A(buffer); |
| 566 | assertTrue(%HaveSameMap(o, o1)); |
| 567 | |
| 568 | gc(); |
| 569 | })(); |
| 570 | |
| 571 | |
| 572 | (function() { |
| 573 | var GeneratorFunction = (function*() {}).constructor; |
| 574 | class A extends GeneratorFunction { |
| 575 | constructor(...args) { |
| 576 | assertFalse(new.target === undefined); |
| 577 | super(...args); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | this.a = 42; |
| 579 | this.d = 4.2; |
| 580 | this.o = {foo:153}; |
| 581 | } |
| 582 | } |
| 583 | var sloppy_func = new A("yield 153;"); |
| 584 | var strict_func = new A("'use strict'; yield 153;"); |
| 585 | // Unfortunately the difference is not observable from outside. |
| 586 | assertThrows("sloppy_func.caller"); |
| 587 | assertThrows("strict_f.caller"); |
| 588 | assertEquals(undefined, Object.getOwnPropertyDescriptor(sloppy_func, "caller")); |
| 589 | assertEquals(undefined, Object.getOwnPropertyDescriptor(strict_func, "caller")); |
| 590 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 591 | function CheckFunction(func) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 592 | assertEquals("function", typeof func); |
| 593 | assertTrue(func instanceof Object); |
| 594 | assertTrue(func instanceof Function); |
| 595 | assertTrue(func instanceof GeneratorFunction); |
| 596 | assertTrue(func instanceof A); |
| 597 | checkPrototypeChain(func, [A, GeneratorFunction, Function, Object]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 598 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 599 | assertEquals(42, func.a); |
| 600 | assertEquals(4.2, func.d); |
| 601 | assertEquals(153, func.o.foo); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 602 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 603 | assertTrue(undefined !== func.prototype); |
| 604 | func.prototype.bar = "func.bar"; |
| 605 | var obj = func(); // Generator object. |
| 606 | assertTrue(obj instanceof Object); |
| 607 | assertTrue(obj instanceof func); |
| 608 | assertEquals("object", typeof obj); |
| 609 | assertEquals("func.bar", obj.bar); |
| 610 | delete func.prototype.bar; |
| 611 | |
| 612 | assertPropertiesEqual({done: false, value: 1}, obj.next()); |
| 613 | assertPropertiesEqual({done: false, value: 1}, obj.next()); |
| 614 | assertPropertiesEqual({done: false, value: 2}, obj.next()); |
| 615 | assertPropertiesEqual({done: false, value: 3}, obj.next()); |
| 616 | assertPropertiesEqual({done: false, value: 5}, obj.next()); |
| 617 | assertPropertiesEqual({done: false, value: 8}, obj.next()); |
| 618 | assertPropertiesEqual({done: true, value: undefined}, obj.next()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | var source = "yield 1; yield 1; yield 2; yield 3; yield 5; yield 8;"; |
| 622 | |
| 623 | // Sloppy generator function |
| 624 | var sloppy_func = new A(source); |
| 625 | assertTrue(undefined !== sloppy_func.prototype); |
| 626 | CheckFunction(sloppy_func, false); |
| 627 | |
| 628 | var sloppy_func1 = new A("yield 312;"); |
| 629 | assertTrue(%HaveSameMap(sloppy_func, sloppy_func1)); |
| 630 | |
| 631 | // Strict generator function |
| 632 | var strict_func = new A("'use strict'; " + source); |
| 633 | assertFalse(%HaveSameMap(strict_func, sloppy_func)); |
| 634 | CheckFunction(strict_func, false); |
| 635 | |
| 636 | var strict_func1 = new A("'use strict'; yield 312;"); |
| 637 | assertTrue(%HaveSameMap(strict_func, strict_func1)); |
| 638 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 639 | gc(); |
| 640 | })(); |
| 641 | |
| 642 | |
| 643 | (function() { |
| 644 | class A extends Promise { |
| 645 | constructor(...args) { |
| 646 | assertFalse(new.target === undefined); |
| 647 | super(...args); |
| 648 | this.a = 42; |
| 649 | this.d = 4.2; |
| 650 | this.o = {foo:153}; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | var o = new A(function(resolve, reject) { |
| 655 | resolve("ok"); |
| 656 | }); |
| 657 | assertTrue(o instanceof Object); |
| 658 | assertTrue(o instanceof Promise); |
| 659 | assertTrue(o instanceof A); |
| 660 | assertEquals("object", typeof o); |
| 661 | checkPrototypeChain(o, [A, Promise, Object]); |
| 662 | assertEquals(42, o.a); |
| 663 | assertEquals(4.2, o.d); |
| 664 | assertEquals(153, o.o.foo); |
| 665 | o.then( |
| 666 | function(val) { assertEquals("ok", val); }, |
| 667 | function(reason) { assertUnreachable(); }) |
| 668 | .catch(function(reason) { %AbortJS("catch handler called: " + reason); }); |
| 669 | |
| 670 | var o1 = new A(function(resolve, reject) { |
| 671 | reject("fail"); |
| 672 | }); |
| 673 | o1.then( |
| 674 | function(val) { assertUnreachable(); }, |
| 675 | function(reason) { assertEquals("fail", reason); }) |
| 676 | .catch(function(reason) { %AbortJS("catch handler called: " + reason); }); |
| 677 | assertTrue(%HaveSameMap(o, o1)); |
| 678 | |
| 679 | gc(); |
| 680 | })(); |
| 681 | |
| 682 | |
| 683 | (function() { |
| 684 | class A extends Boolean { |
| 685 | constructor() { |
| 686 | assertFalse(new.target === undefined); |
| 687 | super(true); |
| 688 | this.a00 = 0 |
| 689 | this.a01 = 0 |
| 690 | this.a02 = 0 |
| 691 | this.a03 = 0 |
| 692 | this.a04 = 0 |
| 693 | this.a05 = 0 |
| 694 | this.a06 = 0 |
| 695 | this.a07 = 0 |
| 696 | this.a08 = 0 |
| 697 | this.a09 = 0 |
| 698 | this.a10 = 0 |
| 699 | this.a11 = 0 |
| 700 | this.a12 = 0 |
| 701 | this.a13 = 0 |
| 702 | this.a14 = 0 |
| 703 | this.a15 = 0 |
| 704 | this.a16 = 0 |
| 705 | this.a17 = 0 |
| 706 | this.a18 = 0 |
| 707 | this.a19 = 0 |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | class B extends A { |
| 712 | constructor() { |
| 713 | assertFalse(new.target === undefined); |
| 714 | super(); |
| 715 | this.b00 = 0 |
| 716 | this.b01 = 0 |
| 717 | this.b02 = 0 |
| 718 | this.b03 = 0 |
| 719 | this.b04 = 0 |
| 720 | this.b05 = 0 |
| 721 | this.b06 = 0 |
| 722 | this.b07 = 0 |
| 723 | this.b08 = 0 |
| 724 | this.b09 = 0 |
| 725 | this.b10 = 0 |
| 726 | this.b11 = 0 |
| 727 | this.b12 = 0 |
| 728 | this.b13 = 0 |
| 729 | this.b14 = 0 |
| 730 | this.b15 = 0 |
| 731 | this.b16 = 0 |
| 732 | this.b17 = 0 |
| 733 | this.b18 = 0 |
| 734 | this.b19 = 0 |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | class C extends B { |
| 739 | constructor() { |
| 740 | assertFalse(new.target === undefined); |
| 741 | super(); |
| 742 | this.c00 = 0 |
| 743 | this.c01 = 0 |
| 744 | this.c02 = 0 |
| 745 | this.c03 = 0 |
| 746 | this.c04 = 0 |
| 747 | this.c05 = 0 |
| 748 | this.c06 = 0 |
| 749 | this.c07 = 0 |
| 750 | this.c08 = 0 |
| 751 | this.c09 = 0 |
| 752 | this.c10 = 0 |
| 753 | this.c11 = 0 |
| 754 | this.c12 = 0 |
| 755 | this.c13 = 0 |
| 756 | this.c14 = 0 |
| 757 | this.c15 = 0 |
| 758 | this.c16 = 0 |
| 759 | this.c17 = 0 |
| 760 | this.c18 = 0 |
| 761 | this.c19 = 0 |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | var o = new C(); |
| 766 | assertTrue(o instanceof Object); |
| 767 | assertTrue(o instanceof Boolean); |
| 768 | assertTrue(o instanceof A); |
| 769 | assertTrue(o instanceof B); |
| 770 | assertTrue(o instanceof C); |
| 771 | assertEquals("object", typeof o); |
| 772 | checkPrototypeChain(o, [C, B, A, Boolean, Object]); |
| 773 | |
| 774 | gc(); |
| 775 | })(); |
| 776 | |
| 777 | |
| 778 | (function() { |
| 779 | assertThrows("class A extends undefined {}"); |
| 780 | assertThrows("class B extends NaN {}"); |
| 781 | assertThrows("class C extends Infinity {}"); |
| 782 | })(); |
| 783 | |
| 784 | |
| 785 | (function() { |
| 786 | class A extends null {} |
| 787 | assertThrows("new A"); |
| 788 | })(); |
| 789 | |
| 790 | |
| 791 | (function() { |
| 792 | class A extends Symbol {} |
| 793 | assertThrows("new A"); |
| 794 | })(); |
| 795 | |
| 796 | |
| 797 | (function() { |
| 798 | function f() {} |
| 799 | |
| 800 | var p = f.prototype; |
| 801 | var p2 = {}; |
| 802 | var o = Reflect.construct( |
| 803 | Number, [{valueOf() { f.prototype=p2; return 10; }}], f); |
| 804 | |
| 805 | assertTrue(o.__proto__ === f.prototype); |
| 806 | assertTrue(p2 === f.prototype); |
| 807 | assertFalse(p === o.__proto__); |
| 808 | assertEquals(10, Number.prototype.valueOf.call(o)); |
| 809 | })(); |
| 810 | |
| 811 | |
| 812 | (function() { |
| 813 | function f() {} |
| 814 | |
| 815 | var p = f.prototype; |
| 816 | var p2 = {}; |
| 817 | var o = Reflect.construct( |
| 818 | String, [{toString() { f.prototype=p2; return "biep"; }}], f); |
| 819 | |
| 820 | assertTrue(o.__proto__ === f.prototype); |
| 821 | assertTrue(p2 === o.__proto__); |
| 822 | assertFalse(p === o.__proto__); |
| 823 | assertEquals("biep", String.prototype.toString.call(o)); |
| 824 | })(); |
| 825 | |
| 826 | |
| 827 | (function() { |
| 828 | function f() {} |
| 829 | |
| 830 | var p = f.prototype; |
| 831 | var p2 = {}; |
| 832 | var o = Reflect.construct( |
| 833 | Date, [{valueOf() { f.prototype=p2; return 1447836899614; }}], f); |
| 834 | |
| 835 | assertTrue(o.__proto__ === f.prototype); |
| 836 | assertTrue(p2 === f.prototype); |
| 837 | assertFalse(p === o.__proto__); |
| 838 | assertEquals(new Date(1447836899614).toString(), |
| 839 | Date.prototype.toString.call(o)); |
| 840 | })(); |
| 841 | |
| 842 | |
| 843 | (function() { |
| 844 | function f() {} |
| 845 | |
| 846 | var p = f.prototype; |
| 847 | var p2 = {}; |
| 848 | var o = Reflect.construct( |
| 849 | Date, [2015, {valueOf() { f.prototype=p2; return 10; }}], f); |
| 850 | |
| 851 | assertTrue(o.__proto__ === f.prototype); |
| 852 | assertTrue(p2 === f.prototype); |
| 853 | assertFalse(p === o.__proto__); |
| 854 | assertEquals(new Date(2015, 10).getYear(), Date.prototype.getYear.call(o)); |
| 855 | assertEquals(new Date(2015, 10).getMonth(), Date.prototype.getMonth.call(o)); |
| 856 | })(); |
| 857 | |
| 858 | |
| 859 | (function() { |
| 860 | function f() {} |
| 861 | |
| 862 | var p = f.prototype; |
| 863 | var p2 = {}; |
| 864 | var o = Reflect.construct( |
| 865 | DataView, [new ArrayBuffer(100), |
| 866 | {valueOf(){ f.prototype=p2; return 5; }}], f); |
| 867 | |
| 868 | var byteOffset = Object.getOwnPropertyDescriptor( |
| 869 | DataView.prototype, "byteOffset").get; |
| 870 | var byteLength = Object.getOwnPropertyDescriptor( |
| 871 | DataView.prototype, "byteLength").get; |
| 872 | |
| 873 | assertTrue(o.__proto__ === f.prototype); |
| 874 | assertTrue(p2 === f.prototype); |
| 875 | assertFalse(p === o.__proto__); |
| 876 | assertEquals(5, byteOffset.call(o)); |
| 877 | assertEquals(95, byteLength.call(o)); |
| 878 | })(); |
| 879 | |
| 880 | |
| 881 | (function() { |
| 882 | function f() {} |
| 883 | |
| 884 | var p = f.prototype; |
| 885 | var p2 = {}; |
| 886 | var o = Reflect.construct( |
| 887 | DataView, [new ArrayBuffer(100), |
| 888 | 30, {valueOf() { f.prototype=p2; return 5; }}], f); |
| 889 | |
| 890 | var byteOffset = Object.getOwnPropertyDescriptor( |
| 891 | DataView.prototype, "byteOffset").get; |
| 892 | var byteLength = Object.getOwnPropertyDescriptor( |
| 893 | DataView.prototype, "byteLength").get; |
| 894 | |
| 895 | assertTrue(o.__proto__ === f.prototype); |
| 896 | assertTrue(p2 === f.prototype); |
| 897 | assertFalse(p === o.__proto__); |
| 898 | assertEquals(30, byteOffset.call(o)); |
| 899 | assertEquals(5, byteLength.call(o)); |
| 900 | })(); |
| 901 | |
| 902 | |
| 903 | (function() { |
| 904 | function f() {} |
| 905 | |
| 906 | var p = f.prototype; |
| 907 | var p2 = {}; |
| 908 | var p3 = {}; |
| 909 | |
| 910 | var log = []; |
| 911 | |
| 912 | var pattern = {toString() { |
| 913 | log.push("tostring"); |
| 914 | f.prototype = p3; return "biep" }}; |
| 915 | |
| 916 | Object.defineProperty(pattern, Symbol.match, { |
| 917 | get() { log.push("match"); f.prototype = p2; return false; }}); |
| 918 | |
| 919 | var o = Reflect.construct(RegExp, [pattern], f); |
| 920 | assertEquals(["match", "tostring"], log); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 921 | // TODO(littledan): Is the RegExp constructor correct to create |
| 922 | // the internal slots and do these type checks this way? |
| 923 | assertEquals("biep", %_RegExpSource(o)); |
| 924 | assertThrows(() => Object.getOwnPropertyDescriptor(RegExp.prototype, |
| 925 | 'source').get(o), |
| 926 | TypeError); |
| 927 | assertEquals("/undefined/undefined", RegExp.prototype.toString.call(o)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 928 | assertTrue(o.__proto__ === p2); |
| 929 | assertTrue(f.prototype === p3); |
| 930 | })(); |