Skip to content

Commit

Permalink
Merge pull request mozilla#18598 from Snuffleupagus/base-class-init-T…
Browse files Browse the repository at this point in the history
…ESTING-check

Limit base-class initialization checks to development and TESTING modes
  • Loading branch information
timvandermeij authored Aug 12, 2024
2 parents 9103cf0 + aebb853 commit d0fbfe1
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/core/base_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { bytesToString, shadow, unreachable } from "../shared/util.js";

class BaseStream {
constructor() {
if (this.constructor === BaseStream) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseStream
) {
unreachable("Cannot initialize BaseStream.");
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) {

class ColorSpace {
constructor(name, numComps) {
if (this.constructor === ColorSpace) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === ColorSpace
) {
unreachable("Cannot initialize ColorSpace.");
}
this.name = name;
Expand Down
5 changes: 4 additions & 1 deletion src/core/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,10 @@ class NullCipher {

class AESBaseCipher {
constructor() {
if (this.constructor === AESBaseCipher) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === AESBaseCipher
) {
unreachable("Cannot initialize AESBaseCipher.");
}

Expand Down
5 changes: 4 additions & 1 deletion src/core/font_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,10 @@ class Commands {

class CompiledFont {
constructor(fontMatrix) {
if (this.constructor === CompiledFont) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === CompiledFont
) {
unreachable("Cannot initialize CompiledFont.");
}
this.fontMatrix = fontMatrix;
Expand Down
5 changes: 4 additions & 1 deletion src/core/image_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { RefSet, RefSetCache } from "./primitives.js";

class BaseLocalCache {
constructor(options) {
if (this.constructor === BaseLocalCache) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseLocalCache
) {
unreachable("Cannot initialize BaseLocalCache.");
}
this._onlyRefs = options?.onlyRefs === true;
Expand Down
5 changes: 4 additions & 1 deletion src/core/name_number_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { FormatError, unreachable, warn } from "../shared/util.js";
*/
class NameOrNumberTree {
constructor(root, xref, type) {
if (this.constructor === NameOrNumberTree) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === NameOrNumberTree
) {
unreachable("Cannot initialize NameOrNumberTree.");
}
this.root = root;
Expand Down
5 changes: 4 additions & 1 deletion src/core/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class BaseShading {
static SMALL_NUMBER = 1e-6;

constructor() {
if (this.constructor === BaseShading) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseShading
) {
unreachable("Cannot initialize BaseShading.");
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function parseDocBaseUrl(url) {

class BasePdfManager {
constructor(args) {
if (this.constructor === BasePdfManager) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BasePdfManager
) {
unreachable("Cannot initialize BasePdfManager.");
}
this._docBaseUrl = parseDocBaseUrl(args.docBaseUrl);
Expand Down
25 changes: 20 additions & 5 deletions src/display/base_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { CMapCompressionType, unreachable } from "../shared/util.js";

class BaseFilterFactory {
constructor() {
if (this.constructor === BaseFilterFactory) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseFilterFactory
) {
unreachable("Cannot initialize BaseFilterFactory.");
}
}
Expand Down Expand Up @@ -49,7 +52,10 @@ class BaseCanvasFactory {
#enableHWA = false;

constructor({ enableHWA = false } = {}) {
if (this.constructor === BaseCanvasFactory) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseCanvasFactory
) {
unreachable("Cannot initialize BaseCanvasFactory.");
}
this.#enableHWA = enableHWA;
Expand Down Expand Up @@ -101,7 +107,10 @@ class BaseCanvasFactory {

class BaseCMapReaderFactory {
constructor({ baseUrl = null, isCompressed = true }) {
if (this.constructor === BaseCMapReaderFactory) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseCMapReaderFactory
) {
unreachable("Cannot initialize BaseCMapReaderFactory.");
}
this.baseUrl = baseUrl;
Expand Down Expand Up @@ -139,7 +148,10 @@ class BaseCMapReaderFactory {

class BaseStandardFontDataFactory {
constructor({ baseUrl = null }) {
if (this.constructor === BaseStandardFontDataFactory) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseStandardFontDataFactory
) {
unreachable("Cannot initialize BaseStandardFontDataFactory.");
}
this.baseUrl = baseUrl;
Expand Down Expand Up @@ -171,7 +183,10 @@ class BaseStandardFontDataFactory {

class BaseSVGFactory {
constructor() {
if (this.constructor === BaseSVGFactory) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseSVGFactory
) {
unreachable("Cannot initialize BaseSVGFactory.");
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ class AnnotationEditor {
* @param {AnnotationEditorParameters} parameters
*/
constructor(parameters) {
if (this.constructor === AnnotationEditor) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === AnnotationEditor
) {
unreachable("Cannot initialize AnnotationEditor.");
}

Expand Down
5 changes: 4 additions & 1 deletion src/display/pattern_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function applyBoundingBox(ctx, bbox) {

class BaseShadingPattern {
constructor() {
if (this.constructor === BaseShadingPattern) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseShadingPattern
) {
unreachable("Cannot initialize BaseShadingPattern.");
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ function shadow(obj, prop, value, nonSerializable = false) {
const BaseException = (function BaseExceptionClosure() {
// eslint-disable-next-line no-shadow
function BaseException(message, name) {
if (this.constructor === BaseException) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseException
) {
unreachable("Cannot initialize BaseException.");
}
this.message = message;
Expand Down
4 changes: 3 additions & 1 deletion web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ class AppOptions {
}

constructor() {
throw new Error("Cannot initialize AppOptions.");
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
throw new Error("Cannot initialize AppOptions.");
}
}

static get(name) {
Expand Down
5 changes: 4 additions & 1 deletion web/base_tree_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const TREEITEM_SELECTED_CLASS = "selected";

class BaseTreeViewer {
constructor(options) {
if (this.constructor === BaseTreeViewer) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseTreeViewer
) {
throw new Error("Cannot initialize BaseTreeViewer.");
}
this.container = options.container;
Expand Down
5 changes: 4 additions & 1 deletion web/external_services.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

class BaseExternalServices {
constructor() {
if (this.constructor === BaseExternalServices) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseExternalServices
) {
throw new Error("Cannot initialize BaseExternalServices.");
}
}
Expand Down
5 changes: 4 additions & 1 deletion web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class BasePreferences {
#initializedPromise = null;

constructor() {
if (this.constructor === BasePreferences) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BasePreferences
) {
throw new Error("Cannot initialize BasePreferences.");
}

Expand Down

0 comments on commit d0fbfe1

Please sign in to comment.