All files / lib/agents jsc.js

90.91% Statements 40/44
80.65% Branches 25/31
100% Functions 6/6
90.91% Lines 40/44

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147    1x 1x 1x 1x 1x   1x   1x                         1x     33x 33x   33x 33x 63x 63x 33x     30x                   33x 3x                 33x         44x                                               44x 44x           44x           44x               151x 3x     151x 2x     151x       150x   150x 117x     33x               150x   150x 33x 33x     150x       1x   1x  
'use strict';
 
const fs = require('fs');
const path = require('path');
const isSymlink = require('is-symlink');
const runtimePath = require('../runtime-path');
const ConsoleAgent = require('../ConsoleAgent');
 
const errorRe = /(?:.*?): (\w+)(?:: (.*))?(?:\r?\nat[^\n]*)?(\r?\n.*@(\[native code\]|(?:.*:\d+:\d+)))*\r?\n/;
 
let DYLD_FRAMEWORK_PATH = process.env.DYLD_FRAMEWORK_PATH;
 
// JSC stack frame format:
// StackFrames: StackFrame+
// StackFrame: FunctionFrame | NativeFrame | EvalMarker
// FunctionFrame: (FunctionName`@`)?SourceInfo
// NativeFrame: (FunctionName`@`)?`[native code]`
// FunctionName: .*
// SourceInfo: File`:`Line`:`Column
// File: .*
// Line: \d+
// Column: \d+
// EvalMarker: `eval code`
const frameRe = /(?:(.*)@)?(\[native code\]|(?:(.*):(\d+):(\d+)))/;
 
function parseStack(stackStr) {
  stackStr = stackStr || '';
  const stack = [];
 
  const lines = stackStr.split(/\r?\n/g);
  lines.forEach(entry => {
    const match = entry.match(frameRe);
    if (match === null) {
      return;
    }
 
    stack.push({
      source: entry,
      functionName: (match[1] || '').trim(),
      fileName: match[3] || match[2],
      lineNumber: Number(match[4]),
      columnNumber: Number(match[5])
    });
  });
 
  // Add dummy frame if no stack frames are present in stack string.
  if (stack.length === 0) {
    stack.push({
      source: '',
      functionName: '',
      fileName: '',
      lineNumber: 1,
      columnNumber: 1
    });
  }
 
  return stack;
}
 
class JSCAgent extends ConsoleAgent {
  constructor(...args) {
    super(...args);
 
    /*
      See: https://webkit.org/running-webkit/
 
      This is frustrating, but necessary. DYLD_FRAMEWORK_PATH won't appear
      in process.env when node.js scripts are executed as binaries,
      ie. have a /usr/bin/env she-bang, because DYLD_* vars are excluded from env output
 
      $ export DYLD_FRAMEWORK_PATH="foo"
      $ node -pe process.env.DYLD_FRAMEWORK_PATH
      foo
      $ env | grep "DYLD_FRAMEWORK_PATH"
      (not found, nothing prints)
 
 
      Still not convinced? The following code shows that even WebKit's own run-jsc
      script must do this dance to ensure that DYLD_FRAMEWORK_PATH is set with the
      correct path for executing built-from-source jsc:
 
      - https://github.com/WebKit/WebKit/blob/main/Tools/Scripts/run-jsc#L60-L63
      - https://github.com/WebKit/WebKit/blob/main/Tools/Scripts/webkitdirs.pm#L2764-L2777
    */
 
    Eif (DYLD_FRAMEWORK_PATH === undefined) {
      Iif (isSymlink.sync(this.hostPath)) {
        let linked = fs.readlinkSync(this.hostPath);
        if (linked.includes('/WebKitBuild/')) {
          DYLD_FRAMEWORK_PATH = path.dirname(linked);
        }
      } else {
        Iif (this.hostPath.includes('/WebKitBuild/')) {
          DYLD_FRAMEWORK_PATH = path.dirname(this.hostPath);
        }
      }
    }
 
    this.cpOptions = {
      env: {
        DYLD_FRAMEWORK_PATH
      }
    };
  }
 
  async evalScript(code, options = {}) {
    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(str) {
    const match = str.match(errorRe);
 
    if (!match) {
      return null;
    }
 
    return {
      name: match[1],
      message: match[2],
      stack: parseStack(match[3])
    };
  }
 
  normalizeResult(result) {
    const match = result.stdout.match(errorRe);
 
    if (match) {
      result.stdout = result.stdout.replace(errorRe, '');
      result.stderr = match[0];
    }
 
    return result;
  }
}
 
JSCAgent.runtime = fs.readFileSync(runtimePath.for('jsc'), 'utf8');
 
module.exports = JSCAgent;