"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.evaluate = evaluate;
exports.evaluateWithAbstractConditional = evaluateWithAbstractConditional;

var _completions = require("../completions.js");

var _errors = require("../errors.js");

var _realm = require("../realm.js");

var _index = require("../values/index.js");

var _environment = require("../environment.js");

var _index2 = require("../methods/index.js");

var _invariant = require("../invariant.js");

var _invariant2 = _interopRequireDefault(_invariant);

var _singletons = require("../singletons.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.
 */

function evaluate(ast, strictCode, env, realm) {
  // 1. Let exprRef be the result of evaluating Expression
  let exprRef = env.evaluate(ast.test, strictCode);
  // 2. Let exprValue be ToBoolean(? GetValue(exprRef))
  let exprValue = _singletons.Environment.GetConditionValue(realm, exprRef);

  if (exprValue instanceof _index.ConcreteValue) {
    let stmtCompletion;
    if (_singletons.To.ToBoolean(realm, exprValue)) {
      // 3.a. Let stmtCompletion be the result of evaluating the first Statement
      stmtCompletion = env.evaluateCompletion(ast.consequent, strictCode);
    } else {
      if (ast.alternate)
        // 4.a. Let stmtCompletion be the result of evaluating the second Statement
        stmtCompletion = env.evaluateCompletion(ast.alternate, strictCode);else
        // 3 (of the if only statement). Return NormalCompletion(undefined)
        stmtCompletion = realm.intrinsics.undefined;
    }
    // 5. Return Completion(UpdateEmpty(stmtCompletion, undefined)
    //if (stmtCompletion instanceof Reference) return stmtCompletion;
    (0, _invariant2.default)(!(stmtCompletion instanceof _environment.Reference));
    stmtCompletion = (0, _index2.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
    if (stmtCompletion instanceof _completions.AbruptCompletion) {
      throw stmtCompletion;
    }
    (0, _invariant2.default)(stmtCompletion instanceof _index.Value);
    return stmtCompletion;
  }
  (0, _invariant2.default)(exprValue instanceof _index.AbstractValue);

  if (!exprValue.mightNotBeTrue()) {
    let stmtCompletion = env.evaluate(ast.consequent, strictCode);
    (0, _invariant2.default)(!(stmtCompletion instanceof _environment.Reference));
    stmtCompletion = (0, _index2.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
    if (stmtCompletion instanceof _completions.AbruptCompletion) {
      throw stmtCompletion;
    }
    (0, _invariant2.default)(stmtCompletion instanceof _index.Value);
    return stmtCompletion;
  } else if (!exprValue.mightNotBeFalse()) {
    let stmtCompletion;
    if (ast.alternate) stmtCompletion = env.evaluate(ast.alternate, strictCode);else stmtCompletion = realm.intrinsics.undefined;
    (0, _invariant2.default)(!(stmtCompletion instanceof _environment.Reference));
    stmtCompletion = (0, _index2.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
    if (stmtCompletion instanceof _completions.AbruptCompletion) {
      throw stmtCompletion;
    }
    (0, _invariant2.default)(stmtCompletion instanceof _index.Value);
    return stmtCompletion;
  } else {
    (0, _invariant2.default)(exprValue instanceof _index.AbstractValue);
    return evaluateWithAbstractConditional(exprValue, ast.consequent, ast.alternate, strictCode, env, realm);
  }
}

function evaluateWithAbstractConditional(condValue, consequent, alternate, strictCode, env, realm) {
  // Evaluate consequent and alternate in sandboxes and get their effects.
  let compl1, gen1, bindings1, properties1, createdObj1;
  try {
    [compl1, gen1, bindings1, properties1, createdObj1] = _singletons.Path.withCondition(condValue, () => {
      return realm.evaluateNodeForEffects(consequent, strictCode, env);
    });
  } catch (e) {
    if (e instanceof _errors.InfeasiblePathError) {
      let stmtCompletion;
      if (alternate)
        // 4.a. Let stmtCompletion be the result of evaluating the second Statement
        stmtCompletion = env.evaluateCompletion(alternate, strictCode);else
        // 3 (of the if only statement). Return NormalCompletion(undefined)
        stmtCompletion = realm.intrinsics.undefined;
      (0, _invariant2.default)(!(stmtCompletion instanceof _environment.Reference));
      stmtCompletion = (0, _index2.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
      if (stmtCompletion instanceof _completions.AbruptCompletion) {
        throw stmtCompletion;
      }
      (0, _invariant2.default)(stmtCompletion instanceof _index.Value);
      return stmtCompletion;
    }
    throw e;
  }

  let compl2, gen2, bindings2, properties2, createdObj2;
  try {
    [compl2, gen2, bindings2, properties2, createdObj2] = _singletons.Path.withInverseCondition(condValue, () => {
      return alternate ? realm.evaluateNodeForEffects(alternate, strictCode, env) : (0, _realm.construct_empty_effects)(realm);
    });
  } catch (e) {
    if (e instanceof _errors.InfeasiblePathError) {
      let stmtCompletion = env.evaluate(consequent, strictCode);
      (0, _invariant2.default)(!(stmtCompletion instanceof _environment.Reference));
      stmtCompletion = (0, _index2.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
      if (stmtCompletion instanceof _completions.AbruptCompletion) {
        throw stmtCompletion;
      }
      (0, _invariant2.default)(stmtCompletion instanceof _index.Value);
      return stmtCompletion;
    }
    throw e;
  }

  // Join the effects, creating an abstract view of what happened, regardless
  // of the actual value of condValue.
  let joinedEffects = _singletons.Join.joinEffects(realm, condValue, [compl1, gen1, bindings1, properties1, createdObj1], [compl2, gen2, bindings2, properties2, createdObj2]);
  let 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.
    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);
  return completion;
}
//# sourceMappingURL=IfStatement.js.map