Skip to content

Commit

Permalink
Replace the testMode parameter in src/pdf.sandbox.js with a const…
Browse files Browse the repository at this point in the history
…ant, set using the pre-processor

This simplifies not just this code, but the unit-tests as well, and should be sufficient as far as I can tell.
Note also that currently, in the *built* `pdf.sandbox.js` file, there's even a line reading `testMode = testMode && false;` because of an accidentally flipped pre-processor statement.

Finally, in the `scripting_spec.js` unit-test, defines `sandboxBundleSrc` at the top of the file to make it easier to find and/or change it when necessary.
  • Loading branch information
Snuffleupagus committed Dec 5, 2020
1 parent c20b7d7 commit 4d61e70
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 8 additions & 10 deletions src/pdf.sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
/* eslint-disable-next-line no-unused-vars */
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");

const TEST_MODE =
typeof PDFJSDev === "undefined" || PDFJSDev.test("!PRODUCTION || TESTING");

class Sandbox {
constructor(module, testMode) {
constructor(module) {
this._evalInSandbox = module.cwrap("evalInSandbox", null, [
"string",
"int",
]);
this._dispatchEventName = null;
this._module = module;
this._testMode = testMode;
this._alertOnError = 1;
}

Expand All @@ -55,7 +57,7 @@ class Sandbox {
"delete module;",
"delete data;",
];
if (!this._testMode) {
if (!TEST_MODE) {
code = code.concat(extra.map(name => `delete ${name};`));
code.push("delete debugMe;");
}
Expand Down Expand Up @@ -86,7 +88,7 @@ class Sandbox {
}

evalForTesting(code, key) {
if (this._testMode) {
if (TEST_MODE) {
this._evalInSandbox(
`try {
send({ id: "${key}", result: ${code} });
Expand All @@ -99,13 +101,9 @@ class Sandbox {
}
}

function QuickJSSandbox(testMode = false) {
testMode =
testMode &&
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING"));
function QuickJSSandbox() {
return ModuleLoader().then(module => {
return new Sandbox(module, testMode);
return new Sandbox(module);
});
}

Expand Down
10 changes: 5 additions & 5 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import { loadScript } from "../../src/display/display_utils.js";

const sandboxBundleSrc = "../../build/generic/build/pdf.sandbox.js";

describe("Scripting", function () {
let sandbox, send_queue, test_id, ref;

Expand Down Expand Up @@ -44,11 +46,9 @@ describe("Scripting", function () {
send_queue.set(event.detail.id, event.detail);
}
};
const promise = loadScript("../../build/generic/build/pdf.sandbox.js").then(
() => {
return window.pdfjsSandbox.QuickJSSandbox(true);
}
);
const promise = loadScript(sandboxBundleSrc).then(() => {
return window.pdfjsSandbox.QuickJSSandbox();
});
sandbox = {
createSandbox(data) {
promise.then(sbx => sbx.create(data));
Expand Down

0 comments on commit 4d61e70

Please sign in to comment.