Convert code in src/core/function.js to use "normal" classes

All of this code predates the existence of native JS classes, however we can now clean this up a bit. This patch thus let us remove some variable "shadowing" from the code.
This commit is contained in:
Jonas Jenwald 2021-02-26 13:15:07 +01:00
parent 061637d3f4
commit e69e8622a9

View File

@ -635,26 +635,28 @@ var PostScriptStack = (function PostScriptStackClosure() {
var MAX_STACK_SIZE = 100;
// eslint-disable-next-line no-shadow
function PostScriptStack(initialStack) {
class PostScriptStack {
constructor(initialStack) {
this.stack = !initialStack
? []
: Array.prototype.slice.call(initialStack, 0);
}
PostScriptStack.prototype = {
push: function PostScriptStack_push(value) {
push(value) {
if (this.stack.length >= MAX_STACK_SIZE) {
throw new Error("PostScript function stack overflow.");
}
this.stack.push(value);
},
pop: function PostScriptStack_pop() {
}
pop() {
if (this.stack.length <= 0) {
throw new Error("PostScript function stack underflow.");
}
return this.stack.pop();
},
copy: function PostScriptStack_copy(n) {
}
copy(n) {
if (this.stack.length + n >= MAX_STACK_SIZE) {
throw new Error("PostScript function stack overflow.");
}
@ -662,12 +664,14 @@ var PostScriptStack = (function PostScriptStackClosure() {
for (var i = stack.length - n, j = n - 1; j >= 0; j--, i++) {
stack.push(stack[i]);
}
},
index: function PostScriptStack_index(n) {
}
index(n) {
this.push(this.stack[this.stack.length - n - 1]);
},
}
// rotate the last n stack elements p times
roll: function PostScriptStack_roll(n, p) {
roll(n, p) {
var stack = this.stack;
var l = stack.length - n;
var r = stack.length - 1,
@ -690,17 +694,18 @@ var PostScriptStack = (function PostScriptStackClosure() {
stack[i] = stack[j];
stack[j] = t;
}
},
};
}
}
return PostScriptStack;
})();
var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
// eslint-disable-next-line no-shadow
function PostScriptEvaluator(operators) {
class PostScriptEvaluator {
constructor(operators) {
this.operators = operators;
}
PostScriptEvaluator.prototype = {
execute: function PostScriptEvaluator_execute(initialStack) {
execute(initialStack) {
var stack = new PostScriptStack(initialStack);
var counter = 0;
var operators = this.operators;
@ -926,10 +931,8 @@ var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
}
}
return stack.stack;
},
};
return PostScriptEvaluator;
})();
}
}
// Most of the PDFs functions consist of simple operations such as:
// roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
@ -938,84 +941,100 @@ var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
// optimize some expressions using basic math properties. Keeping track of
// min/max values will allow us to avoid extra Math.min/Math.max calls.
var PostScriptCompiler = (function PostScriptCompilerClosure() {
function AstNode(type) {
class AstNode {
constructor(type) {
this.type = type;
}
AstNode.prototype.visit = function (visitor) {
unreachable("abstract method");
};
function AstArgument(index, min, max) {
AstNode.call(this, "args");
visit(visitor) {
unreachable("abstract method");
}
}
class AstArgument extends AstNode {
constructor(index, min, max) {
super("args");
this.index = index;
this.min = min;
this.max = max;
}
AstArgument.prototype = Object.create(AstNode.prototype);
AstArgument.prototype.visit = function (visitor) {
visitor.visitArgument(this);
};
function AstLiteral(number) {
AstNode.call(this, "literal");
visit(visitor) {
visitor.visitArgument(this);
}
}
class AstLiteral extends AstNode {
constructor(number) {
super("literal");
this.number = number;
this.min = number;
this.max = number;
}
AstLiteral.prototype = Object.create(AstNode.prototype);
AstLiteral.prototype.visit = function (visitor) {
visitor.visitLiteral(this);
};
function AstBinaryOperation(op, arg1, arg2, min, max) {
AstNode.call(this, "binary");
visit(visitor) {
visitor.visitLiteral(this);
}
}
class AstBinaryOperation extends AstNode {
constructor(op, arg1, arg2, min, max) {
super("binary");
this.op = op;
this.arg1 = arg1;
this.arg2 = arg2;
this.min = min;
this.max = max;
}
AstBinaryOperation.prototype = Object.create(AstNode.prototype);
AstBinaryOperation.prototype.visit = function (visitor) {
visitor.visitBinaryOperation(this);
};
function AstMin(arg, max) {
AstNode.call(this, "max");
visit(visitor) {
visitor.visitBinaryOperation(this);
}
}
class AstMin extends AstNode {
constructor(arg, max) {
super("max");
this.arg = arg;
this.min = arg.min;
this.max = max;
}
AstMin.prototype = Object.create(AstNode.prototype);
AstMin.prototype.visit = function (visitor) {
visitor.visitMin(this);
};
function AstVariable(index, min, max) {
AstNode.call(this, "var");
visit(visitor) {
visitor.visitMin(this);
}
}
class AstVariable extends AstNode {
constructor(index, min, max) {
super("var");
this.index = index;
this.min = min;
this.max = max;
}
AstVariable.prototype = Object.create(AstNode.prototype);
AstVariable.prototype.visit = function (visitor) {
visitor.visitVariable(this);
};
function AstVariableDefinition(variable, arg) {
AstNode.call(this, "definition");
visit(visitor) {
visitor.visitVariable(this);
}
}
class AstVariableDefinition extends AstNode {
constructor(variable, arg) {
super("definition");
this.variable = variable;
this.arg = arg;
}
AstVariableDefinition.prototype = Object.create(AstNode.prototype);
AstVariableDefinition.prototype.visit = function (visitor) {
visitor.visitVariableDefinition(this);
};
function ExpressionBuilderVisitor() {
visit(visitor) {
visitor.visitVariableDefinition(this);
}
}
class ExpressionBuilderVisitor {
constructor() {
this.parts = [];
}
ExpressionBuilderVisitor.prototype = {
visitArgument(arg) {
this.parts.push(
"Math.max(",
@ -1026,36 +1045,42 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
arg.index,
"]))"
);
},
}
visitVariable(variable) {
this.parts.push("v", variable.index);
},
}
visitLiteral(literal) {
this.parts.push(literal.number);
},
}
visitBinaryOperation(operation) {
this.parts.push("(");
operation.arg1.visit(this);
this.parts.push(" ", operation.op, " ");
operation.arg2.visit(this);
this.parts.push(")");
},
}
visitVariableDefinition(definition) {
this.parts.push("var ");
definition.variable.visit(this);
this.parts.push(" = ");
definition.arg.visit(this);
this.parts.push(";");
},
}
visitMin(max) {
this.parts.push("Math.min(");
max.arg.visit(this);
this.parts.push(", ", max.max, ")");
},
}
toString() {
return this.parts.join("");
},
};
}
}
function buildAddOperation(num1, num2) {
if (num2.type === "literal" && num2.number === 0) {
@ -1156,9 +1181,8 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
}
// eslint-disable-next-line no-shadow
function PostScriptCompiler() {}
PostScriptCompiler.prototype = {
compile: function PostScriptCompiler_compile(code, domain, range) {
class PostScriptCompiler {
compile(code, domain, range) {
var stack = [];
var instructions = [];
var inputSize = domain.length >> 1,
@ -1337,8 +1361,8 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
result.push(out.join(""));
});
return result.join("\n");
},
};
}
}
return PostScriptCompiler;
})();