This commit is contained in:
2025-09-19 14:25:20 +08:00
parent 269893a435
commit fbf3f77229
24949 changed files with 2839404 additions and 0 deletions

View File

@@ -0,0 +1,225 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/**
* @callback MappingsSerializer
* @param {number} generatedLine generated line
* @param {number} generatedColumn generated column
* @param {number} sourceIndex source index
* @param {number} originalLine original line
* @param {number} originalColumn generated line
* @param {number} nameIndex generated line
* @returns {string} result
*/
const ALPHABET = [
..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
];
const CONTINUATION_BIT = 0x20;
const createFullMappingsSerializer = () => {
let currentLine = 1;
let currentColumn = 0;
let currentSourceIndex = 0;
let currentOriginalLine = 1;
let currentOriginalColumn = 0;
let currentNameIndex = 0;
let activeMapping = false;
let activeName = false;
let initial = true;
/** @type {MappingsSerializer} */
return (
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
if (activeMapping && currentLine === generatedLine) {
// A mapping is still active
if (
sourceIndex === currentSourceIndex &&
originalLine === currentOriginalLine &&
originalColumn === currentOriginalColumn &&
!activeName &&
nameIndex < 0
) {
// avoid repeating the same original mapping
return "";
}
}
// No mapping is active
else if (sourceIndex < 0) {
// avoid writing unneccessary generated mappings
return "";
}
/** @type {undefined | string} */
let str;
if (currentLine < generatedLine) {
str = ";".repeat(generatedLine - currentLine);
currentLine = generatedLine;
currentColumn = 0;
initial = false;
} else if (initial) {
str = "";
initial = false;
} else {
str = ",";
}
/**
* @param {number} value value
* @returns {void}
*/
const writeValue = (value) => {
const sign = (value >>> 31) & 1;
const mask = value >> 31;
const absValue = (value + mask) ^ mask;
let data = (absValue << 1) | sign;
for (;;) {
const sextet = data & 0x1f;
data >>= 5;
if (data === 0) {
str += ALPHABET[sextet];
break;
} else {
str += ALPHABET[sextet | CONTINUATION_BIT];
}
}
};
writeValue(generatedColumn - currentColumn);
currentColumn = generatedColumn;
if (sourceIndex >= 0) {
activeMapping = true;
if (sourceIndex === currentSourceIndex) {
str += "A";
} else {
writeValue(sourceIndex - currentSourceIndex);
currentSourceIndex = sourceIndex;
}
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
if (originalColumn === currentOriginalColumn) {
str += "A";
} else {
writeValue(originalColumn - currentOriginalColumn);
currentOriginalColumn = originalColumn;
}
if (nameIndex >= 0) {
writeValue(nameIndex - currentNameIndex);
currentNameIndex = nameIndex;
activeName = true;
} else {
activeName = false;
}
} else {
activeMapping = false;
}
return str;
};
};
const createLinesOnlyMappingsSerializer = () => {
let lastWrittenLine = 0;
let currentLine = 1;
let currentSourceIndex = 0;
let currentOriginalLine = 1;
/** @type {MappingsSerializer} */
return (
generatedLine,
_generatedColumn,
sourceIndex,
originalLine,
_originalColumn,
_nameIndex,
) => {
if (sourceIndex < 0) {
// avoid writing generated mappings at all
return "";
}
if (lastWrittenLine === generatedLine) {
// avoid writing multiple original mappings per line
return "";
}
/** @type {undefined | string} */
let str;
/**
* @param {number} value value
* @returns {void}
*/
const writeValue = (value) => {
const sign = (value >>> 31) & 1;
const mask = value >> 31;
const absValue = (value + mask) ^ mask;
let data = (absValue << 1) | sign;
for (;;) {
const sextet = data & 0x1f;
data >>= 5;
if (data === 0) {
str += ALPHABET[sextet];
break;
} else {
str += ALPHABET[sextet | CONTINUATION_BIT];
}
}
};
lastWrittenLine = generatedLine;
if (generatedLine === currentLine + 1) {
currentLine = generatedLine;
if (sourceIndex === currentSourceIndex) {
if (originalLine === currentOriginalLine + 1) {
currentOriginalLine = originalLine;
return ";AACA";
}
str = ";AA";
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
return `${str}A`;
}
str = ";A";
writeValue(sourceIndex - currentSourceIndex);
currentSourceIndex = sourceIndex;
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
return `${str}A`;
}
str = ";".repeat(generatedLine - currentLine);
currentLine = generatedLine;
if (sourceIndex === currentSourceIndex) {
if (originalLine === currentOriginalLine + 1) {
currentOriginalLine = originalLine;
return `${str}AACA`;
}
str += "AA";
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
return `${str}A`;
}
str += "A";
writeValue(sourceIndex - currentSourceIndex);
currentSourceIndex = sourceIndex;
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
return `${str}A`;
};
};
/**
* @param {{ columns?: boolean }=} options options
* @returns {MappingsSerializer} mappings serializer
*/
const createMappingsSerializer = (options) => {
const linesOnly = options && options.columns === false;
return linesOnly
? createLinesOnlyMappingsSerializer()
: createFullMappingsSerializer();
};
module.exports = createMappingsSerializer;

View File

