promise-capability-default-closures.js 1.35 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --allow-natives-syntax
6 7 8 9 10

(function() {
  var resolve, value;
  (new Promise(r => resolve = r)).then(v => value = v);
  function foo() { resolve(1); }
11
  %PrepareFunctionForOptimization(foo);
12 13 14 15 16 17 18 19 20 21 22
  foo();
  foo();
  %OptimizeFunctionOnNextCall(foo);
  foo();
  setTimeout(_ => assertEquals(1, value));
})();

(function() {
  var reject, value;
  (new Promise((_, r) => reject = r)).catch(v => value = v);
  function foo() { reject(1); }
23
  %PrepareFunctionForOptimization(foo);
24 25 26 27 28 29
  foo();
  foo();
  %OptimizeFunctionOnNextCall(foo);
  foo();
  setTimeout(_ => assertEquals(1, value));
})();
30 31 32 33

(function() {
  var value;
  function foo(x) { return new Promise((resolve, reject) => resolve(x)); }
34
  %PrepareFunctionForOptimization(foo);
35 36 37 38 39 40 41 42 43 44
  foo(1);
  foo(1);
  %OptimizeFunctionOnNextCall(foo);
  foo(1).then(v => value = v);
  setTimeout(_ => assertEquals(1, value));
})();

(function() {
  var value;
  function foo(x) { return new Promise((resolve, reject) => reject(x)); }
45
  %PrepareFunctionForOptimization(foo);
46 47
  foo(1).catch(() => { /* ignore */ });
  foo(1).catch(() => { /* ignore */ });
48 49 50 51
  %OptimizeFunctionOnNextCall(foo);
  foo(1).catch(v => value = v);
  setTimeout(_ => assertEquals(1, value));
})();