Skip to content

Commit a3cfbf2

Browse files
committed
feat(core): Add support for modules in realms
This change makes realms other than the main one support modules by having a module map associated to each realm, rather than one per module. As part of this, it also: - Adds an argument to `JsRuntime::create_realm` to set the realm's module loader. This allows different realms to have different module loading strategies (different import maps, for example). - Moves all of the methods of `JsRuntime` related to module loading into `JsRealm`. To minimize changing unrelated code, the public and crate-private methods in `JsRuntime` are kept, with their implementation replaced by a call to the corresponding method of the main realm's `JsRealm`. - Removes the `module_map` argument to the `EventLoopPendingState` constructor, instead accessing each realm's corresponding module map as part of the existing iteration. - Changes the parts of `JsRuntime::poll_event_loop` that deal with module evaluation and detecting stalled top-level awaits to support multiple module maps and multiple top-level module evaluations at the same time. - Moves `pending_mod_evaluate` and `pending_dyn_mod_evaluate` from `JsRuntimeState` to `ContextState`. Towards #13239.
1 parent 8c4f37d commit a3cfbf2

File tree

4 files changed

+1399
-745
lines changed

4 files changed

+1399
-745
lines changed

core/bindings.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn host_import_module_dynamically_callback<'s>(
289289
let resolver_handle = v8::Global::new(scope, resolver);
290290
{
291291
let state_rc = JsRuntime::state(scope);
292-
let module_map_rc = JsRuntime::module_map(scope);
292+
let module_map_rc = JsRealm::module_map_from_scope(scope);
293293

294294
debug!(
295295
"dyn_import specifier {} referrer {} ",
@@ -336,7 +336,7 @@ pub extern "C" fn host_initialize_import_meta_object_callback(
336336
) {
337337
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
338338
let scope = &mut unsafe { v8::CallbackScope::new(context) };
339-
let module_map_rc = JsRuntime::module_map(scope);
339+
let module_map_rc = JsRealm::module_map_from_scope(scope);
340340
let module_map = module_map_rc.borrow();
341341

342342
let module_global = v8::Global::new(scope, module);
@@ -379,7 +379,7 @@ fn import_meta_resolve(
379379
let url_prop = args.data();
380380
url_prop.to_rust_string_lossy(scope)
381381
};
382-
let module_map_rc = JsRuntime::module_map(scope);
382+
let module_map_rc = JsRealm::module_map_from_scope(scope);
383383
let (loader, snapshot_loaded_and_not_snapshotting) = {
384384
let module_map = module_map_rc.borrow();
385385
(
@@ -510,7 +510,7 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
510510
};
511511

512512
if has_unhandled_rejection_handler {
513-
let state_rc = JsRuntime::state(tc_scope);
513+
let state_rc = JsRealm::state_from_scope(tc_scope);
514514
let mut state = state_rc.borrow_mut();
515515
if let Some(pending_mod_evaluate) = state.pending_mod_evaluate.as_mut() {
516516
if !pending_mod_evaluate.has_evaluated {
@@ -608,7 +608,7 @@ pub fn module_resolve_callback<'s>(
608608
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
609609
let scope = &mut unsafe { v8::CallbackScope::new(context) };
610610

611-
let module_map_rc = JsRuntime::module_map(scope);
611+
let module_map_rc = JsRealm::module_map_from_scope(scope);
612612
let module_map = module_map_rc.borrow();
613613

614614
let referrer_global = v8::Global::new(scope, referrer);

core/modules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::module_specifier::ModuleSpecifier;
99
use crate::resolve_import;
1010
use crate::resolve_url;
1111
use crate::snapshot_util::SnapshottedData;
12-
use crate::JsRuntime;
12+
use crate::JsRealm;
1313
use crate::OpState;
1414
use anyhow::Error;
1515
use core::panic;
@@ -135,7 +135,7 @@ fn json_module_evaluation_steps<'a>(
135135
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
136136
let scope = &mut unsafe { v8::CallbackScope::new(context) };
137137
let tc_scope = &mut v8::TryCatch::new(scope);
138-
let module_map = JsRuntime::module_map(tc_scope);
138+
let module_map = JsRealm::module_map_from_scope(tc_scope);
139139

140140
let handle = v8::Global::<v8::Module>::new(tc_scope, module);
141141
let value_handle = module_map

0 commit comments

Comments
 (0)