From 81009d42cfd6ed49546b151db566cc70f4a83ed5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 24 Jul 2021 12:59:53 +0200 Subject: [PATCH 1/2] Remove the closure used with the `PostScriptStack` class This patch uses the same approach as used in lots of other parts of the code-base, which thus *slightly* reduces the size of this code. --- src/core/function.js | 105 +++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/src/core/function.js b/src/core/function.js index 075a9970a77d1..caa19e8edf223 100644 --- a/src/core/function.js +++ b/src/core/function.js @@ -517,72 +517,69 @@ function isPDFFunction(v) { return fnDict.has("FunctionType"); } -const PostScriptStack = (function PostScriptStackClosure() { - const MAX_STACK_SIZE = 100; +class PostScriptStack { + static get MAX_STACK_SIZE() { + return shadow(this, "MAX_STACK_SIZE", 100); + } - // eslint-disable-next-line no-shadow - class PostScriptStack { - constructor(initialStack) { - this.stack = !initialStack - ? [] - : Array.prototype.slice.call(initialStack, 0); - } + constructor(initialStack) { + this.stack = !initialStack + ? [] + : Array.prototype.slice.call(initialStack, 0); + } - push(value) { - if (this.stack.length >= MAX_STACK_SIZE) { - throw new Error("PostScript function stack overflow."); - } - this.stack.push(value); + push(value) { + if (this.stack.length >= PostScriptStack.MAX_STACK_SIZE) { + throw new Error("PostScript function stack overflow."); } + this.stack.push(value); + } - pop() { - if (this.stack.length <= 0) { - throw new Error("PostScript function stack underflow."); - } - return this.stack.pop(); + pop() { + if (this.stack.length <= 0) { + throw new Error("PostScript function stack underflow."); } + return this.stack.pop(); + } - copy(n) { - if (this.stack.length + n >= MAX_STACK_SIZE) { - throw new Error("PostScript function stack overflow."); - } - const stack = this.stack; - for (let i = stack.length - n, j = n - 1; j >= 0; j--, i++) { - stack.push(stack[i]); - } + copy(n) { + if (this.stack.length + n >= PostScriptStack.MAX_STACK_SIZE) { + throw new Error("PostScript function stack overflow."); } - - index(n) { - this.push(this.stack[this.stack.length - n - 1]); + const stack = this.stack; + for (let i = stack.length - n, j = n - 1; j >= 0; j--, i++) { + stack.push(stack[i]); } + } - // rotate the last n stack elements p times - roll(n, p) { - const stack = this.stack; - const l = stack.length - n; - const r = stack.length - 1; - const c = l + (p - Math.floor(p / n) * n); + index(n) { + this.push(this.stack[this.stack.length - n - 1]); + } - for (let i = l, j = r; i < j; i++, j--) { - const t = stack[i]; - stack[i] = stack[j]; - stack[j] = t; - } - for (let i = l, j = c - 1; i < j; i++, j--) { - const t = stack[i]; - stack[i] = stack[j]; - stack[j] = t; - } - for (let i = c, j = r; i < j; i++, j--) { - const t = stack[i]; - stack[i] = stack[j]; - stack[j] = t; - } + // rotate the last n stack elements p times + roll(n, p) { + const stack = this.stack; + const l = stack.length - n; + const r = stack.length - 1; + const c = l + (p - Math.floor(p / n) * n); + + for (let i = l, j = r; i < j; i++, j--) { + const t = stack[i]; + stack[i] = stack[j]; + stack[j] = t; + } + for (let i = l, j = c - 1; i < j; i++, j--) { + const t = stack[i]; + stack[i] = stack[j]; + stack[j] = t; + } + for (let i = c, j = r; i < j; i++, j--) { + const t = stack[i]; + stack[i] = stack[j]; + stack[j] = t; } } - - return PostScriptStack; -})(); +} class PostScriptEvaluator { constructor(operators) { From ebbbc973a5e76564a05e4401f52b75b56d3b0b8b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 24 Jul 2021 13:05:46 +0200 Subject: [PATCH 2/2] Remove the closure used with the `PostScriptToken` class This patch uses the same approach as used in lots of other parts of the code-base, which thus *slightly* reduces the size of this code. --- src/core/ps_parser.js | 92 +++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/src/core/ps_parser.js b/src/core/ps_parser.js index 23ecf743ba6b2..a509bbd61bcf0 100644 --- a/src/core/ps_parser.js +++ b/src/core/ps_parser.js @@ -109,61 +109,59 @@ const PostScriptTokenTypes = { IFELSE: 5, }; -const PostScriptToken = (function PostScriptTokenClosure() { - const opCache = Object.create(null); +class PostScriptToken { + static get opCache() { + return shadow(this, "opCache", Object.create(null)); + } - // eslint-disable-next-line no-shadow - class PostScriptToken { - constructor(type, value) { - this.type = type; - this.value = value; - } + constructor(type, value) { + this.type = type; + this.value = value; + } - static getOperator(op) { - const opValue = opCache[op]; - if (opValue) { - return opValue; - } - return (opCache[op] = new PostScriptToken( - PostScriptTokenTypes.OPERATOR, - op - )); + static getOperator(op) { + const opValue = PostScriptToken.opCache[op]; + if (opValue) { + return opValue; } + return (PostScriptToken.opCache[op] = new PostScriptToken( + PostScriptTokenTypes.OPERATOR, + op + )); + } - static get LBRACE() { - return shadow( - this, - "LBRACE", - new PostScriptToken(PostScriptTokenTypes.LBRACE, "{") - ); - } + static get LBRACE() { + return shadow( + this, + "LBRACE", + new PostScriptToken(PostScriptTokenTypes.LBRACE, "{") + ); + } - static get RBRACE() { - return shadow( - this, - "RBRACE", - new PostScriptToken(PostScriptTokenTypes.RBRACE, "}") - ); - } + static get RBRACE() { + return shadow( + this, + "RBRACE", + new PostScriptToken(PostScriptTokenTypes.RBRACE, "}") + ); + } - static get IF() { - return shadow( - this, - "IF", - new PostScriptToken(PostScriptTokenTypes.IF, "IF") - ); - } + static get IF() { + return shadow( + this, + "IF", + new PostScriptToken(PostScriptTokenTypes.IF, "IF") + ); + } - static get IFELSE() { - return shadow( - this, - "IFELSE", - new PostScriptToken(PostScriptTokenTypes.IFELSE, "IFELSE") - ); - } + static get IFELSE() { + return shadow( + this, + "IFELSE", + new PostScriptToken(PostScriptTokenTypes.IFELSE, "IFELSE") + ); } - return PostScriptToken; -})(); +} class PostScriptLexer { constructor(stream) {