Skip to content

Commit

Permalink
fix: preserve cped in dynamic imports (#888)
Browse files Browse the repository at this point in the history
Fix for denoland/deno#25275

This code is like hacks on top of hacks, but basically save & restore
cped in the dynamic import flow.
  • Loading branch information
devsnek committed Sep 10, 2024
1 parent 3c7c0ff commit 688c93f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
39 changes: 31 additions & 8 deletions core/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ struct DynImportModEvaluate {
module: v8::Global<v8::Module>,
}

struct DynImportState {
resolver: v8::Global<v8::PromiseResolver>,
cped: v8::Global<v8::Value>,
}

/// A collection of JS modules.
pub(crate) struct ModuleMap {
// Handling of futures for loading module sources
Expand All @@ -123,8 +128,7 @@ pub(crate) struct ModuleMap {
pub(crate) import_meta_resolve_cb: ImportMetaResolveCallback,

exception_state: Rc<ExceptionState>,
dynamic_import_map:
RefCell<HashMap<ModuleLoadId, v8::Global<v8::PromiseResolver>>>,
dynamic_import_map: RefCell<HashMap<ModuleLoadId, DynImportState>>,
preparing_dynamic_imports:
RefCell<FuturesUnordered<Pin<Box<PrepareLoadFuture>>>>,
preparing_dynamic_imports_pending: Cell<bool>,
Expand Down Expand Up @@ -941,6 +945,7 @@ impl ModuleMap {
referrer: &str,
requested_module_type: RequestedModuleType,
resolver_handle: v8::Global<v8::PromiseResolver>,
cped_handle: v8::Global<v8::Value>,
) {
let load = RecursiveModuleLoad::dynamic_import(
specifier,
Expand All @@ -949,10 +954,13 @@ impl ModuleMap {
self.clone(),
);

self
.dynamic_import_map
.borrow_mut()
.insert(load.id, resolver_handle);
self.dynamic_import_map.borrow_mut().insert(
load.id,
DynImportState {
resolver: resolver_handle,
cped: cped_handle,
},
);

let resolve_result =
self.resolve(specifier, referrer, ResolutionKind::DynamicImport);
Expand Down Expand Up @@ -1230,6 +1238,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()
.cped
.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);

Expand Down Expand Up @@ -1351,7 +1372,8 @@ impl ModuleMap {
.dynamic_import_map
.borrow_mut()
.remove(&id)
.expect("Invalid dynamic import id");
.expect("Invalid dynamic import id")
.resolver;
let resolver = resolver_handle.open(scope);

let exception = v8::Local::new(scope, exception);
Expand All @@ -1369,7 +1391,8 @@ impl ModuleMap {
.dynamic_import_map
.borrow_mut()
.remove(&id)
.expect("Invalid dynamic import id");
.expect("Invalid dynamic import id")
.resolver;
let resolver = resolver_handle.open(scope);

let module = self
Expand Down
4 changes: 4 additions & 0 deletions core/runtime/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Local<'s, v8::Promise>> {
let cped = scope.get_continuation_preserved_embedder_data();

// NOTE(bartlomieju): will crash for non-UTF-8 specifier
let specifier_str = specifier
.to_string(scope)
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down

0 comments on commit 688c93f

Please sign in to comment.