Repository: denoland/deno_core
Pull request #465 (2024-01-23 02:36:06)

refactor: Remove RuntimeOptions::preserve_snapshotted_modules


Patch:
diff --git a/core/modules/loaders.rs b/core/modules/loaders.rs
index 97b93fc..6686fa3 100644
--- a/core/modules/loaders.rs
+++ b/core/modules/loaders.rs
@@ -419,10 +419,6 @@ impl<L: ModuleLoader> TestingModuleLoader<L> {
     }
   }
 
-  pub fn log(&self) -> Vec<ModuleSpecifier> {
-    self.log.borrow().clone()
-  }
-
   /// Retrieve the current module load event counts.
   pub fn counts(&self) -> ModuleLoadEventCounts {
     ModuleLoadEventCounts {
diff --git a/core/modules/map.rs b/core/modules/map.rs
index 82568a4..a9c62c8 100644
--- a/core/modules/map.rs
+++ b/core/modules/map.rs
@@ -190,16 +190,6 @@ impl ModuleMap {
     new
   }
 
-  fn get_handle_by_name(
-    &self,
-    name: impl AsRef<str>,
-  ) -> Option<v8::Global<v8::Module>> {
-    let id = self
-      .get_id(name.as_ref(), RequestedModuleType::None)
-      .or_else(|| self.get_id(name.as_ref(), RequestedModuleType::Json))?;
-    self.get_handle(id)
-  }
-
   /// Get module id, following all aliases in case of module specifier
   /// that had been redirected.
   pub(crate) fn get_id(
@@ -753,21 +743,6 @@ impl ModuleMap {
     None
   }
 
-  pub(crate) fn inject_handle(
-    &self,
-    name: ModuleName,
-    module_type: ModuleType,
-    handle: v8::Global<v8::Module>,
-  ) {
-    self.data.borrow_mut().create_module_info(
-      name,
-      module_type,
-      handle,
-      false,
-      vec![],
-    );
-  }
-
   pub(crate) fn get_requested_modules(
     &self,
     id: ModuleId,
@@ -1372,24 +1347,6 @@ impl ModuleMap {
     Ok(v8::Global::new(scope, module_namespace))
   }
 
-  /// 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(&self, exceptions: &'static [&'static str]) {
-    let handles = exceptions
-      .iter()
-      .map(|mod_name| (self.get_handle_by_name(mod_name).unwrap(), mod_name))
-      .collect::<Vec<_>>();
-    *self.data.borrow_mut() = ModuleMapData::default();
-    for (handle, new_name) in handles {
-      self.inject_handle(
-        ModuleName::from_static(new_name),
-        ModuleType::JavaScript,
-        handle,
-      )
-    }
-  }
-
   fn get_stalled_top_level_await_message_for_module(
     &self,
     scope: &mut v8::HandleScope,
diff --git a/core/runtime/jsruntime.rs b/core/runtime/jsruntime.rs
index a842efd..2726f3e 100644
--- a/core/runtime/jsruntime.rs
+++ b/core/runtime/jsruntime.rs
@@ -459,10 +459,6 @@ pub struct RuntimeOptions {
   /// JavaScript sources in the extensions.
   pub extensions: Vec<Extension>,
 
-  /// If provided, the module map will be cleared and left only with the specifiers
-  /// in this list. If not provided, the module map is left intact.
-  pub preserve_snapshotted_modules: Option<&'static [&'static str]>,
-
   /// V8 snapshot that should be loaded on startup.
   pub startup_snapshot: Option<Snapshot>,
 
@@ -948,14 +944,6 @@ impl JsRuntime {
       .init_extension_js()
       .expect("Failed to evaluate extension JS");
 
-    // If the embedder has requested to clear the module map resulting from
-    // extensions, possibly with exceptions.
-    if let Some(preserve_snapshotted_modules) =
-      options.preserve_snapshotted_modules
-    {
-      module_map.clear_module_map(preserve_snapshotted_modules);
-    }
-
     js_runtime
   }
 


Test patch:
diff --git a/core/runtime/tests/snapshot.rs b/core/runtime/tests/snapshot.rs
index 4e360a1..32676eb 100644
--- a/core/runtime/tests/snapshot.rs
+++ b/core/runtime/tests/snapshot.rs
@@ -2,13 +2,10 @@
 use crate::extensions::Op;
 use crate::modules::ModuleInfo;
 use crate::modules::RequestedModuleType;
-use crate::modules::TestingModuleLoader;
 use crate::runtime::NO_OF_BUILTIN_MODULES;
 use crate::*;
 use anyhow::Error;
 use std::borrow::Cow;
-use std::rc::Rc;
-use url::Url;
 
 #[test]
 fn will_snapshot() {
@@ -330,112 +327,3 @@ pub(crate) fn es_snapshot_without_runtime_module_loader() {
     r#"Uncaught (in promise) TypeError: Importing ext: modules is only allowed from ext: and node: modules. Tried to import ext:module_snapshot/test2.js from (no referrer)"#
   );
 }
-
-pub(crate) fn generic_preserve_snapshotted_modules_test(test_snapshot: bool) {
-  let extension = Extension {
-    name: "module_snapshot",
-    esm_files: Cow::Borrowed(&[
-      ExtensionFileSource {
-        specifier: "test:preserved",
-        code: ExtensionFileSourceCode::IncludedInBinary(
-          "export const TEST = 'foo';",
-        ),
-      },
-      ExtensionFileSource {
-        specifier: "test:not-preserved",
-        code: ExtensionFileSourceCode::IncludedInBinary(
-          "import 'test:preserved'; export const TEST = 'bar';",
-        ),
-      },
-    ]),
-    esm_entry_point: Some("test:not-preserved"),
-    ..Default::default()
-  };
-
-  let loader = Rc::new(TestingModuleLoader::new(NoopModuleLoader));
-
-  let mut runtime = if test_snapshot {
-    let snapshot_runtime = JsRuntimeForSnapshot::new(RuntimeOptions {
-      extensions: vec![extension],
-      ..Default::default()
-    });
-    let startup_data = snapshot_runtime.snapshot();
-
-    JsRuntime::new(RuntimeOptions {
-      module_loader: Some(loader.clone()),
-      startup_snapshot: Some(Snapshot::JustCreated(startup_data)),
-      preserve_snapshotted_modules: Some(&["test:preserved"]),
-      ..Default::default()
-    })
-  } else {
-    JsRuntime::new(RuntimeOptions {
-      module_loader: Some(loader.clone()),
-      extensions: vec![extension],
-      preserve_snapshotted_modules: Some(&["test:preserved"]),
-      ..Default::default()
-    })
-  };
-
-  let realm = runtime.main_realm();
-
-  // We can't import "test:not-preserved"
-  {
-    assert_eq!(loader.log(), vec![]);
-    let dyn_import_promise = realm
-      .execute_script_static(
-        runtime.v8_isolate(),
-        "",
-        "import('test:not-preserved')",
-      )
-      .unwrap();
-    #[allow(deprecated)]
-    let dyn_import_result =
-      futures::executor::block_on(runtime.resolve_value(dyn_import_promise));
-    assert!(dyn_import_result.is_err());
-    assert_eq!(
-      dyn_import_result.err().unwrap().to_string().as_str(),
-      "Uncaught (in promise) TypeError: Module loading is not supported; attempted to load: \"test:not-preserved\" from \"(no referrer)\""
-    );
-    // Ensure that we tried to load `test:not-preserved`
-    assert_eq!(
-      loader.log(),
-      vec![Url::parse("test:not-preserved").unwrap()]
-    );
-  }
-
-  // But we can import "test:preserved"
-  {
-    let dyn_import_promise = realm
-      .execute_script_static(
-        runtime.v8_isolate(),
-        "",
-        "import('test:preserved').then(module => module.TEST)",
-      )
-      .unwrap();
-    #[allow(deprecated)]
-    let dyn_import_result =
-      futures::executor::block_on(runtime.resolve_value(dyn_import_promise))
-        .unwrap();
-    let scope = &mut realm.handle_scope(runtime.v8_isolate());
-    assert!(dyn_import_result.open(scope).is_string());
-    assert_eq!(
-      dyn_import_result
-        .open(scope)
-        .to_rust_string_lossy(scope)
-        .as_str(),
-      "foo"
-    );
-  }
-}
-
-#[test]
-fn preserve_snapshotted_modules() {
-  generic_preserve_snapshotted_modules_test(true)
-}
-
-/// Test that `RuntimeOptions::preserve_snapshotted_modules` also works without
-/// a snapshot.
-#[test]
-fn non_snapshot_preserve_snapshotted_modules() {
-  generic_preserve_snapshotted_modules_test(false)
-}