Merge pull request #20901 from Snuffleupagus/Dict-private-map

Convert the internal `Map` to a properly private field in the `Dict` class
This commit is contained in:
Tim van der Meij 2026-03-19 20:57:13 +01:00 committed by GitHub
commit 746e6b419d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -67,13 +67,18 @@ const nonSerializable = function nonSerializableClosure() {
}; };
class Dict { class Dict {
__nonSerializable__ = nonSerializable; // Disable cloning of the Dict.
#map = new Map();
objId = null;
suppressEncryption = false;
xref;
constructor(xref = null) { constructor(xref = null) {
// Map should only be used internally, use functions below to access.
this._map = new Map();
this.xref = xref; this.xref = xref;
this.objId = null;
this.suppressEncryption = false;
this.__nonSerializable__ = nonSerializable; // Disable cloning of the Dict.
} }
assignXref(newXref) { assignXref(newXref) {
@ -81,11 +86,11 @@ class Dict {
} }
get size() { get size() {
return this._map.size; return this.#map.size;
} }
#getValue(isAsync, key1, key2, key3) { #getValue(isAsync, key1, key2, key3) {
let value = this._map.get(key1); let value = this.#map.get(key1);
if (value === undefined && key2 !== undefined) { if (value === undefined && key2 !== undefined) {
if ( if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
@ -93,7 +98,7 @@ class Dict {
) { ) {
unreachable("Dict.#getValue: Expected keys to be ordered by length."); unreachable("Dict.#getValue: Expected keys to be ordered by length.");
} }
value = this._map.get(key2); value = this.#map.get(key2);
if (value === undefined && key3 !== undefined) { if (value === undefined && key3 !== undefined) {
if ( if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
@ -101,7 +106,7 @@ class Dict {
) { ) {
unreachable("Dict.#getValue: Expected keys to be ordered by length."); unreachable("Dict.#getValue: Expected keys to be ordered by length.");
} }
value = this._map.get(key3); value = this.#map.get(key3);
} }
} }
if (value instanceof Ref && this.xref) { if (value instanceof Ref && this.xref) {
@ -139,20 +144,20 @@ class Dict {
// No dereferencing. // No dereferencing.
getRaw(key) { getRaw(key) {
return this._map.get(key); return this.#map.get(key);
} }
getKeys() { getKeys() {
return this._map.keys(); return this.#map.keys();
} }
// No dereferencing. // No dereferencing.
getRawValues() { getRawValues() {
return this._map.values(); return this.#map.values();
} }
getRawEntries() { getRawEntries() {
return this._map.entries(); return this.#map.entries();
} }
set(key, value) { set(key, value) {
@ -163,7 +168,7 @@ class Dict {
unreachable('Dict.set: The "value" cannot be undefined.'); unreachable('Dict.set: The "value" cannot be undefined.');
} }
} }
this._map.set(key, value); this.#map.set(key, value);
} }
setIfNotExists(key, value) { setIfNotExists(key, value) {
@ -205,11 +210,11 @@ class Dict {
} }
has(key) { has(key) {
return this._map.has(key); return this.#map.has(key);
} }
*[Symbol.iterator]() { *[Symbol.iterator]() {
for (const [key, value] of this._map) { for (const [key, value] of this.#map) {
yield [ yield [
key, key,
value instanceof Ref && this.xref value instanceof Ref && this.xref
@ -236,7 +241,7 @@ class Dict {
if (!(dict instanceof Dict)) { if (!(dict instanceof Dict)) {
continue; continue;
} }
for (const [key, value] of dict._map) { for (const [key, value] of dict.getRawEntries()) {
let property = properties.get(key); let property = properties.get(key);
if (property === undefined) { if (property === undefined) {
property = []; property = [];
@ -252,20 +257,18 @@ class Dict {
} }
for (const [name, values] of properties) { for (const [name, values] of properties) {
if (values.length === 1 || !(values[0] instanceof Dict)) { if (values.length === 1 || !(values[0] instanceof Dict)) {
mergedDict._map.set(name, values[0]); mergedDict.set(name, values[0]);
continue; continue;
} }
const subDict = new Dict(xref); const subDict = new Dict(xref);
for (const dict of values) { for (const dict of values) {
for (const [key, value] of dict._map) { for (const [key, value] of dict.getRawEntries()) {
if (!subDict._map.has(key)) { subDict.setIfNotExists(key, value);
subDict._map.set(key, value);
}
} }
} }
if (subDict.size > 0) { if (subDict.size > 0) {
mergedDict._map.set(name, subDict); mergedDict.set(name, subDict);
} }
} }
properties.clear(); properties.clear();
@ -275,14 +278,14 @@ class Dict {
clone() { clone() {
const dict = new Dict(this.xref); const dict = new Dict(this.xref);
for (const [key, rawVal] of this.getRawEntries()) { for (const [key, value] of this.#map) {
dict.set(key, rawVal); dict.set(key, value);
} }
return dict; return dict;
} }
delete(key) { delete(key) {
this._map.delete(key); this.#map.delete(key);
} }
} }