@@ -0,0 +1,159 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const createMappingsSerializer = require("./createMappingsSerializer");
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/** @typedef {import("../Source").SourceAndMap} SourceAndMap */
/** @typedef {import("./streamChunks").Options} Options */
/** @typedef {import("./streamChunks").StreamChunksFunction} StreamChunksFunction */
/** @typedef {{ streamChunks: StreamChunksFunction }} SourceLikeWithStreamChunks */
/**
* @param {SourceLikeWithStreamChunks} inputSource input source
* @param {Options=} options options
* @returns {SourceAndMap} map
*/
module.exports.getSourceAndMap = (inputSource, options) => {
let code = "";
let mappings = "";
/** @type {(string | null)[]} */
const potentialSources = [];
/** @type {(string | null)[]} */
const potentialSourcesContent = [];
/** @type {(string | null)[]} */
const potentialNames = [];
const addMapping = createMappingsSerializer(options);
const { source } = inputSource.streamChunks(
{ ...options, finalSource: true },
(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
if (chunk !== undefined) code += chunk;
mappings += addMapping(
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
},
(sourceIndex, source, sourceContent) => {
while (potentialSources.length < sourceIndex) {
potentialSources.push(null);
}
potentialSources[sourceIndex] = source;
if (sourceContent !== undefined) {
while (potentialSourcesContent.length < sourceIndex) {
potentialSourcesContent.push(null);
}
potentialSourcesContent[sourceIndex] = sourceContent;
}
},
(nameIndex, name) => {
while (potentialNames.length < nameIndex) {
potentialNames.push(null);
}
potentialNames[nameIndex] = name;
},
);
return {
source: source !== undefined ? source : code,
map:
mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null,
};
};
/**
* @param {SourceLikeWithStreamChunks} source source
* @param {Options=} options options
* @returns {RawSourceMap | null} map
*/
module.exports.getMap = (source, options) => {
let mappings = "";
/** @type {(string | null)[]} */
const potentialSources = [];
/** @type {(string | null)[]} */
const potentialSourcesContent = [];
/** @type {(string | null)[]} */
const potentialNames = [];
const addMapping = createMappingsSerializer(options);
source.streamChunks(
{ ...options, source: false, finalSource: true },
(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
mappings += addMapping(
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
},
(sourceIndex, source, sourceContent) => {
while (potentialSources.length < sourceIndex) {
potentialSources.push(null);
}
potentialSources[sourceIndex] = source;
if (sourceContent !== undefined) {
while (potentialSourcesContent.length < sourceIndex) {
potentialSourcesContent.push(null);
}
potentialSourcesContent[sourceIndex] = sourceContent;
}
},
(nameIndex, name) => {
while (potentialNames.length < nameIndex) {
potentialNames.push(null);
}
potentialNames[nameIndex] = name;
},
);
return mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null;
};

View File

@@ -0,0 +1,44 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const CHAR_CODE_NEW_LINE = "\n".charCodeAt(0);
/**
* @typedef {object} GeneratedSourceInfo
* @property {number=} generatedLine generated line
* @property {number=} generatedColumn generated column
* @property {string=} source source
*/
/**
* @param {string | undefined} source source
* @returns {GeneratedSourceInfo} source info
*/
const getGeneratedSourceInfo = (source) => {
if (source === undefined) {
return {};
}
const lastLineStart = source.lastIndexOf("\n");
if (lastLineStart === -1) {
return {
generatedLine: 1,
generatedColumn: source.length,
source,
};
}
let generatedLine = 2;
for (let i = 0; i < lastLineStart; i++) {
if (source.charCodeAt(i) === CHAR_CODE_NEW_LINE) generatedLine++;
}
return {
generatedLine,
generatedColumn: source.length - lastLineStart - 1,
source,
};
};
module.exports = getGeneratedSourceInfo;

21
node_modules/webpack-sources/lib/helpers/getName.js generated vendored Normal file
View File

@@ -0,0 +1,21 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/**
* @param {RawSourceMap} sourceMap source map
* @param {number} index index
* @returns {string | undefined | null} name
*/
const getName = (sourceMap, index) => {
if (index < 0) return null;
const { names } = sourceMap;
return names[index];
};
module.exports = getName;

24
node_modules/webpack-sources/lib/helpers/getSource.js generated vendored Normal file
View File

@@ -0,0 +1,24 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/**
* @param {RawSourceMap} sourceMap source map
* @param {number} index index
* @returns {string | null} name
*/
const getSource = (sourceMap, index) => {
if (index < 0) return null;
const { sourceRoot, sources } = sourceMap;
const source = sources[index];
if (!sourceRoot) return source;
if (sourceRoot.endsWith("/")) return sourceRoot + source;
return `${sourceRoot}/${source}`;
};
module.exports = getSource;

View File

@@ -0,0 +1,120 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const CONTINUATION_BIT = 0x20;
const END_SEGMENT_BIT = 0x40;
const NEXT_LINE = END_SEGMENT_BIT | 0x01;
const INVALID = END_SEGMENT_BIT | 0x02;
const DATA_MASK = 0x1f;
const ccToValue = new Uint8Array("z".charCodeAt(0) + 1);
ccToValue.fill(INVALID);
for (let i = 0; i < ALPHABET.length; i++) {
ccToValue[ALPHABET.charCodeAt(i)] = i;
}
ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
ccToValue[";".charCodeAt(0)] = NEXT_LINE;
const ccMax = ccToValue.length - 1;
/** @typedef {(generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnMapping */
/**
* @param {string} mappings the mappings string
* @param {OnMapping} onMapping called for each mapping
* @returns {void}
*/
const readMappings = (mappings, onMapping) => {
// generatedColumn, [sourceIndex, originalLine, orignalColumn, [nameIndex]]
const currentData = new Uint32Array([0, 0, 1, 0, 0]);
let currentDataPos = 0;
// currentValue will include a sign bit at bit 0
let currentValue = 0;
let currentValuePos = 0;
let generatedLine = 1;
let generatedColumn = -1;
for (let i = 0; i < mappings.length; i++) {
const cc = mappings.charCodeAt(i);
if (cc > ccMax) continue;
const value = ccToValue[cc];
if ((value & END_SEGMENT_BIT) !== 0) {
// End current segment
if (currentData[0] > generatedColumn) {
if (currentDataPos === 1) {
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
} else if (currentDataPos === 4) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
-1,
);
} else if (currentDataPos === 5) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
currentData[4],
);
}
[generatedColumn] = currentData;
}
currentDataPos = 0;
if (value === NEXT_LINE) {
// Start new line
generatedLine++;
currentData[0] = 0;
generatedColumn = -1;
}
} else if ((value & CONTINUATION_BIT) === 0) {
// last sextet
currentValue |= value << currentValuePos;
const finalValue =
currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
currentData[currentDataPos++] += finalValue;
currentValuePos = 0;
currentValue = 0;
} else {
currentValue |= (value & DATA_MASK) << currentValuePos;
currentValuePos += 5;
}
}
// End current segment
if (currentDataPos === 1) {
onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
} else if (currentDataPos === 4) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
-1,
);
} else if (currentDataPos === 5) {
onMapping(
generatedLine,
currentData[0],
currentData[1],
currentData[2],
currentData[3],
currentData[4],
);
}
};
module.exports = readMappings;

