Files
asciidisco.com/build/node_modules/prepack/lib/partial-evaluators/IfStatement.js
2023-08-01 13:49:46 +02:00

148 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.default = function (ast, strictCode, env, realm) {
var _env$partiallyEvaluat = env.partiallyEvaluateCompletionDeref(ast.test, strictCode),
_env$partiallyEvaluat2 = _slicedToArray(_env$partiallyEvaluat, 3),
exprValue = _env$partiallyEvaluat2[0],
exprAst = _env$partiallyEvaluat2[1],
exprIO = _env$partiallyEvaluat2[2];
if (exprValue instanceof _completions.AbruptCompletion) return [exprValue, t.expressionStatement(exprAst), exprIO];
var completion = void 0;
if (exprValue instanceof _completions.PossiblyNormalCompletion) {
completion = exprValue;
exprValue = completion.value;
}
(0, _invariant2.default)(exprValue instanceof _index2.Value);
if (!exprValue.mightNotBeTrue()) {
// 3.a. Let stmtCompletion be the result of evaluating the first Statement
var _env$partiallyEvaluat3 = env.partiallyEvaluateCompletionDeref(ast.consequent, strictCode),
_env$partiallyEvaluat4 = _slicedToArray(_env$partiallyEvaluat3, 3),
stmtCompletion = _env$partiallyEvaluat4[0],
stmtAst = _env$partiallyEvaluat4[1],
stmtIO = _env$partiallyEvaluat4[2];
// 5. Return Completion(UpdateEmpty(stmtCompletion, undefined)
stmtCompletion = (0, _index.UpdateEmpty)(realm, stmtCompletion, realm.intrinsics.undefined);
return [stmtCompletion, stmtAst, exprIO.concat(stmtIO)];
} else if (!exprValue.mightNotBeFalse()) {
var _stmtCompletion = void 0,
_stmtAst = void 0,
_stmtIO = void 0;
if (ast.alternate) {
// 4.a. Let stmtCompletion be the result of evaluating the second Statement
;
var _env$partiallyEvaluat5 = env.partiallyEvaluateCompletionDeref(ast.alternate, strictCode);
var _env$partiallyEvaluat6 = _slicedToArray(_env$partiallyEvaluat5, 3);
_stmtCompletion = _env$partiallyEvaluat6[0];
_stmtAst = _env$partiallyEvaluat6[1];
_stmtIO = _env$partiallyEvaluat6[2];
} else {
// 3 (of the if only statement). Return NormalCompletion(undefined)
_stmtCompletion = realm.intrinsics.undefined;
_stmtAst = t.emptyStatement();
_stmtIO = [];
}
// 5. Return Completion(UpdateEmpty(stmtCompletion, undefined)
_stmtCompletion = (0, _index.UpdateEmpty)(realm, _stmtCompletion, realm.intrinsics.undefined);
return [_stmtCompletion, _stmtAst, exprIO.concat(_stmtIO)];
}
(0, _invariant2.default)(exprValue instanceof _index2.AbstractValue);
// Evaluate consequent and alternate in sandboxes and get their effects.
var _realm$partiallyEvalu = realm.partiallyEvaluateNodeForEffects(ast.consequent, strictCode, env),
_realm$partiallyEvalu2 = _slicedToArray(_realm$partiallyEvalu, 3),
consequentEffects = _realm$partiallyEvalu2[0],
conAst = _realm$partiallyEvalu2[1],
conIO = _realm$partiallyEvalu2[2];
var _consequentEffects = _slicedToArray(consequentEffects, 5),
conCompl = _consequentEffects[0],
gen1 = _consequentEffects[1],
bindings1 = _consequentEffects[2],
properties1 = _consequentEffects[3],
createdObj1 = _consequentEffects[4];
var consequentAst = conAst;
if (conIO.length > 0) consequentAst = t.blockStatement(conIO.concat(consequentAst));
var _ref = ast.alternate ? realm.partiallyEvaluateNodeForEffects(ast.alternate, strictCode, env) : [(0, _realm.construct_empty_effects)(realm), undefined, []],
_ref2 = _slicedToArray(_ref, 3),
alternateEffects = _ref2[0],
altAst = _ref2[1],
altIO = _ref2[2];
var _alternateEffects = _slicedToArray(alternateEffects, 5),
altCompl = _alternateEffects[0],
gen2 = _alternateEffects[1],
bindings2 = _alternateEffects[2],
properties2 = _alternateEffects[3],
createdObj2 = _alternateEffects[4];
var alternateAst = altAst;
if (altIO.length > 0) alternateAst = t.blockStatement(altIO.concat(alternateAst));
// Join the effects, creating an abstract view of what happened, regardless
// of the actual value of exprValue.
var joinedEffects = _singletons.Join.joinEffects(realm, exprValue, [conCompl, gen1, bindings1, properties1, createdObj1], [altCompl, gen2, bindings2, properties2, createdObj2]);
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.
realm.captureEffects(completion);
}
// Note that the effects of (non joining) abrupt branches are not included
// in joinedEffects, but are tracked separately inside completion.
realm.applyEffects(joinedEffects);
var resultAst = t.ifStatement(exprAst, consequentAst, alternateAst);
(0, _invariant2.default)(!(completion instanceof _environment.Reference));
return [completion, resultAst, exprIO];
};
var _completions = require("../completions.js");
var _environment = require("../environment.js");
var _index = require("../methods/index.js");
var _index2 = require("../values/index.js");
var _realm = require("../realm.js");
var _singletons = require("../singletons.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; } }
//# sourceMappingURL=IfStatement.js.map