Remove the remaining closure in the src/core/function.js file

Given that the code is written with JavaScript module-syntax, none of this functionality will "leak" outside of this file with these changes.
By removing this closure the file-size is decreased, even for the *built* `pdf.worker.js` file, since there's now less overall indentation in the code.
This commit is contained in:
Jonas Jenwald 2022-08-13 12:46:37 +02:00
parent 3cf31a8b17
commit 9dcfdb9578

View File

@ -815,14 +815,7 @@ class PostScriptEvaluator {
}
}
// Most of the PDFs functions consist of simple operations such as:
// roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
//
// We can compile most of such programs, and at the same moment, we can
// optimize some expressions using basic math properties. Keeping track of
// min/max values will allow us to avoid extra Math.min/Math.max calls.
const PostScriptCompiler = (function PostScriptCompilerClosure() {
class AstNode {
class AstNode {
constructor(type) {
this.type = type;
}
@ -830,9 +823,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
unreachable("abstract method");
}
}
}
class AstArgument extends AstNode {
class AstArgument extends AstNode {
constructor(index, min, max) {
super("args");
this.index = index;
@ -843,9 +836,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitArgument(this);
}
}
}
class AstLiteral extends AstNode {
class AstLiteral extends AstNode {
constructor(number) {
super("literal");
this.number = number;
@ -856,9 +849,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitLiteral(this);
}
}
}
class AstBinaryOperation extends AstNode {
class AstBinaryOperation extends AstNode {
constructor(op, arg1, arg2, min, max) {
super("binary");
this.op = op;
@ -871,9 +864,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitBinaryOperation(this);
}
}
}
class AstMin extends AstNode {
class AstMin extends AstNode {
constructor(arg, max) {
super("max");
this.arg = arg;
@ -884,9 +877,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitMin(this);
}
}
}
class AstVariable extends AstNode {
class AstVariable extends AstNode {
constructor(index, min, max) {
super("var");
this.index = index;
@ -897,9 +890,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitVariable(this);
}
}
}
class AstVariableDefinition extends AstNode {
class AstVariableDefinition extends AstNode {
constructor(variable, arg) {
super("definition");
this.variable = variable;
@ -909,9 +902,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) {
visitor.visitVariableDefinition(this);
}
}
}
class ExpressionBuilderVisitor {
class ExpressionBuilderVisitor {
constructor() {
this.parts = [];
}
@ -961,9 +954,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
toString() {
return this.parts.join("");
}
}
}
function buildAddOperation(num1, num2) {
function buildAddOperation(num1, num2) {
if (num2.type === "literal" && num2.number === 0) {
// optimization: second operand is 0
return num1;
@ -983,9 +976,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.min + num2.min,
num1.max + num2.max
);
}
}
function buildMulOperation(num1, num2) {
function buildMulOperation(num1, num2) {
if (num2.type === "literal") {
// optimization: second operands is a literal...
if (num2.number === 0) {
@ -1018,9 +1011,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.max * num2.max
);
return new AstBinaryOperation("*", num1, num2, min, max);
}
}
function buildSubOperation(num1, num2) {
function buildSubOperation(num1, num2) {
if (num2.type === "literal") {
// optimization: second operands is a literal...
if (num2.number === 0) {
@ -1048,9 +1041,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.min - num2.max,
num1.max - num2.min
);
}
}
function buildMinOperation(num1, max) {
function buildMinOperation(num1, max) {
if (num1.min >= max) {
// optimization: num1 min value is not less than required max
return new AstLiteral(max); // just returning max
@ -1059,10 +1052,15 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
return num1; // just returning an argument
}
return new AstMin(num1, max);
}
}
// eslint-disable-next-line no-shadow
class PostScriptCompiler {
// Most of the PDFs functions consist of simple operations such as:
// roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
//
// We can compile most of such programs, and at the same moment, we can
// optimize some expressions using basic math properties. Keeping track of
// min/max values will allow us to avoid extra Math.min/Math.max calls.
class PostScriptCompiler {
compile(code, domain, range) {
const stack = [];
const instructions = [];
@ -1244,10 +1242,7 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
}
return result.join("\n");
}
}
return PostScriptCompiler;
})();
}
export {
isPDFFunction,