View File

@@ -0,0 +1,33 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/**
* @param {string} str string
* @returns {string[]} array of string separated by lines
*/
const splitIntoLines = (str) => {
const results = [];
const len = str.length;
let i = 0;
while (i < len) {
const cc = str.charCodeAt(i);
// 10 is "\n".charCodeAt(0)
if (cc === 10) {
results.push("\n");
i++;
} else {
let j = i + 1;
// 10 is "\n".charCodeAt(0)
while (j < len && str.charCodeAt(j) !== 10) j++;
results.push(str.slice(i, j + 1));
i = j + 1;
}
}
return results;
};
module.exports = splitIntoLines;

View File

@@ -0,0 +1,53 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
// \n = 10
// ; = 59
// { = 123
// } = 125
// <space> = 32
// \r = 13
// \t = 9
/**
* @param {string} str string
* @returns {string[] | null} array of string separated by potential tokens
*/
const splitIntoPotentialTokens = (str) => {
const len = str.length;
if (len === 0) return null;
const results = [];
let i = 0;
while (i < len) {
const start = i;
block: {
let cc = str.charCodeAt(i);
while (cc !== 10 && cc !== 59 && cc !== 123 && cc !== 125) {
if (++i >= len) break block;
cc = str.charCodeAt(i);
}
while (
cc === 59 ||
cc === 32 ||
cc === 123 ||
cc === 125 ||
cc === 13 ||
cc === 9
) {
if (++i >= len) break block;
cc = str.charCodeAt(i);
}
if (cc === 10) {
i++;
}
}
results.push(str.slice(start, i));
}
return results;
};
module.exports = splitIntoPotentialTokens;

View File

