2015-05-20 07:38:19 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-09-16 21:00:45 +00:00
|
|
|
(function (global, binding, v8) {
|
2015-05-20 07:38:19 +00:00
|
|
|
'use strict';
|
2015-09-16 21:00:45 +00:00
|
|
|
binding.testExtraShouldReturnFive = function() {
|
2015-05-20 07:38:19 +00:00
|
|
|
return 5;
|
|
|
|
};
|
2015-06-11 12:19:40 +00:00
|
|
|
|
2015-08-06 23:52:47 +00:00
|
|
|
binding.testExtraShouldCallToRuntime = function() {
|
|
|
|
return binding.runtime(3);
|
2015-06-11 12:19:40 +00:00
|
|
|
};
|
2015-09-16 21:00:45 +00:00
|
|
|
|
2016-02-05 12:33:20 +00:00
|
|
|
binding.testFunctionToString = function() {
|
|
|
|
function foo() { return 1; }
|
|
|
|
return foo.toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
binding.testStackTrace = function(f) {
|
|
|
|
return f();
|
|
|
|
}
|
|
|
|
|
2015-09-16 21:00:45 +00:00
|
|
|
// Exercise all of the extras utils:
|
|
|
|
// - v8.createPrivateSymbol
|
2018-02-06 08:05:37 +00:00
|
|
|
// - v8.uncurryThis
|
2015-09-16 21:00:45 +00:00
|
|
|
// - 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;
|
2018-02-06 08:05:37 +00:00
|
|
|
const Promise_resolve = Promise.resolve.bind(Promise);
|
2015-09-16 21:00:45 +00:00
|
|
|
|
2015-10-14 17:40:43 +00:00
|
|
|
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;
|
|
|
|
|
2018-02-16 19:10:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-16 21:00:45 +00:00
|
|
|
binding.testExtraCanUseUtils = function() {
|
|
|
|
const fulfilledPromise = v8.createPromise();
|
|
|
|
v8.resolvePromise(
|
|
|
|
fulfilledPromise,
|
2017-01-12 18:57:48 +00:00
|
|
|
hasOwn({ test: 'test' }, 'test') ? 1 : -1,
|
|
|
|
undefined // pass an extra arg to test arguments adapter frame
|
2015-09-16 21:00:45 +00:00
|
|
|
);
|
|
|
|
|
2015-10-14 17:40:43 +00:00
|
|
|
const fulfilledPromise2 = Promise_resolve(call(function (arg1, arg2) {
|
|
|
|
return (this.prop === arg1 && arg1 === 'value' && arg2) ? 2 : -1;
|
|
|
|
}, { prop: 'value' }, 'value', arraysOK));
|
2015-09-16 21:00:45 +00:00
|
|
|
|
|
|
|
const rejectedPromise = v8.createPromise();
|
|
|
|
v8.rejectPromise(rejectedPromise, apply(function (arg1, arg2) {
|
|
|
|
return (arg1 === arg2 && arg2 === 'x') ? 3 : -1;
|
2018-12-12 15:15:13 +00:00
|
|
|
}, null, ['x', 'x']));
|
2015-09-16 21:00:45 +00:00
|
|
|
|
2016-11-15 21:24:58 +00:00
|
|
|
const rejectedButHandledPromise = v8.createPromise();
|
|
|
|
v8.rejectPromise(rejectedButHandledPromise, 4);
|
|
|
|
v8.markPromiseAsHandled(rejectedButHandledPromise);
|
|
|
|
|
2017-03-31 05:58:15 +00:00
|
|
|
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);
|
|
|
|
|
2019-01-07 05:08:08 +00:00
|
|
|
const uncurryThis = v8.uncurryThis(function (a, b, c, d, e) {
|
|
|
|
return (this + a + b + c + d + e) === 21;
|
|
|
|
})(1, 2, 3, 4, 5, 6);
|
|
|
|
|
2015-09-16 21:00:45 +00:00
|
|
|
return {
|
|
|
|
privateSymbol: v8.createPrivateSymbol('sym'),
|
|
|
|
fulfilledPromise, // should be fulfilled with 1
|
|
|
|
fulfilledPromise2, // should be fulfilled with 2
|
2016-11-15 21:24:58 +00:00
|
|
|
rejectedPromise, // should be rejected with 3
|
2017-03-31 05:58:15 +00:00
|
|
|
rejectedButHandledPromise, // should be rejected but have a handler
|
2017-04-06 13:16:35 +00:00
|
|
|
promiseStates, // should be the string "pending fulfilled rejected"
|
|
|
|
promiseIsPromise: v8.isPromise(fulfilledPromise), // should be true
|
2019-01-07 05:08:08 +00:00
|
|
|
thenableIsPromise: v8.isPromise({ then() { } }), // should be false
|
|
|
|
uncurryThis // should be true
|
2015-09-16 21:00:45 +00:00
|
|
|
};
|
|
|
|
};
|
2015-05-20 07:38:19 +00:00
|
|
|
})
|