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