UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

119 lines (90 loc) 4.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _realm = require("../realm.js"); var _completions = require("../completions.js"); var _errors = require("../errors.js"); var _index = require("../values/index.js"); var _environment = require("../environment.js"); var _singletons = require("../singletons.js"); var _invariant = _interopRequireDefault(require("../invariant.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. */ /* strict-local */ function _default(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, _invariant.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, _invariant.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; (0, _invariant.default)(lcond instanceof _index.AbstractValue); // Create empty effects for the case where ast.right is not evaluated let { result: result1, generator: generator1, modifiedBindings: modifiedBindings1, modifiedProperties: modifiedProperties1, createdObjects: createdObjects1 } = (0, _realm.construct_empty_effects)(realm); result1; // ignore // Evaluate ast.right in a sandbox to get its effects let result2, generator2, modifiedBindings2, modifiedProperties2, createdObjects2; try { let wrapper = ast.operator === "&&" ? _singletons.Path.withCondition : _singletons.Path.withInverseCondition; ({ result: result2, generator: generator2, modifiedBindings: modifiedBindings2, modifiedProperties: modifiedProperties2, createdObjects: createdObjects2 } = wrapper(lcond, () => realm.evaluateNodeForEffects(ast.right, strictCode, env))); } catch (e) { if (e instanceof _errors.InfeasiblePathError) { // if && then lcond cannot be true on this path else lcond cannot be false on this path. // Either way, we need to return just lval and not evaluate ast.right return lval; } throw e; } // 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(lcond, new _realm.Effects(result2, generator2, modifiedBindings2, modifiedProperties2, createdObjects2), new _realm.Effects(new _completions.SimpleNormalCompletion(lval), generator1, modifiedBindings1, modifiedProperties1, createdObjects1)); } else { joinedEffects = _singletons.Join.joinEffects(lcond, new _realm.Effects(new _completions.SimpleNormalCompletion(lval), generator1, modifiedBindings1, modifiedProperties1, createdObjects1), new _realm.Effects(result2, generator2, modifiedBindings2, modifiedProperties2, createdObjects2)); } realm.applyEffects(joinedEffects); let completion = realm.returnOrThrowCompletion(joinedEffects.result); if (lval instanceof _index.Value && result2.value 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, result2.value, ast.loc); } return completion; } //# sourceMappingURL=LogicalExpression.js.map