Skip to content

Commit 746e6b4

Browse files
Merge pull request #20901 from Snuffleupagus/Dict-private-map
Convert the internal `Map` to a properly private field in the `Dict` class
2 parents 083735b + e52f2d1 commit 746e6b4

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

src/core/primitives.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,46 @@ const nonSerializable = function nonSerializableClosure() {
6767
};
6868

6969
class Dict {
70+
__nonSerializable__ = nonSerializable; // Disable cloning of the Dict.
71+
72+
#map = new Map();
73+
74+
objId = null;
75+
76+
suppressEncryption = false;
77+
78+
xref;
79+
7080
constructor(xref = null) {
71-
// Map should only be used internally, use functions below to access.
72-
this._map = new Map();
7381
this.xref = xref;
74-
this.objId = null;
75-
this.suppressEncryption = false;
76-
this.__nonSerializable__ = nonSerializable; // Disable cloning of the Dict.
7782
}
7883

7984
assignXref(newXref) {
8085
this.xref = newXref;
8186
}
8287

8388
get size() {
84-
return this._map.size;
89+
return this.#map.size;
8590
}
8691

8792
#getValue(isAsync, key1, key2, key3) {
88-
let value = this._map.get(key1);
93+
let value = this.#map.get(key1);
8994
if (value === undefined && key2 !== undefined) {
9095
if (
9196
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
9297
key2.length < key1.length
9398
) {
9499
unreachable("Dict.#getValue: Expected keys to be ordered by length.");
95100
}
96-
value = this._map.get(key2);
101+
value = this.#map.get(key2);
97102
if (value === undefined && key3 !== undefined) {
98103
if (
99104
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
100105
key3.length < key2.length
101106
) {
102107
unreachable("Dict.#getValue: Expected keys to be ordered by length.");
103108
}
104-
value = this._map.get(key3);
109+
value = this.#map.get(key3);
105110
}
106111
}
107112
if (value instanceof Ref && this.xref) {
@@ -139,20 +144,20 @@ class Dict {
139144

140145
// No dereferencing.
141146
getRaw(key) {
142-
return this._map.get(key);
147+
return this.#map.get(key);
143148
}
144149

145150
getKeys() {
146-
return this._map.keys();
151+
return this.#map.keys();
147152
}
148153

149154
// No dereferencing.
150155
getRawValues() {
151-
return this._map.values();
156+
return this.#map.values();
152157
}
153158

154159
getRawEntries() {
155-
return this._map.entries();
160+
return this.#map.entries();
156161
}
157162

158163
set(key, value) {
@@ -163,7 +168,7 @@ class Dict {
163168
unreachable('Dict.set: The "value" cannot be undefined.');
164169
}
165170
}
166-
this._map.set(key, value);
171+
this.#map.set(key, value);
167172
}
168173

169174
setIfNotExists(key, value) {
@@ -205,11 +210,11 @@ class Dict {
205210
}
206211

207212
has(key) {
208-
return this._map.has(key);
213+
return this.#map.has(key);
209214
}
210215

211216
*[Symbol.iterator]() {
212-
for (const [key, value] of this._map) {
217+
for (const [key, value] of this.#map) {
213218
yield [
214219
key,
215220
value instanceof Ref && this.xref
@@ -236,7 +241,7 @@ class Dict {
236241
if (!(dict instanceof Dict)) {
237242
continue;
238243
}
239-
for (const [key, value] of dict._map) {
244+
for (const [key, value] of dict.getRawEntries()) {
240245
let property = properties.get(key);
241246
if (property === undefined) {
242247
property = [];
@@ -252,20 +257,18 @@ class Dict {
252257
}
253258
for (const [name, values] of properties) {
254259
if (values.length === 1 || !(values[0] instanceof Dict)) {
255-
mergedDict._map.set(name, values[0]);
260+
mergedDict.set(name, values[0]);
256261
continue;
257262
}
258263
const subDict = new Dict(xref);
259264

260265
for (const dict of values) {
261-
for (const [key, value] of dict._map) {
262-
if (!subDict._map.has(key)) {
263-
subDict._map.set(key, value);
264-
}
266+
for (const [key, value] of dict.getRawEntries()) {
267+
subDict.setIfNotExists(key, value);
265268
}
266269
}
267270
if (subDict.size > 0) {
268-
mergedDict._map.set(name, subDict);
271+
mergedDict.set(name, subDict);
269272
}
270273
}
271274
properties.clear();
@@ -275,14 +278,14 @@ class Dict {
275278

276279
clone() {
277280
const dict = new Dict(this.xref);
278-
for (const [key, rawVal] of this.getRawEntries()) {
279-
dict.set(key, rawVal);
281+
for (const [key, value] of this.#map) {
282+
dict.set(key, value);
280283
}
281284
return dict;
282285
}
283286

284287
delete(key) {
285-
this._map.delete(key);
288+
this.#map.delete(key);
286289
}
287290
}
288291

0 commit comments

Comments
 (0)