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

ref(sourcemapcache): Implement AsSelf for SourceMapCache #742

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `PortablePdbDebugSession` now returns files referenced in the Portable PDB file. ([#729](https://github.com/getsentry/symbolic/pull/729))
- `PortablePdbDebugSession` now returns source files embedded in the Portable PDB file. ([#734](https://github.com/getsentry/symbolic/pull/734))
- Implement `symbolic_common::AsSelf` `for SourceMapCache` ([#742](https://github.com/getsentry/symbolic/pull/742))

**Breaking changes**:

Expand Down
26 changes: 3 additions & 23 deletions symbolic-cabi/src/sourcemapcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,18 @@ use std::slice;
use crate::utils::ForeignObject;
use crate::SymbolicStr;

use symbolic::common::AsSelf;
use symbolic::common::ByteView;
use symbolic::common::SelfCell;
use symbolic::sourcemapcache::SourceLocation;
use symbolic::sourcemapcache::{
ScopeLookupResult, SourceMapCache, SourceMapCacheWriter, SourcePosition,
};

struct Inner<'a> {
cache: SourceMapCache<'a>,
}

impl<'slf, 'a: 'slf> AsSelf<'slf> for Inner<'a> {
type Ref = Inner<'slf>;

fn as_self(&'slf self) -> &Self::Ref {
self
}
}

pub struct OwnedSourceMapCache<'a> {
inner: SelfCell<ByteView<'a>, Inner<'a>>,
}

/// Represents an sourcemapcache.
pub struct SymbolicSourceMapCache;

impl ForeignObject for SymbolicSourceMapCache {
type RustObject = OwnedSourceMapCache<'static>;
type RustObject = SelfCell<ByteView<'static>, SourceMapCache<'static>>;
}

#[repr(C)]
Expand Down Expand Up @@ -101,12 +84,11 @@ ffi_fn! {
let file = buf_writer.into_inner()?;

let byteview = ByteView::map_file(file)?;
let inner = SelfCell::try_new::<symbolic::sourcemapcache::SourceMapCacheError, _>(byteview, |data| {
let cache = SelfCell::try_new::<symbolic::sourcemapcache::SourceMapCacheError, _>(byteview, |data| {
let cache = SourceMapCache::parse(&*data)?;
Ok(Inner { cache })
Ok(cache)
})?;

let cache = OwnedSourceMapCache { inner };
Ok(SymbolicSourceMapCache::from_rust(cache))
}
}
Expand Down Expand Up @@ -181,9 +163,7 @@ ffi_fn! {
) -> Result<*mut SymbolicSmTokenMatch> {
// Sentry JS events are 1-indexed, where SourcePosition is using 0-indexed locations
let token_match = SymbolicSourceMapCache::as_rust(source_map)
.inner
.get()
.cache
.lookup(SourcePosition::new(line - 1, col - 1))
.map(|sp| make_token_match(sp, context_lines))
.unwrap_or_else(ptr::null_mut);
Expand Down
1 change: 1 addition & 0 deletions symbolic-sourcemapcache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ edition = "2021"
js-source-scopes = "0.3.1"
thiserror = "1.0.31"
sourcemap = "6.1.0"
symbolic-common = { version = "10.2.1", path = "../symbolic-common" }
tracing = "0.1.36"
watto = { version = "0.1.0", features = ["writer", "strings"] }
itertools = "0.10.3"
Expand Down
9 changes: 9 additions & 0 deletions symbolic-sourcemapcache/src/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use symbolic_common::AsSelf;
use watto::{align_to, Pod, StringTable};

use crate::{ScopeLookupResult, SourcePosition};
Expand Down Expand Up @@ -78,6 +79,14 @@ pub struct SourceMapCache<'data> {
string_bytes: &'data [u8],
}

impl<'slf, 'a: 'slf> AsSelf<'slf> for SourceMapCache<'a> {
type Ref = SourceMapCache<'slf>;

fn as_self(&'slf self) -> &Self::Ref {
self
}
}

impl<'data> std::fmt::Debug for SourceMapCache<'data> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SourceMapCache")
Expand Down