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,21 +22,21 @@ 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 {
constructor(name) {
this.name = name; this.name = name;
} }
Name.prototype = {}; static get(name) {
Name.get = function Name_get(name) {
const nameValue = nameCache[name]; const nameValue = nameCache[name];
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
return nameValue ? nameValue : (nameCache[name] = new Name(name)); return nameValue ? nameValue : (nameCache[name] = new Name(name));
}; }
Name._clearCache = function () { static _clearCache() {
nameCache = Object.create(null); nameCache = Object.create(null);
}; }
}
return Name; return Name;
})(); })();
@ -45,51 +45,48 @@ 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 {
constructor(cmd) {
this.cmd = cmd; this.cmd = cmd;
} }
Cmd.prototype = {}; static get(cmd) {
Cmd.get = function Cmd_get(cmd) {
const cmdValue = cmdCache[cmd]; const cmdValue = cmdCache[cmd];
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)); return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
}; }
Cmd._clearCache = function () { static _clearCache() {
cmdCache = Object.create(null); 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) {
@ -102,7 +99,7 @@ const Dict = (function DictClosure() {
return this.xref.fetch(value, this.suppressEncryption); return this.xref.fetch(value, this.suppressEncryption);
} }
return value; return value;
}, }
// Same as get(), but returns a promise and uses fetchIfRefAsync(). // Same as get(), but returns a promise and uses fetchIfRefAsync().
async getAsync(key1, key2, key3) { async getAsync(key1, key2, key3) {
@ -117,7 +114,7 @@ const Dict = (function DictClosure() {
return this.xref.fetchAsync(value, 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. // Same as get(), but dereferences all elements if the result is an Array.
getArray(key1, key2, key3) { getArray(key1, key2, key3) {
@ -133,23 +130,23 @@ const Dict = (function DictClosure() {
value[i] = this.xref.fetch(value[i], this.suppressEncryption); value[i] = this.xref.fetch(value[i], this.suppressEncryption);
} }
return value; return value;
}, }
// no dereferencing // No dereferencing.
getRaw: function Dict_getRaw(key) { getRaw(key) {
return this._map[key]; return this._map[key];
}, }
getKeys: function Dict_getKeys() { getKeys() {
return Object.keys(this._map); return Object.keys(this._map);
}, }
// no dereferencing // No dereferencing.
getRawValues: function Dict_getRawValues() { getRawValues() {
return Object.values(this._map); return Object.values(this._map);
}, }
set: function Dict_set(key, value) { set(key, value) {
if ( if (
(typeof PDFJSDev === "undefined" || (typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")) && PDFJSDev.test("!PRODUCTION || TESTING")) &&
@ -158,29 +155,28 @@ const Dict = (function DictClosure() {
unreachable('Dict.set: The "value" cannot be undefined.'); unreachable('Dict.set: The "value" cannot be undefined.');
} }
this._map[key] = value; this._map[key] = value;
}, }
has: function Dict_has(key) { has(key) {
return this._map[key] !== undefined; return this._map[key] !== undefined;
}, }
forEach: function Dict_forEach(callback) { forEach(callback) {
for (const key in this._map) { for (const key in this._map) {
callback(key, this.get(key)); callback(key, this.get(key));
} }
}, }
};
Dict.empty = (function () { static get empty() {
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 {
constructor(num, gen) {
this.num = num; this.num = num;
this.gen = gen; 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,