Repository: denoland/deno_core
Pull request #42 (2023-07-17 23:57:08)

refactor: Add CreateRealmOptions
Extracted from https://github.com/denoland/deno_core/pull/41.

Current this struct is not used.


Patch:
diff --git a/core/lib.rs b/core/lib.rs
index ce05894..8f276bc 100644
--- a/core/lib.rs
+++ b/core/lib.rs
@@ -111,6 +111,7 @@ pub use crate::resources::Resource;
 pub use crate::resources::ResourceId;
 pub use crate::resources::ResourceTable;
 pub use crate::runtime::CompiledWasmModuleStore;
+pub use crate::runtime::CreateRealmOptions;
 pub use crate::runtime::CrossIsolateStore;
 pub use crate::runtime::JsRealm;
 pub use crate::runtime::JsRuntime;
diff --git a/core/runtime/jsrealm.rs b/core/runtime/jsrealm.rs
index 496a113..2b290a9 100644
--- a/core/runtime/jsrealm.rs
+++ b/core/runtime/jsrealm.rs
@@ -83,10 +83,11 @@ pub(crate) struct ContextState {
 /// ```
 /// use deno_core::JsRuntime;
 /// use deno_core::RuntimeOptions;
+/// use deno_core::CreateRealmOptions;
 ///
 /// let mut runtime = JsRuntime::new(RuntimeOptions::default());
 /// let new_realm = runtime
-///         .create_realm()
+///         .create_realm(CreateRealmOptions::default())
 ///         .expect("Handle the error properly");
 /// let source_code = "var a = 0; a + 1";
 /// let result = new_realm
diff --git a/core/runtime/jsruntime.rs b/core/runtime/jsruntime.rs
index 41c917f..63dc82b 100644
--- a/core/runtime/jsruntime.rs
+++ b/core/runtime/jsruntime.rs
@@ -451,6 +451,16 @@ pub struct RuntimeSnapshotOptions {
   pub snapshot_module_load_cb: Option<ExtModuleLoaderCb>,
 }
 
+#[derive(Default)]
+pub struct CreateRealmOptions {
+  /// Implementation of `ModuleLoader` which will be
+  /// called when V8 requests to load ES modules in the realm.
+  ///
+  /// If not provided, there will be an error if code being
+  /// executed tries to load modules from the realm.
+  pub module_loader: Option<Rc<dyn ModuleLoader>>,
+}
+
 impl JsRuntime {
   /// Only constructor, configuration is done through `options`.
   pub fn new(mut options: RuntimeOptions) -> JsRuntime {
@@ -800,7 +810,12 @@ impl JsRuntime {
   /// pre-initialized with all of the extensions that were passed in
   /// [`RuntimeOptions::extensions`] when the [`JsRuntime`] was
   /// constructed.
-  pub fn create_realm(&mut self) -> Result<JsRealm, Error> {
+  // TODO(bartlomieju): options is not used right now - will be used in a follow
+  // up PR.
+  pub fn create_realm(
+    &mut self,
+    _options: CreateRealmOptions,
+  ) -> Result<JsRealm, Error> {
     let realm = {
       let context_state = Rc::new(RefCell::new(ContextState::default()));
       let op_ctxs: Box<[OpCtx]> = self
@@ -847,6 +862,7 @@ impl JsRuntime {
         self.init_mode,
       );
       context.set_slot(scope, context_state.clone());
+
       let realm = JsRealmInner::new(
         context_state,
         v8::Global::new(scope, context),
diff --git a/core/runtime/mod.rs b/core/runtime/mod.rs
index aa546b8..d30ee67 100644
--- a/core/runtime/mod.rs
+++ b/core/runtime/mod.rs
@@ -15,6 +15,7 @@ pub const V8_WRAPPER_OBJECT_INDEX: i32 = 1;
 pub(crate) use jsrealm::ContextState;
 pub use jsrealm::JsRealm;
 pub use jsruntime::CompiledWasmModuleStore;
+pub use jsruntime::CreateRealmOptions;
 pub use jsruntime::CrossIsolateStore;
 pub(crate) use jsruntime::InitMode;
 pub use jsruntime::JsRuntime;


Test patch:
diff --git a/core/runtime/tests/jsrealm.rs b/core/runtime/tests/jsrealm.rs
index 8e9d8c5..bd4c28f 100644
--- a/core/runtime/tests/jsrealm.rs
+++ b/core/runtime/tests/jsrealm.rs
@@ -17,8 +17,8 @@ use std::task::Poll;
 async fn test_set_promise_reject_callback_realms() {
   let mut runtime = JsRuntime::new(RuntimeOptions::default());
   let main_realm = runtime.main_realm();
-  let realm1 = runtime.create_realm().unwrap();
-  let realm2 = runtime.create_realm().unwrap();
+  let realm1 = runtime.create_realm(Default::default()).unwrap();
+  let realm2 = runtime.create_realm(Default::default()).unwrap();
 
   let realm_expectations = &[
     (&main_realm, "main_realm", 42),
@@ -70,7 +70,7 @@ fn js_realm_simple() {
     v8::Global::new(scope, local_global)
   };
 
-  let realm = runtime.create_realm().unwrap();
+  let realm = runtime.create_realm(Default::default()).unwrap();
   assert_ne!(realm.context(), &main_context);
   assert_ne!(realm.global_object(runtime.v8_isolate()), main_global);
 
@@ -93,7 +93,7 @@ fn js_realm_init() {
     extensions: vec![test_ext::init_ops()],
     ..Default::default()
   });
-  let realm = runtime.create_realm().unwrap();
+  let realm = runtime.create_realm(Default::default()).unwrap();
   let ret = realm
     .execute_script_static(runtime.v8_isolate(), "", "Deno.core.ops.op_test()")
     .unwrap();
@@ -122,7 +122,7 @@ fn js_realm_init_snapshot() {
     extensions: vec![test_ext::init_ops()],
     ..Default::default()
   });
-  let realm = runtime.create_realm().unwrap();
+  let realm = runtime.create_realm(Default::default()).unwrap();
   let ret = realm
     .execute_script_static(runtime.v8_isolate(), "", "Deno.core.ops.op_test()")
     .unwrap();
@@ -155,7 +155,7 @@ fn js_realm_sync_ops() {
     }),
     ..Default::default()
   });
-  let new_realm = runtime.create_realm().unwrap();
+  let new_realm = runtime.create_realm(Default::default()).unwrap();
 
   // Test in both realms
   for realm in [runtime.main_realm(), new_realm].into_iter() {
@@ -205,7 +205,7 @@ async fn js_realm_async_ops() {
   });
 
   let main_realm = runtime.main_realm();
-  let new_realm = runtime.create_realm().unwrap();
+  let new_realm = runtime.create_realm(Default::default()).unwrap();
 
   let mut rets = vec![];
 
@@ -286,7 +286,7 @@ async fn js_realm_gc() {
     .put(opstate_drop_detect.clone());
   assert_eq!(Rc::strong_count(&opstate_drop_detect), 2);
 
-  let other_realm = runtime.create_realm().unwrap();
+  let other_realm = runtime.create_realm(Default::default()).unwrap();
   other_realm
     .execute_script(
       runtime.v8_isolate(),
@@ -327,7 +327,7 @@ async fn js_realm_ref_unref_ops() {
 
   poll_fn(move |cx| {
     let main_realm = runtime.main_realm();
-    let other_realm = runtime.create_realm().unwrap();
+    let other_realm = runtime.create_realm(Default::default()).unwrap();
 
     main_realm
       .execute_script_static(