From a632e56f73995e9eb14bb99027b4595e775f9762 Mon Sep 17 00:00:00 2001 From: snek Date: Wed, 28 Aug 2024 18:48:51 -0700 Subject: [PATCH] fix: preserve cped in dynamic imports --- core/modules/map.rs | 30 +++++++++++++++++++++++++----- core/runtime/bindings.rs | 4 ++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/core/modules/map.rs b/core/modules/map.rs index be2d70ef7..8be528df1 100644 --- a/core/modules/map.rs +++ b/core/modules/map.rs @@ -123,8 +123,12 @@ pub(crate) struct ModuleMap { pub(crate) import_meta_resolve_cb: ImportMetaResolveCallback, exception_state: Rc, - dynamic_import_map: - RefCell>>, + dynamic_import_map: RefCell< + HashMap< + ModuleLoadId, + (v8::Global, v8::Global), + >, + >, preparing_dynamic_imports: RefCell>>>, preparing_dynamic_imports_pending: Cell, @@ -941,6 +945,7 @@ impl ModuleMap { referrer: &str, requested_module_type: RequestedModuleType, resolver_handle: v8::Global, + cped_handle: v8::Global, ) { let load = RecursiveModuleLoad::dynamic_import( specifier, @@ -952,7 +957,7 @@ impl ModuleMap { self .dynamic_import_map .borrow_mut() - .insert(load.id, resolver_handle); + .insert(load.id, (resolver_handle, cped_handle)); let resolve_result = self.resolve(specifier, referrer, ResolutionKind::DynamicImport); @@ -1223,6 +1228,19 @@ impl ModuleMap { // https://github.com/denoland/deno/issues/4908 // https://v8.dev/features/top-level-await#module-execution-order let tc_scope = &mut v8::TryCatch::new(scope); + + { + let cped = self + .dynamic_import_map + .borrow() + .get(&load_id) + .unwrap() + .1 + .clone(); + let cped = v8::Local::new(tc_scope, cped); + tc_scope.set_continuation_preserved_embedder_data(cped); + } + let module = v8::Local::new(tc_scope, &module_handle); let maybe_value = module.evaluate(tc_scope); @@ -1344,7 +1362,8 @@ impl ModuleMap { .dynamic_import_map .borrow_mut() .remove(&id) - .expect("Invalid dynamic import id"); + .expect("Invalid dynamic import id") + .0; let resolver = resolver_handle.open(scope); let exception = v8::Local::new(scope, exception); @@ -1362,7 +1381,8 @@ impl ModuleMap { .dynamic_import_map .borrow_mut() .remove(&id) - .expect("Invalid dynamic import id"); + .expect("Invalid dynamic import id") + .0; let resolver = resolver_handle.open(scope); let module = self diff --git a/core/runtime/bindings.rs b/core/runtime/bindings.rs index 2d85094c1..eb9ae8889 100644 --- a/core/runtime/bindings.rs +++ b/core/runtime/bindings.rs @@ -426,6 +426,8 @@ pub fn host_import_module_dynamically_callback<'s>( specifier: v8::Local<'s, v8::String>, import_attributes: v8::Local<'s, v8::FixedArray>, ) -> Option> { + let cped = scope.get_continuation_preserved_embedder_data(); + // NOTE(bartlomieju): will crash for non-UTF-8 specifier let specifier_str = specifier .to_string(scope) @@ -465,6 +467,7 @@ pub fn host_import_module_dynamically_callback<'s>( get_requested_module_type_from_attributes(&assertions); let resolver_handle = v8::Global::new(scope, resolver); + let cped_handle = v8::Global::new(scope, cped); { let state = JsRuntime::state_from(scope); let module_map_rc = JsRealm::module_map_from(scope); @@ -475,6 +478,7 @@ pub fn host_import_module_dynamically_callback<'s>( &referrer_name_str, requested_module_type, resolver_handle, + cped_handle, ); state.notify_new_dynamic_import(); }