Remove the remaining unnecessary closures in the src/core/primitives.js file

This commit is contained in:
Jonas Jenwald 2023-04-21 18:43:13 +02:00
parent e304423ba1
commit 9cb3236ac0

View File

@ -18,61 +18,49 @@ import { assert, shadow, unreachable } from "../shared/util.js";
const CIRCULAR_REF = Symbol("CIRCULAR_REF"); const CIRCULAR_REF = Symbol("CIRCULAR_REF");
const EOF = Symbol("EOF"); const EOF = Symbol("EOF");
const Name = (function NameClosure() { let CmdCache = Object.create(null);
let nameCache = Object.create(null); let NameCache = Object.create(null);
let RefCache = Object.create(null);
// eslint-disable-next-line no-shadow function clearPrimitiveCaches() {
class Name { CmdCache = Object.create(null);
constructor(name) { NameCache = Object.create(null);
if ( RefCache = Object.create(null);
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && }
typeof name !== "string"
) {
unreachable('Name: The "name" must be a string.');
}
this.name = name;
}
static get(name) { class Name {
// eslint-disable-next-line no-restricted-syntax constructor(name) {
return nameCache[name] || (nameCache[name] = new Name(name)); if (
} (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
typeof name !== "string"
static _clearCache() { ) {
nameCache = Object.create(null); unreachable('Name: The "name" must be a string.');
} }
this.name = name;
} }
return Name; static get(name) {
})(); // eslint-disable-next-line no-restricted-syntax
return NameCache[name] || (NameCache[name] = new Name(name));
}
}
const Cmd = (function CmdClosure() { class Cmd {
let cmdCache = Object.create(null); constructor(cmd) {
if (
// eslint-disable-next-line no-shadow (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
class Cmd { typeof cmd !== "string"
constructor(cmd) { ) {
if ( unreachable('Cmd: The "cmd" must be a string.');
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
typeof cmd !== "string"
) {
unreachable('Cmd: The "cmd" must be a string.');
}
this.cmd = cmd;
}
static get(cmd) {
// eslint-disable-next-line no-restricted-syntax
return cmdCache[cmd] || (cmdCache[cmd] = new Cmd(cmd));
}
static _clearCache() {
cmdCache = Object.create(null);
} }
this.cmd = cmd;
} }
return Cmd; static get(cmd) {
})(); // eslint-disable-next-line no-restricted-syntax
return CmdCache[cmd] || (CmdCache[cmd] = new Cmd(cmd));
}
}
const nonSerializable = function nonSerializableClosure() { const nonSerializable = function nonSerializableClosure() {
return nonSerializable; // Creating closure on some variable. return nonSerializable; // Creating closure on some variable.
@ -276,38 +264,27 @@ class Dict {
} }
} }
const Ref = (function RefClosure() { class Ref {
let refCache = Object.create(null); constructor(num, gen) {
this.num = num;
// eslint-disable-next-line no-shadow this.gen = gen;
class Ref {
constructor(num, gen) {
this.num = num;
this.gen = gen;
}
toString() {
// 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.
if (this.gen === 0) {
return `${this.num}R`;
}
return `${this.num}R${this.gen}`;
}
static get(num, gen) {
const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
// eslint-disable-next-line no-restricted-syntax
return refCache[key] || (refCache[key] = new Ref(num, gen));
}
static _clearCache() {
refCache = Object.create(null);
}
} }
return Ref; toString() {
})(); // 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.
if (this.gen === 0) {
return `${this.num}R`;
}
return `${this.num}R${this.gen}`;
}
static get(num, gen) {
const key = gen === 0 ? `${num}R` : `${num}R${gen}`;
// eslint-disable-next-line no-restricted-syntax
return RefCache[key] || (RefCache[key] = new Ref(num, gen));
}
}
// The reference is identified by number and generation. // The reference is identified by number and generation.
// This structure stores only one instance of the reference. // This structure stores only one instance of the reference.
@ -402,12 +379,6 @@ function isRefsEqual(v1, v2) {
return v1.num === v2.num && v1.gen === v2.gen; return v1.num === v2.num && v1.gen === v2.gen;
} }
function clearPrimitiveCaches() {
Cmd._clearCache();
Name._clearCache();
Ref._clearCache();
}
export { export {
CIRCULAR_REF, CIRCULAR_REF,
clearPrimitiveCaches, clearPrimitiveCaches,