Re-factor the isLittleEndian/isEvalSupported caching

This functionality is very old, hence we should be able to improve the caching a little bit with modern JavaScript features.
This commit is contained in:
Jonas Jenwald 2022-04-05 16:01:01 +02:00
parent 27e738dff9
commit 1dc4713a0b
4 changed files with 17 additions and 18 deletions

View File

@ -15,9 +15,9 @@
import { Dict, Ref } from "./primitives.js"; import { Dict, Ref } from "./primitives.js";
import { import {
FeatureTest,
FormatError, FormatError,
info, info,
IsEvalSupportedCached,
shadow, shadow,
unreachable, unreachable,
} from "../shared/util.js"; } from "../shared/util.js";
@ -438,7 +438,7 @@ class PDFFunction {
const parser = new PostScriptParser(lexer); const parser = new PostScriptParser(lexer);
const code = parser.parse(); const code = parser.parse();
if (isEvalSupported && IsEvalSupportedCached.value) { if (isEvalSupported && FeatureTest.isEvalSupported) {
const compiled = new PostScriptCompiler().compile(code, domain, range); const compiled = new PostScriptCompiler().compile(code, domain, range);
if (compiled) { if (compiled) {
// Compiled function consists of simple expressions such as addition, // Compiled function consists of simple expressions such as addition,

View File

@ -14,11 +14,11 @@
*/ */
import { import {
FeatureTest,
FONT_IDENTITY_MATRIX, FONT_IDENTITY_MATRIX,
IDENTITY_MATRIX, IDENTITY_MATRIX,
ImageKind, ImageKind,
info, info,
IsLittleEndianCached,
OPS, OPS,
shadow, shadow,
TextRenderingMode, TextRenderingMode,
@ -702,7 +702,7 @@ function putBinaryImageData(ctx, imgData, transferMaps = null) {
const dest32DataLength = dest32.length; const dest32DataLength = dest32.length;
const fullSrcDiff = (width + 7) >> 3; const fullSrcDiff = (width + 7) >> 3;
let white = 0xffffffff; let white = 0xffffffff;
let black = IsLittleEndianCached.value ? 0xff000000 : 0x000000ff; let black = FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
if (transferMapGray) { if (transferMapGray) {
if (transferMapGray[0] === 0xff && transferMapGray[0xff] === 0) { if (transferMapGray[0] === 0xff && transferMapGray[0xff] === 0) {

View File

@ -16,7 +16,7 @@
import { import {
assert, assert,
bytesToString, bytesToString,
IsEvalSupportedCached, FeatureTest,
shadow, shadow,
string32, string32,
unreachable, unreachable,
@ -455,7 +455,7 @@ class FontFaceObject {
} }
// If we can, compile cmds into JS for MAXIMUM SPEED... // If we can, compile cmds into JS for MAXIMUM SPEED...
if (this.isEvalSupported && IsEvalSupportedCached.value) { if (this.isEvalSupported && FeatureTest.isEvalSupported) {
const jsBuf = []; const jsBuf = [];
for (const current of cmds) { for (const current of cmds) {
const args = current.args !== undefined ? current.args.join(",") : ""; const args = current.args !== undefined ? current.args.join(",") : "";

View File

@ -682,11 +682,6 @@ function isLittleEndian() {
const view32 = new Uint32Array(buffer8.buffer, 0, 1); const view32 = new Uint32Array(buffer8.buffer, 0, 1);
return view32[0] === 1; return view32[0] === 1;
} }
const IsLittleEndianCached = {
get value() {
return shadow(this, "value", isLittleEndian());
},
};
// Checks if it's possible to eval JS expressions. // Checks if it's possible to eval JS expressions.
function isEvalSupported() { function isEvalSupported() {
@ -697,11 +692,16 @@ function isEvalSupported() {
return false; return false;
} }
} }
const IsEvalSupportedCached = {
get value() { class FeatureTest {
return shadow(this, "value", isEvalSupported()); static get isLittleEndian() {
}, return shadow(this, "isLittleEndian", isLittleEndian());
}; }
static get isEvalSupported() {
return shadow(this, "isEvalSupported", isEvalSupported());
}
}
const hexNumbers = [...Array(256).keys()].map(n => const hexNumbers = [...Array(256).keys()].map(n =>
n.toString(16).padStart(2, "0") n.toString(16).padStart(2, "0")
@ -1103,6 +1103,7 @@ export {
createValidAbsoluteUrl, createValidAbsoluteUrl,
DocumentActionEventType, DocumentActionEventType,
escapeString, escapeString,
FeatureTest,
FONT_IDENTITY_MATRIX, FONT_IDENTITY_MATRIX,
FontType, FontType,
FormatError, FormatError,
@ -1115,8 +1116,6 @@ export {
isArrayBuffer, isArrayBuffer,
isArrayEqual, isArrayEqual,
isAscii, isAscii,
IsEvalSupportedCached,
IsLittleEndianCached,
MissingPDFException, MissingPDFException,
objectFromMap, objectFromMap,
objectSize, objectSize,