Files
react-native/Libraries/Core/Devtools/parseErrorStack.js
T
Moti Zilberman 75d03b56fa Implement a dedicated Hermes stack trace parser
Summary:
Makes stack trace parsing return a consistent representation of column numbers when using Hermes, whether we're executing bytecode (in prod) or source code (in dev). This is achieved by creating a new full-fidelity stack trace parser for Hermes.

NOTE:  We still use the `stacktrace-parser` package for other engines, so this fix applies only to Hermes and not to JSC - that will be fixed separately in an upcoming diff.

This fixes a bug where we were skewing some stack traces by sending 1-based column numbers to the Metro symbolication endpoint, which expects them to be 0-based. The bug is only noticeable in *some* cases, namely where the column immediately following the correct one is in a different function.

Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D18627930

fbshipit-source-id: abd80846f00f24428670b2c92153564fb4bb2aff
2019-12-02 06:02:25 -08:00

66 lines
1.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import type {StackFrame} from '../NativeExceptionsManager';
import type {HermesParsedStack} from './parseHermesStack';
const parseHermesStack = require('./parseHermesStack');
export type ExtendedError = Error & {
jsEngine?: string,
preventSymbolication?: boolean,
componentStack?: string,
forceRedbox?: boolean,
isComponentError?: boolean,
...
};
function convertHermesStack(stack: HermesParsedStack): Array<StackFrame> {
const frames = [];
for (const entry of stack.entries) {
if (entry.type !== 'FRAME') {
continue;
}
const {location, functionName} = entry;
if (location.type === 'NATIVE') {
continue;
}
frames.push({
methodName: functionName,
file: location.sourceUrl,
lineNumber: location.line1Based,
column:
location.type === 'SOURCE'
? location.column1Based - 1
: location.virtualOffset0Based,
});
}
return frames;
}
function parseErrorStack(e: ExtendedError): Array<StackFrame> {
if (!e || !e.stack) {
return [];
}
const stacktraceParser = require('stacktrace-parser');
const stack = Array.isArray(e.stack)
? e.stack
: global.HermesInternal
? convertHermesStack(parseHermesStack(e.stack))
: stacktraceParser.parse(e.stack);
return stack;
}
module.exports = parseErrorStack;