From 3eec73ff904e54b6e90879d93b09be4330c63712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 20 Dec 2020 15:14:19 +0100 Subject: Revert "fix: TLA in web worker (#8809)" (#8839) This reverts commit e924bbdf3606e83ff9eef3a8ed640c4ecc34444f. --- core/runtime.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'core/runtime.rs') diff --git a/core/runtime.rs b/core/runtime.rs index e9949bc8e..24bdf4dc2 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -825,17 +825,12 @@ 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. - pub fn mod_evaluate( + fn mod_evaluate_inner( &mut self, id: ModuleId, ) -> mpsc::Receiver> { @@ -907,6 +902,24 @@ 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(); @@ -1097,8 +1110,7 @@ impl JsRuntime { v8::PromiseState::Fulfilled => { state.pending_mod_evaluate.take(); scope.perform_microtask_checkpoint(); - // Receiver end might have been already dropped, ignore the result - let _ = sender.try_send(Ok(())); + sender.try_send(Ok(())).unwrap(); } v8::PromiseState::Rejected => { let exception = promise.result(scope); @@ -1108,8 +1120,7 @@ impl JsRuntime { let err1 = exception_to_err_result::<()>(scope, exception, false) .map_err(|err| attach_handle_to_error(scope, err, exception)) .unwrap_err(); - // Receiver end might have been already dropped, ignore the result - let _ = sender.try_send(Err(err1)); + sender.try_send(Err(err1)).unwrap(); } } } @@ -2248,7 +2259,7 @@ pub mod tests { runtime.mod_instantiate(mod_a).unwrap(); assert_eq!(dispatch_count.load(Ordering::Relaxed), 0); - runtime.mod_evaluate(mod_a); + runtime.mod_evaluate_inner(mod_a); assert_eq!(dispatch_count.load(Ordering::Relaxed), 1); } @@ -2491,8 +2502,7 @@ pub mod tests { ) .unwrap(); - runtime.mod_evaluate(module_id); - futures::executor::block_on(runtime.run_event_loop()).unwrap(); + futures::executor::block_on(runtime.mod_evaluate(module_id)).unwrap(); let _snapshot = runtime.snapshot(); } -- cgit v1.2.3