Merge pull request #13366 from Snuffleupagus/primitives-class

Convert the remaining functions in `src/core/primitives.js` to use standard classes
This commit is contained in:
Tim van der Meij 2021-05-12 20:20:35 +02:00 committed by GitHub
commit 1cf9f42ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 136 additions and 142 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { assert, unreachable } from "../shared/util.js"; import { assert, shadow, unreachable } from "../shared/util.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
const EOF = {}; const EOF = {};
@ -22,22 +22,22 @@ const Name = (function NameClosure() {
let nameCache = Object.create(null); let nameCache = Object.create(null);
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Name(name) { class Name {
this.name = name; constructor(name) {
this.name = name;
}
static get(name) {
const nameValue = nameCache[name];
// eslint-disable-next-line no-restricted-syntax
return nameValue ? nameValue : (nameCache[name] = new Name(name));
}
static _clearCache() {
nameCache = Object.create(null);
}
} }
Name.prototype = {};
Name.get = function Name_get(name) {
const nameValue = nameCache[name];
// eslint-disable-next-line no-restricted-syntax
return nameValue ? nameValue : (nameCache[name] = new Name(name));
};
Name._clearCache = function () {
nameCache = Object.create(null);
};
return Name; return Name;
})(); })();
@ -45,142 +45,138 @@ const Cmd = (function CmdClosure() {
let cmdCache = Object.create(null); let cmdCache = Object.create(null);
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Cmd(cmd) { class Cmd {
this.cmd = cmd; constructor(cmd) {
this.cmd = cmd;
}
static get(cmd) {
const cmdValue = cmdCache[cmd];
// eslint-disable-next-line no-restricted-syntax
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
}
static _clearCache() {
cmdCache = Object.create(null);
}
} }
Cmd.prototype = {};
Cmd.get = function Cmd_get(cmd) {
const cmdValue = cmdCache[cmd];
// eslint-disable-next-line no-restricted-syntax
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
};
Cmd._clearCache = function () {
cmdCache = Object.create(null);
};
return Cmd; return Cmd;
})(); })();
const Dict = (function DictClosure() { const nonSerializable = function nonSerializableClosure() {
const nonSerializable = function nonSerializableClosure() { return nonSerializable; // Creating closure on some variable.
return nonSerializable; // creating closure on some variable };
};
// xref is optional class Dict {
// eslint-disable-next-line no-shadow constructor(xref = null) {
function Dict(xref) {
// Map should only be used internally, use functions below to access. // Map should only be used internally, use functions below to access.
this._map = Object.create(null); this._map = Object.create(null);
this.xref = xref; this.xref = xref;
this.objId = null; this.objId = null;
this.suppressEncryption = false; this.suppressEncryption = false;
this.__nonSerializable__ = nonSerializable; // disable cloning of the Dict this.__nonSerializable__ = nonSerializable; // Disable cloning of the Dict.
} }
Dict.prototype = { assignXref(newXref) {
assignXref: function Dict_assignXref(newXref) { this.xref = newXref;
this.xref = newXref; }
},
get size() { get size() {
return Object.keys(this._map).length; return Object.keys(this._map).length;
}, }
// automatically dereferences Ref objects // Automatically dereferences Ref objects.
get(key1, key2, key3) { get(key1, key2, key3) {
let value = this._map[key1]; let value = this._map[key1];
if (value === undefined && key2 !== undefined) { if (value === undefined && key2 !== undefined) {
value = this._map[key2]; value = this._map[key2];
if (value === undefined && key3 !== undefined) { if (value === undefined && key3 !== undefined) {
value = this._map[key3]; value = this._map[key3];
}
} }
if (value instanceof Ref && this.xref) { }
return this.xref.fetch(value, this.suppressEncryption); if (value instanceof Ref && this.xref) {
return this.xref.fetch(value, this.suppressEncryption);
}
return value;
}
// Same as get(), but returns a promise and uses fetchIfRefAsync().
async getAsync(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && key3 !== undefined) {
value = this._map[key3];
} }
}
if (value instanceof Ref && this.xref) {
return this.xref.fetchAsync(value, this.suppressEncryption);
}
return value;
}
// Same as get(), but dereferences all elements if the result is an Array.
getArray(key1, key2, key3) {
let value = this.get(key1, key2, key3);
if (!Array.isArray(value) || !this.xref) {
return value; return value;
}, }
value = value.slice(); // Ensure that we don't modify the Dict data.
// Same as get(), but returns a promise and uses fetchIfRefAsync(). for (let i = 0, ii = value.length; i < ii; i++) {
async getAsync(key1, key2, key3) { if (!(value[i] instanceof Ref)) {
let value = this._map[key1]; continue;
if (value === undefined && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && key3 !== undefined) {
value = this._map[key3];
}
} }
if (value instanceof Ref && this.xref) { value[i] = this.xref.fetch(value[i], this.suppressEncryption);
return this.xref.fetchAsync(value, this.suppressEncryption); }
} return value;
return value; }
},
// Same as get(), but dereferences all elements if the result is an Array. // No dereferencing.
getArray(key1, key2, key3) { getRaw(key) {
let value = this.get(key1, key2, key3); return this._map[key];
if (!Array.isArray(value) || !this.xref) { }
return value;
}
value = value.slice(); // Ensure that we don't modify the Dict data.
for (let i = 0, ii = value.length; i < ii; i++) {
if (!(value[i] instanceof Ref)) {
continue;
}
value[i] = this.xref.fetch(value[i], this.suppressEncryption);
}
return value;
},
// no dereferencing getKeys() {
getRaw: function Dict_getRaw(key) { return Object.keys(this._map);
return this._map[key]; }
},
getKeys: function Dict_getKeys() { // No dereferencing.
return Object.keys(this._map); getRawValues() {
}, return Object.values(this._map);
}
// no dereferencing set(key, value) {
getRawValues: function Dict_getRawValues() { if (
return Object.values(this._map); (typeof PDFJSDev === "undefined" ||
}, PDFJSDev.test("!PRODUCTION || TESTING")) &&
value === undefined
) {
unreachable('Dict.set: The "value" cannot be undefined.');
}
this._map[key] = value;
}
set: function Dict_set(key, value) { has(key) {
if ( return this._map[key] !== undefined;
(typeof PDFJSDev === "undefined" || }
PDFJSDev.test("!PRODUCTION || TESTING")) &&
value === undefined
) {
unreachable('Dict.set: The "value" cannot be undefined.');
}
this._map[key] = value;
},
has: function Dict_has(key) { forEach(callback) {
return this._map[key] !== undefined; for (const key in this._map) {
}, callback(key, this.get(key));
}
}
forEach: function Dict_forEach(callback) { static get empty() {
for (const key in this._map) {
callback(key, this.get(key));
}
},
};
Dict.empty = (function () {
const emptyDict = new Dict(null); const emptyDict = new Dict(null);
emptyDict.set = (key, value) => { emptyDict.set = (key, value) => {
unreachable("Should not call `set` on the empty dictionary."); unreachable("Should not call `set` on the empty dictionary.");
}; };
return emptyDict; return shadow(this, "empty", emptyDict);
})(); }
Dict.merge = function ({ xref, dictArray, mergeSubDicts = false }) { static merge({ xref, dictArray, mergeSubDicts = false }) {
const mergedDict = new Dict(xref); const mergedDict = new Dict(xref);
if (!mergeSubDicts) { if (!mergeSubDicts) {
@ -235,41 +231,39 @@ const Dict = (function DictClosure() {
properties.clear(); properties.clear();
return mergedDict.size > 0 ? mergedDict : Dict.empty; return mergedDict.size > 0 ? mergedDict : Dict.empty;
}; }
}
return Dict;
})();
const Ref = (function RefClosure() { const Ref = (function RefClosure() {
let refCache = Object.create(null); let refCache = Object.create(null);
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Ref(num, gen) { class Ref {
this.num = num; constructor(num, gen) {
this.gen = gen; this.num = num;
} this.gen = gen;
}
Ref.prototype = { toString() {
toString: function Ref_toString() {
// This function is hot, so we make the string as compact as possible. // This function is hot, so we make the string as compact as possible.
// |this.gen| is almost always zero, so we treat that case specially. // |this.gen| is almost always zero, so we treat that case specially.
if (this.gen === 0) { if (this.gen === 0) {
return `${this.num}R`; return `${this.num}R`;
} }
return `${this.num}R${this.gen}`; return `${this.num}R${this.gen}`;
}, }
};
Ref.get = function (num, gen) { static get(num, gen) {
const key = gen === 0 ? `${num}R` : `${num}R${gen}`; const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
const refValue = refCache[key]; const refValue = refCache[key];
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
return refValue ? refValue : (refCache[key] = new Ref(num, gen)); return refValue ? refValue : (refCache[key] = new Ref(num, gen));
}; }
Ref._clearCache = function () { static _clearCache() {
refCache = Object.create(null); refCache = Object.create(null);
}; }
}
return Ref; return Ref;
})(); })();

View File

@ -1192,8 +1192,8 @@ describe("annotation", function () {
expect(data.url).toBeUndefined(); expect(data.url).toBeUndefined();
expect(data.unsafeUrl).toBeUndefined(); expect(data.unsafeUrl).toBeUndefined();
expect(data.dest).toEqual([ expect(data.dest).toEqual([
{ num: 17, gen: 0 }, Ref.get(17, 0),
{ name: "XYZ" }, Name.get("XYZ"),
0, 0,
841.89, 841.89,
null, null,