All files / lib/agents engine262.js

88.37% Statements 38/43
81.25% Branches 26/32
100% Functions 4/4
88.37% Lines 38/43

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    1x 1x 1x 1x 1x   1x       1x 1x 1x       31x       31x       31x       74x         74x       30x 30x     30x 22x     30x 21x     9x     9x     9x             9x               30x 30x   30x 1x 1x   29x 29x 29x   29x 8x       8x       30x       1x   1x  
'use strict';
 
const cp = require('child_process');
const fs = require('fs');
const runtimePath = require('../runtime-path');
const ConsoleAgent = require('../ConsoleAgent');
const ErrorParser = require('../parse-error.js');
 
const isWindows = process.platform === 'win32' ||
  process.env.OSTYPE === 'cygwin' ||
  process.env.OSTYPE === 'msys';
 
const builtinerrorexp = /^(\w+):? ?(.*)$/m;
const errorexp = /(?:\{(?:\s+name:\s+'(.*)',\s+message:\s+'(.*)'\s+)\})/gm;
const nodeerrorexp = /^([\w\d]+)(?:\s+)(?:\{\s+message:\s+'(.*)'\s+\})/gm;
 
class Engine262 extends ConsoleAgent {
  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);
  }
 
  stop() {
    Iif (isWindows && this[Symbol.for('cp')]) {
      // This is necessary for killing a node.js .cmd process on windows
      const child = this[Symbol.for('cp')];
      cp.exec(`taskkill -F -T -PID ${child.pid}`);
    }
    return super.stop();
  }
 
  parseError(str) {
    errorexp.lastIndex = -1;
    let match = errorexp.exec(str);
 
    // This is utterly atrocious.
    if (!match) {
      match = /(?:\{(?:\s+name:\s+'(.*)'\s+)\})/gm.exec(str);
    }
 
    if (!match) {
      return null;
    }
 
    const stackStr = match[4] || '';
 
    let stack;
    Iif (stackStr.trim().length > 0) {
      stack = ErrorParser.parseStack(stackStr);
    } else {
      stack = [{
        source: match[0],
        fileName: match[1],
        lineNumber: 1
      }];
    }
 
    return {
      name: match[1],
      message: match[2],
      stack,
    };
  }
 
  normalizeResult(result) {
    errorexp.lastIndex = -1;
    let ematch = errorexp.exec(result.stderr);
 
    if (ematch) {
      result.stderr = ematch[0];
      result.stdout = '';
    } else {
      let bmatch = builtinerrorexp.exec(result.stderr);
      let nmatch = nodeerrorexp.exec(result.stderr);
      let match = nmatch || bmatch;
 
      if (match) {
        result.stderr = match[2]
          ? `{ name: '${match[1]}', message: '${match[2]}' }`
          : `{ name: '${match[1]}' }`;
 
        result.stdout = '';
      }
    }
 
    return result;
  }
}
 
Engine262.runtime = fs.readFileSync(runtimePath.for('engine262'), 'utf8');
 
module.exports = Engine262;