147 lines
9.3 KiB
JavaScript
147 lines
9.3 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.ResidualHeapValueIdentifiers = undefined;
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
|
|
* 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.
|
|
*/
|
|
|
|
var _index = require("../values/index.js");
|
|
|
|
var _invariant = require("../invariant.js");
|
|
|
|
var _invariant2 = _interopRequireDefault(_invariant);
|
|
|
|
var _babelTypes = require("babel-types");
|
|
|
|
var t = _interopRequireWildcard(_babelTypes);
|
|
|
|
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; } }
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
// 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.
|
|
var ResidualHeapValueIdentifiers = exports.ResidualHeapValueIdentifiers = function () {
|
|
function ResidualHeapValueIdentifiers(values, preludeGenerator) {
|
|
_classCallCheck(this, ResidualHeapValueIdentifiers);
|
|
|
|
this.collectValToRefCountOnly = false;
|
|
this._valueNameGenerator = preludeGenerator.createNameGenerator("_");
|
|
this._populateIdentifierMap(values);
|
|
}
|
|
|
|
_createClass(ResidualHeapValueIdentifiers, [{
|
|
key: "initPass1",
|
|
value: function initPass1() {
|
|
this.collectValToRefCountOnly = true;
|
|
this.valToRefCount = new Map();
|
|
}
|
|
}, {
|
|
key: "initPass2",
|
|
value: function initPass2() {
|
|
this.collectValToRefCountOnly = false;
|
|
}
|
|
}, {
|
|
key: "_populateIdentifierMap",
|
|
value: function _populateIdentifierMap(values) {
|
|
this.refs = new Map();
|
|
var _iteratorNormalCompletion = true;
|
|
var _didIteratorError = false;
|
|
var _iteratorError = undefined;
|
|
|
|
try {
|
|
for (var _iterator = values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var val = _step.value;
|
|
|
|
this._setIdentifier(val, this._createNewIdentifier(val));
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, {
|
|
key: "_createNewIdentifier",
|
|
value: function _createNewIdentifier(val) {
|
|
var name = this._valueNameGenerator.generate(val.__originalName || "");
|
|
return t.identifier(name);
|
|
}
|
|
}, {
|
|
key: "_setIdentifier",
|
|
value: function _setIdentifier(val, id) {
|
|
(0, _invariant2.default)(!this.refs.has(val));
|
|
this.refs.set(val, id);
|
|
}
|
|
}, {
|
|
key: "getIdentifier",
|
|
value: function getIdentifier(val) {
|
|
var id = this.refs.get(val);
|
|
(0, _invariant2.default)(id !== undefined);
|
|
return id;
|
|
}
|
|
}, {
|
|
key: "deleteIdentifier",
|
|
value: function deleteIdentifier(val) {
|
|
(0, _invariant2.default)(this.refs.has(val));
|
|
this.refs.delete(val);
|
|
}
|
|
}, {
|
|
key: "getIdentifierAndIncrementReferenceCount",
|
|
value: function getIdentifierAndIncrementReferenceCount(val) {
|
|
this.incrementReferenceCount(val);
|
|
var id = this.refs.get(val);
|
|
(0, _invariant2.default)(id !== undefined, "Value Id cannot be null or undefined");
|
|
return id;
|
|
}
|
|
}, {
|
|
key: "incrementReferenceCount",
|
|
value: function incrementReferenceCount(val) {
|
|
if (this.collectValToRefCountOnly) {
|
|
var valToRefCount = this.valToRefCount;
|
|
(0, _invariant2.default)(valToRefCount !== undefined);
|
|
var refCount = valToRefCount.get(val);
|
|
if (refCount) {
|
|
refCount++;
|
|
} else {
|
|
refCount = 1;
|
|
}
|
|
valToRefCount.set(val, refCount);
|
|
}
|
|
}
|
|
}, {
|
|
key: "needsIdentifier",
|
|
value: function needsIdentifier(val) {
|
|
if (this.collectValToRefCountOnly || this.valToRefCount === undefined) return true;
|
|
var refCount = this.valToRefCount.get(val);
|
|
(0, _invariant2.default)(refCount !== undefined && refCount > 0);
|
|
return refCount !== 1;
|
|
}
|
|
}]);
|
|
|
|
return ResidualHeapValueIdentifiers;
|
|
}();
|
|
//# sourceMappingURL=ResidualHeapValueIdentifiers.js.map
|