prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
90 lines (72 loc) • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (ast, strictCode, env, realm) {
let lref = env.evaluate(ast.left, strictCode);
let lval = _singletons.Environment.GetValue(realm, lref);
if (lval instanceof _index.ConcreteValue) {
let lbool = _singletons.To.ToBoolean(realm, lval);
if (ast.operator === "&&") {
// ECMA262 12.13.3
if (lbool === false) return lval;
} else {
(0, _invariant2.default)(ast.operator === "||");
// ECMA262 12.13.3
if (lbool === true) return lval;
}
let rref = env.evaluate(ast.right, strictCode);
return _singletons.Environment.GetValue(realm, rref);
}
(0, _invariant2.default)(lval instanceof _index.AbstractValue);
let lcond = _singletons.Environment.GetConditionValue(realm, lref);
if (!lcond.mightNotBeFalse()) return ast.operator === "||" ? env.evaluate(ast.right, strictCode) : lval;
if (!lcond.mightNotBeTrue()) return ast.operator === "&&" ? env.evaluate(ast.right, strictCode) : lval;
// Create empty effects for the case where ast.right is not evaluated
let [compl1, gen1, bindings1, properties1, createdObj1] = (0, _realm.construct_empty_effects)(realm);
compl1; // ignore
// Evaluate ast.right in a sandbox to get its effects
let wrapper = ast.operator === "&&" ? _singletons.Path.withCondition : _singletons.Path.withInverseCondition;
let [compl2, gen2, bindings2, properties2, createdObj2] = wrapper(lval, () => realm.evaluateNodeForEffects(ast.right, strictCode, env));
// Join the effects, creating an abstract view of what happened, regardless
// of the actual value of lval.
// Note that converting a value to boolean never has a side effect, so we can
// use lval as is for the join condition.
let joinedEffects;
if (ast.operator === "&&") {
joinedEffects = _singletons.Join.joinEffects(realm, lval, [compl2, gen2, bindings2, properties2, createdObj2], [lval, gen1, bindings1, properties1, createdObj1]);
} else {
joinedEffects = _singletons.Join.joinEffects(realm, lval, [lval, gen1, bindings1, properties1, createdObj1], [compl2, gen2, bindings2, properties2, createdObj2]);
}
let completion = joinedEffects[0];
if (completion instanceof _completions.PossiblyNormalCompletion) {
// in this case the evaluation of ast.right 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.
completion = realm.composeWithSavedCompletion(completion);
}
// Note that the effects of (non joining) abrupt branches are not included
// in joinedEffects, but are tracked separately inside completion.
realm.applyEffects(joinedEffects);
// return or throw completion
if (completion instanceof _completions.AbruptCompletion) throw completion;
(0, _invariant2.default)(completion instanceof _index.Value); // references do not survive join
if (lval instanceof _index.Value && compl2 instanceof _index.Value) {
// joinEffects does the right thing for the side effects of the second expression but for the result the join
// produces a conditional expressions of the form (a ? b : a) for a && b and (a ? a : b) for a || b
// Rather than look for this pattern everywhere, we override this behavior and replace the completion with
// the actual logical operator. This helps with simplification and reasoning when dealing with path conditions.
completion = _index.AbstractValue.createFromLogicalOp(realm, ast.operator, lval, compl2, ast.loc);
}
return completion;
};
var _completions = require("../completions.js");
var _realm = require("../realm.js");
var _index = require("../values/index.js");
var _environment = require("../environment.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=LogicalExpression.js.map