UNPKG

prepack

Version:

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

121 lines (86 loc) 4.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _errors = require("../errors.js"); var _index = require("../methods/index.js"); var _index2 = require("../values/index.js"); var _singletons = require("../singletons.js"); var _invariant = _interopRequireDefault(require("../invariant.js")); var _index3 = require("../domains/index.js"); var _generator = require("../utils/generator.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) { // ECMA262 12.4 Update Expressions // Let expr be the result of evaluating UnaryExpression. let expr = env.evaluate(ast.argument, strictCode); // Let oldValue be ? ToNumber(? GetValue(expr)). let oldExpr = _singletons.Environment.GetValue(realm, expr); if (oldExpr instanceof _index2.AbstractValue) { (0, _invariant.default)(ast.operator === "++" || ast.operator === "--"); // As per BabelNodeUpdateExpression let op = ast.operator === "++" ? "+" : "-"; let newAbstractValue = _index2.AbstractValue.createFromBinaryOp(realm, op, oldExpr, new _index2.NumberValue(realm, 1), ast.loc); if (!_singletons.To.IsToNumberPure(realm, oldExpr)) { if (realm.isInPureScope()) { // In pure scope we have to treat the ToNumber operation as temporal since it // might throw or mutate something. We also need to leak the argument due to the // possible mutations. _singletons.Leak.value(realm, oldExpr); newAbstractValue = realm.evaluateWithPossibleThrowCompletion(() => _index2.AbstractValue.createTemporalFromBuildFunction(realm, _index2.NumberValue, [oldExpr], (0, _generator.createOperationDescriptor)("UPDATE_INCREMENTOR", { incrementor: op })), _index3.TypesDomain.topVal, _index3.ValuesDomain.topVal); } else { let error = new _errors.CompilerDiagnostic("might be a symbol or an object with an unknown valueOf or toString or Symbol.toPrimitive method", ast.argument.loc, "PP0008", "RecoverableError"); if (realm.handleError(error) === "Fail") throw new _errors.FatalError(); } } _singletons.Properties.PutValue(realm, expr, newAbstractValue); if (ast.prefix === true) { return newAbstractValue; } else { return oldExpr; } } let oldValue = _singletons.To.ToNumber(realm, oldExpr); if (ast.prefix === true) { if (ast.operator === "++") { // ECMA262 12.4.6.1 // 3. Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.8.5) let newValue = (0, _index.Add)(realm, oldValue, 1); // 4. Perform ? PutValue(expr, newValue). _singletons.Properties.PutValue(realm, expr, newValue); // 5. Return newValue. return newValue; } else if (ast.operator === "--") { // ECMA262 12.4.7.1 // 3. Let newValue be the result of subtracting the value 1 from oldValue, using the same rules as for the - operator (see 12.8.5). let newValue = (0, _index.Add)(realm, oldValue, -1); // 4. Perform ? PutValue(expr, newValue). _singletons.Properties.PutValue(realm, expr, newValue); // 5. Return newValue. return newValue; } (0, _invariant.default)(false); } else { if (ast.operator === "++") { // ECMA262 12.4.4.1 // 3. Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.8.5). let newValue = (0, _index.Add)(realm, oldValue, 1); // 4. Perform ? PutValue(lhs, newValue). _singletons.Properties.PutValue(realm, expr, newValue); // 5. Return oldValue. return _index2.IntegralValue.createFromNumberValue(realm, oldValue); } else if (ast.operator === "--") { // ECMA262 12.4.5.1 // 3. Let newValue be the result of subtracting the value 1 from oldValue, using the same rules as for the - operator (see 12.8.5). let newValue = (0, _index.Add)(realm, oldValue, -1); // 4. Perform ? PutValue(lhs, newValue). _singletons.Properties.PutValue(realm, expr, newValue); // 5. Return oldValue. return _index2.IntegralValue.createFromNumberValue(realm, oldValue); } (0, _invariant.default)(false); } } //# sourceMappingURL=UpdateExpression.js.map