• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// Flags: --harmony-species --allow-natives-syntax
6
7// Test that Promises use @@species appropriately
8
9// Another constructor with no species will not be instantiated
10var test = new Promise(function(){});
11var bogoCount = 0;
12function bogusConstructor() { bogoCount++; }
13test.constructor = bogusConstructor;
14assertTrue(Promise.resolve(test) instanceof Promise);
15assertFalse(Promise.resolve(test) instanceof bogusConstructor);
16// Tests that chromium:575314 is fixed thoroughly
17Promise.resolve(test).catch(e => %AbortJS("Error " + e)).then(() => {
18  if (bogoCount != 0) %AbortJS("bogoCount was " + bogoCount + " should be 0");
19});
20
21// If there is a species, it will be instantiated
22// @@species will be read exactly once, and the constructor is called with a
23// function
24var count = 0;
25var params;
26class MyPromise extends Promise {
27  constructor(...args) {
28    super(...args);
29    params = args;
30  }
31  static get [Symbol.species]() {
32    count++
33    return this;
34  }
35}
36
37var myPromise = MyPromise.resolve().then();
38assertEquals(1, count);
39assertEquals(1, params.length);
40assertEquals('function', typeof(params[0]));
41assertTrue(myPromise instanceof MyPromise);
42assertTrue(myPromise instanceof Promise);
43