UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

259 lines (208 loc) 9.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (realm) { // ECMA262 25.4.3.1 let func = new _index.NativeFunctionValue(realm, "Promise", "Promise", 1, (context, [executor], argCount, NewTarget) => { // 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]] »). let 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). let resolvingFunctions = (0, _promise.CreateResolvingFunctions)(realm, promise); // 9. Let completion be Call(executor, undefined, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »). let completion; 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, (context, [iterable]) => { // 1. Let C be the this value. let 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). let promiseCapability = (0, _promise.NewPromiseCapability)(realm, C); // 4. Let iterator be GetIterator(iterable). let iterator; 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}. let iteratorRecord = { $Iterator: iterator, $Done: false }; // 7. Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability). let result; 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, (context, [iterable]) => { // 1. Let C be the this value. let 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). let promiseCapability = (0, _promise.NewPromiseCapability)(realm, C); // 4. Let iterator be GetIterator(iterable). let iterator; 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}. let iteratorRecord = { $Iterator: iterator, $Done: false }; // 7. Let result be PerformPromiseRace(iteratorRecord, promiseCapability, C). let result; 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, (context, [r]) => { // 1. Let C be the this value. let 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). let 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, (context, [x]) => { // 1. Let C be the this value. let 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"). let 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). let 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, 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