Skip to content

Commit

Permalink
Merge pull request #12695 from Snuffleupagus/sandbox-build
Browse files Browse the repository at this point in the history
Various clean-up and improvements related to `pdf.sandbox.js` building, and the related default-viewer functionality
  • Loading branch information
timvandermeij authored Dec 6, 2020
2 parents 54ca67d + b2dfb55 commit 3df72c3
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 404 deletions.
390 changes: 216 additions & 174 deletions gulpfile.js

Large diffs are not rendered by default.

100 changes: 0 additions & 100 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"gulp-postcss": "^9.0.0",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.0.0",
"gulp-strip-comments": "^2.5.2",
"gulp-zip": "^5.0.2",
"jasmine": "^3.6.3",
"jsdoc": "^3.6.6",
Expand Down
27 changes: 15 additions & 12 deletions src/scripting_api/quickjs-sandbox.js → src/pdf.sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
* limitations under the License.
*/

import ModuleLoader from "../../external/quickjs/quickjs-eval.js";
import ModuleLoader from "../external/quickjs/quickjs-eval.js";

/* eslint-disable-next-line no-unused-vars */
const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
/* eslint-disable-next-line no-unused-vars */
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");

const TESTING =
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 @@ -43,14 +50,14 @@ class Sandbox {
"module = Object.create(null);",
// Next line is replaced by code from initialization.js
// when we create the bundle for the sandbox.
"/* INITIALIZATION_CODE */",
PDFJSDev.eval("PDF_SCRIPTING_JS_SOURCE"),
`data = ${sandboxData};`,
`module.exports.initSandbox({ data, extra: {${extraStr}}, out: this});`,
"delete exports;",
"delete module;",
"delete data;",
];
if (!this._testMode) {
if (!TESTING) {
code = code.concat(extra.map(name => `delete ${name};`));
code.push("delete debugMe;");
}
Expand Down Expand Up @@ -81,7 +88,7 @@ class Sandbox {
}

evalForTesting(code, key) {
if (this._testMode) {
if (TESTING) {
this._evalInSandbox(
`try {
send({ id: "${key}", result: ${code} });
Expand All @@ -94,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
Loading

0 comments on commit 3df72c3

Please sign in to comment.