UNPKG

prepack

Version:

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

96 lines (76 loc) 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (ast, strictCode, env, realm, labelSet) { let { body, test } = ast; // 1. Let V be undefined. let V = realm.intrinsics.undefined; // 2. Repeat let resultOrDiagnostic = realm.evaluateWithUndoForDiagnostic(() => { while (true) { // a. Let stmt be the result of evaluating Statement. let stmt = env.evaluateCompletion(body, strictCode); //todo: check if stmt is a PossiblyNormalCompletion and defer to fixpoint computation below (0, _invariant2.default)(stmt instanceof _index.Value || stmt instanceof _completions.AbruptCompletion); // b. If LoopContinues(stmt, labelSet) is false, return Completion(UpdateEmpty(stmt, V)). if ((0, _ForOfStatement.LoopContinues)(realm, stmt, labelSet) === false) { (0, _invariant2.default)(stmt instanceof _completions.AbruptCompletion); // ECMA262 13.1.7 if (stmt instanceof _completions.BreakCompletion) { if (!stmt.target) return (0, _index2.UpdateEmpty)(realm, stmt, V).value; } throw (0, _index2.UpdateEmpty)(realm, stmt, V); } // c. If stmt.[[Value]] is not empty, let V be stmt.[[Value]]. let resultValue = (0, _ForOfStatement.InternalGetResultValue)(realm, stmt); if (!(resultValue instanceof _index.EmptyValue)) V = resultValue; // d. Let exprRef be the result of evaluating Expression. let exprRef = env.evaluate(test, strictCode); // e. Let exprValue be ? GetValue(exprRef). let exprValue = _singletons.Environment.GetConditionValue(realm, exprRef); // f. If ToBoolean(exprValue) is false, return NormalCompletion(V). if (_singletons.To.ToBooleanPartial(realm, exprValue) === false) return V; } (0, _invariant2.default)(false); }); if (resultOrDiagnostic instanceof _index.Value) return resultOrDiagnostic; // If we get here then unrolling the loop did not work, possibly because the value of the loop condition is not known, // so instead try to compute a fixpoint for it let ftest = () => { let exprRef = env.evaluate(test, strictCode); return _singletons.Environment.GetConditionValue(realm, exprRef); }; let fbody = () => env.evaluateCompletion(body, strictCode); let effects = realm.evaluateForFixpointEffects(ftest, fbody); if (effects !== undefined) { let [outsideEffects, insideEffects] = effects; let [rval] = outsideEffects; let [, bodyGenerator] = insideEffects; realm.applyEffects(outsideEffects); let exprRef = env.evaluate(test, strictCode); let exprValue = _singletons.Environment.GetValue(realm, exprRef); (0, _invariant2.default)(exprValue instanceof _index.AbstractValue); let cond = bodyGenerator.derive(exprValue.types, exprValue.values, [exprValue], ([n]) => n, { skipInvariant: true }); let generator = realm.generator; (0, _invariant2.default)(generator !== undefined); generator.emitDoWhileStatement(cond, bodyGenerator); (0, _invariant2.default)(rval instanceof _index.Value, "todo: handle loops that throw exceptions or return"); return rval; } // If we get here the fixpoint computation failed as well. Report the diagnostic from the unrolling and throw. realm.handleError(resultOrDiagnostic); throw new _errors.FatalError(); }; var _errors = require("../errors.js"); var _index = require("../values/index.js"); var _index2 = require("../methods/index.js"); var _ForOfStatement = require("./ForOfStatement.js"); var _completions = require("../completions.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=DoWhileStatement.js.map