Files
asciidisco.com/build/node_modules/prepack/lib/intrinsics/ecma262/Promise.js
2023-08-01 13:49:46 +02:00

283 lines
15 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
exports.default = function (realm) {
// ECMA262 25.4.3.1
var func = new _index.NativeFunctionValue(realm, "Promise", "Promise", 1, function (context, _ref, argCount, NewTarget) {
var _ref2 = _slicedToArray(_ref, 1),
executor = _ref2[0];
// 1. If NewTarget is undefined, throw a TypeError exception.
if (!NewTarget) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 2. If IsCallable(executor) is false, throw a TypeError exception.
if (!(0, _index2.IsCallable)(realm, executor)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. Let promise be ? OrdinaryCreateFromConstructor(NewTarget, "%PromisePrototype%", « [[PromiseState]], [[PromiseResult]], [[PromiseFulfillReactions]], [[PromiseRejectReactions]], [[PromiseIsHandled]] »).
var promise = _singletons.Create.OrdinaryCreateFromConstructor(realm, NewTarget, "PromisePrototype", {
$PromiseState: undefined,
$PromiseResult: undefined,
$PromiseFulfillReactions: undefined,
$PromiseRejectReactions: undefined,
$PromiseIsHandled: undefined
});
// 4. Set promise's [[PromiseState]] internal slot to "pending".
promise.$PromiseState = "pending";
// 5. Set promise's [[PromiseFulfillReactions]] internal slot to a new empty List.
promise.$PromiseFulfillReactions = [];
// 6. Set promise's [[PromiseRejectReactions]] internal slot to a new empty List.
promise.$PromiseRejectReactions = [];
// 7. Set promise's [[PromiseIsHandled]] internal slot to false.
promise.$PromiseIsHandled = false;
// 8. Let resolvingFunctions be CreateResolvingFunctions(promise).
var resolvingFunctions = (0, _promise.CreateResolvingFunctions)(realm, promise);
// 9. Let completion be Call(executor, undefined, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
var completion = void 0;
try {
completion = (0, _index2.Call)(realm, executor, realm.intrinsics.undefined, [resolvingFunctions.resolve, resolvingFunctions.reject]);
} catch (err) {
if (err instanceof _completions.AbruptCompletion) {
completion = err;
} else {
throw err;
}
}
// 10. If completion is an abrupt completion, then
if (completion instanceof _completions.AbruptCompletion) {
// a. Perform ? Call(resolvingFunctions.[[Reject]], undefined, « completion.[[Value]] »).
(0, _index2.Call)(realm, resolvingFunctions.reject, realm.intrinsics.undefined, [completion.value]);
}
// 11. Return promise.
return promise;
});
// ECMA262 25.4.4.1
func.defineNativeMethod("all", 1, function (context, _ref3) {
var _ref4 = _slicedToArray(_ref3, 1),
iterable = _ref4[0];
// 1. Let C be the this value.
var C = context.throwIfNotConcrete();
// 2. If Type(C) is not Object, throw a TypeError exception.
if (!(C instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. Let promiseCapability be ? NewPromiseCapability(C).
var promiseCapability = (0, _promise.NewPromiseCapability)(realm, C);
// 4. Let iterator be GetIterator(iterable).
var iterator = void 0;
try {
iterator = (0, _index2.GetIterator)(realm, iterable);
} catch (e) {
if (e instanceof _completions.AbruptCompletion) {
// 5. IfAbruptRejectPromise(iterator, promiseCapability).
(0, _index2.Call)(realm, promiseCapability.reject, realm.intrinsics.undefined, [e.value]);
return promiseCapability.promise;
} else throw e;
}
// 6. Let iteratorRecord be Record {[[Iterator]]: iterator, [[Done]]: false}.
var iteratorRecord = { $Iterator: iterator, $Done: false };
// 7. Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
var result = void 0;
try {
(0, _invariant2.default)(C instanceof _index.FunctionValue);
result = (0, _promise.PerformPromiseAll)(realm, iteratorRecord, C, promiseCapability);
} catch (e) {
// 8. If result is an abrupt completion, then
if (e instanceof _completions.AbruptCompletion) {
// a. If iteratorRecord.[[Done]] is false, let result be IteratorClose(iterator, result).
if (iteratorRecord.$Done === false) {
try {
result = (0, _iterator.IteratorClose)(realm, iterator, e).value;
} catch (resultCompletion) {
if (resultCompletion instanceof _completions.AbruptCompletion) {
result = resultCompletion.value;
} else throw resultCompletion;
}
} else {
result = e.value;
}
// b. IfAbruptRejectPromise(result, promiseCapability).
(0, _index2.Call)(realm, promiseCapability.reject, realm.intrinsics.undefined, [result]);
return promiseCapability.promise;
} else throw e;
}
// 9. Return Completion(result).
return result;
});
// ECMA262 25.4.4.3
func.defineNativeMethod("race", 1, function (context, _ref5) {
var _ref6 = _slicedToArray(_ref5, 1),
iterable = _ref6[0];
// 1. Let C be the this value.
var C = context.throwIfNotConcrete();
// 2. If Type(C) is not Object, throw a TypeError exception.
if (!(C instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. Let promiseCapability be ? NewPromiseCapability(C).
var promiseCapability = (0, _promise.NewPromiseCapability)(realm, C);
// 4. Let iterator be GetIterator(iterable).
var iterator = void 0;
try {
iterator = (0, _index2.GetIterator)(realm, iterable);
} catch (e) {
if (e instanceof _completions.AbruptCompletion) {
// 5. IfAbruptRejectPromise(iterator, promiseCapability).
(0, _index2.Call)(realm, promiseCapability.reject, realm.intrinsics.undefined, [e.value]);
return promiseCapability.promise;
} else throw e;
}
// 6. Let iteratorRecord be Record {[[Iterator]]: iterator, [[Done]]: false}.
var iteratorRecord = { $Iterator: iterator, $Done: false };
// 7. Let result be PerformPromiseRace(iteratorRecord, promiseCapability, C).
var result = void 0;
try {
result = (0, _promise.PerformPromiseRace)(realm, iteratorRecord, promiseCapability, C);
} catch (e) {
// 8. If result is an abrupt completion, then
if (e instanceof _completions.AbruptCompletion) {
// a. If iteratorRecord.[[Done]] is false, let result be IteratorClose(iterator, result).
if (iteratorRecord.$Done === false) {
try {
result = (0, _iterator.IteratorClose)(realm, iterator, e).value;
} catch (resultCompletion) {
if (resultCompletion instanceof _completions.AbruptCompletion) {
result = resultCompletion.value;
} else throw resultCompletion;
}
} else {
result = e.value;
}
// b. IfAbruptRejectPromise(result, promiseCapability).
(0, _index2.Call)(realm, promiseCapability.reject, realm.intrinsics.undefined, [result]);
return promiseCapability.promise;
} else throw e;
}
// 9. Return Completion(result).
return result;
});
// ECMA262 25.4.4.4
func.defineNativeMethod("reject", 1, function (context, _ref7) {
var _ref8 = _slicedToArray(_ref7, 1),
r = _ref8[0];
// 1. Let C be the this value.
var C = context.throwIfNotConcrete();
// 2. If Type(C) is not Object, throw a TypeError exception.
if (!(C instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. Let promiseCapability be ? NewPromiseCapability(C).
var promiseCapability = (0, _promise.NewPromiseCapability)(realm, C);
// 4. Perform ? Call(promiseCapability.[[Reject]], undefined, « r »).
(0, _index2.Call)(realm, promiseCapability.reject, realm.intrinsics.undefined, [r]);
// 5. Return promiseCapability.[[Promise]].
return promiseCapability.promise;
});
// ECMA262 25.4.4.5
func.defineNativeMethod("resolve", 1, function (context, _ref9) {
var _ref10 = _slicedToArray(_ref9, 1),
x = _ref10[0];
// 1. Let C be the this value.
var C = context.throwIfNotConcrete();
// 2. If Type(C) is not Object, throw a TypeError exception.
if (!(C instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If IsPromise(x) is true, then
if ((0, _index2.IsPromise)(realm, x)) {
(0, _invariant2.default)(x instanceof _index.ObjectValue);
// a. Let xConstructor be ? Get(x, "constructor").
var xConstructor = (0, _index2.Get)(realm, x, "constructor");
// b. If SameValue(xConstructor, C) is true, return x.
if ((0, _index2.SameValuePartial)(realm, xConstructor, C)) return x;
}
// 4. Let promiseCapability be ? NewPromiseCapability(C).
var promiseCapability = (0, _promise.NewPromiseCapability)(realm, C);
// 5. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
(0, _index2.Call)(realm, promiseCapability.resolve, realm.intrinsics.undefined, [x]);
// 6. Return promiseCapability.[[Promise]].
return promiseCapability.promise;
});
// ECMA262 25.4.4.6
func.defineNativeGetter(realm.intrinsics.SymbolSpecies, function (context) {
// 1. Return the this value
return context;
});
return func;
};
var _index = require("../../values/index.js");
var _completions = require("../../completions.js");
var _promise = require("../../methods/promise.js");
var _index2 = require("../../methods/index.js");
var _iterator = require("../../methods/iterator.js");
var _singletons = require("../../singletons.js");
var _invariant = require("../../invariant.js");
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=Promise.js.map