Skip to content

Commit

Permalink
implement writing and serializing of hermes source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Feb 3, 2020
1 parent 2e11457 commit d459c64
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
47 changes: 37 additions & 10 deletions src/hermes.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::decoder::{decode, decode_regular, decode_slice};
use crate::encoder::Encodable;
use crate::encoder::{encode, Encodable};
use crate::errors::{Error, Result};
use crate::jsontypes::FacebookScopeMapping;
use crate::jsontypes::RawSourceMap;
use crate::types::{DecodedMap, SourceMap};
use crate::jsontypes::{FacebookScopeMapping, FacebookSources, RawSourceMap};
use crate::types::{DecodedMap, RewriteOptions, SourceMap};
use crate::vlq::parse_vlq_segment;
use std::cmp::Ordering;
use std::io::Read;
use std::io::{Read, Write};
use std::ops::{Deref, DerefMut};

/// These are starting locations of scopes.
Expand All @@ -27,6 +26,7 @@ pub struct SourceMapHermes {
pub(crate) sm: SourceMap,
// There should be one `HermesFunctionMap` per each `sources` entry in the main SourceMap.
function_maps: Vec<Option<HermesFunctionMap>>,
raw_facebook_sources: FacebookSources,
}

impl Deref for SourceMapHermes {
Expand All @@ -46,7 +46,9 @@ impl DerefMut for SourceMapHermes {
impl Encodable for SourceMapHermes {
fn as_raw_sourcemap(&self) -> RawSourceMap {
// TODO: need to serialize the `HermesFunctionMap` mappings
self.sm.as_raw_sourcemap()
let mut rsm = self.sm.as_raw_sourcemap();
rsm.x_facebook_sources = self.raw_facebook_sources.clone();
rsm
}
}

Expand All @@ -65,6 +67,10 @@ impl SourceMapHermes {
}
}

pub fn to_writer<W: Write>(&self, w: W) -> Result<()> {
encode(self, w)
}

pub fn get_original_function_name(&self, bytecode_offset: u32) -> Option<&str> {
let token = self.sm.lookup_token(0, bytecode_offset)?;

Expand All @@ -90,6 +96,20 @@ impl SourceMapHermes {
.get(name_index as usize)
.map(|n| n.as_str())
}

pub fn rewrite(self, options: &RewriteOptions<'_>) -> Result<Self> {
let Self {
sm,
function_maps,
raw_facebook_sources,
} = self;
let sm = sm.rewrite(options)?;
Ok(Self {
sm,
function_maps,
raw_facebook_sources,
})
}
}

pub fn decode_hermes(mut rsm: RawSourceMap) -> Result<SourceMapHermes> {
Expand All @@ -102,12 +122,12 @@ pub fn decode_hermes(mut rsm: RawSourceMap) -> Result<SourceMapHermes> {
// https://github.com/facebook/metro/blob/63b523eb20e7bdf62018aeaf195bb5a3a1a67f36/packages/metro-symbolicate/src/SourceMetadataMapConsumer.js#L182-L202

let function_maps = x_facebook_sources
.into_iter()
.iter()
.map(|v| {
let FacebookScopeMapping {
names,
mappings: raw_mappings,
} = v.into_iter().next()?;
} = v.as_ref()?.iter().next()?;

let mut mappings = vec![];
let mut line = 1;
Expand Down Expand Up @@ -137,10 +157,17 @@ pub fn decode_hermes(mut rsm: RawSourceMap) -> Result<SourceMapHermes> {
});
}
}
Some(HermesFunctionMap { names, mappings })
Some(HermesFunctionMap {
names: names.clone(),
mappings,
})
})
.collect();

let sm = decode_regular(rsm)?;
Ok(SourceMapHermes { sm, function_maps })
Ok(SourceMapHermes {
sm,
function_maps,
raw_facebook_sources: Some(x_facebook_sources),
})
}
4 changes: 2 additions & 2 deletions src/jsontypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct RawSection {
pub map: Option<Box<RawSourceMap>>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct FacebookScopeMapping {
pub names: Vec<String>,
pub mappings: String,
Expand All @@ -25,7 +25,7 @@ pub struct FacebookScopeMapping {
// It has a list of metadata, the first one of which is a *function map*,
// containing scope information as a nested source map.
// See the decoder in `hermes.rs` for details.
type FacebookSources = Option<Vec<Vec<FacebookScopeMapping>>>;
pub type FacebookSources = Option<Vec<Option<Vec<FacebookScopeMapping>>>>;

#[derive(Serialize, Deserialize)]
pub struct RawSourceMap {
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ impl SourceMap {
}
}

/// This rewrites the sourcemap accoridng to the provided rewrite
/// This rewrites the sourcemap according to the provided rewrite
/// options.
///
/// The default behavior is to just deduplicate the sourcemap, something
Expand Down

0 comments on commit d459c64

Please sign in to comment.