prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
94 lines (74 loc) • 4.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (ast, strictCode, env, realm) {
let [exprValue, exprAst, exprIO] = env.partiallyEvaluateCompletionDeref(ast.test, strictCode);
if (exprValue instanceof _completions.AbruptCompletion) return [exprValue, t.expressionStatement(exprAst), exprIO];
let completion;
if (exprValue instanceof _completions.PossiblyNormalCompletion) {
completion = exprValue;
exprValue = completion.value;
}
(0, _invariant2.default)(exprValue instanceof _index2.Value);
if (!exprValue.mightNotBeTrue()) {
// 3.a. Let stmtCompletion be the result of evaluating the first Statement
let [stmtCompletion, stmtAst, stmtIO] = env.partiallyEvaluateCompletionDeref(ast.consequent, strictCode);
// 5. Return Completion(UpdateEmpty(stmtCompletion, undefined)
stmtCompletion = (0, _index.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
return [stmtCompletion, stmtAst, exprIO.concat(stmtIO)];
} else if (!exprValue.mightNotBeFalse()) {
let stmtCompletion, stmtAst, stmtIO;
if (ast.alternate)
// 4.a. Let stmtCompletion be the result of evaluating the second Statement
[stmtCompletion, stmtAst, stmtIO] = env.partiallyEvaluateCompletionDeref(ast.alternate, strictCode);else {
// 3 (of the if only statement). Return NormalCompletion(undefined)
stmtCompletion = realm.intrinsics.undefined;
stmtAst = t.emptyStatement();
stmtIO = [];
}
// 5. Return Completion(UpdateEmpty(stmtCompletion, undefined)
stmtCompletion = (0, _index.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
return [stmtCompletion, stmtAst, exprIO.concat(stmtIO)];
}
(0, _invariant2.default)(exprValue instanceof _index2.AbstractValue);
// Evaluate consequent and alternate in sandboxes and get their effects.
let [consequentEffects, conAst, conIO] = realm.partiallyEvaluateNodeForEffects(ast.consequent, strictCode, env);
let [conCompl, gen1, bindings1, properties1, createdObj1] = consequentEffects;
let consequentAst = conAst;
if (conIO.length > 0) consequentAst = t.blockStatement(conIO.concat(consequentAst));
let [alternateEffects, altAst, altIO] = ast.alternate ? realm.partiallyEvaluateNodeForEffects(ast.alternate, strictCode, env) : [(0, _realm.construct_empty_effects)(realm), undefined, []];
let [altCompl, gen2, bindings2, properties2, createdObj2] = alternateEffects;
let alternateAst = altAst;
if (altIO.length > 0) alternateAst = t.blockStatement(altIO.concat(alternateAst));
// Join the effects, creating an abstract view of what happened, regardless
// of the actual value of exprValue.
let joinedEffects = _singletons.Join.joinEffects(realm, exprValue, [conCompl, gen1, bindings1, properties1, createdObj1], [altCompl, gen2, bindings2, properties2, createdObj2]);
completion = joinedEffects[0];
if (completion instanceof _completions.PossiblyNormalCompletion) {
// in this case one of the branches may complete abruptly, which means that
// not all control flow branches join into one flow at this point.
// Consequently we have to continue tracking changes until the point where
// all the branches come together into one.
realm.captureEffects(completion);
}
// Note that the effects of (non joining) abrupt branches are not included
// in joinedEffects, but are tracked separately inside completion.
realm.applyEffects(joinedEffects);
let resultAst = t.ifStatement(exprAst, consequentAst, alternateAst);
(0, _invariant2.default)(!(completion instanceof _environment.Reference));
return [completion, resultAst, exprIO];
};
var _completions = require("../completions.js");
var _environment = require("../environment.js");
var _index = require("../methods/index.js");
var _index2 = require("../values/index.js");
var _realm = require("../realm.js");
var _singletons = require("../singletons.js");
var _babelTypes = require("babel-types");
var t = _interopRequireWildcard(_babelTypes);
var _invariant = require("../invariant.js");
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
//# sourceMappingURL=IfStatement.js.map