|
| 1 | +use crate::decoder::{decode, decode_regular, decode_slice}; |
| 2 | +use crate::encoder::{encode, Encodable}; |
| 3 | +use crate::errors::{Error, Result}; |
| 4 | +use crate::jsontypes::{FacebookScopeMapping, FacebookSources, RawSourceMap}; |
| 5 | +use crate::types::{DecodedMap, RewriteOptions, SourceMap}; |
| 6 | +use crate::vlq::parse_vlq_segment; |
| 7 | +use std::cmp::Ordering; |
| 8 | +use std::io::{Read, Write}; |
| 9 | +use std::ops::{Deref, DerefMut}; |
| 10 | + |
| 11 | +/// These are starting locations of scopes. |
| 12 | +/// The `name_index` represents the index into the `HermesFunctionMap.names` vec, |
| 13 | +/// which represents the function names/scopes. |
| 14 | +pub struct HermesScopeOffset { |
| 15 | + line: u32, |
| 16 | + column: u32, |
| 17 | + name_index: u32, |
| 18 | +} |
| 19 | + |
| 20 | +pub struct HermesFunctionMap { |
| 21 | + names: Vec<String>, |
| 22 | + mappings: Vec<HermesScopeOffset>, |
| 23 | +} |
| 24 | + |
| 25 | +/// Represents a `react-native`-style SourceMap, which has additional scope |
| 26 | +/// information embedded. |
| 27 | +pub struct SourceMapHermes { |
| 28 | + pub(crate) sm: SourceMap, |
| 29 | + // There should be one `HermesFunctionMap` per each `sources` entry in the main SourceMap. |
| 30 | + function_maps: Vec<Option<HermesFunctionMap>>, |
| 31 | + // XXX: right now, I am too lazy to actually serialize the above `function_maps` |
| 32 | + // back into json types, so just keep the original json. Might be a bit inefficient, but meh. |
| 33 | + raw_facebook_sources: FacebookSources, |
| 34 | +} |
| 35 | + |
| 36 | +impl Deref for SourceMapHermes { |
| 37 | + type Target = SourceMap; |
| 38 | + |
| 39 | + fn deref(&self) -> &Self::Target { |
| 40 | + &self.sm |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl DerefMut for SourceMapHermes { |
| 45 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 46 | + &mut self.sm |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Encodable for SourceMapHermes { |
| 51 | + fn as_raw_sourcemap(&self) -> RawSourceMap { |
| 52 | + // TODO: need to serialize the `HermesFunctionMap` mappings |
| 53 | + let mut rsm = self.sm.as_raw_sourcemap(); |
| 54 | + rsm.x_facebook_sources = self.raw_facebook_sources.clone(); |
| 55 | + rsm |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl SourceMapHermes { |
| 60 | + /// Creates a sourcemap from a reader over a JSON stream in UTF-8 |
| 61 | + /// format. |
| 62 | + /// |
| 63 | + /// See [`SourceMap::from_reader`](struct.SourceMap.html#method.from_reader) |
| 64 | + pub fn from_reader<R: Read>(rdr: R) -> Result<Self> { |
| 65 | + match decode(rdr)? { |
| 66 | + DecodedMap::Hermes(sm) => Ok(sm), |
| 67 | + _ => Err(Error::IncompatibleSourceMap), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// Creates a sourcemap from a reader over a JSON byte slice in UTF-8 |
| 72 | + /// format. |
| 73 | + /// |
| 74 | + /// See [`SourceMap::from_slice`](struct.SourceMap.html#method.from_slice) |
| 75 | + pub fn from_slice(slice: &[u8]) -> Result<Self> { |
| 76 | + match decode_slice(slice)? { |
| 77 | + DecodedMap::Hermes(sm) => Ok(sm), |
| 78 | + _ => Err(Error::IncompatibleSourceMap), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// Writes a sourcemap into a writer. |
| 83 | + /// |
| 84 | + /// See [`SourceMap::to_writer`](struct.SourceMap.html#method.to_writer) |
| 85 | + pub fn to_writer<W: Write>(&self, w: W) -> Result<()> { |
| 86 | + encode(self, w) |
| 87 | + } |
| 88 | + |
| 89 | + /// Given a bytecode offset, this will find the enclosing scopes function |
| 90 | + /// name. |
| 91 | + pub fn get_original_function_name(&self, bytecode_offset: u32) -> Option<&str> { |
| 92 | + let token = self.sm.lookup_token(0, bytecode_offset)?; |
| 93 | + |
| 94 | + let function_map = (self.function_maps.get(token.get_src_id() as usize))?.as_ref()?; |
| 95 | + |
| 96 | + // Find the closest mapping, just like here: |
| 97 | + // https://github.com/facebook/metro/blob/63b523eb20e7bdf62018aeaf195bb5a3a1a67f36/packages/metro-symbolicate/src/SourceMetadataMapConsumer.js#L204-L231 |
| 98 | + let mapping = |
| 99 | + function_map |
| 100 | + .mappings |
| 101 | + .binary_search_by(|o| match o.line.cmp(&token.get_src_line()) { |
| 102 | + Ordering::Equal => o.column.cmp(&token.get_src_col()), |
| 103 | + x => x, |
| 104 | + }); |
| 105 | + let name_index = function_map |
| 106 | + .mappings |
| 107 | + .get(match mapping { |
| 108 | + Ok(a) => a, |
| 109 | + Err(a) => a.saturating_sub(1), |
| 110 | + })? |
| 111 | + .name_index; |
| 112 | + |
| 113 | + function_map |
| 114 | + .names |
| 115 | + .get(name_index as usize) |
| 116 | + .map(|n| n.as_str()) |
| 117 | + } |
| 118 | + |
| 119 | + /// This rewrites the sourcemap according to the provided rewrite |
| 120 | + /// options. |
| 121 | + /// |
| 122 | + /// See [`SourceMap::rewrite`](struct.SourceMap.html#method.rewrite) |
| 123 | + pub fn rewrite(self, options: &RewriteOptions<'_>) -> Result<Self> { |
| 124 | + let Self { |
| 125 | + sm, |
| 126 | + function_maps, |
| 127 | + raw_facebook_sources, |
| 128 | + } = self; |
| 129 | + let sm = sm.rewrite(options)?; |
| 130 | + Ok(Self { |
| 131 | + sm, |
| 132 | + function_maps, |
| 133 | + raw_facebook_sources, |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +pub fn decode_hermes(mut rsm: RawSourceMap) -> Result<SourceMapHermes> { |
| 139 | + let x_facebook_sources = rsm |
| 140 | + .x_facebook_sources |
| 141 | + .take() |
| 142 | + .ok_or(Error::IncompatibleSourceMap)?; |
| 143 | + |
| 144 | + // This is basically the logic from here: |
| 145 | + // https://github.com/facebook/metro/blob/63b523eb20e7bdf62018aeaf195bb5a3a1a67f36/packages/metro-symbolicate/src/SourceMetadataMapConsumer.js#L182-L202 |
| 146 | + |
| 147 | + let function_maps = x_facebook_sources |
| 148 | + .iter() |
| 149 | + .map(|v| { |
| 150 | + let FacebookScopeMapping { |
| 151 | + names, |
| 152 | + mappings: raw_mappings, |
| 153 | + } = v.as_ref()?.iter().next()?; |
| 154 | + |
| 155 | + let mut mappings = vec![]; |
| 156 | + let mut line = 1; |
| 157 | + let mut name_index = 0; |
| 158 | + |
| 159 | + for line_mapping in raw_mappings.split(';') { |
| 160 | + if line_mapping.is_empty() { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + let mut column = 0; |
| 165 | + |
| 166 | + for mapping in line_mapping.split(',') { |
| 167 | + if mapping.is_empty() { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + let mut nums = parse_vlq_segment(mapping).ok()?.into_iter(); |
| 172 | + |
| 173 | + column = (i64::from(column) + nums.next()?) as u32; |
| 174 | + name_index = (i64::from(name_index) + nums.next().unwrap_or(0)) as u32; |
| 175 | + line = (i64::from(line) + nums.next().unwrap_or(0)) as u32; |
| 176 | + mappings.push(HermesScopeOffset { |
| 177 | + column, |
| 178 | + line, |
| 179 | + name_index, |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + Some(HermesFunctionMap { |
| 184 | + names: names.clone(), |
| 185 | + mappings, |
| 186 | + }) |
| 187 | + }) |
| 188 | + .collect(); |
| 189 | + |
| 190 | + let sm = decode_regular(rsm)?; |
| 191 | + Ok(SourceMapHermes { |
| 192 | + sm, |
| 193 | + function_maps, |
| 194 | + raw_facebook_sources: Some(x_facebook_sources), |
| 195 | + }) |
| 196 | +} |
0 commit comments