All files / lib/agents graaljs.js

87.88% Statements 29/33
81.82% Branches 18/22
100% Functions 4/4
87.88% Lines 29/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84    1x 1x 1x 1x   1x 1x       38x   38x                 33x       33x       33x       32x 32x   32x 32x       32x 32x       32x 23x     9x     9x 5x   4x     9x               32x   32x         32x       1x   1x  
'use strict';
 
const fs = require('fs');
const runtimePath = require('../runtime-path');
const ConsoleAgent = require('../ConsoleAgent');
const ErrorParser = require('../parse-error.js');
 
const errorexp = /(.*?): (.*)(\n(\s*)at (.+))*/gm;
const customexp = /^(\w+):? ?(.*)$/gm;
 
class GraalJSAgent extends ConsoleAgent {
  constructor(options) {
    super(options);
 
    this.args.unshift(
      '--js.test262-mode=true',
      '--js.intl-402=true',
      '--js.ecmascript-version=2022',
      '--experimental-options'
    );
  }
 
  async evalScript(code, options = {}) {
    Iif (options.module && this.args[0] !== '--module') {
      this.args.unshift('--module');
    }
 
    Iif (!options.module && this.args[0] === '--module') {
      this.args.shift();
    }
 
    return super.evalScript(code, options);
  }
 
  parseError(str) {
    errorexp.lastIndex = -1;
    customexp.lastIndex = -1;
 
    const ematch = errorexp.exec(str);
    const cmatch = customexp.exec(str);
 
    // If the full error expression has a match with a stack,
    // use that as the match object.
    const hasStack = ematch && (ematch[3] && ematch[3].trim() !== '');
    const match = hasStack
      ? ematch
      : cmatch;
 
    if (!match) {
      return null;
    }
 
    const stackStr = match[3] || '';
 
    let stack;
    if (hasStack) {
      stack = ErrorParser.parseStack(stackStr.trim());
    } else {
      stack = [];
    }
 
    return {
      name: match[1],
      message: match[2],
      stack: stack,
    };
  }
 
  normalizeResult(result) {
    const match = result.stdout.match(errorexp);
 
    Iif (match) {
      result.stdout = result.stdout.replace(errorexp, '');
      result.stderr = match[0];
    }
 
    return result;
  }
}
 
GraalJSAgent.runtime = fs.readFileSync(runtimePath.for('graaljs'), 'utf8');
 
module.exports = GraalJSAgent;