136 lines
11 KiB
JavaScript
136 lines
11 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.evaluate = evaluate;
|
|
exports.evaluateWithAbstractConditional = evaluateWithAbstractConditional;
|
|
|
|
var _completions = require("../completions.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 }; }
|
|
|
|
function evaluate(ast, strictCode, env, realm) {
|
|
// 1. Let exprRef be the result of evaluating Expression
|
|
var exprRef = env.evaluate(ast.test, strictCode);
|
|
// 2. Let exprValue be ToBoolean(? GetValue(exprRef))
|
|
var exprValue = _singletons.Environment.GetConditionValue(realm, exprRef);
|
|
|
|
if (exprValue instanceof _index.ConcreteValue) {
|
|
var stmtCompletion = void 0;
|
|
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()) {
|
|
var _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()) {
|
|
var _stmtCompletion2 = void 0;
|
|
if (ast.alternate) _stmtCompletion2 = env.evaluate(ast.alternate, strictCode);else _stmtCompletion2 = realm.intrinsics.undefined;
|
|
(0, _invariant2.default)(!(_stmtCompletion2 instanceof _environment.Reference));
|
|
_stmtCompletion2 = (0, _index2.UpdateEmpty)(realm, _stmtCompletion2, realm.intrinsics.undefined);
|
|
if (_stmtCompletion2 instanceof _completions.AbruptCompletion) {
|
|
throw _stmtCompletion2;
|
|
}
|
|
(0, _invariant2.default)(_stmtCompletion2 instanceof _index.Value);
|
|
return _stmtCompletion2;
|
|
} 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.
|
|
var _Path$withCondition = _singletons.Path.withCondition(condValue, function () {
|
|
return realm.evaluateNodeForEffects(consequent, strictCode, env);
|
|
}),
|
|
_Path$withCondition2 = _slicedToArray(_Path$withCondition, 5),
|
|
compl1 = _Path$withCondition2[0],
|
|
gen1 = _Path$withCondition2[1],
|
|
bindings1 = _Path$withCondition2[2],
|
|
properties1 = _Path$withCondition2[3],
|
|
createdObj1 = _Path$withCondition2[4];
|
|
|
|
var _Path$withInverseCond = _singletons.Path.withInverseCondition(condValue, function () {
|
|
return alternate ? realm.evaluateNodeForEffects(alternate, strictCode, env) : (0, _realm.construct_empty_effects)(realm);
|
|
}),
|
|
_Path$withInverseCond2 = _slicedToArray(_Path$withInverseCond, 5),
|
|
compl2 = _Path$withInverseCond2[0],
|
|
gen2 = _Path$withInverseCond2[1],
|
|
bindings2 = _Path$withInverseCond2[2],
|
|
properties2 = _Path$withInverseCond2[3],
|
|
createdObj2 = _Path$withInverseCond2[4];
|
|
|
|
// Join the effects, creating an abstract view of what happened, regardless
|
|
// of the actual value of condValue.
|
|
|
|
|
|
var joinedEffects = _singletons.Join.joinEffects(realm, condValue, [compl1, gen1, bindings1, properties1, createdObj1], [compl2, gen2, bindings2, properties2, createdObj2]);
|
|
var 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
|