Merge pull request #15309 from Snuffleupagus/function-rm-closure

Remove the remaining closure in the `src/core/function.js` file
This commit is contained in:
Tim van der Meij 2022-08-13 15:05:17 +02:00 committed by GitHub
commit c81903d72d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -815,14 +815,7 @@ class PostScriptEvaluator {
} }
} }
// Most of the PDFs functions consist of simple operations such as: class AstNode {
// 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 {
constructor(type) { constructor(type) {
this.type = type; this.type = type;
} }
@ -830,9 +823,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
unreachable("abstract method"); unreachable("abstract method");
} }
} }
class AstArgument extends AstNode { class AstArgument extends AstNode {
constructor(index, min, max) { constructor(index, min, max) {
super("args"); super("args");
this.index = index; this.index = index;
@ -843,9 +836,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitArgument(this); visitor.visitArgument(this);
} }
} }
class AstLiteral extends AstNode { class AstLiteral extends AstNode {
constructor(number) { constructor(number) {
super("literal"); super("literal");
this.number = number; this.number = number;
@ -856,9 +849,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitLiteral(this); visitor.visitLiteral(this);
} }
} }
class AstBinaryOperation extends AstNode { class AstBinaryOperation extends AstNode {
constructor(op, arg1, arg2, min, max) { constructor(op, arg1, arg2, min, max) {
super("binary"); super("binary");
this.op = op; this.op = op;
@ -871,9 +864,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitBinaryOperation(this); visitor.visitBinaryOperation(this);
} }
} }
class AstMin extends AstNode { class AstMin extends AstNode {
constructor(arg, max) { constructor(arg, max) {
super("max"); super("max");
this.arg = arg; this.arg = arg;
@ -884,9 +877,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitMin(this); visitor.visitMin(this);
} }
} }
class AstVariable extends AstNode { class AstVariable extends AstNode {
constructor(index, min, max) { constructor(index, min, max) {
super("var"); super("var");
this.index = index; this.index = index;
@ -897,9 +890,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitVariable(this); visitor.visitVariable(this);
} }
} }
class AstVariableDefinition extends AstNode { class AstVariableDefinition extends AstNode {
constructor(variable, arg) { constructor(variable, arg) {
super("definition"); super("definition");
this.variable = variable; this.variable = variable;
@ -909,9 +902,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
visit(visitor) { visit(visitor) {
visitor.visitVariableDefinition(this); visitor.visitVariableDefinition(this);
} }
} }
class ExpressionBuilderVisitor { class ExpressionBuilderVisitor {
constructor() { constructor() {
this.parts = []; this.parts = [];
} }
@ -961,9 +954,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
toString() { toString() {
return this.parts.join(""); return this.parts.join("");
} }
} }
function buildAddOperation(num1, num2) { function buildAddOperation(num1, num2) {
if (num2.type === "literal" && num2.number === 0) { if (num2.type === "literal" && num2.number === 0) {
// optimization: second operand is 0 // optimization: second operand is 0
return num1; return num1;
@ -983,9 +976,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.min + num2.min, num1.min + num2.min,
num1.max + num2.max num1.max + num2.max
); );
} }
function buildMulOperation(num1, num2) { function buildMulOperation(num1, num2) {
if (num2.type === "literal") { if (num2.type === "literal") {
// optimization: second operands is a literal... // optimization: second operands is a literal...
if (num2.number === 0) { if (num2.number === 0) {
@ -1018,9 +1011,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.max * num2.max num1.max * num2.max
); );
return new AstBinaryOperation("*", num1, num2, min, max); return new AstBinaryOperation("*", num1, num2, min, max);
} }
function buildSubOperation(num1, num2) { function buildSubOperation(num1, num2) {
if (num2.type === "literal") { if (num2.type === "literal") {
// optimization: second operands is a literal... // optimization: second operands is a literal...
if (num2.number === 0) { if (num2.number === 0) {
@ -1048,9 +1041,9 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
num1.min - num2.max, num1.min - num2.max,
num1.max - num2.min num1.max - num2.min
); );
} }
function buildMinOperation(num1, max) { function buildMinOperation(num1, max) {
if (num1.min >= max) { if (num1.min >= max) {
// optimization: num1 min value is not less than required max // optimization: num1 min value is not less than required max
return new AstLiteral(max); // just returning max return new AstLiteral(max); // just returning max
@ -1059,10 +1052,15 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
return num1; // just returning an argument return num1; // just returning an argument
} }
return new AstMin(num1, max); return new AstMin(num1, max);
} }
// eslint-disable-next-line no-shadow // Most of the PDFs functions consist of simple operations such as:
class PostScriptCompiler { // 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) { compile(code, domain, range) {
const stack = []; const stack = [];
const instructions = []; const instructions = [];
@ -1244,10 +1242,7 @@ const PostScriptCompiler = (function PostScriptCompilerClosure() {
} }
return result.join("\n"); return result.join("\n");
} }
} }
return PostScriptCompiler;
})();
export { export {
isPDFFunction, isPDFFunction,