Skip to content

Commit

Permalink
feat(core): Add support for modules in realms.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreubotella committed Oct 8, 2022
1 parent 7c3df66 commit eb0b5f4
Show file tree
Hide file tree
Showing 3 changed files with 1,066 additions and 829 deletions.
14 changes: 7 additions & 7 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub extern "C" fn host_import_module_dynamically_callback(
let resolver_handle = v8::Global::new(scope, resolver);
{
let state_rc = JsRuntime::state(scope);
let module_map_rc = JsRuntime::module_map(scope);
let module_map_rc = JsRealm::module_map_from_scope(scope);

debug!(
"dyn_import specifier {} referrer {} ",
Expand Down Expand Up @@ -308,7 +308,7 @@ pub extern "C" fn host_initialize_import_meta_object_callback(
) {
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
let scope = &mut unsafe { v8::CallbackScope::new(context) };
let module_map_rc = JsRuntime::module_map(scope);
let module_map_rc = JsRealm::module_map_from_scope(scope);
let module_map = module_map_rc.borrow();

let module_global = v8::Global::new(scope, module);
Expand Down Expand Up @@ -349,7 +349,7 @@ fn import_meta_resolve(
let url_prop = args.data().unwrap();
url_prop.to_rust_string_lossy(scope)
};
let module_map_rc = JsRuntime::module_map(scope);
let module_map_rc = JsRealm::module_map_from_scope(scope);
let loader = {
let module_map = module_map_rc.borrow();
module_map.loader.clone()
Expand Down Expand Up @@ -459,9 +459,9 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
};

if has_unhandled_rejection_handler {
let state_rc = JsRuntime::state(tc_scope);
let mut state = state_rc.borrow_mut();
if let Some(pending_mod_evaluate) = state.pending_mod_evaluate.as_mut() {
if let Some(pending_mod_evaluate) =
realm_state_rc.borrow_mut().pending_mod_evaluate.as_mut()
{
if !pending_mod_evaluate.has_evaluated {
pending_mod_evaluate
.handled_promise_rejections
Expand Down Expand Up @@ -557,7 +557,7 @@ pub fn module_resolve_callback<'s>(
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
let scope = &mut unsafe { v8::CallbackScope::new(context) };

let module_map_rc = JsRuntime::module_map(scope);
let module_map_rc = JsRealm::module_map_from_scope(scope);
let module_map = module_map_rc.borrow();

let referrer_global = v8::Global::new(scope, referrer);
Expand Down
40 changes: 26 additions & 14 deletions core/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::generic_error;
use crate::module_specifier::ModuleSpecifier;
use crate::resolve_import;
use crate::resolve_url;
use crate::JsRealm;
use crate::OpState;
use anyhow::Error;
use futures::future::FutureExt;
Expand Down Expand Up @@ -125,10 +126,7 @@ fn json_module_evaluation_steps<'a>(
// SAFETY: `CallbackScope` can be safely constructed from `Local<Context>`
let scope = &mut unsafe { v8::CallbackScope::new(context) };
let tc_scope = &mut v8::TryCatch::new(scope);
let module_map = tc_scope
.get_slot::<Rc<RefCell<ModuleMap>>>()
.unwrap()
.clone();
let module_map = JsRealm::module_map_from_scope(tc_scope);

let handle = v8::Global::<v8::Module>::new(tc_scope, module);
let value_handle = module_map
Expand Down Expand Up @@ -1392,7 +1390,7 @@ import "/a.js";
]
);

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc = runtime.global_realm().module_map(runtime.v8_isolate());
let modules = module_map_rc.borrow();

assert_eq!(
Expand Down Expand Up @@ -1504,7 +1502,7 @@ import "/a.js";

assert_eq!(DISPATCH_COUNT.load(Ordering::Relaxed), 0);

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc = runtime.global_realm().module_map(runtime.v8_isolate());

let (mod_a, mod_b) = {
let scope = &mut runtime.handle_scope();
Expand Down Expand Up @@ -1547,11 +1545,17 @@ import "/a.js";
(mod_a, mod_b)
};

runtime.instantiate_module(mod_b).unwrap();
runtime
.global_realm()
.instantiate_module(runtime.v8_isolate(), mod_b)
.unwrap();
assert_eq!(DISPATCH_COUNT.load(Ordering::Relaxed), 0);
assert_eq!(resolve_count.load(Ordering::SeqCst), 1);

runtime.instantiate_module(mod_a).unwrap();
runtime
.global_realm()
.instantiate_module(runtime.v8_isolate(), mod_a)
.unwrap();
assert_eq!(DISPATCH_COUNT.load(Ordering::Relaxed), 0);

let _ = runtime.mod_evaluate(mod_a);
Expand Down Expand Up @@ -1611,7 +1615,7 @@ import "/a.js";
)
.unwrap();

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc = runtime.global_realm().module_map(runtime.v8_isolate());

let (mod_a, mod_b) = {
let scope = &mut runtime.handle_scope();
Expand Down Expand Up @@ -1651,10 +1655,16 @@ import "/a.js";
(mod_a, mod_b)
};

runtime.instantiate_module(mod_b).unwrap();
runtime
.global_realm()
.instantiate_module(runtime.v8_isolate(), mod_b)
.unwrap();
assert_eq!(resolve_count.load(Ordering::SeqCst), 1);

runtime.instantiate_module(mod_a).unwrap();
runtime
.global_realm()
.instantiate_module(runtime.v8_isolate(), mod_a)
.unwrap();

let receiver = runtime.mod_evaluate(mod_a);
futures::executor::block_on(runtime.run_event_loop(false)).unwrap();
Expand Down Expand Up @@ -1946,7 +1956,8 @@ import "/a.js";
]
);

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc =
runtime.global_realm().module_map(runtime.v8_isolate());
let modules = module_map_rc.borrow();

assert_eq!(
Expand Down Expand Up @@ -2025,7 +2036,8 @@ import "/a.js";
]
);

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc =
runtime.global_realm().module_map(runtime.v8_isolate());
let modules = module_map_rc.borrow();

assert_eq!(
Expand Down Expand Up @@ -2180,7 +2192,7 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error();
vec!["file:///b.js", "file:///c.js", "file:///d.js"]
);

let module_map_rc = JsRuntime::module_map(runtime.v8_isolate());
let module_map_rc = runtime.global_realm().module_map(runtime.v8_isolate());
let modules = module_map_rc.borrow();

assert_eq!(
Expand Down
Loading

0 comments on commit eb0b5f4

Please sign in to comment.