summaryrefslogtreecommitdiff
path: root/core/runtime.rs
diff options
context:
space:
mode:
authorAndreu Botella <andreu@andreubotella.com>2022-06-25 20:56:29 +0200
committerGitHub <noreply@github.com>2022-06-25 20:56:29 +0200
commit38505db39137f33bfdb942658ea892a617ac0980 (patch)
treedd8a03dc9e8bcb436f78f8e9a0c91f9be958e083 /core/runtime.rs
parent18c9a7ad641302a9f5e0ccb07da732890f8e0505 (diff)
fix(modules): Immediately resolve follow-up dyn imports to a dyn imported module (#14958)
When a dynamically imported module gets resolved, any code that comes after an await import() to that module will continue running. However, if that is the last code in the evaluation of another dynamically imported module, that second module will not resolve until the next iteration of the event loop, even though it does not depend on the event loop at all. When the event loop is being blocked by a long-running operation, such as a long-running timer, or by an async op that might never end, such as with workers or BroadcastChannels, that will result in the second dynamically imported module not being resolved for a while, or ever. This change fixes this by running the dynamic module loading steps in a loop until no more dynamic modules can be resolved.
Diffstat (limited to 'core/runtime.rs')
-rw-r--r--core/runtime.rs31
1 files changed, 25 insertions, 6 deletions
diff --git a/core/runtime.rs b/core/runtime.rs
index 97c822848..f82f06207 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -886,13 +886,28 @@ impl JsRuntime {
// Dynamic module loading - ie. modules loaded using "import()"
{
- let poll_imports = self.prepare_dyn_imports(cx)?;
- assert!(poll_imports.is_ready());
+ // Run in a loop so that dynamic imports that only depend on another
+ // dynamic import can be resolved in this event loop iteration.
+ //
+ // For example, a dynamically imported module like the following can be
+ // immediately resolved after `dependency.ts` is fully evaluated, but it
+ // wouldn't if not for this loop.
+ //
+ // await delay(1000);
+ // await import("./dependency.ts");
+ // console.log("test")
+ //
+ loop {
+ let poll_imports = self.prepare_dyn_imports(cx)?;
+ assert!(poll_imports.is_ready());
- let poll_imports = self.poll_dyn_imports(cx)?;
- assert!(poll_imports.is_ready());
+ let poll_imports = self.poll_dyn_imports(cx)?;
+ assert!(poll_imports.is_ready());
- self.evaluate_dyn_imports();
+ if !self.evaluate_dyn_imports() {
+ break;
+ }
+ }
self.check_promise_exceptions()?;
}
@@ -1505,7 +1520,9 @@ impl JsRuntime {
}
}
- fn evaluate_dyn_imports(&mut self) {
+ // Returns true if some dynamic import was resolved.
+ fn evaluate_dyn_imports(&mut self) -> bool {
+ let mut resolved_any = false;
let state_rc = Self::state(self.v8_isolate());
let mut still_pending = vec![];
let pending =
@@ -1536,6 +1553,7 @@ impl JsRuntime {
};
if let Some(result) = maybe_result {
+ resolved_any = true;
match result {
Ok((dyn_import_id, module_id)) => {
self.dynamic_import_resolve(dyn_import_id, module_id);
@@ -1547,6 +1565,7 @@ impl JsRuntime {
}
}
state_rc.borrow_mut().pending_dyn_mod_evaluate = still_pending;
+ resolved_any
}
/// Asynchronously load specified module and all of its dependencies.