prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
285 lines (214 loc) • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.JoinedNormalAndAbruptCompletions = exports.JoinedAbruptCompletions = exports.ReturnCompletion = exports.BreakCompletion = exports.ContinueCompletion = exports.ThrowCompletion = exports.AbruptCompletion = exports.SimpleNormalCompletion = exports.NormalCompletion = exports.Completion = void 0;
var _invariant = _interopRequireDefault(require("./invariant.js"));
var _types = require("./types.js");
var _index = require("./values/index.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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.
*/
class Completion {
constructor(value, location, target) {
this.value = value;
this.target = target;
this.location = location;
(0, _invariant.default)(this.constructor !== Completion, "Completion is an abstract base class");
}
containsSelectedCompletion(selector) {
return selector(this);
}
shallowClone() {
(0, _invariant.default)(false, "abstract base method");
}
toDisplayString() {
return "[" + this.constructor.name + " value " + (this.value ? this.value.toDisplayString() : "undefined") + "]";
}
static makeAllNormalCompletionsResultInUndefined(completion) {
let undefinedVal = completion.value.$Realm.intrinsics.undefined;
if (completion instanceof SimpleNormalCompletion) completion.value = undefinedVal;else if (completion instanceof JoinedNormalAndAbruptCompletions) {
if (completion.composedWith !== undefined) Completion.makeAllNormalCompletionsResultInUndefined(completion.composedWith);
Completion.makeAllNormalCompletionsResultInUndefined(completion.consequent);
Completion.makeAllNormalCompletionsResultInUndefined(completion.alternate);
}
}
static makeSelectedCompletionsInfeasible(selector, completion) {
let bottomValue = completion.value.$Realm.intrinsics.__bottomValue;
if (selector(completion)) completion.value = bottomValue;else if (completion instanceof JoinedNormalAndAbruptCompletions || completion instanceof JoinedAbruptCompletions) {
if (completion instanceof JoinedNormalAndAbruptCompletions && completion.composedWith !== undefined) Completion.makeSelectedCompletionsInfeasible(selector, completion.composedWith);
Completion.makeSelectedCompletionsInfeasible(selector, completion.consequent);
Completion.makeSelectedCompletionsInfeasible(selector, completion.alternate);
}
}
static makeSelectedCompletionsInfeasibleInCopy(selector, completion) {
let bottomValue = completion.value.$Realm.intrinsics.__bottomValue;
let clone = completion.shallowClone();
if (selector(clone)) clone.value = bottomValue;else if (clone instanceof JoinedNormalAndAbruptCompletions || clone instanceof JoinedAbruptCompletions) {
clone.consequent = Completion.makeSelectedCompletionsInfeasibleInCopy(selector, clone.consequent);
clone.alternate = Completion.makeSelectedCompletionsInfeasibleInCopy(selector, clone.alternate);
if (clone.consequent.value === bottomValue) {
return clone.alternate;
}
if (clone.alternate.value === bottomValue) {
return clone.consequent;
}
}
return clone;
}
static normalizeSelectedCompletions(selector, completion) {
if (selector(completion)) return new SimpleNormalCompletion(completion.value);
let normalizedComposedWith;
if (completion instanceof JoinedNormalAndAbruptCompletions) {
(0, _invariant.default)(completion.savedEffects === undefined); // caller should not used a still saved completion for this
if (completion.composedWith !== undefined) normalizedComposedWith = Completion.normalizeSelectedCompletions(selector, completion.composedWith);
}
if (completion instanceof JoinedNormalAndAbruptCompletions || completion instanceof JoinedAbruptCompletions) {
let nc = Completion.normalizeSelectedCompletions(selector, completion.consequent);
let na = Completion.normalizeSelectedCompletions(selector, completion.alternate);
if (normalizedComposedWith === undefined) {
if (nc === completion.consequent && na === completion.alternate) return completion;
if (nc instanceof AbruptCompletion && na instanceof AbruptCompletion) return completion;
if (nc instanceof SimpleNormalCompletion && na instanceof SimpleNormalCompletion) return new SimpleNormalCompletion(_index.AbstractValue.createFromConditionalOp(completion.value.$Realm, completion.joinCondition, nc.value, na.value));
(0, _invariant.default)(nc instanceof AbruptCompletion || nc instanceof NormalCompletion);
(0, _invariant.default)(na instanceof AbruptCompletion || na instanceof NormalCompletion);
return new JoinedNormalAndAbruptCompletions(completion.joinCondition, nc, na);
}
(0, _invariant.default)(nc instanceof AbruptCompletion || nc instanceof NormalCompletion);
(0, _invariant.default)(na instanceof AbruptCompletion || na instanceof NormalCompletion);
let result = new JoinedNormalAndAbruptCompletions(completion.joinCondition, nc, na);
if (normalizedComposedWith instanceof JoinedNormalAndAbruptCompletions) result.composedWith = normalizedComposedWith;
return result;
}
return completion;
}
} // Normal completions are returned just like spec completions
exports.Completion = Completion;
class NormalCompletion extends Completion {
constructor(value, location, target) {
super(value, location, target);
(0, _invariant.default)(this.constructor !== NormalCompletion, "NormalCompletion is an abstract base class");
}
} // SimpleNormalCompletions are returned just like spec completions.
// They chiefly exist for use in joined completions.
exports.NormalCompletion = NormalCompletion;
class SimpleNormalCompletion extends NormalCompletion {
shallowClone() {
return new SimpleNormalCompletion(this.value, this.location, this.target);
}
} // Abrupt completions are thrown as exeptions, to make it a easier
// to quickly get to the matching high level construct.
exports.SimpleNormalCompletion = SimpleNormalCompletion;
class AbruptCompletion extends Completion {
constructor(value, location, target) {
super(value, location, target);
(0, _invariant.default)(this.constructor !== AbruptCompletion, "AbruptCompletion is an abstract base class");
}
}
exports.AbruptCompletion = AbruptCompletion;
class ThrowCompletion extends AbruptCompletion {
constructor(value, location, nativeStack, emitWarning = true) {
super(value, location);
this.nativeStack = nativeStack || new Error().stack;
}
shallowClone() {
return new ThrowCompletion(this.value, this.location, this.nativeStack, false);
}
}
exports.ThrowCompletion = ThrowCompletion;
class ContinueCompletion extends AbruptCompletion {
constructor(value, location, target) {
super(value, location, target || null);
}
shallowClone() {
return new ContinueCompletion(this.value, this.location, this.target);
}
}
exports.ContinueCompletion = ContinueCompletion;
class BreakCompletion extends AbruptCompletion {
constructor(value, location, target) {
super(value, location, target || null);
}
shallowClone() {
return new BreakCompletion(this.value, this.location, this.target);
}
}
exports.BreakCompletion = BreakCompletion;
class ReturnCompletion extends AbruptCompletion {
constructor(value, location) {
super(value, location);
if (value instanceof _index.EmptyValue) {
this.value = value.$Realm.intrinsics.undefined;
}
}
shallowClone() {
return new ReturnCompletion(this.value, this.location);
}
}
exports.ReturnCompletion = ReturnCompletion;
class JoinedAbruptCompletions extends AbruptCompletion {
constructor(joinCondition, consequent, alternate) {
super(joinCondition.$Realm.intrinsics.empty, consequent.location);
this.joinCondition = joinCondition;
this.consequent = consequent;
this.alternate = alternate;
}
containsSelectedCompletion(selector) {
if (selector(this.consequent)) return true;
if (selector(this.alternate)) return true;
if (this.consequent instanceof JoinedAbruptCompletions) {
if (this.consequent.containsSelectedCompletion(selector)) return true;
}
if (this.alternate instanceof JoinedAbruptCompletions) {
if (this.alternate.containsSelectedCompletion(selector)) return true;
}
return false;
}
shallowClone() {
return new JoinedAbruptCompletions(this.joinCondition, this.consequent, this.alternate);
}
toDisplayString() {
let superString = super.toDisplayString().slice(0, -1);
return superString + " c: [" + this.consequent.toDisplayString() + "] a: [" + this.alternate.toDisplayString() + "]]";
}
} // This should never be thrown, therefore it is treated as a NormalCompletion even though it is also Abrupt.
exports.JoinedAbruptCompletions = JoinedAbruptCompletions;
class JoinedNormalAndAbruptCompletions extends NormalCompletion {
constructor(joinCondition, consequent, alternate) {
super(consequent instanceof NormalCompletion ? consequent.value : alternate.value, consequent.location);
this.joinCondition = joinCondition;
this.consequent = consequent;
this.alternate = alternate;
this.pathConditionsAtCreation = joinCondition.$Realm.pathConditions;
}
containsSelectedCompletion(selector) {
if (this.composedWith !== undefined && this.composedWith.containsSelectedCompletion(selector)) return true;
if (selector(this.consequent)) return true;
if (selector(this.alternate)) return true;
if (this.consequent instanceof JoinedAbruptCompletions || this.consequent instanceof JoinedNormalAndAbruptCompletions) {
if (this.consequent.containsSelectedCompletion(selector)) return true;
}
if (this.alternate instanceof JoinedAbruptCompletions || this.alternate instanceof JoinedNormalAndAbruptCompletions) {
if (this.alternate.containsSelectedCompletion(selector)) return true;
}
return false;
}
shallowClone() {
let clone = new JoinedNormalAndAbruptCompletions(this.joinCondition, this.consequent, this.alternate);
clone.composedWith = this.composedWith;
clone.pathConditionsAtCreation = this.pathConditionsAtCreation;
return clone;
}
toDisplayString() {
let superString = super.toDisplayString().slice(0, -1);
return superString + " c: [" + this.consequent.toDisplayString() + "] a: [" + this.alternate.toDisplayString() + "]]";
}
}
exports.JoinedNormalAndAbruptCompletions = JoinedNormalAndAbruptCompletions;
//# sourceMappingURL=completions.js.map