Skip to content

Commit

Permalink
refactor: Simplify SourceMapGetter and SourceMapper (#821)
Browse files Browse the repository at this point in the history
Splitting out a simplification from
#819.
  • Loading branch information
bartlomieju authored Jul 12, 2024
1 parent 4aa0dfa commit b5218a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
5 changes: 2 additions & 3 deletions core/extension_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::ModuleCodeString;
use crate::OpDecl;
use crate::OpMetricsFactoryFn;
use crate::OpState;
use crate::SourceMapGetter;

/// Contribute to the `OpState` from each extension.
pub fn setup_op_state(op_state: &mut OpState, extensions: &mut [Extension]) {
Expand Down Expand Up @@ -242,7 +241,7 @@ impl<'a> IntoIterator for &'a mut LoadedSources {
fn load(
transpiler: Option<&ExtensionTranspiler>,
source: &ExtensionFileSource,
source_mapper: &mut SourceMapper<Rc<dyn SourceMapGetter>>,
source_mapper: &mut SourceMapper,
load_callback: &mut impl FnMut(&ExtensionFileSource),
) -> Result<ModuleCodeString, AnyError> {
load_callback(source);
Expand All @@ -263,7 +262,7 @@ fn load(
pub fn into_sources(
transpiler: Option<&ExtensionTranspiler>,
extensions: &[Extension],
source_mapper: &mut SourceMapper<Rc<dyn SourceMapGetter>>,
source_mapper: &mut SourceMapper,
mut load_callback: impl FnMut(&ExtensionFileSource),
) -> Result<LoadedSources, AnyError> {
let mut sources = LoadedSources::default();
Expand Down
5 changes: 2 additions & 3 deletions core/runtime/jsruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub type CompiledWasmModuleStore = CrossIsolateStore<v8::CompiledWasmModule>;
/// Internal state for JsRuntime which is stored in one of v8::Isolate's
/// embedder slots.
pub struct JsRuntimeState {
pub(crate) source_mapper: RefCell<SourceMapper<Rc<dyn SourceMapGetter>>>,
pub(crate) source_mapper: RefCell<SourceMapper>,
pub(crate) op_state: Rc<RefCell<OpState>>,
pub(crate) shared_array_buffer_store: Option<SharedArrayBufferStore>,
pub(crate) compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
Expand Down Expand Up @@ -672,8 +672,7 @@ impl JsRuntime {

// Load the sources and source maps
let mut files_loaded = Vec::with_capacity(128);
let mut source_mapper: SourceMapper<Rc<dyn SourceMapGetter>> =
SourceMapper::new(options.source_map_getter);
let mut source_mapper = SourceMapper::new(options.source_map_getter);
let mut sources = extension_set::into_sources(
options.extension_transpiler.as_deref(),
&extensions,
Expand Down
27 changes: 5 additions & 22 deletions core/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ pub trait SourceMapGetter {
) -> Option<String>;
}

impl<T> SourceMapGetter for Rc<T>
where
T: SourceMapGetter + ?Sized,
{
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
(**self).get_source_map(file_name)
}

fn get_source_line(
&self,
file_name: &str,
line_number: usize,
) -> Option<String> {
(**self).get_source_line(file_name, line_number)
}
}

pub enum SourceMapApplication {
/// No mapping was applied, the location is unchanged.
Unchanged,
Expand All @@ -54,18 +37,18 @@ pub enum SourceMapApplication {

pub type SourceMapData = Cow<'static, [u8]>;

pub struct SourceMapper<G: SourceMapGetter> {
pub struct SourceMapper {
maps: HashMap<String, Option<SourceMap>>,
source_lines: HashMap<(String, i64), Option<String>>,
getter: Option<G>,
getter: Option<Rc<dyn SourceMapGetter>>,
pub(crate) ext_source_maps: HashMap<String, SourceMapData>,
// This is not the right place for this, but it's the easiest way to make
// op_apply_source_map a fast op. This stashing should happen in #[op2].
pub(crate) stashed_file_name: Option<String>,
}

impl<G: SourceMapGetter> SourceMapper<G> {
pub fn new(getter: Option<G>) -> Self {
impl SourceMapper {
pub fn new(getter: Option<Rc<dyn SourceMapGetter>>) -> Self {
Self {
maps: Default::default(),
source_lines: Default::default(),
Expand All @@ -75,7 +58,7 @@ impl<G: SourceMapGetter> SourceMapper<G> {
}
}

pub fn has_user_sources(&self) -> bool {
pub(crate) fn has_user_sources(&self) -> bool {
self.getter.is_some()
}

Expand Down

0 comments on commit b5218a5

Please sign in to comment.