UNPKG

prepack

Version:

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

117 lines (91 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResidualHeapValueIdentifiers = void 0; var _index = require("../values/index.js"); var _invariant = _interopRequireDefault(require("../invariant.js")); var t = _interopRequireWildcard(require("@babel/types")); 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } 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 */ // This class maintains a map of values to babel identifiers. // This class can optionally track how often such value identifiers are referenced // when pass 1 is activated, which is usually followed by pass 2 in which // unneeded identifiers (those which were only ever referenced once) are // eliminated as the defining expression can be inlined. class ResidualHeapValueIdentifiers { constructor(values, preludeGenerator) { this.collectValToRefCountOnly = false; this._valueNameGenerator = preludeGenerator.createNameGenerator("_"); this._populateIdentifierMap(values); } initPass1() { this.collectValToRefCountOnly = true; this.valToRefCount = new Map(); } initPass2() { this.collectValToRefCountOnly = false; } _populateIdentifierMap(values) { this.refs = new Map(); for (const val of values) { this._setIdentifier(val, this._createNewIdentifier(val)); } } _createNewIdentifier(val) { const name = this._valueNameGenerator.generate(val.__originalName || ""); return t.identifier(name); } _setIdentifier(val, id) { (0, _invariant.default)(!this.refs.has(val)); this.refs.set(val, id); } hasIdentifier(val) { return this.refs.has(val); } getIdentifier(val) { let id = this.refs.get(val); (0, _invariant.default)(id !== undefined); return id; } deleteIdentifier(val) { (0, _invariant.default)(this.refs.has(val)); this.refs.delete(val); } getIdentifierAndIncrementReferenceCount(val) { this.incrementReferenceCount(val); let id = this.refs.get(val); (0, _invariant.default)(id !== undefined, "Value Id cannot be null or undefined"); return id; } incrementReferenceCount(val) { if (this.collectValToRefCountOnly) { let valToRefCount = this.valToRefCount; (0, _invariant.default)(valToRefCount !== undefined); let refCount = valToRefCount.get(val); if (refCount !== undefined) { refCount++; } else { refCount = 1; } valToRefCount.set(val, refCount); } } needsIdentifier(val) { if (this.collectValToRefCountOnly || this.valToRefCount === undefined) return true; let refCount = this.valToRefCount.get(val); (0, _invariant.default)(refCount !== undefined && refCount > 0); return refCount !== 1; } } exports.ResidualHeapValueIdentifiers = ResidualHeapValueIdentifiers; //# sourceMappingURL=ResidualHeapValueIdentifiers.js.map