Repository: denoland/deno_core
Pull request #624 (2024-03-01 00:44:53)

refactor(core): StaticModuleLoader::default()
Split out from #620 


Patch:
diff --git a/core/modules/loaders.rs b/core/modules/loaders.rs
index 3f1df07..228d03b 100644
--- a/core/modules/loaders.rs
+++ b/core/modules/loaders.rs
@@ -330,6 +330,7 @@ impl ModuleLoader for FsModuleLoader {
 
 /// A module loader that you can pre-load a number of modules into and resolve from. Useful for testing and
 /// embedding situations where the filesystem and snapshot systems are not usable or a good fit.
+#[derive(Default)]
 pub struct StaticModuleLoader {
   map: HashMap<ModuleSpecifier, ModuleCodeString>,
 }


Test patch:
diff --git a/core/modules/tests.rs b/core/modules/tests.rs
index d7edb34..fa04287 100644
--- a/core/modules/tests.rs
+++ b/core/modules/tests.rs
@@ -442,7 +442,7 @@ fn test_lazy_loaded_esm() {
 
 #[test]
 fn test_json_module() {
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     ..Default::default()
@@ -552,7 +552,7 @@ fn test_validate_import_attributes_default() {
   // Verify that unless `validate_import_attributes_cb` is passed all import
   // are allowed and don't have any problem executing "invalid" code.
 
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     ..Default::default()
@@ -610,7 +610,7 @@ fn test_validate_import_attributes_callback() {
     }
   }
 
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     validate_import_attributes_cb: Some(Box::new(validate_import_attributes)),
@@ -679,7 +679,7 @@ fn test_validate_import_attributes_callback2() {
     scope.throw_exception(ex);
   }
 
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     validate_import_attributes_cb: Some(Box::new(validate_import_attrs)),
@@ -712,7 +712,7 @@ fn test_validate_import_attributes_callback2() {
 
 #[test]
 fn test_custom_module_type_default() {
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     ..Default::default()
@@ -778,7 +778,7 @@ fn test_custom_module_type_callback_synthetic() {
     Ok(CustomModuleEvaluationKind::Synthetic(value_global))
   }
 
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     custom_module_evaluation_cb: Some(Box::new(custom_eval_cb)),
@@ -873,7 +873,7 @@ export const foo = bytes;
     ))
   }
 
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     custom_module_evaluation_cb: Some(Box::new(custom_eval_cb)),
@@ -933,7 +933,7 @@ export const foo = bytes;
 
 #[tokio::test]
 async fn dyn_import_err() {
-  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::new([])));
+  let loader = Rc::new(TestingModuleLoader::new(StaticModuleLoader::default()));
   let mut runtime = JsRuntime::new(RuntimeOptions {
     module_loader: Some(loader.clone()),
     ..Default::default()
diff --git a/core/runtime/tests/jsrealm.rs b/core/runtime/tests/jsrealm.rs
index a942e96..bccf8d4 100644
--- a/core/runtime/tests/jsrealm.rs
+++ b/core/runtime/tests/jsrealm.rs
@@ -136,7 +136,7 @@ fn es_snapshot() {
 
     let runtime = JsRuntimeForSnapshot::new(RuntimeOptions {
       extensions: vec![module_snapshot::init_ops_and_esm()],
-      module_loader: Some(Rc::new(StaticModuleLoader::new([]))),
+      module_loader: Some(Rc::new(StaticModuleLoader::default())),
       ..Default::default()
     });
     runtime.snapshot()
diff --git a/core/runtime/tests/misc.rs b/core/runtime/tests/misc.rs
index d6e8a26..cdd5654 100644
--- a/core/runtime/tests/misc.rs
+++ b/core/runtime/tests/misc.rs
@@ -1039,7 +1039,7 @@ async fn tla_in_esm_extensions_panics() {
 
   // Panics
   let _runtime = JsRuntime::new(RuntimeOptions {
-    module_loader: Some(Rc::new(StaticModuleLoader::new([]))),
+    module_loader: Some(Rc::new(StaticModuleLoader::default())),
     extensions: vec![test_ext::init_ops_and_esm()],
     ..Default::default()
   });
@@ -1076,7 +1076,7 @@ async fn esm_extensions_throws() {
 
   // Panics
   let _runtime = JsRuntime::new(RuntimeOptions {
-    module_loader: Some(Rc::new(StaticModuleLoader::new([]))),
+    module_loader: Some(Rc::new(StaticModuleLoader::default())),
     extensions: vec![test_ext::init_ops_and_esm()],
     ..Default::default()
   });