prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
98 lines (81 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (ast, strictCode, env, realm) {
let [lval, leftAst, leftIO] = env.partiallyEvaluateCompletionDeref(ast.left, strictCode);
if (lval instanceof _completions.AbruptCompletion) return [lval, leftAst, leftIO];
let leftCompletion;
[leftCompletion, lval] = _singletons.Join.unbundleNormalCompletion(lval);
(0, _invariant2.default)(lval instanceof _index.Value);
let [rval, rightAst, rightIO] = env.partiallyEvaluateCompletionDeref(ast.right, strictCode);
let io = leftIO.concat(rightIO);
if (rval instanceof _completions.AbruptCompletion) {
// todo: if leftCompletion is a PossiblyNormalCompletion, compose
return [rval, t.binaryExpression(ast.operator, leftAst, rightAst), io];
}
let rightCompletion;
[rightCompletion, rval] = _singletons.Join.unbundleNormalCompletion(rval);
(0, _invariant2.default)(rval instanceof _index.Value);
let op = ast.operator;
let resultValue, resultAst;
if (lval instanceof _index.ConcreteValue) {
if (rval instanceof _index.ConcreteValue) {
resultValue = (0, _BinaryExpression.computeBinary)(realm, op, lval, rval);
resultAst = t.valueToNode(resultValue.serialize());
}
}
// if resultValue is undefined, one or both operands are abstract.
if (resultValue === undefined && (op === "==" || op === "===" || op === "!=" || op === "!==")) {
// When comparing to null or undefined, we can return a compile time value if we know the
// other operand must be an object.
if (!lval.mightNotBeObject() && (rval instanceof _index.NullValue || rval instanceof _index.UndefinedValue) || !rval.mightNotBeObject() && (lval instanceof _index.NullValue || lval instanceof _index.UndefinedValue)) {
resultValue = new _index.BooleanValue(realm, op[0] !== "=");
resultAst = t.valueToNode(resultValue.serialize());
}
}
// todo: special case if one result is known to be 0 or 1
if (resultAst === undefined) {
resultAst = t.binaryExpression(op, leftAst, rightAst);
}
return createAbstractValueForBinary(op, resultAst, lval, rval, leftAst.loc, rightAst.loc, leftCompletion, rightCompletion, resultValue, io, realm);
};
exports.createAbstractValueForBinary = createAbstractValueForBinary;
var _BinaryExpression = require("../evaluators/BinaryExpression.js");
var _completions = require("../completions.js");
var _errors = require("../errors.js");
var _singletons = require("../singletons.js");
var _index = require("../values/index.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; } }
function createAbstractValueForBinary(op, ast, lval, rval, lloc, rloc, leftCompletion, rightCompletion, resultValue, io, realm) {
if (resultValue === undefined) {
let resultType = (0, _BinaryExpression.getPureBinaryOperationResultType)(realm, op, lval, rval, lloc, rloc);
if (resultType === undefined) {
// The operation may result in side effects that we cannot track.
// Since we have no idea what those effects are, we can either forget
// (havoc) everything we know at this stage, or we can fault the
// program and/or native model and stop evaluating.
// We choose to do the latter.
// TODO: report the error and carry on assuming no side effects.
let val = lval instanceof _index.AbstractValue ? lval : rval;
_index.AbstractValue.reportIntrospectionError(val);
throw new _errors.FatalError();
}
resultValue = _index.AbstractValue.createFromBinaryOp(realm, op, lval, rval, ast.loc);
}
let r = _singletons.Join.composeNormalCompletions(leftCompletion, rightCompletion, resultValue, realm);
return [r, ast, io];
} /**
* 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.
*/
//# sourceMappingURL=BinaryExpression.js.map