Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support react-native hermes sourcemaps #187

Merged
merged 4 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 84 additions & 18 deletions sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ pub struct SourceView<'a> {

/// Represents a source map.
pub struct SourceMapView {
sm: sourcemap::SourceMap,
dm: sourcemap::DecodedMap,
}

/// A matched token.
#[derive(Debug, Default, PartialEq)]
pub struct TokenMatch<'a> {
/// The line number in the original source file.
pub src_line: u32,
Expand Down Expand Up @@ -114,46 +115,60 @@ impl SourceMapView {
/// If the sourcemap is an index it is being flattened. If flattening
/// is not possible then an error is raised.
pub fn from_json_slice(buffer: &[u8]) -> Result<Self, ParseSourceMapError> {
use sourcemap::DecodedMap::*;
Swatinem marked this conversation as resolved.
Show resolved Hide resolved
Ok(SourceMapView {
sm: match sourcemap::decode_slice(buffer)? {
sourcemap::DecodedMap::Regular(sm) => sm,
sourcemap::DecodedMap::Index(smi) => smi.flatten()?,
dm: match sourcemap::decode_slice(buffer)? {
Regular(sm) => Regular(sm),
Index(smi) => Regular(smi.flatten()?),
Hermes(smh) => Hermes(smh),
_ => panic!("invalid sourcemap type"),
Swatinem marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

fn as_sm(&self) -> &sourcemap::SourceMap {
use sourcemap::DecodedMap::*;
match &self.dm {
Regular(sm) => sm,
Hermes(smh) => smh,
_ => unreachable!(),
Swatinem marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Looks up a token and returns it.
pub fn lookup_token(&self, line: u32, col: u32) -> Option<TokenMatch<'_>> {
self.sm
self.as_sm()
.lookup_token(line, col)
.map(|tok| self.make_token_match(tok))
}

/// Returns a token for a specific index.
pub fn get_token(&self, idx: u32) -> Option<TokenMatch<'_>> {
self.sm.get_token(idx).map(|tok| self.make_token_match(tok))
self.as_sm()
.get_token(idx)
.map(|tok| self.make_token_match(tok))
}

/// Returns the number of tokens.
pub fn get_token_count(&self) -> u32 {
self.sm.get_token_count()
self.as_sm().get_token_count()
}

/// Returns a source view for the given source.
pub fn get_source_view(&self, idx: u32) -> Option<&SourceView<'_>> {
self.sm
self.as_sm()
.get_source_view(idx)
.map(|s| unsafe { &*(s as *const _ as *const SourceView<'_>) })
}

/// Returns the source name for an index.
pub fn get_source_name(&self, idx: u32) -> Option<&str> {
self.sm.get_source(idx)
self.as_sm().get_source(idx)
}

/// Returns the number of sources.
pub fn get_source_count(&self) -> u32 {
self.sm.get_source_count()
self.as_sm().get_source_count()
}

/// Looks up a token and the original function name.
Expand All @@ -169,14 +184,28 @@ impl SourceMapView {
minified_name: &str,
source: &SourceView<'b>,
) -> Option<TokenMatch<'a>> {
self.sm.lookup_token(line, col).map(|token| {
let mut rv = self.make_token_match(token);
rv.function_name = source
.sv
.get_original_function_name(token, minified_name)
.map(str::to_owned);
rv
})
use sourcemap::DecodedMap::*;
match &self.dm {
Regular(sm) => sm.lookup_token(line, col).map(|token| {
let mut rv = self.make_token_match(token);
rv.function_name = source
.sv
.get_original_function_name(token, minified_name)
.map(str::to_owned);
rv
}),
Hermes(smh) => {
// we use `col + 1` here, since hermes uses bytecode offsets which are 0-based,
// and the upstream python code does a `- 1` here:
// https://github.com/getsentry/sentry/blob/fdabccac7576c80674c2fed556d4c5407657dc4c/src/sentry/lang/javascript/processor.py#L584-L586
smh.lookup_token(line, col + 1).map(|token| {
let mut rv = self.make_token_match(token);
rv.function_name = smh.get_original_function_name(col + 1).map(str::to_owned);
rv
})
}
_ => unreachable!(),
}
}

fn make_token_match<'a>(&'a self, tok: sourcemap::Token<'a>) -> TokenMatch<'a> {
Expand All @@ -192,3 +221,40 @@ impl SourceMapView {
}
}
}

#[test]
fn test_react_native_hermes() {
let bytes = include_bytes!("../tests/fixtures/react-native-hermes.map");
let smv = SourceMapView::from_json_slice(bytes).unwrap();
let sv = SourceView::new("");

// at foo (address at unknown:1:11939)
assert_eq!(
smv.lookup_token_with_function_name(0, 11939, "", &sv),
Some(TokenMatch {
src_line: 1,
src_col: 10,
dst_line: 0,
dst_col: 11939,
src_id: 5,
name: None,
src: Some("module.js"),
function_name: Some("foo".into())
})
);

// at anonymous (address at unknown:1:11857)
assert_eq!(
smv.lookup_token_with_function_name(0, 11857, "", &sv),
Some(TokenMatch {
src_line: 2,
src_col: 0,
dst_line: 0,
dst_col: 11857,
src_id: 4,
name: None,
src: Some("input.js"),
function_name: Some("<global>".into())
})
);
}
5 changes: 5 additions & 0 deletions sourcemap/tests/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## react-native-hermes.map

This SourceMap is one generated by the `react-native` + `hermes` pipeline.
See the `rust-sourcemap` repo for instructions on how to generate it.
Additionally, it was processed by `sentry-cli`, which basically inlines all the `sourceContent`s.
1 change: 1 addition & 0 deletions sourcemap/tests/fixtures/react-native-hermes.map

Large diffs are not rendered by default.