v8/test/cctest/test-extra.js
Benedikt Meurer 13ca9a0fd1 [turbofan] Properly optimize calls to promise extras.
Add TurboFan inlining support for the following V8 Extras:

 - v8.createPromise
 - v8.rejectPromise
 - v8.resolvePromise

These are used by the streams implementation in Chrome currently, and
were previously not inlined into TurboFan, although TurboFan already
had all the necessary functionality (namely the JSCreatePromise,
JSRejectPromise and JSResolvePromise operators). We might eventually
want to use these functions in Node core as well (at least short-term
for Node 10), to replace the C++ internal API functions with the same
name that are currently being used by parts of Node core.

For this to work, the rejectPromise and resolvePromise builtins had
to be moved back to CSA, as for JavaScript builtins we still have the
policy that the optimizing compiler must not inline them. But that's
straight-forward since the CSA has all the necessary functionality
available anyways.

Bug: v8:7253
Change-Id: I39ab015c379956cd58ace866e17f8ec23b2257b2
Reviewed-on: https://chromium-review.googlesource.com/924146
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51332}
2018-02-16 19:46:30 +00:00

118 lines
3.8 KiB
JavaScript

// Copyright 2015 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.
(function (global, binding, v8) {
'use strict';
binding.testExtraShouldReturnFive = function() {
return 5;
};
binding.testExtraShouldCallToRuntime = function() {
return binding.runtime(3);
};
binding.testFunctionToString = function() {
function foo() { return 1; }
return foo.toString();
};
binding.testStackTrace = function(f) {
return f();
}
// Exercise all of the extras utils:
// - v8.createPrivateSymbol
// - v8.uncurryThis
// - v8.InternalPackedArray
// - v8.createPromise, v8.resolvePromise, v8.rejectPromise
const Object = global.Object;
const hasOwn = v8.uncurryThis(Object.prototype.hasOwnProperty);
const Function = global.Function;
const call = v8.uncurryThis(Function.prototype.call);
const apply = v8.uncurryThis(Function.prototype.apply);
const Promise = global.Promise;
const Promise_resolve = Promise.resolve.bind(Promise);
const arrayToTest = new v8.InternalPackedArray();
arrayToTest.push(1);
arrayToTest.push(2);
arrayToTest.pop();
arrayToTest.unshift("a", "b", "c");
arrayToTest.shift();
arrayToTest.splice(0, 1);
const slicedArray = arrayToTest.slice();
const arraysOK = arrayToTest.length === 2 && arrayToTest[0] === "c" &&
arrayToTest[1] === 1 && slicedArray.length === 2 &&
slicedArray[0] === "c" && slicedArray[1] === 1;
binding.testCreatePromise = function() {
return v8.createPromise();
}
binding.testCreatePromiseWithParent = function(parent) {
return v8.createPromise(parent);
}
binding.testRejectPromise = function(promise, reason) {
return v8.rejectPromise(promise, reason);
}
binding.testResolvePromise = function(promise, resolution) {
return v8.resolvePromise(promise, resolution);
}
binding.testExtraCanUseUtils = function() {
const fulfilledPromise = v8.createPromise();
v8.resolvePromise(
fulfilledPromise,
hasOwn({ test: 'test' }, 'test') ? 1 : -1,
undefined // pass an extra arg to test arguments adapter frame
);
const fulfilledPromise2 = Promise_resolve(call(function (arg1, arg2) {
return (this.prop === arg1 && arg1 === 'value' && arg2) ? 2 : -1;
}, { prop: 'value' }, 'value', arraysOK));
const rejectedPromise = v8.createPromise();
v8.rejectPromise(rejectedPromise, apply(function (arg1, arg2) {
return (arg1 === arg2 && arg2 === 'x') ? 3 : -1;
}, null, new v8.InternalPackedArray('x', 'x')));
const rejectedButHandledPromise = v8.createPromise();
v8.rejectPromise(rejectedButHandledPromise, 4);
v8.markPromiseAsHandled(rejectedButHandledPromise);
function promiseStateToString(promise) {
switch (v8.promiseState(promise)) {
case v8.kPROMISE_PENDING:
return "pending";
case v8.kPROMISE_FULFILLED:
return "fulfilled";
case v8.kPROMISE_REJECTED:
return "rejected";
default:
throw new Error("Unexpected value for promiseState");
}
}
let promiseStates = promiseStateToString(new Promise(() => {})) + ' ' +
promiseStateToString(fulfilledPromise) + ' ' +
promiseStateToString(rejectedPromise);
return {
privateSymbol: v8.createPrivateSymbol('sym'),
fulfilledPromise, // should be fulfilled with 1
fulfilledPromise2, // should be fulfilled with 2
rejectedPromise, // should be rejected with 3
rejectedButHandledPromise, // should be rejected but have a handler
promiseStates, // should be the string "pending fulfilled rejected"
promiseIsPromise: v8.isPromise(fulfilledPromise), // should be true
thenableIsPromise: v8.isPromise({ then() { } }) // should be false
};
};
})