Convert the internal Map to a properly private field in the Dict class

This commit is contained in:
Jonas Jenwald 2026-03-17 12:05:14 +01:00
parent 6c6bb19324
commit e52f2d1d67

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
@ -273,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);
} }
} }