@@ -0,0 +1,123 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const createMappingsSerializer = require("./createMappingsSerializer");
const streamChunks = require("./streamChunks");
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/** @typedef {import("./streamChunks").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {import("./streamChunks").OnChunk} OnChunk */
/** @typedef {import("./streamChunks").OnName} OnName */
/** @typedef {import("./streamChunks").OnSource} OnSource */
/** @typedef {import("./streamChunks").Options} Options */
/** @typedef {import("./streamChunks").SourceMaybeWithStreamChunksFunction} SourceMaybeWithStreamChunksFunction */
/**
* @param {SourceMaybeWithStreamChunksFunction} inputSource input source
* @param {Options} options options
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @returns {{ result: GeneratedSourceInfo, source: string, map: RawSourceMap | null }} result
*/
const streamAndGetSourceAndMap = (
inputSource,
options,
onChunk,
onSource,
onName,
) => {
let code = "";
let mappings = "";
/** @type {(string | null)[]} */
const potentialSources = [];
/** @type {(string | null)[]} */
const potentialSourcesContent = [];
/** @type {(string | null)[]} */
const potentialNames = [];
const addMapping = createMappingsSerializer({ ...options, columns: true });
const finalSource = Boolean(options && options.finalSource);
const { generatedLine, generatedColumn, source } = streamChunks(
inputSource,
options,
(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
if (chunk !== undefined) code += chunk;
mappings += addMapping(
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
return onChunk(
finalSource ? undefined : chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
},
(sourceIndex, source, sourceContent) => {
while (potentialSources.length < sourceIndex) {
potentialSources.push(null);
}
potentialSources[sourceIndex] = source;
if (sourceContent !== undefined) {
while (potentialSourcesContent.length < sourceIndex) {
potentialSourcesContent.push(null);
}
potentialSourcesContent[sourceIndex] = sourceContent;
}
return onSource(sourceIndex, source, sourceContent);
},
(nameIndex, name) => {
while (potentialNames.length < nameIndex) {
potentialNames.push(null);
}
potentialNames[nameIndex] = name;
return onName(nameIndex, name);
},
);
const resultSource = source !== undefined ? source : code;
return {
result: {
generatedLine,
generatedColumn,
source: finalSource ? resultSource : undefined,
},
source: resultSource,
map:
mappings.length > 0
? {
version: 3,
file: "x",
mappings,
// We handle broken sources as `null`, in spec this field should be string, but no information what we should do in such cases if we change type it will be breaking change
sources: /** @type {string[]} */ (potentialSources),
sourcesContent:
potentialSourcesContent.length > 0
? /** @type {string[]} */ (potentialSourcesContent)
: undefined,
names: /** @type {string[]} */ (potentialNames),
}
: null,
};
};
module.exports = streamAndGetSourceAndMap;

View File

@@ -0,0 +1,62 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const streamChunksOfRawSource = require("./streamChunksOfRawSource");
const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
/** @typedef {import("../Source")} Source */
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {(chunk: string | undefined, generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnChunk */
/** @typedef {(sourceIndex: number, source: string | null, sourceContent: string | undefined) => void} OnSource */
/** @typedef {(nameIndex: number, name: string) => void} OnName */
/** @typedef {{ source?: boolean, finalSource?: boolean, columns?: boolean }} Options */
/**
* @callback StreamChunksFunction
* @param {Options} options options
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
*/
/** @typedef {Source & { streamChunks?: StreamChunksFunction }} SourceMaybeWithStreamChunksFunction */
/**
* @param {SourceMaybeWithStreamChunksFunction} source source
* @param {Options} options options
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @returns {GeneratedSourceInfo} generated source info
*/
module.exports = (source, options, onChunk, onSource, onName) => {
if (typeof source.streamChunks === "function") {
return source.streamChunks(options, onChunk, onSource, onName);
}
const sourceAndMap = source.sourceAndMap(options);
if (sourceAndMap.map) {
return streamChunksOfSourceMap(
/** @type {string} */
(sourceAndMap.source),
sourceAndMap.map,
onChunk,
onSource,
onName,
Boolean(options && options.finalSource),
Boolean(options && options.columns !== false),
);
}
return streamChunksOfRawSource(
/** @type {string} */
(sourceAndMap.source),
onChunk,
onSource,
onName,
Boolean(options && options.finalSource),
);
};

View File

@@ -0,0 +1,366 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const splitIntoLines = require("./splitIntoLines");
const streamChunksOfSourceMap = require("./streamChunksOfSourceMap");
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {import("./streamChunks").OnChunk} onChunk */
/** @typedef {import("./streamChunks").OnName} OnName */
/** @typedef {import("./streamChunks").OnSource} OnSource */
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {string} innerSourceName inner source name
* @param {string} innerSource inner source
* @param {RawSourceMap} innerSourceMap inner source map
* @param {boolean | undefined} removeInnerSource do remove inner source
* @param {onChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @param {boolean} finalSource finalSource
* @param {boolean} columns columns
* @returns {GeneratedSourceInfo} generated source info
*/
const streamChunksOfCombinedSourceMap = (
source,
sourceMap,
innerSourceName,
innerSource,
innerSourceMap,
removeInnerSource,
onChunk,
onSource,
onName,
finalSource,
columns,
) => {
/** @type {Map<string | null, number>} */
const sourceMapping = new Map();
/** @type {Map<string, number>} */
const nameMapping = new Map();
/** @type {number[]} */
const sourceIndexMapping = [];
/** @type {number[]} */
const nameIndexMapping = [];
/** @type {string[]} */
const nameIndexValueMapping = [];
let outerSourceIndex = -2;
/** @type {number[]} */
const innerSourceIndexMapping = [];
/** @type {[string | null, string | undefined][]} */
const innerSourceIndexValueMapping = [];
/** @type {(string | undefined)[]} */
const innerSourceContents = [];
/** @type {(null | undefined | string[])[]} */
const innerSourceContentLines = [];
/** @type {number[]} */
const innerNameIndexMapping = [];
/** @type {string[]} */
const innerNameIndexValueMapping = [];
/** @typedef {[number, number, number, number, number] | number[]} MappingsData */
/** @type {{ chunks: string[], mappingsData: MappingsData }[]} */
const innerSourceMapLineData = [];
/**
* @param {number} line line
* @param {number} column column
* @returns {number} result
*/
const findInnerMapping = (line, column) => {
if (line > innerSourceMapLineData.length) return -1;
const { mappingsData } = innerSourceMapLineData[line - 1];
let l = 0;
let r = mappingsData.length / 5;
while (l < r) {
const m = (l + r) >> 1;
if (mappingsData[m * 5] <= column) {
l = m + 1;
} else {
r = m;
}
}
if (l === 0) return -1;
return l - 1;
};
return streamChunksOfSourceMap(
source,
sourceMap,
(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
// Check if this is a mapping to the inner source
if (sourceIndex === outerSourceIndex) {
// Check if there is a mapping in the inner source
const idx = findInnerMapping(originalLine, originalColumn);
if (idx !== -1) {
const { chunks, mappingsData } =
innerSourceMapLineData[originalLine - 1];
const mi = idx * 5;
const innerSourceIndex = mappingsData[mi + 1];
const innerOriginalLine = mappingsData[mi + 2];
let innerOriginalColumn = mappingsData[mi + 3];
let innerNameIndex = mappingsData[mi + 4];
if (innerSourceIndex >= 0) {
// Check for an identity mapping
// where we are allowed to adjust the original column
const innerChunk = chunks[idx];
const innerGeneratedColumn = mappingsData[mi];
const locationInChunk = originalColumn - innerGeneratedColumn;
if (locationInChunk > 0) {
let originalSourceLines =
innerSourceIndex < innerSourceContentLines.length
? innerSourceContentLines[innerSourceIndex]
: null;
if (originalSourceLines === undefined) {
const originalSource = innerSourceContents[innerSourceIndex];
originalSourceLines = originalSource
? splitIntoLines(originalSource)
: null;
innerSourceContentLines[innerSourceIndex] = originalSourceLines;
}
if (originalSourceLines !== null) {
const originalChunk =
innerOriginalLine <= originalSourceLines.length
? originalSourceLines[innerOriginalLine - 1].slice(
innerOriginalColumn,
innerOriginalColumn + locationInChunk,
)
: "";
if (innerChunk.slice(0, locationInChunk) === originalChunk) {
innerOriginalColumn += locationInChunk;
innerNameIndex = -1;
}
}
}
// We have a inner mapping to original source
// emit source when needed and compute global source index
let sourceIndex =
innerSourceIndex < innerSourceIndexMapping.length
? innerSourceIndexMapping[innerSourceIndex]
: -2;
if (sourceIndex === -2) {
const [source, sourceContent] =
innerSourceIndex < innerSourceIndexValueMapping.length
? innerSourceIndexValueMapping[innerSourceIndex]
: [null, undefined];
let globalIndex = sourceMapping.get(source);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, source, sourceContent);
}
sourceIndex = globalIndex;
innerSourceIndexMapping[innerSourceIndex] = sourceIndex;
}
// emit name when needed and compute global name index
let finalNameIndex = -1;
if (innerNameIndex >= 0) {
// when we have a inner name
finalNameIndex =
innerNameIndex < innerNameIndexMapping.length
? innerNameIndexMapping[innerNameIndex]
: -2;
if (finalNameIndex === -2) {
const name =
innerNameIndex < innerNameIndexValueMapping.length
? innerNameIndexValueMapping[innerNameIndex]
: undefined;
if (name) {
let globalIndex = nameMapping.get(name);
if (globalIndex === undefined) {
nameMapping.set(name, (globalIndex = nameMapping.size));
onName(globalIndex, name);
}
finalNameIndex = globalIndex;
} else {
finalNameIndex = -1;
}
innerNameIndexMapping[innerNameIndex] = finalNameIndex;
}
} else if (nameIndex >= 0) {
// when we don't have an inner name,
// but we have an outer name
// it can be used when inner original code equals to the name
let originalSourceLines =
innerSourceContentLines[innerSourceIndex];
if (originalSourceLines === undefined) {
const originalSource = innerSourceContents[innerSourceIndex];
originalSourceLines = originalSource
? splitIntoLines(originalSource)
: null;
innerSourceContentLines[innerSourceIndex] = originalSourceLines;
}
if (originalSourceLines !== null) {
const name = nameIndexValueMapping[nameIndex];
const originalName =
innerOriginalLine <= originalSourceLines.length
? originalSourceLines[innerOriginalLine - 1].slice(
innerOriginalColumn,
innerOriginalColumn + name.length,
)
: "";
if (name === originalName) {
finalNameIndex =
nameIndex < nameIndexMapping.length
? nameIndexMapping[nameIndex]
: -2;
if (finalNameIndex === -2) {
const name = nameIndexValueMapping[nameIndex];
if (name) {
let globalIndex = nameMapping.get(name);
if (globalIndex === undefined) {
nameMapping.set(name, (globalIndex = nameMapping.size));
onName(globalIndex, name);
}
finalNameIndex = globalIndex;
} else {
finalNameIndex = -1;
}
nameIndexMapping[nameIndex] = finalNameIndex;
}
}
}
}
onChunk(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
innerOriginalLine,
innerOriginalColumn,
finalNameIndex,
);
return;
}
}
// We have a mapping to the inner source, but no inner mapping
if (removeInnerSource) {
onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
return;
}
if (sourceIndexMapping[sourceIndex] === -2) {
let globalIndex = sourceMapping.get(innerSourceName);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, innerSourceName, innerSource);
}
sourceIndexMapping[sourceIndex] = globalIndex;
}
}
const finalSourceIndex =
sourceIndex < 0 || sourceIndex >= sourceIndexMapping.length
? -1
: sourceIndexMapping[sourceIndex];
if (finalSourceIndex < 0) {
// no source, so we make it a generated chunk
onChunk(chunk, generatedLine, generatedColumn, -1, -1, -1, -1);
} else {
// Pass through the chunk with mapping
let finalNameIndex = -1;
if (nameIndex >= 0 && nameIndex < nameIndexMapping.length) {
finalNameIndex = nameIndexMapping[nameIndex];
if (finalNameIndex === -2) {
const name = nameIndexValueMapping[nameIndex];
let globalIndex = nameMapping.get(name);
if (globalIndex === undefined) {
nameMapping.set(name, (globalIndex = nameMapping.size));
onName(globalIndex, name);
}
finalNameIndex = globalIndex;
nameIndexMapping[nameIndex] = finalNameIndex;
}
}
onChunk(
chunk,
generatedLine,
generatedColumn,
finalSourceIndex,
originalLine,
originalColumn,
finalNameIndex,
);
}
},
(i, source, sourceContent) => {
if (source === innerSourceName) {
outerSourceIndex = i;
if (innerSource !== undefined) sourceContent = innerSource;
else innerSource = /** @type {string} */ (sourceContent);
sourceIndexMapping[i] = -2;
streamChunksOfSourceMap(
/** @type {string} */
(sourceContent),
innerSourceMap,
(
chunk,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
while (innerSourceMapLineData.length < generatedLine) {
innerSourceMapLineData.push({
mappingsData: [],
chunks: [],
});
}
const data = innerSourceMapLineData[generatedLine - 1];
data.mappingsData.push(
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
data.chunks.push(/** @type {string} */ (chunk));
},
(i, source, sourceContent) => {
innerSourceContents[i] = sourceContent;
innerSourceContentLines[i] = undefined;
innerSourceIndexMapping[i] = -2;
innerSourceIndexValueMapping[i] = [source, sourceContent];
},
(i, name) => {
innerNameIndexMapping[i] = -2;
innerNameIndexValueMapping[i] = name;
},
false,
columns,
);
} else {
let globalIndex = sourceMapping.get(source);
if (globalIndex === undefined) {
sourceMapping.set(source, (globalIndex = sourceMapping.size));
onSource(globalIndex, source, sourceContent);
}
sourceIndexMapping[i] = globalIndex;
}
},
(i, name) => {
nameIndexMapping[i] = -2;
nameIndexValueMapping[i] = name;
},
finalSource,
columns,
);
};
module.exports = streamChunksOfCombinedSourceMap;

View File

@@ -0,0 +1,54 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
const splitIntoLines = require("./splitIntoLines");
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {import("./streamChunks").OnChunk} OnChunk */
/** @typedef {import("./streamChunks").OnName} OnName */
/** @typedef {import("./streamChunks").OnSource} OnSource */
/**
* @param {string} source source
* @param {OnChunk} onChunk on chunk
* @param {OnSource} _onSource on source
* @param {OnName} _onName on name
* @returns {GeneratedSourceInfo} source info
*/
const streamChunksOfRawSource = (source, onChunk, _onSource, _onName) => {
let line = 1;
const matches = splitIntoLines(source);
/** @type {undefined | string} */
let match;
for (match of matches) {
onChunk(match, line, 0, -1, -1, -1, -1);
line++;
}
return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
? {
generatedLine: matches.length + 1,
generatedColumn: 0,
}
: {
generatedLine: matches.length,
generatedColumn: /** @type {string} */ (match).length,
};
};
/**
* @param {string} source source
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @param {boolean} finalSource is final source
* @returns {GeneratedSourceInfo} source info
*/
module.exports = (source, onChunk, onSource, onName, finalSource) =>
finalSource
? getGeneratedSourceInfo(source)
: streamChunksOfRawSource(source, onChunk, onSource, onName);

View File

@@ -0,0 +1,499 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
const getSource = require("./getSource");
const readMappings = require("./readMappings");
const splitIntoLines = require("./splitIntoLines");
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
/** @typedef {import("./getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
/** @typedef {import("./streamChunks").OnChunk} OnChunk */
/** @typedef {import("./streamChunks").OnName} OnName */
/** @typedef {import("./streamChunks").OnSource} OnSource */
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @returns {GeneratedSourceInfo} generated source info
*/
const streamChunksOfSourceMapFull = (
source,
sourceMap,
onChunk,
onSource,
onName,
) => {
const lines = splitIntoLines(source);
if (lines.length === 0) {
return {
generatedLine: 1,
generatedColumn: 0,
};
}
const { sources, sourcesContent, names, mappings } = sourceMap;
for (let i = 0; i < sources.length; i++) {
onSource(
i,
getSource(sourceMap, i),
(sourcesContent && sourcesContent[i]) || undefined,
);
}
if (names) {
for (let i = 0; i < names.length; i++) {
onName(i, names[i]);
}
}
const lastLine = lines[lines.length - 1];
const lastNewLine = lastLine.endsWith("\n");
const finalLine = lastNewLine ? lines.length + 1 : lines.length;
const finalColumn = lastNewLine ? 0 : lastLine.length;
let currentGeneratedLine = 1;
let currentGeneratedColumn = 0;
let mappingActive = false;
let activeMappingSourceIndex = -1;
let activeMappingOriginalLine = -1;
let activeMappingOriginalColumn = -1;
let activeMappingNameIndex = -1;
/**
* @param {number} generatedLine generated line
* @param {number} generatedColumn generated column
* @param {number} sourceIndex source index
* @param {number} originalLine original line
* @param {number} originalColumn original column
* @param {number} nameIndex name index
* @returns {void}
*/
const onMapping = (
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
if (mappingActive && currentGeneratedLine <= lines.length) {
let chunk;
const mappingLine = currentGeneratedLine;
const mappingColumn = currentGeneratedColumn;
const line = lines[currentGeneratedLine - 1];
if (generatedLine !== currentGeneratedLine) {
chunk = line.slice(currentGeneratedColumn);
currentGeneratedLine++;
currentGeneratedColumn = 0;
} else {
chunk = line.slice(currentGeneratedColumn, generatedColumn);
currentGeneratedColumn = generatedColumn;
}
if (chunk) {
onChunk(
chunk,
mappingLine,
mappingColumn,
activeMappingSourceIndex,
activeMappingOriginalLine,
activeMappingOriginalColumn,
activeMappingNameIndex,
);
}
mappingActive = false;
}
if (generatedLine > currentGeneratedLine && currentGeneratedColumn > 0) {
if (currentGeneratedLine <= lines.length) {
const chunk = lines[currentGeneratedLine - 1].slice(
currentGeneratedColumn,
);
onChunk(
chunk,
currentGeneratedLine,
currentGeneratedColumn,
-1,
-1,
-1,
-1,
);
}
currentGeneratedLine++;
currentGeneratedColumn = 0;
}
while (generatedLine > currentGeneratedLine) {
if (currentGeneratedLine <= lines.length) {
onChunk(
lines[currentGeneratedLine - 1],
currentGeneratedLine,
0,
-1,
-1,
-1,
-1,
);
}
currentGeneratedLine++;
}
if (generatedColumn > currentGeneratedColumn) {
if (currentGeneratedLine <= lines.length) {
const chunk = lines[currentGeneratedLine - 1].slice(
currentGeneratedColumn,
generatedColumn,
);
onChunk(
chunk,
currentGeneratedLine,
currentGeneratedColumn,
-1,
-1,
-1,
-1,
);
}
currentGeneratedColumn = generatedColumn;
}
if (
sourceIndex >= 0 &&
(generatedLine < finalLine ||
(generatedLine === finalLine && generatedColumn < finalColumn))
) {
mappingActive = true;
activeMappingSourceIndex = sourceIndex;
activeMappingOriginalLine = originalLine;
activeMappingOriginalColumn = originalColumn;
activeMappingNameIndex = nameIndex;
}
};
readMappings(mappings, onMapping);
onMapping(finalLine, finalColumn, -1, -1, -1, -1);
return {
generatedLine: finalLine,
generatedColumn: finalColumn,
};
};
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} _onName on name
* @returns {GeneratedSourceInfo} generated source info
*/
const streamChunksOfSourceMapLinesFull = (
source,
sourceMap,
onChunk,
onSource,
_onName,
) => {
const lines = splitIntoLines(source);
if (lines.length === 0) {
return {
generatedLine: 1,
generatedColumn: 0,
};
}
const { sources, sourcesContent, mappings } = sourceMap;
for (let i = 0; i < sources.length; i++) {
onSource(
i,
getSource(sourceMap, i),
(sourcesContent && sourcesContent[i]) || undefined,
);
}
let currentGeneratedLine = 1;
/**
* @param {number} generatedLine generated line
* @param {number} _generatedColumn generated column
* @param {number} sourceIndex source index
* @param {number} originalLine original line
* @param {number} originalColumn original column
* @param {number} _nameIndex name index
* @returns {void}
*/
const onMapping = (
generatedLine,
_generatedColumn,
sourceIndex,
originalLine,
originalColumn,
_nameIndex,
) => {
if (
sourceIndex < 0 ||
generatedLine < currentGeneratedLine ||
generatedLine > lines.length
) {
return;
}
while (generatedLine > currentGeneratedLine) {
if (currentGeneratedLine <= lines.length) {
onChunk(
lines[currentGeneratedLine - 1],
currentGeneratedLine,
0,
-1,
-1,
-1,
-1,
);
}
currentGeneratedLine++;
}
if (generatedLine <= lines.length) {
onChunk(
lines[generatedLine - 1],
generatedLine,
0,
sourceIndex,
originalLine,
originalColumn,
-1,
);
currentGeneratedLine++;
}
};
readMappings(mappings, onMapping);
for (; currentGeneratedLine <= lines.length; currentGeneratedLine++) {
onChunk(
lines[currentGeneratedLine - 1],
currentGeneratedLine,
0,
-1,
-1,
-1,
-1,
);
}
const lastLine = lines[lines.length - 1];
const lastNewLine = lastLine.endsWith("\n");
const finalLine = lastNewLine ? lines.length + 1 : lines.length;
const finalColumn = lastNewLine ? 0 : lastLine.length;
return {
generatedLine: finalLine,
generatedColumn: finalColumn,
};
};
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @returns {GeneratedSourceInfo} generated source info
*/
const streamChunksOfSourceMapFinal = (
source,
sourceMap,
onChunk,
onSource,
onName,
) => {
const result = getGeneratedSourceInfo(source);
const { generatedLine: finalLine, generatedColumn: finalColumn } = result;
if (finalLine === 1 && finalColumn === 0) return result;
const { sources, sourcesContent, names, mappings } = sourceMap;
for (let i = 0; i < sources.length; i++) {
onSource(
i,
getSource(sourceMap, i),
(sourcesContent && sourcesContent[i]) || undefined,
);
}
if (names) {
for (let i = 0; i < names.length; i++) {
onName(i, names[i]);
}
}
let mappingActiveLine = 0;
/**
* @param {number} generatedLine generated line
* @param {number} generatedColumn generated column
* @param {number} sourceIndex source index
* @param {number} originalLine original line
* @param {number} originalColumn original column
* @param {number} nameIndex name index
* @returns {void}
*/
const onMapping = (
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
) => {
if (
generatedLine >= /** @type {number} */ (finalLine) &&
(generatedColumn >= /** @type {number} */ (finalColumn) ||
generatedLine > /** @type {number} */ (finalLine))
) {
return;
}
if (sourceIndex >= 0) {
onChunk(
undefined,
generatedLine,
generatedColumn,
sourceIndex,
originalLine,
originalColumn,
nameIndex,
);
mappingActiveLine = generatedLine;
} else if (mappingActiveLine === generatedLine) {
onChunk(undefined, generatedLine, generatedColumn, -1, -1, -1, -1);
mappingActiveLine = 0;
}
};
readMappings(mappings, onMapping);
return result;
};
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} _onName on name
* @returns {GeneratedSourceInfo} generated source info
*/
const streamChunksOfSourceMapLinesFinal = (
source,
sourceMap,
onChunk,
onSource,
_onName,
) => {
const result = getGeneratedSourceInfo(source);
const { generatedLine, generatedColumn } = result;
if (generatedLine === 1 && generatedColumn === 0) {
return {
generatedLine: 1,
generatedColumn: 0,
};
}
const { sources, sourcesContent, mappings } = sourceMap;
for (let i = 0; i < sources.length; i++) {
onSource(
i,
getSource(sourceMap, i),
(sourcesContent && sourcesContent[i]) || undefined,
);
}
const finalLine =
generatedColumn === 0
? /** @type {number} */ (generatedLine) - 1
: /** @type {number} */ (generatedLine);
let currentGeneratedLine = 1;
/**
* @param {number} generatedLine generated line
* @param {number} _generatedColumn generated column
* @param {number} sourceIndex source index
* @param {number} originalLine original line
* @param {number} originalColumn original column
* @param {number} _nameIndex name index
* @returns {void}
*/
const onMapping = (
generatedLine,
_generatedColumn,
sourceIndex,
originalLine,
originalColumn,
_nameIndex,
) => {
if (
sourceIndex >= 0 &&
currentGeneratedLine <= generatedLine &&
generatedLine <= finalLine
) {
onChunk(
undefined,
generatedLine,
0,
sourceIndex,
originalLine,
originalColumn,
-1,
);
currentGeneratedLine = generatedLine + 1;
}
};
readMappings(mappings, onMapping);
return result;
};
/**
* @param {string} source source
* @param {RawSourceMap} sourceMap source map
* @param {OnChunk} onChunk on chunk
* @param {OnSource} onSource on source
* @param {OnName} onName on name
* @param {boolean} finalSource final source
* @param {boolean} columns columns
* @returns {GeneratedSourceInfo} generated source info
*/
module.exports = (
source,
sourceMap,
onChunk,
onSource,
onName,
finalSource,
columns,
) => {
if (columns) {
return finalSource
? streamChunksOfSourceMapFinal(
source,
sourceMap,
onChunk,
onSource,
onName,
)
: streamChunksOfSourceMapFull(
source,
sourceMap,
onChunk,
onSource,
onName,
);
}
return finalSource
? streamChunksOfSourceMapLinesFinal(
source,
sourceMap,
onChunk,
onSource,
onName,
)
: streamChunksOfSourceMapLinesFull(
source,
sourceMap,
onChunk,
onSource,
onName,
);
};

