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

View File

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

View File

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

View File

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