From f1f6a53f127a767d37c5061a56eeb319427b50aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 13 Jul 2024 00:58:19 +0200 Subject: [PATCH] refactor: Simplify SourceMapGetter and SourceMapper --- core/extension_set.rs | 5 ++--- core/runtime/jsruntime.rs | 5 ++--- core/source_map.rs | 27 +++++---------------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/core/extension_set.rs b/core/extension_set.rs index 938a771ea..fafa6a6a6 100644 --- a/core/extension_set.rs +++ b/core/extension_set.rs @@ -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]) { @@ -242,7 +241,7 @@ impl<'a> IntoIterator for &'a mut LoadedSources { fn load( transpiler: Option<&ExtensionTranspiler>, source: &ExtensionFileSource, - source_mapper: &mut SourceMapper>, + source_mapper: &mut SourceMapper, load_callback: &mut impl FnMut(&ExtensionFileSource), ) -> Result { load_callback(source); @@ -263,7 +262,7 @@ fn load( pub fn into_sources( transpiler: Option<&ExtensionTranspiler>, extensions: &[Extension], - source_mapper: &mut SourceMapper>, + source_mapper: &mut SourceMapper, mut load_callback: impl FnMut(&ExtensionFileSource), ) -> Result { let mut sources = LoadedSources::default(); diff --git a/core/runtime/jsruntime.rs b/core/runtime/jsruntime.rs index ce658acae..50e856341 100644 --- a/core/runtime/jsruntime.rs +++ b/core/runtime/jsruntime.rs @@ -407,7 +407,7 @@ pub type CompiledWasmModuleStore = CrossIsolateStore; /// Internal state for JsRuntime which is stored in one of v8::Isolate's /// embedder slots. pub struct JsRuntimeState { - pub(crate) source_mapper: RefCell>>, + pub(crate) source_mapper: RefCell, pub(crate) op_state: Rc>, pub(crate) shared_array_buffer_store: Option, pub(crate) compiled_wasm_module_store: Option, @@ -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> = - 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, diff --git a/core/source_map.rs b/core/source_map.rs index 396e21370..97e8b4fdc 100644 --- a/core/source_map.rs +++ b/core/source_map.rs @@ -19,23 +19,6 @@ pub trait SourceMapGetter { ) -> Option; } -impl SourceMapGetter for Rc -where - T: SourceMapGetter + ?Sized, -{ - fn get_source_map(&self, file_name: &str) -> Option> { - (**self).get_source_map(file_name) - } - - fn get_source_line( - &self, - file_name: &str, - line_number: usize, - ) -> Option { - (**self).get_source_line(file_name, line_number) - } -} - pub enum SourceMapApplication { /// No mapping was applied, the location is unchanged. Unchanged, @@ -54,18 +37,18 @@ pub enum SourceMapApplication { pub type SourceMapData = Cow<'static, [u8]>; -pub struct SourceMapper { +pub struct SourceMapper { maps: HashMap>, source_lines: HashMap<(String, i64), Option>, - getter: Option, + getter: Option>, pub(crate) ext_source_maps: HashMap, // 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, } -impl SourceMapper { - pub fn new(getter: Option) -> Self { +impl SourceMapper { + pub fn new(getter: Option>) -> Self { Self { maps: Default::default(), source_lines: Default::default(), @@ -75,7 +58,7 @@ impl SourceMapper { } } - pub fn has_user_sources(&self) -> bool { + pub(crate) fn has_user_sources(&self) -> bool { self.getter.is_some() }