Best JavaScript code snippet using wpt
function.js
Source:function.js  
...334                                          IR) {335      var domain = IR[1];336      var range = IR[2];337      var code = IR[3];338      var compiled = (new PostScriptCompiler()).compile(code, domain, range);339      if (compiled) {340        // Compiled function consists of simple expressions such as addition,341        // subtraction, Math.max, and also contains 'var' and 'return'342        // statements. See the generation in the PostScriptCompiler below.343        /*jshint -W054 */344        return new Function('src', 'srcOffset', 'dest', 'destOffset', compiled);345      }346      info('Unable to compile PS function');347      var numOutputs = range.length >> 1;348      var numInputs = domain.length >> 1;349      var evaluator = new PostScriptEvaluator(code);350      // Cache the values for a big speed up, the cache size is limited though351      // since the number of possible values can be huge from a PS function.352      var cache = {};353      // The MAX_CACHE_SIZE is set to ~4x the maximum number of distinct values354      // seen in our tests.355      var MAX_CACHE_SIZE = 2048 * 4;356      var cache_available = MAX_CACHE_SIZE;357      var tmpBuf = new Float32Array(numInputs);358      return function constructPostScriptFromIRResult(src, srcOffset,359                                                      dest, destOffset) {360        var i, value;361        var key = '';362        var input = tmpBuf;363        for (i = 0; i < numInputs; i++) {364          value = src[srcOffset + i];365          input[i] = value;366          key += value + '_';367        }368        var cachedValue = cache[key];369        if (cachedValue !== undefined) {370          dest.set(cachedValue, destOffset);371          return;372        }373        var output = new Float32Array(numOutputs);374        var stack = evaluator.execute(input);375        var stackIndex = stack.length - numOutputs;376        for (i = 0; i < numOutputs; i++) {377          value = stack[stackIndex + i];378          var bound = range[i * 2];379          if (value < bound) {380            value = bound;381          } else {382            bound = range[i * 2 +1];383            if (value > bound) {384              value = bound;385            }386          }387          output[i] = value;388        }389        if (cache_available > 0) {390          cache_available--;391          cache[key] = output;392        }393        dest.set(output, destOffset);394      };395    }396  };397})();398function isPDFFunction(v) {399  var fnDict;400  if (typeof v !== 'object') {401    return false;402  } else if (isDict(v)) {403    fnDict = v;404  } else if (isStream(v)) {405    fnDict = v.dict;406  } else {407    return false;408  }409  return fnDict.has('FunctionType');410}411var PostScriptStack = (function PostScriptStackClosure() {412  var MAX_STACK_SIZE = 100;413  function PostScriptStack(initialStack) {414    this.stack = !initialStack ? [] :415                 Array.prototype.slice.call(initialStack, 0);416  }417  PostScriptStack.prototype = {418    push: function PostScriptStack_push(value) {419      if (this.stack.length >= MAX_STACK_SIZE) {420        error('PostScript function stack overflow.');421      }422      this.stack.push(value);423    },424    pop: function PostScriptStack_pop() {425      if (this.stack.length <= 0) {426        error('PostScript function stack underflow.');427      }428      return this.stack.pop();429    },430    copy: function PostScriptStack_copy(n) {431      if (this.stack.length + n >= MAX_STACK_SIZE) {432        error('PostScript function stack overflow.');433      }434      var stack = this.stack;435      for (var i = stack.length - n, j = n - 1; j >= 0; j--, i++) {436        stack.push(stack[i]);437      }438    },439    index: function PostScriptStack_index(n) {440      this.push(this.stack[this.stack.length - n - 1]);441    },442    // rotate the last n stack elements p times443    roll: function PostScriptStack_roll(n, p) {444      var stack = this.stack;445      var l = stack.length - n;446      var r = stack.length - 1, c = l + (p - Math.floor(p / n) * n), i, j, t;447      for (i = l, j = r; i < j; i++, j--) {448        t = stack[i]; stack[i] = stack[j]; stack[j] = t;449      }450      for (i = l, j = c - 1; i < j; i++, j--) {451        t = stack[i]; stack[i] = stack[j]; stack[j] = t;452      }453      for (i = c, j = r; i < j; i++, j--) {454        t = stack[i]; stack[i] = stack[j]; stack[j] = t;455      }456    }457  };458  return PostScriptStack;459})();460var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {461  function PostScriptEvaluator(operators) {462    this.operators = operators;463  }464  PostScriptEvaluator.prototype = {465    execute: function PostScriptEvaluator_execute(initialStack) {466      var stack = new PostScriptStack(initialStack);467      var counter = 0;468      var operators = this.operators;469      var length = operators.length;470      var operator, a, b;471      while (counter < length) {472        operator = operators[counter++];473        if (typeof operator === 'number') {474          // Operator is really an operand and should be pushed to the stack.475          stack.push(operator);476          continue;477        }478        switch (operator) {479          // non standard ps operators480          case 'jz': // jump if false481            b = stack.pop();482            a = stack.pop();483            if (!a) {484              counter = b;485            }486            break;487          case 'j': // jump488            a = stack.pop();489            counter = a;490            break;491          // all ps operators in alphabetical order (excluding if/ifelse)492          case 'abs':493            a = stack.pop();494            stack.push(Math.abs(a));495            break;496          case 'add':497            b = stack.pop();498            a = stack.pop();499            stack.push(a + b);500            break;501          case 'and':502            b = stack.pop();503            a = stack.pop();504            if (isBool(a) && isBool(b)) {505              stack.push(a && b);506            } else {507              stack.push(a & b);508            }509            break;510          case 'atan':511            a = stack.pop();512            stack.push(Math.atan(a));513            break;514          case 'bitshift':515            b = stack.pop();516            a = stack.pop();517            if (a > 0) {518              stack.push(a << b);519            } else {520              stack.push(a >> b);521            }522            break;523          case 'ceiling':524            a = stack.pop();525            stack.push(Math.ceil(a));526            break;527          case 'copy':528            a = stack.pop();529            stack.copy(a);530            break;531          case 'cos':532            a = stack.pop();533            stack.push(Math.cos(a));534            break;535          case 'cvi':536            a = stack.pop() | 0;537            stack.push(a);538            break;539          case 'cvr':540            // noop541            break;542          case 'div':543            b = stack.pop();544            a = stack.pop();545            stack.push(a / b);546            break;547          case 'dup':548            stack.copy(1);549            break;550          case 'eq':551            b = stack.pop();552            a = stack.pop();553            stack.push(a === b);554            break;555          case 'exch':556            stack.roll(2, 1);557            break;558          case 'exp':559            b = stack.pop();560            a = stack.pop();561            stack.push(Math.pow(a, b));562            break;563          case 'false':564            stack.push(false);565            break;566          case 'floor':567            a = stack.pop();568            stack.push(Math.floor(a));569            break;570          case 'ge':571            b = stack.pop();572            a = stack.pop();573            stack.push(a >= b);574            break;575          case 'gt':576            b = stack.pop();577            a = stack.pop();578            stack.push(a > b);579            break;580          case 'idiv':581            b = stack.pop();582            a = stack.pop();583            stack.push((a / b) | 0);584            break;585          case 'index':586            a = stack.pop();587            stack.index(a);588            break;589          case 'le':590            b = stack.pop();591            a = stack.pop();592            stack.push(a <= b);593            break;594          case 'ln':595            a = stack.pop();596            stack.push(Math.log(a));597            break;598          case 'log':599            a = stack.pop();600            stack.push(Math.log(a) / Math.LN10);601            break;602          case 'lt':603            b = stack.pop();604            a = stack.pop();605            stack.push(a < b);606            break;607          case 'mod':608            b = stack.pop();609            a = stack.pop();610            stack.push(a % b);611            break;612          case 'mul':613            b = stack.pop();614            a = stack.pop();615            stack.push(a * b);616            break;617          case 'ne':618            b = stack.pop();619            a = stack.pop();620            stack.push(a !== b);621            break;622          case 'neg':623            a = stack.pop();624            stack.push(-a);625            break;626          case 'not':627            a = stack.pop();628            if (isBool(a)) {629              stack.push(!a);630            } else {631              stack.push(~a);632            }633            break;634          case 'or':635            b = stack.pop();636            a = stack.pop();637            if (isBool(a) && isBool(b)) {638              stack.push(a || b);639            } else {640              stack.push(a | b);641            }642            break;643          case 'pop':644            stack.pop();645            break;646          case 'roll':647            b = stack.pop();648            a = stack.pop();649            stack.roll(a, b);650            break;651          case 'round':652            a = stack.pop();653            stack.push(Math.round(a));654            break;655          case 'sin':656            a = stack.pop();657            stack.push(Math.sin(a));658            break;659          case 'sqrt':660            a = stack.pop();661            stack.push(Math.sqrt(a));662            break;663          case 'sub':664            b = stack.pop();665            a = stack.pop();666            stack.push(a - b);667            break;668          case 'true':669            stack.push(true);670            break;671          case 'truncate':672            a = stack.pop();673            a = a < 0 ? Math.ceil(a) : Math.floor(a);674            stack.push(a);675            break;676          case 'xor':677            b = stack.pop();678            a = stack.pop();679            if (isBool(a) && isBool(b)) {680              stack.push(a !== b);681            } else {682              stack.push(a ^ b);683            }684            break;685          default:686            error('Unknown operator ' + operator);687            break;688        }689      }690      return stack.stack;691    }692  };693  return PostScriptEvaluator;694})();695// Most of the PDFs functions consist of simple operations such as:696//   roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.697//698// We can compile most of such programs, and at the same moment, we can699// optimize some expressions using basic math properties. Keeping track of700// min/max values will allow us to avoid extra Math.min/Math.max calls.701var PostScriptCompiler = (function PostScriptCompilerClosure() {702  function AstNode(type) {703    this.type = type;704  }705  AstNode.prototype.visit = function (visitor) {706    throw new Error('abstract method');707  };708  function AstArgument(index, min, max) {709    AstNode.call(this, 'args');710    this.index = index;711    this.min = min;712    this.max = max;713  }714  AstArgument.prototype = Object.create(AstNode.prototype);715  AstArgument.prototype.visit = function (visitor) {716    visitor.visitArgument(this);717  };718  function AstLiteral(number) {719    AstNode.call(this, 'literal');720    this.number = number;721    this.min = number;722    this.max = number;723  }724  AstLiteral.prototype = Object.create(AstNode.prototype);725  AstLiteral.prototype.visit = function (visitor) {726    visitor.visitLiteral(this);727  };728  function AstBinaryOperation(op, arg1, arg2, min, max) {729    AstNode.call(this, 'binary');730    this.op = op;731    this.arg1 = arg1;732    this.arg2 = arg2;733    this.min = min;734    this.max = max;735  }736  AstBinaryOperation.prototype = Object.create(AstNode.prototype);737  AstBinaryOperation.prototype.visit = function (visitor) {738    visitor.visitBinaryOperation(this);739  };740  function AstMin(arg, max) {741    AstNode.call(this, 'max');742    this.arg = arg;743    this.min = arg.min;744    this.max = max;745  }746  AstMin.prototype = Object.create(AstNode.prototype);747  AstMin.prototype.visit = function (visitor) {748    visitor.visitMin(this);749  };750  function AstVariable(index, min, max) {751    AstNode.call(this, 'var');752    this.index = index;753    this.min = min;754    this.max = max;755  }756  AstVariable.prototype = Object.create(AstNode.prototype);757  AstVariable.prototype.visit = function (visitor) {758    visitor.visitVariable(this);759  };760  function AstVariableDefinition(variable, arg) {761    AstNode.call(this, 'definition');762    this.variable = variable;763    this.arg = arg;764  }765  AstVariableDefinition.prototype = Object.create(AstNode.prototype);766  AstVariableDefinition.prototype.visit = function (visitor) {767    visitor.visitVariableDefinition(this);768  };769  function ExpressionBuilderVisitor() {770    this.parts = [];771  }772  ExpressionBuilderVisitor.prototype = {773    visitArgument: function (arg) {774      this.parts.push('Math.max(', arg.min, ', Math.min(',775                      arg.max, ', src[srcOffset + ', arg.index, ']))');776    },777    visitVariable: function (variable) {778      this.parts.push('v', variable.index);779    },780    visitLiteral: function (literal) {781      this.parts.push(literal.number);782    },783    visitBinaryOperation: function (operation) {784      this.parts.push('(');785      operation.arg1.visit(this);786      this.parts.push(' ', operation.op, ' ');787      operation.arg2.visit(this);788      this.parts.push(')');789    },790    visitVariableDefinition: function (definition) {791      this.parts.push('var ');792      definition.variable.visit(this);793      this.parts.push(' = ');794      definition.arg.visit(this);795      this.parts.push(';');796    },797    visitMin: function (max) {798      this.parts.push('Math.min(');799      max.arg.visit(this);800      this.parts.push(', ', max.max, ')');801    },802    toString: function () {803      return this.parts.join('');804    }805  };806  function buildAddOperation(num1, num2) {807    if (num2.type === 'literal' && num2.number === 0) {808      // optimization: second operand is 0809      return num1;810    }811    if (num1.type === 'literal' && num1.number === 0) {812      // optimization: first operand is 0813      return num2;814    }815    if (num2.type === 'literal' && num1.type === 'literal') {816      // optimization: operands operand are literals817      return new AstLiteral(num1.number + num2.number);818    }819    return new AstBinaryOperation('+', num1, num2,820                                  num1.min + num2.min, num1.max + num2.max);821  }822  function buildMulOperation(num1, num2) {823    if (num2.type === 'literal') {824      // optimization: second operands is a literal...825      if (num2.number === 0) {826        return new AstLiteral(0); // and it's 0827      } else if (num2.number === 1) {828        return num1; // and it's 1829      } else if (num1.type === 'literal') {830        // ... and first operands is a literal too831        return new AstLiteral(num1.number * num2.number);832      }833    }834    if (num1.type === 'literal') {835      // optimization: first operands is a literal...836      if (num1.number === 0) {837        return new AstLiteral(0); // and it's 0838      } else if (num1.number === 1) {839        return num2; // and it's 1840      }841    }842    var min = Math.min(num1.min * num2.min, num1.min * num2.max,843                       num1.max * num2.min, num1.max * num2.max);844    var max = Math.max(num1.min * num2.min, num1.min * num2.max,845                       num1.max * num2.min, num1.max * num2.max);846    return new AstBinaryOperation('*', num1, num2, min, max);847  }848  function buildSubOperation(num1, num2) {849    if (num2.type === 'literal') {850      // optimization: second operands is a literal...851      if (num2.number === 0) {852        return num1; // ... and it's 0853      } else if (num1.type === 'literal') {854        // ... and first operands is a literal too855        return new AstLiteral(num1.number - num2.number);856      }857    }858    if (num2.type === 'binary' && num2.op === '-' &&859      num1.type === 'literal' && num1.number === 1 &&860      num2.arg1.type === 'literal' && num2.arg1.number === 1) {861      // optimization for case: 1 - (1 - x)862      return num2.arg2;863    }864    return new AstBinaryOperation('-', num1, num2,865                                  num1.min - num2.max, num1.max - num2.min);866  }867  function buildMinOperation(num1, max) {868    if (num1.min >= max) {869      // optimization: num1 min value is not less than required max870      return new AstLiteral(max); // just returning max871    } else if (num1.max <= max) {872      // optimization: num1 max value is not greater than required max873      return num1; // just returning an argument874    }875    return new AstMin(num1, max);876  }877  function PostScriptCompiler() {}878  PostScriptCompiler.prototype = {879    compile: function PostScriptCompiler_compile(code, domain, range) {880      var stack = [];881      var i, ii;882      var instructions = [];883      var inputSize = domain.length >> 1, outputSize = range.length >> 1;884      var lastRegister = 0;885      var n, j, min, max;886      var num1, num2, ast1, ast2, tmpVar, item;887      for (i = 0; i < inputSize; i++) {888        stack.push(new AstArgument(i, domain[i * 2], domain[i * 2 + 1]));889      }890      for (i = 0, ii = code.length; i < ii; i++) {891        item = code[i];...Using AI Code Generation
1var wptools = require('wptools');2var PostScriptCompiler = wptools.PostScriptCompiler;3var fs = require('fs');4var psc = new PostScriptCompiler();5var code = fs.readFileSync('test.ps', 'utf8');6var js = psc.compile(code);7console.log(js);8fs.writeFileSync('test.js', js, 'utf8');9var x = 10;10var y = 20;11var z = x + y;12var rect = [x, y, z, z];13var x = 10;14var y = 20;15var z = x + y;16var rect = [x, y, z, z];17this.stroke(rect);18var x = 10;19var y = 20;20var z = x + y;21var rect = [x, y, z, z];22var x = 10;23var y = 20;24var z = x + y;25var rect = [x, y, z, z];26this.stroke(rect);Using AI Code Generation
1var wptext2pdf = require('wptext2pdf');2var fs = require('fs');3var text = fs.readFileSync('text.txt').toString();4var pdf = wptext2pdf.PostScriptCompiler(text);5fs.writeFileSync('test.pdf', pdf);6var wptext2pdf = require('wptext2pdf');7var fs = require('fs');8var text = fs.readFileSync('text.txt').toString();9var pdf = wptext2pdf.PostScriptCompiler(text);10fs.writeFileSync('test.pdf', pdf);11var wptext2pdf = require('wptext2pdf');12var fs = require('fs');13var text = fs.readFileSync('text.txt').toString();14var pdf = wptext2pdf.PostScriptCompiler(text);15fs.writeFileSync('test.pdf', pdf);16var wptext2pdf = require('wptext2pdf');17var fs = require('fs');18var text = fs.readFileSync('text.txt').toString();19var pdf = wptext2pdf.PostScriptCompiler(text);20fs.writeFileSync('test.pdf', pdf);Using AI Code Generation
1var wptools = require('wptools');2var psc = new wptools.PostScriptCompiler();3var ps = psc.compileFile('test.ps');4var result = ps.run();5console.log(result);6{...} exec7{...} forall8{...} for9{...} loop10{...} stopped11{...} stopped { ... } ifUsing AI Code Generation
1var wptoolkit = require("wptoolkit");2var postScriptCompiler = new wptoolkit.PostScriptCompiler();3postScriptCompiler.compile("test.ps", "test.pdf", function(err){4    if(err){5        console.log(err);6    }7    else{8        console.log("File compiled successfully");9    }10});11compile(inputFile, outputFile, callback)12compileToBuffer(inputFile, callback)13var wptoolkit = require("wptoolkit");14var postScriptCompiler = new wptoolkit.PostScriptCompiler();15postScriptCompiler.compile("test.ps", "test.pdf", function(err){16    if(err){17        console.log(err);18    }19    else{20        console.log("File compiled successfully");21    }22});23compile(inputFile, outputFile, callback)24compileToBuffer(inputFile, callback)Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var wp = new wptoolkit();3var postScriptCompiler = new wp.PostScriptCompiler();4postScriptCompiler.compile('test.ps', 'test.pdf', function (err) {5    if (err) {6        console.log('Error: ' + err);7    } else {8        console.log('PostScript file compiled to PDF file');9    }10});11postScriptCompiler.compile('test.ps', 'test.pdf', function (err) {12    if (err) {13        console.log('Error: ' + err);14    } else {15        console.log('PostScript file compiled to PDF file');16    }17});18postScriptCompiler.compile('test.ps', 'test.pdf')19    .then(function () {20        console.log('PostScript file compiled to PDF file');21    })22    .catch(function (err) {23        console.log('Error: ' + err);24    });25postScriptCompiler.compile('test.ps', 'test.pdf', { compress: true }, function (err) {26    if (err) {27        console.log('Error: ' + err);28    } else {29        console.log('PostScript file compiled to PDF file');30    }31});32postScriptCompiler.compile('test.ps', 'test.pdf', { compress: true })33    .then(function () {34        console.log('PostScript file compiled to PDF file');35    })36    .catch(function (err) {37        console.log('Error: ' + err);38    });39postScriptCompiler.compile('test.ps', 'test.pdf', { compress: true }, function (err) {40    if (errUsing AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var code = fs.readFileSync('pscode.ps', 'utf8');4var compiler = new wptools.PostScriptCompiler();5var compiled = compiler.compile(code);6var pscode = compiled.pscode;7var pscode2 = compiled.pscode2;8var pscode3 = compiled.pscode3;9console.log(pscode);10console.log(pscode2);11console.log(pscode3);Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
