Files
2023-08-01 13:49:46 +02:00

118 lines
10 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /**
* 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.
*/
exports.default = function (ast, strictCode, env, realm) {
var lref = env.evaluate(ast.left, strictCode);
var lval = _singletons.Environment.GetValue(realm, lref);
if (lval instanceof _index.ConcreteValue) {
var 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;
}
var rref = env.evaluate(ast.right, strictCode);
return _singletons.Environment.GetValue(realm, rref);
}
(0, _invariant2.default)(lval instanceof _index.AbstractValue);
var 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
var _construct_empty_effe = (0, _realm.construct_empty_effects)(realm),
_construct_empty_effe2 = _slicedToArray(_construct_empty_effe, 5),
compl1 = _construct_empty_effe2[0],
gen1 = _construct_empty_effe2[1],
bindings1 = _construct_empty_effe2[2],
properties1 = _construct_empty_effe2[3],
createdObj1 = _construct_empty_effe2[4];
compl1; // ignore
// Evaluate ast.right in a sandbox to get its effects
var wrapper = ast.operator === "&&" ? _singletons.Path.withCondition : _singletons.Path.withInverseCondition;
var _wrapper = wrapper(lval, function () {
return realm.evaluateNodeForEffects(ast.right, strictCode, env);
}),
_wrapper2 = _slicedToArray(_wrapper, 5),
compl2 = _wrapper2[0],
gen2 = _wrapper2[1],
bindings2 = _wrapper2[2],
properties2 = _wrapper2[3],
createdObj2 = _wrapper2[4];
// 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.
var joinedEffects = void 0;
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]);
}
var 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