Merge pull request #14156 from Snuffleupagus/escodegen-fork

Add support for modern ECMAScript `class` features
This commit is contained in:
Tim van der Meij 2021-10-23 19:12:44 +02:00 committed by GitHub
commit 0aaa4e3dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 75 additions and 78 deletions

View File

@ -1,6 +1,6 @@
{ {
"parserOptions": { "parserOptions": {
"ecmaVersion": 2021, "ecmaVersion": 2022,
"sourceType": "module", "sourceType": "module",
}, },

View File

@ -24,8 +24,8 @@ Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mo
### Online demo ### Online demo
Please note that the "Modern browsers" version assumes native support for Please note that the "Modern browsers" version assumes native support for
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining, and features such as e.g. `async`/`await`, `ReadableStream`, optional chaining,
nullish coalescing. nullish coalescing, and private `class` fields/methods.
+ Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html + Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html

View File

@ -1,14 +1,14 @@
"use strict"; "use strict";
const acorn = require("acorn"); const acorn = require("acorn");
const escodegen = require("escodegen"); const escodegen = require("@javascript-obfuscator/escodegen");
const vm = require("vm"); const vm = require("vm");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev"; const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
const ROOT_PREFIX = "$ROOT/"; const ROOT_PREFIX = "$ROOT/";
const ACORN_ECMA_VERSION = 2021; const ACORN_ECMA_VERSION = 2022;
function isLiteral(obj, value) { function isLiteral(obj, value) {
return obj.type === "Literal" && obj.value === value; return obj.type === "Literal" && obj.value === value;

View File

@ -8,7 +8,8 @@ This is a pre-built version of the PDF.js source code. It is automatically
generated by the build scripts. generated by the build scripts.
For usage with older browsers or environments, without support for modern For usage with older browsers or environments, without support for modern
features such as e.g. `async`/`await`, `ReadableStream`, optional chaining, and features such as e.g. `async`/`await`, `ReadableStream`, optional chaining,
nullish coalescing; please see the `legacy` folder. nullish coalescing, and private `class` fields/methods; please see the `legacy`
folder.
See https://github.com/mozilla/pdf.js for learning and contributing. See https://github.com/mozilla/pdf.js for learning and contributing.

View File

@ -1375,7 +1375,7 @@ gulp.task("jsdoc", function (done) {
gulp.task("types", function (done) { gulp.task("types", function (done) {
console.log("### Generating TypeScript definitions using `tsc`"); console.log("### Generating TypeScript definitions using `tsc`");
const args = [ const args = [
"target ES2020", "target ESNext",
"allowJS", "allowJS",
"declaration", "declaration",
`outDir ${TYPES_DIR}`, `outDir ${TYPES_DIR}`,

40
package-lock.json generated
View File

@ -1774,6 +1774,25 @@
"integrity": "sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==", "integrity": "sha512-wdppn25U8z/2yiaT6YGquE6X8sSv7hNMWSXYSSU1jGv/yd6XqjXgTDJ8KP4NgjTXfJ3GbRjeeb8RTV7a/VpM+w==",
"dev": true "dev": true
}, },
"@javascript-obfuscator/escodegen": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/@javascript-obfuscator/escodegen/-/escodegen-2.2.2.tgz",
"integrity": "sha512-0VoGJun/lpGEPv8J8R8fpwt1iAGyctUjHTQoYoCbKY2rcGc6d+NycrqSMo61vRXhZiTnHgud1UF8dOI7UAPdTw==",
"dev": true,
"requires": {
"@javascript-obfuscator/estraverse": "^5.3.0",
"esprima": "^4.0.1",
"esutils": "^2.0.2",
"optionator": "^0.8.1",
"source-map": "~0.6.1"
}
},
"@javascript-obfuscator/estraverse": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/@javascript-obfuscator/estraverse/-/estraverse-5.3.0.tgz",
"integrity": "sha512-SxIFtV5/wlXYS7G3zLVj7CddLolX8Bm/hr68fiyNL3MyG2k4FwF9B5Z5GTpVLhw2EELYNwyoYBvFlR4gGnQPdw==",
"dev": true
},
"@mapbox/node-pre-gyp": { "@mapbox/node-pre-gyp": {
"version": "1.0.5", "version": "1.0.5",
"resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.5.tgz", "resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.5.tgz",
@ -4315,27 +4334,6 @@
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
"dev": true "dev": true
}, },
"escodegen": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/escodegen/-/escodegen-2.0.0.tgz",
"integrity": "sha512-mmHKys/C8BFUGI+MAWNcSYoORYLMdPzjrknd2Vc+bUsjN5bXcr8EhrNB+UTqfL1y3I9c4fw2ihgtMPQLBRiQxw==",
"dev": true,
"requires": {
"esprima": "^4.0.1",
"estraverse": "^5.2.0",
"esutils": "^2.0.2",
"optionator": "^0.8.1",
"source-map": "~0.6.1"
},
"dependencies": {
"estraverse": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.2.0.tgz",
"integrity": "sha512-BxbNGGNm0RyRYvUdHpIwv9IWzeM9XClbOxwoATuFdOE7ZE6wHL+HQ5T8hoPM+zHvmKzzsEqhgy0GrQ5X13afiQ==",
"dev": true
}
}
},
"eslint": { "eslint": {
"version": "8.0.1", "version": "8.0.1",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-8.0.1.tgz", "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.0.1.tgz",

View File

@ -14,7 +14,7 @@
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"dommatrix": "^0.0.6", "dommatrix": "^0.0.6",
"es-module-shims": "^1.3.0", "es-module-shims": "^1.3.0",
"escodegen": "^2.0.0", "@javascript-obfuscator/escodegen": "2.2.2",
"eslint": "^8.0.1", "eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-plugin-fetch-options": "^0.0.5", "eslint-plugin-fetch-options": "^0.0.5",

View File

@ -16,25 +16,29 @@
import { objectFromMap } from "../shared/util.js"; import { objectFromMap } from "../shared/util.js";
class Metadata { class Metadata {
#metadataMap;
#data;
constructor({ parsedData, rawData }) { constructor({ parsedData, rawData }) {
this._metadataMap = parsedData; this.#metadataMap = parsedData;
this._data = rawData; this.#data = rawData;
} }
getRaw() { getRaw() {
return this._data; return this.#data;
} }
get(name) { get(name) {
return this._metadataMap.get(name) ?? null; return this.#metadataMap.get(name) ?? null;
} }
getAll() { getAll() {
return objectFromMap(this._metadataMap); return objectFromMap(this.#metadataMap);
} }
has(name) { has(name) {
return this._metadataMap.has(name); return this.#metadataMap.has(name);
} }
} }

View File

@ -6,14 +6,14 @@
"moduleResolution": "node", "moduleResolution": "node",
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"target": "es5", "target": "ESNext",
"module": "es2015", "module": "ESNext",
"baseUrl": "./", "baseUrl": "./",
"strict": true, "strict": true,
"types": [], "types": [],
"lib": [ "lib": [
"es2017", "ESNext",
"dom" "DOM"
], ],
"paths": { "paths": {
"pdfjs-dist": ["../../build/typestest"], "pdfjs-dist": ["../../build/typestest"],

View File

@ -36,16 +36,11 @@ const THUMBNAIL_WIDTH = 98; // px
* @property {IL10n} l10n - Localization service. * @property {IL10n} l10n - Localization service.
*/ */
const TempImageFactory = (function TempImageFactoryClosure() { class TempImageFactory {
let tempCanvasCache = null; static #tempCanvas = null;
return { static getCanvas(width, height) {
getCanvas(width, height) { const tempCanvas = (this.#tempCanvas ||= document.createElement("canvas"));
let tempCanvas = tempCanvasCache;
if (!tempCanvas) {
tempCanvas = document.createElement("canvas");
tempCanvasCache = tempCanvas;
}
tempCanvas.width = width; tempCanvas.width = width;
tempCanvas.height = height; tempCanvas.height = height;
@ -64,20 +59,19 @@ const TempImageFactory = (function TempImageFactoryClosure() {
ctx.fillRect(0, 0, width, height); ctx.fillRect(0, 0, width, height);
ctx.restore(); ctx.restore();
return [tempCanvas, tempCanvas.getContext("2d")]; return [tempCanvas, tempCanvas.getContext("2d")];
}, }
destroyCanvas() { static destroyCanvas() {
const tempCanvas = tempCanvasCache; const tempCanvas = this.#tempCanvas;
if (tempCanvas) { if (tempCanvas) {
// Zeroing the width and height causes Firefox to release graphics // Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
tempCanvas.width = 0; tempCanvas.width = 0;
tempCanvas.height = 0; tempCanvas.height = 0;
} }
tempCanvasCache = null; this.#tempCanvas = null;
}, }
}; }
})();
/** /**
* @implements {IRenderableView} * @implements {IRenderableView}