Skip to content

Commit

Permalink
refactor: Support specifying specifiers for extension files (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jul 2, 2023
1 parent 4ada523 commit 52ec884
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
34 changes: 34 additions & 0 deletions core/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ macro_rules! extension {
$(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )? ),+ $(,)? ] )?
$(, esm_entry_point = $esm_entry_point:literal )?
$(, esm = [ $( dir $dir_esm:literal , )? $( $esm:literal ),* $(,)? ] )?
$(, esm_with_specifiers = [ $( dir $dir_esm2:literal , )? $( ($esm_specifier:literal, $esm_file:literal) ),* $(,)? ] )?
$(, js = [ $( dir $dir_js:literal , )? $( $js:literal ),* $(,)? ] )?
$(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )?
$(, middleware = $middleware_fn:expr )?
Expand Down Expand Up @@ -218,6 +219,9 @@ macro_rules! extension {
$( ext.esm(
$crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm , )* )
); )?
$( ext.esm(
$crate::include_js_files_with_specifiers!( $name $( dir $dir_esm2 , )? $( ( $esm_specifier, $esm_file) , )* )
); )?
$(
ext.esm_entry_point($esm_entry_point);
)?
Expand Down Expand Up @@ -643,3 +647,33 @@ macro_rules! include_js_files {
]
};
}

#[cfg(not(feature = "include_js_files_for_snapshotting"))]
#[macro_export]
macro_rules! include_js_files_with_specifiers {
($name:ident dir $dir:literal, $( ( $specifier:literal , $file:literal ),)+) => {
vec![
$($crate::ExtensionFileSource {
specifier: $specifier,
code: $crate::ExtensionFileSourceCode::IncludedInBinary(
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $dir, "/", $file))
),
},)+
]
};
}

#[cfg(feature = "include_js_files_for_snapshotting")]
#[macro_export]
macro_rules! include_js_files_with_specifiers {
($name:ident dir $dir:literal, $( ( $specifier:literal , $file:literal ),)+) => {
vec![
$($crate::ExtensionFileSource {
specifier: $specifier,
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file)
),
},)+
]
};
}
10 changes: 3 additions & 7 deletions core/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,14 +888,10 @@ impl ModuleMap {
/// Clear the module map, meant to be used after initializing extensions.
/// Optionally pass a list of exceptions `(old_name, new_name)` representing
/// specifiers which will be renamed and preserved in the module map.
pub fn clear_module_map(
&mut self,
exceptions: impl Iterator<Item = (&'static str, &'static str)>,
) {
pub fn clear_module_map(&mut self, exceptions: &'static [&'static str]) {
let handles = exceptions
.map(|(old_name, new_name)| {
(self.get_handle_by_name(old_name).unwrap(), new_name)
})
.iter()
.map(|mod_name| (self.get_handle_by_name(mod_name).unwrap(), mod_name))
.collect::<Vec<_>>();
self.clear();
for (handle, new_name) in handles {
Expand Down
8 changes: 5 additions & 3 deletions core/runtime/jsruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub struct RuntimeOptions {
/// If provided, the module map will be cleared and left only with the specifiers
/// in this list, with the new names provided. If not provided, the module map is
/// left intact.
pub rename_modules: Option<Vec<(&'static str, &'static str)>>,
pub preserve_snapshotted_modules: Option<&'static [&'static str]>,

/// V8 snapshot that should be loaded on startup.
pub startup_snapshot: Option<Snapshot>,
Expand Down Expand Up @@ -711,11 +711,13 @@ impl JsRuntime {
.unwrap();

// If the user has requested that we rename modules
if let Some(rename_modules) = options.rename_modules {
if let Some(preserve_snapshotted_modules) =
options.preserve_snapshotted_modules
{
js_runtime
.module_map
.borrow_mut()
.clear_module_map(rename_modules.into_iter());
.clear_module_map(preserve_snapshotted_modules);
}

js_runtime
Expand Down

0 comments on commit 52ec884

Please sign in to comment.