View File

@@ -0,0 +1,117 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Mark Knichel @mknichel
*/
"use strict";
let dualStringBufferCaching = true;
/**
* @returns {boolean} Whether the optimization to cache copies of both the
* string and buffer version of source content is enabled. This is enabled by
* default to improve performance but can consume more memory since values are
* stored twice.
*/
function isDualStringBufferCachingEnabled() {
return dualStringBufferCaching;
}
/**
* Enables an optimization to save both string and buffer in memory to avoid
* repeat conversions between the two formats when they are requested. This
* is enabled by default. This option can improve performance but can consume
* additional memory since values are stored twice.
* @returns {void}
*/
function enableDualStringBufferCaching() {
dualStringBufferCaching = true;
}
/**
* Disables the optimization to save both string and buffer in memory. This
* may increase performance but should reduce memory usage in the Webpack
* compiler.
* @returns {void}
*/
function disableDualStringBufferCaching() {
dualStringBufferCaching = false;
}
const interningStringMap = new Map();
let enableStringInterningRefCount = 0;
/**
* @returns {boolean} value
*/
function isStringInterningEnabled() {
return enableStringInterningRefCount > 0;
}
/**
* Starts a memory optimization to avoid repeat copies of the same string in
* memory by caching a single reference to the string. This can reduce memory
* usage if the same string is repeated many times in the compiler, such as
* when Webpack layers are used with the same files.
*
* {@link exitStringInterningRange} should be called when string interning is
* no longer necessary to free up the memory used by the interned strings. If
* {@link enterStringInterningRange} has been called multiple times, then
* this method may not immediately free all the memory until
* {@link exitStringInterningRange} has been called to end all string
* interning ranges.
* @returns {void}
*/
function enterStringInterningRange() {
enableStringInterningRefCount++;
}
/**
* Stops the current string interning range. Once all string interning ranges
* have been exited, this method will free all the memory used by the interned
* strings. This method should be called once for each time that
* {@link enterStringInterningRange} was called.
* @returns {void}
*/
function exitStringInterningRange() {
if (--enableStringInterningRefCount <= 0) {
interningStringMap.clear();
enableStringInterningRefCount = 0;
}
}
/**
* Saves the string in a map to ensure that only one copy of the string exists
* in memory at a given time. This is controlled by {@link enableStringInterning}
* and {@link disableStringInterning}. Callers are expect to manage the memory
* of the interned strings by calling {@link disableStringInterning} after the
* compiler no longer needs to save the interned memory.
* @param {string} str A string to be interned.
* @returns {string} The original string or a reference to an existing string of the same value if it has already been interned.
*/
function internString(str) {
if (
!isStringInterningEnabled() ||
!str ||
str.length < 128 ||
typeof str !== "string"
) {
return str;
}
let internedString = interningStringMap.get(str);
if (internedString === undefined) {
internedString = str;
interningStringMap.set(str, internedString);
}
return internedString;
}
module.exports = {
disableDualStringBufferCaching,
enableDualStringBufferCaching,
internString,
isDualStringBufferCachingEnabled,
enterStringInterningRange,
exitStringInterningRange,
};