All files / lib/agents hermes.js

0% Statements 0/34
0% Branches 0/21
0% Functions 0/4
0% Lines 0/34

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 101 102 103                                                                                                                                                                                                             
'use strict';
 
const fs = require('fs');
const runtimePath = require('../runtime-path');
const ConsoleAgent = require('../ConsoleAgent');
 
const errorRe1 = /^(\w+): (.*)$/m;
const errorRe2 = /^(?:(\w+): (.*))|(?:(\w+))$/m;
 
function parseSyntaxError(syntaxErrorMessage) {
  const matches = syntaxErrorMessage.match(/:(\d+):(\d+): (.*)/);
  if (matches && matches.length) {
    return {
      message: matches[3].replace('error: ', ''),
      lineNumber: Number(matches[1]),
      columnNumber: Number(matches[2])
    };
  }
  return null;
}
 
 
class HermesAgent extends ConsoleAgent {
  constructor(options) {
    super(options);
  }
 
  async evalScript(code, options = {}) {
    // By default eshost must target an environment that can
    // evaluate non-strict mode code
    if (!options.module) {
      this.args.unshift('-non-strict');
    }
 
    this.args.unshift('-Xintl', '-enable-eval', '-fenable-tdz');
 
    // There is currently no flag for supporting modules in Hermes
    // if (options.module && this.args[0] !== '-m') {
    //   this.args.unshift('-m');
    // }
 
    // if (!options.module && this.args[0] === '-m') {
    //   this.args.shift();
    // }
 
    return super.evalScript(code, options);
  }
 
  parseError(rawstr) {
    const str = rawstr.replace(/^Uncaught /, '');
 
    let match = str.match(errorRe1);
 
    if (match) {
      return {
        name: match[1],
        message: match[2],
        stack: [],
      };
    } else {
 
      // Syntax errors don't have nice error messages...
      let error = null;
      let errors = str.match(/:(\d+):(\d+): (.*)/gm);
 
      if (errors && errors.length) {
        error = {
          name: 'SyntaxError',
          message: '',
          stack: []
        };
 
        const stack = parseSyntaxError(errors[0]);
 
        if (stack) {
          error.stack.push(stack);
          error.message = stack.message;
        }
      }
 
      if (error) {
        return error;
      }
 
      // Last chance...
      errors = str.match(errorRe2);
 
      if (errors && errors[0]) {
        return {
          name: errors[0],
          message: errors[1],
          stack: [],
        };
      }
    }
 
    return null;
  }
}
HermesAgent.runtime = fs.readFileSync(runtimePath.for('hermes'), 'utf8');
 
module.exports = HermesAgent;