summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/modules.rs14
-rw-r--r--core/runtime.rs36
2 files changed, 21 insertions, 29 deletions
diff --git a/core/modules.rs b/core/modules.rs
index 546f2464f..8248eb32d 100644
--- a/core/modules.rs
+++ b/core/modules.rs
@@ -718,8 +718,8 @@ mod tests {
let spec = ModuleSpecifier::resolve_url("file:///a.js").unwrap();
let a_id_fut = runtime.load_module(&spec, None);
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");
-
- futures::executor::block_on(runtime.mod_evaluate(a_id)).unwrap();
+ runtime.mod_evaluate(a_id);
+ futures::executor::block_on(runtime.run_event_loop()).unwrap();
let l = loads.lock().unwrap();
assert_eq!(
l.to_vec(),
@@ -786,7 +786,8 @@ mod tests {
let result = runtime.load_module(&spec, None).await;
assert!(result.is_ok());
let circular1_id = result.unwrap();
- runtime.mod_evaluate(circular1_id).await.unwrap();
+ runtime.mod_evaluate(circular1_id);
+ runtime.run_event_loop().await.unwrap();
let l = loads.lock().unwrap();
assert_eq!(
@@ -863,7 +864,8 @@ mod tests {
println!(">> result {:?}", result);
assert!(result.is_ok());
let redirect1_id = result.unwrap();
- runtime.mod_evaluate(redirect1_id).await.unwrap();
+ runtime.mod_evaluate(redirect1_id);
+ runtime.run_event_loop().await.unwrap();
let l = loads.lock().unwrap();
assert_eq!(
l.to_vec(),
@@ -1012,8 +1014,8 @@ mod tests {
.boxed_local();
let main_id =
futures::executor::block_on(main_id_fut).expect("Failed to load");
-
- futures::executor::block_on(runtime.mod_evaluate(main_id)).unwrap();
+ runtime.mod_evaluate(main_id);
+ futures::executor::block_on(runtime.run_event_loop()).unwrap();
let l = loads.lock().unwrap();
assert_eq!(
diff --git a/core/runtime.rs b/core/runtime.rs
index 24bdf4dc2..e9949bc8e 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -825,12 +825,17 @@ impl JsRuntime {
Ok(())
}
+ // TODO(bartlomieju): make it return `ModuleEvaluationFuture`?
/// Evaluates an already instantiated ES module.
///
+ /// Returns a receiver handle that resolves when module promise resolves.
+ /// Implementors must manually call `run_event_loop()` to drive module
+ /// evaluation future.
+ ///
/// `AnyError` can be downcast to a type that exposes additional information
/// about the V8 exception. By default this type is `JsError`, however it may
/// be a different type if `RuntimeOptions::js_error_create_fn` has been set.
- fn mod_evaluate_inner(
+ pub fn mod_evaluate(
&mut self,
id: ModuleId,
) -> mpsc::Receiver<Result<(), AnyError>> {
@@ -902,24 +907,6 @@ impl JsRuntime {
receiver
}
- pub async fn mod_evaluate(&mut self, id: ModuleId) -> Result<(), AnyError> {
- let mut receiver = self.mod_evaluate_inner(id);
-
- poll_fn(|cx| {
- if let Poll::Ready(maybe_result) = receiver.poll_next_unpin(cx) {
- debug!("received module evaluate {:#?}", maybe_result);
- // If `None` is returned it means that runtime was destroyed before
- // evaluation was complete. This can happen in Web Worker when `self.close()`
- // is called at top level.
- let result = maybe_result.unwrap_or(Ok(()));
- return Poll::Ready(result);
- }
- let _r = self.poll_event_loop(cx)?;
- Poll::Pending
- })
- .await
- }
-
fn dyn_import_error(&mut self, id: ModuleLoadId, err: AnyError) {
let state_rc = Self::state(self.v8_isolate());
let context = self.global_context();
@@ -1110,7 +1097,8 @@ impl JsRuntime {
v8::PromiseState::Fulfilled => {
state.pending_mod_evaluate.take();
scope.perform_microtask_checkpoint();
- sender.try_send(Ok(())).unwrap();
+ // Receiver end might have been already dropped, ignore the result
+ let _ = sender.try_send(Ok(()));
}
v8::PromiseState::Rejected => {
let exception = promise.result(scope);
@@ -1120,7 +1108,8 @@ impl JsRuntime {
let err1 = exception_to_err_result::<()>(scope, exception, false)
.map_err(|err| attach_handle_to_error(scope, err, exception))
.unwrap_err();
- sender.try_send(Err(err1)).unwrap();
+ // Receiver end might have been already dropped, ignore the result
+ let _ = sender.try_send(Err(err1));
}
}
}
@@ -2259,7 +2248,7 @@ pub mod tests {
runtime.mod_instantiate(mod_a).unwrap();
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
- runtime.mod_evaluate_inner(mod_a);
+ runtime.mod_evaluate(mod_a);
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
}
@@ -2502,7 +2491,8 @@ pub mod tests {
)
.unwrap();
- futures::executor::block_on(runtime.mod_evaluate(module_id)).unwrap();
+ runtime.mod_evaluate(module_id);
+ futures::executor::block_on(runtime.run_event_loop()).unwrap();
let _snapshot = runtime.snapshot();
}