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