From 7d68d4e658d5159a11c9bda14e3fd4369af02027 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 30 May 2025 10:46:54 +0200 Subject: [PATCH 1/4] fix: Correctly interpret missing line/column numbers --- src/decoder.rs | 52 +++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 1e7e999..e60b0fc 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -148,10 +148,13 @@ fn decode_rmi(rmi_str: &str, val: &mut BitVec) -> Result<()> { pub fn decode_regular(rsm: RawSourceMap) -> Result { let mut dst_col; - let mut src_id = 0; - let mut src_line = 0; - let mut src_col = 0; - let mut name_id = 0; + + // Source IDs, lines, columns, and names are "running" values. + // Each token (except the first) contains the delta from the previous value. + let mut running_src_id = 0; + let mut running_src_line = 0; + let mut running_src_col = 0; + let mut running_name_id = 0; let names = rsm.names.unwrap_or_default(); let sources = rsm.sources.unwrap_or_default(); @@ -185,28 +188,37 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { parse_vlq_segment_into(segment, &mut nums)?; dst_col = (i64::from(dst_col) + nums[0]) as u32; - let mut src = !0; - let mut name = !0; + // The source file , source line, source column, and name + // may not be present in the current token. We use `u32::MAX` + // as the placeholder for missing values. + let mut current_src = !0; + let mut current_name = !0; + let mut current_src_line = !0; + let mut current_src_col = !0; if nums.len() > 1 { if nums.len() != 4 && nums.len() != 5 { return Err(Error::BadSegmentSize(nums.len() as u32)); } - src_id = (i64::from(src_id) + nums[1]) as u32; - if src_id >= sources.len() as u32 { - return Err(Error::BadSourceReference(src_id)); + + running_src_id = (i64::from(running_src_id) + nums[1]) as u32; + if running_src_id >= sources.len() as u32 { + return Err(Error::BadSourceReference(running_src_id)); } - src = src_id; - src_line = (i64::from(src_line) + nums[2]) as u32; - src_col = (i64::from(src_col) + nums[3]) as u32; + running_src_line = (i64::from(running_src_line) + nums[2]) as u32; + running_src_col = (i64::from(running_src_col) + nums[3]) as u32; + + current_src = running_src_id; + current_src_line = running_src_line; + current_src_col = running_src_col; if nums.len() > 4 { - name_id = (i64::from(name_id) + nums[4]) as u32; - if name_id >= names.len() as u32 { - return Err(Error::BadNameReference(name_id)); + running_name_id = (i64::from(running_name_id) + nums[4]) as u32; + if running_name_id >= names.len() as u32 { + return Err(Error::BadNameReference(running_name_id)); } - name = name_id; + current_name = running_name_id; } } @@ -215,10 +227,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { tokens.push(RawToken { dst_line: dst_line as u32, dst_col, - src_line, - src_col, - src_id: src, - name_id: name, + src_line: current_src_line, + src_col: current_src_col, + src_id: current_src, + name_id: current_name, is_range, }); } From 7b34adc6d0a4dabd23afb5e4bbda89c60bad2f35 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 30 May 2025 12:44:42 +0200 Subject: [PATCH 2/4] Docstrings --- src/types.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 52eb869..5ef415b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -242,12 +242,18 @@ impl<'a> Token<'a> { (self.get_dst_line(), self.get_dst_col()) } - /// get the source line number + /// Get the source line number. + /// + /// `u32::MAX` is a sentinel value meaning + /// this token is unmapped. pub fn get_src_line(&self) -> u32 { self.raw.src_line } - /// get the source column number + /// Get the source column number. + /// + /// `u32::MAX` is a sentinel value meaning + /// this token is unmapped. pub fn get_src_col(&self) -> u32 { self.raw.src_col.saturating_add(self.offset) } From 0917a7645051866037b2756e53607d26e53b81fb Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 30 May 2025 12:49:05 +0200 Subject: [PATCH 3/4] Small ref --- src/decoder.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index e60b0fc..d4391fd 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -186,22 +186,24 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { nums.clear(); parse_vlq_segment_into(segment, &mut nums)?; + match nums.len() { + 1 | 4 | 5 => {} + _ => return Err(Error::BadSegmentSize(nums.len() as u32)), + } + dst_col = (i64::from(dst_col) + nums[0]) as u32; // The source file , source line, source column, and name // may not be present in the current token. We use `u32::MAX` // as the placeholder for missing values. - let mut current_src = !0; - let mut current_name = !0; + let mut current_src_id = !0; let mut current_src_line = !0; let mut current_src_col = !0; + let mut current_name = !0; if nums.len() > 1 { - if nums.len() != 4 && nums.len() != 5 { - return Err(Error::BadSegmentSize(nums.len() as u32)); - } - running_src_id = (i64::from(running_src_id) + nums[1]) as u32; + if running_src_id >= sources.len() as u32 { return Err(Error::BadSourceReference(running_src_id)); } @@ -209,7 +211,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { running_src_line = (i64::from(running_src_line) + nums[2]) as u32; running_src_col = (i64::from(running_src_col) + nums[3]) as u32; - current_src = running_src_id; + current_src_id = running_src_id; current_src_line = running_src_line; current_src_col = running_src_col; @@ -229,7 +231,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { dst_col, src_line: current_src_line, src_col: current_src_col, - src_id: current_src, + src_id: current_src_id, name_id: current_name, is_range, }); From 3fb9bb0933602ae3ae87bf441183a86e3066bc89 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 30 May 2025 12:52:58 +0200 Subject: [PATCH 4/4] ref --- src/decoder.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index d4391fd..1581d00 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -199,7 +199,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { let mut current_src_id = !0; let mut current_src_line = !0; let mut current_src_col = !0; - let mut current_name = !0; + let mut current_name_id = !0; if nums.len() > 1 { running_src_id = (i64::from(running_src_id) + nums[1]) as u32; @@ -220,7 +220,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { if running_name_id >= names.len() as u32 { return Err(Error::BadNameReference(running_name_id)); } - current_name = running_name_id; + current_name_id = running_name_id; } } @@ -232,7 +232,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { src_line: current_src_line, src_col: current_src_col, src_id: current_src_id, - name_id: current_name, + name_id: current_name_id, is_range, }); }