2018-02-19 13:31:18 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-06-23 14:45:51 +00:00
|
|
|
// Flags: --allow-natives-syntax --ignore-unhandled-promises
|
2018-02-19 13:31:18 +00:00
|
|
|
|
|
|
|
// We have to patch mjsunit because normal assertion failures just throw
|
|
|
|
// exceptions which are swallowed in a then clause.
|
|
|
|
failWithMessage = (msg) => %AbortJS(msg);
|
|
|
|
|
|
|
|
// Don't crash.
|
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
let resolve, reject, promise;
|
|
|
|
promise = new Promise((a, b) => { resolve = a; reject = b; });
|
|
|
|
|
|
|
|
return {resolve, reject, promise};
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
2018-02-21 09:07:57 +00:00
|
|
|
// Check that when executor is non-callable, the constructor throws.
|
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
return new Promise(1);
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-21 09:07:57 +00:00
|
|
|
assertThrows(foo, TypeError);
|
|
|
|
assertThrows(foo, TypeError);
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
assertThrows(foo, TypeError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Check that when the promise constructor throws because the executor is
|
|
|
|
// non-callable, the stack contains 'new Promise'.
|
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
return new Promise(1);
|
|
|
|
}
|
2018-02-19 13:31:18 +00:00
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-21 09:07:57 +00:00
|
|
|
let threw;
|
|
|
|
try {
|
|
|
|
threw = false;
|
|
|
|
foo();
|
|
|
|
} catch (e) {
|
|
|
|
threw = true;
|
|
|
|
assertContains('new Promise', e.stack);
|
|
|
|
} finally {
|
|
|
|
assertTrue(threw);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
threw = false;
|
|
|
|
foo();
|
|
|
|
} catch (e) {
|
|
|
|
threw = true;
|
|
|
|
assertContains('new Promise', e.stack);
|
|
|
|
} finally {
|
|
|
|
assertTrue(threw);
|
|
|
|
}
|
|
|
|
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
try {
|
|
|
|
threw = false;
|
|
|
|
foo();
|
|
|
|
} catch (e) {
|
|
|
|
threw = true;
|
2018-02-23 13:14:07 +00:00
|
|
|
assertContains('new Promise', e.stack);
|
2018-02-21 09:07:57 +00:00
|
|
|
} finally {
|
|
|
|
assertTrue(threw);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Check that when executor throws, the promise is rejected.
|
2018-02-19 13:31:18 +00:00
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
return new Promise((a, b) => { throw new Error(); });
|
|
|
|
}
|
2019-06-14 13:05:18 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
|
|
|
|
function bar(i) {
|
2018-02-21 09:07:57 +00:00
|
|
|
let error = null;
|
|
|
|
foo().then(_ => error = 1, e => error = e);
|
|
|
|
setTimeout(_ => assertInstanceof(error, Error));
|
|
|
|
if (i == 1) %OptimizeFunctionOnNextCall(foo);
|
|
|
|
if (i > 0) setTimeout(bar.bind(null, i - 1));
|
|
|
|
}
|
|
|
|
bar(3);
|
2018-02-19 13:31:18 +00:00
|
|
|
})();
|
|
|
|
|
2018-02-21 09:07:57 +00:00
|
|
|
// Check that when executor causes lazy deoptimization of the inlined
|
|
|
|
// constructor, we return the promise value and not the return value of the
|
|
|
|
// executor function itself.
|
2018-02-19 13:31:18 +00:00
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
let p;
|
|
|
|
try {
|
|
|
|
p = new Promise((a, b) => { %DeoptimizeFunction(foo); });
|
|
|
|
} catch (e) {
|
|
|
|
// Nothing should throw
|
|
|
|
assertUnreachable();
|
|
|
|
}
|
2018-02-21 09:07:57 +00:00
|
|
|
assertInstanceof(p, Promise);
|
2018-02-19 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
2018-02-21 09:07:57 +00:00
|
|
|
// The same as above, except that the executor function also creates a promise
|
|
|
|
// and both executor functions cause a lazy deopt of the calling function.
|
2018-02-19 13:31:18 +00:00
|
|
|
(function() {
|
2018-02-21 09:07:57 +00:00
|
|
|
function executor(a, b) {
|
|
|
|
%DeoptimizeFunction(foo);
|
|
|
|
let p = new Promise((a, b) => { %DeoptimizeFunction(executor); });
|
|
|
|
}
|
2018-02-19 13:31:18 +00:00
|
|
|
function foo() {
|
|
|
|
let p;
|
|
|
|
try {
|
2018-02-21 09:07:57 +00:00
|
|
|
p = new Promise(executor);
|
|
|
|
} catch (e) {
|
|
|
|
// Nothing should throw
|
|
|
|
assertUnreachable();
|
|
|
|
}
|
|
|
|
assertInstanceof(p, Promise);
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-21 09:07:57 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Check that when the executor causes lazy deoptimization of the inlined
|
|
|
|
// constructor, and then throws, the deopt continuation catches and then calls
|
|
|
|
// the reject function instead of propagating the exception.
|
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
let p;
|
|
|
|
try {
|
|
|
|
p = new Promise((a, b) => {
|
|
|
|
%DeoptimizeFunction(foo);
|
|
|
|
throw new Error();
|
|
|
|
});
|
2018-02-19 13:31:18 +00:00
|
|
|
} catch (e) {
|
|
|
|
// The promise constructor should catch the exception and reject the
|
|
|
|
// promise instead.
|
2018-04-04 06:31:33 +00:00
|
|
|
assertUnreachable();
|
2018-02-19 13:31:18 +00:00
|
|
|
}
|
2018-04-04 06:31:33 +00:00
|
|
|
assertInstanceof(p, Promise);
|
2018-02-19 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
2018-04-04 06:31:33 +00:00
|
|
|
|
|
|
|
// Check that when the promise constructor is marked for lazy deoptimization
|
|
|
|
// from below, but not immediatelly deoptimized, and then throws, the deopt continuation
|
|
|
|
// catches and calls the reject function instead of propagating the exception.
|
|
|
|
(function() {
|
|
|
|
function foo() {
|
|
|
|
let p;
|
|
|
|
try {
|
|
|
|
p = new Promise((resolve, reject) => { bar(); resolve()});
|
|
|
|
} catch (e) {
|
|
|
|
// The promise constructor should catch the exception and reject the
|
|
|
|
// promise instead.
|
|
|
|
assertUnreachable();
|
|
|
|
}
|
|
|
|
assertInstanceof(p, Promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() {
|
|
|
|
%DeoptimizeFunction(foo);
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
%NeverOptimizeFunction(bar);
|
|
|
|
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-04-04 06:31:33 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
2018-02-19 13:31:18 +00:00
|
|
|
// Test when the executor is not inlined.
|
|
|
|
(function() {
|
|
|
|
let resolve, reject, promise;
|
|
|
|
function bar(a, b) {
|
|
|
|
resolve = a; reject = b;
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
function foo() {
|
|
|
|
promise = new Promise(bar);
|
|
|
|
}
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%NeverOptimizeFunction(bar);
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test that the stack trace contains 'new Promise'
|
|
|
|
(function() {
|
|
|
|
let resolve, reject, promise;
|
|
|
|
function bar(a, b) {
|
|
|
|
resolve = a; reject = b;
|
|
|
|
let stack = new Error().stack;
|
2018-02-23 13:14:07 +00:00
|
|
|
assertContains("new Promise", stack);
|
2018-02-19 13:31:18 +00:00
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
function foo() {
|
|
|
|
promise = new Promise(bar);
|
|
|
|
}
|
2019-03-01 10:19:54 +00:00
|
|
|
%PrepareFunctionForOptimization(foo);
|
2018-02-19 13:31:18 +00:00
|
|
|
foo();
|
|
|
|
foo();
|
|
|
|
%OptimizeFunctionOnNextCall(foo);
|
|
|
|
foo();
|
|
|
|
})();
|