diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-05-26 17:47:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-26 17:47:33 +0200 |
commit | e9edd7e14d9d78f03c5f2e67fcc44e4dbaab8f2c (patch) | |
tree | ea6b4d3eb12e36c74d906b7bc683a18932f6916f /runtime/worker.rs | |
parent | 02a4e7dc7cce4df8f2e8e69d3aa4e2eed22e627d (diff) |
refactor: Rewrite Inspector implementation (#10725)
This commit refactors implementation of inspector.
The intention is to be able to move inspector implementation to "deno_core".
Following things were done to make that possible:
* "runtime/inspector.rs" was split into "runtime/inspector/mod.rs"
and "runtime/inspector/server.rs", separating inspector implementation
from Websocket server implementation.
* "DenoInspector" was renamed to "JsRuntimeInspector" and reference to "server"
was removed from the structure, making it independent of Websocket server
used to connect to Chrome Devtools.
* "WebsocketSession" was renamed to "InspectorSession" and rewritten in such
a way that it's not tied to Websockets anymore; instead it accepts a pair
of "proxy" channel ends that allow to integrate the session with different
"transports".
* "InspectorSession" was renamed to "LocalInspectorSession" to better indicate
that it's an "in-memory" session and doesn't require Websocket server. It was
also rewritten in such a way that it uses "InspectorSession" from previous point
instead of reimplementing "v8::inspector::ChannelImpl" trait; this is done by using
the "proxy" channels to communicate with the V8 session.
Consequently "LocalInspectorSession" is now a frontend to "InspectorSession". This
introduces a small inconvenience that awaiting responses for "LocalInspectorSession" requires
to concurrently poll worker's event loop. This arises from the fact that "InspectorSession"
is now owned by "JsRuntimeInspector", which in turn is owned by "Worker" or "WebWorker".
To ease this situation "Worker::with_event_loop" helper method was added, that takes
a future and concurrently polls it along with the event loop (using "tokio::select!" macro
inside a loop).
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r-- | runtime/worker.rs | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/runtime/worker.rs b/runtime/worker.rs index 9ffd0b5ab..1ffd32b48 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -1,8 +1,8 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::inspector::DenoInspector; use crate::inspector::InspectorServer; -use crate::inspector::InspectorSession; +use crate::inspector::JsRuntimeInspector; +use crate::inspector::LocalInspectorSession; use crate::js; use crate::metrics; use crate::ops; @@ -13,6 +13,7 @@ use deno_core::error::Context as ErrorContext; use deno_core::futures::future::poll_fn; use deno_core::futures::future::FutureExt; use deno_core::futures::stream::StreamExt; +use deno_core::futures::Future; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::url::Url; @@ -27,6 +28,7 @@ use deno_core::RuntimeOptions; use deno_file::BlobUrlStore; use log::debug; use std::env; +use std::pin::Pin; use std::rc::Rc; use std::sync::Arc; use std::task::Context; @@ -40,7 +42,7 @@ use std::task::Poll; /// All `WebWorker`s created during program execution /// are descendants of this worker. pub struct MainWorker { - inspector: Option<Box<DenoInspector>>, + inspector: Option<Box<JsRuntimeInspector>>, pub js_runtime: JsRuntime, should_break_on_first_statement: bool, } @@ -147,10 +149,15 @@ impl MainWorker { }); let inspector = if options.attach_inspector { - Some(DenoInspector::new( - &mut js_runtime, - options.maybe_inspector_server.clone(), - )) + let mut inspector = JsRuntimeInspector::new(&mut js_runtime); + + if let Some(server) = options.maybe_inspector_server.clone() { + let session_sender = inspector.get_session_sender(); + let deregister_rx = inspector.add_deregister_handler(); + server.register_inspector(session_sender, deregister_rx); + } + + Some(inspector) } else { None }; @@ -243,10 +250,9 @@ impl MainWorker { /// Create new inspector session. This function panics if Worker /// was not configured to create inspector. - pub fn create_inspector_session(&mut self) -> Box<InspectorSession> { - let inspector = self.inspector.as_mut().unwrap(); - - InspectorSession::new(&mut **inspector) + pub async fn create_inspector_session(&mut self) -> LocalInspectorSession { + let inspector = self.inspector.as_ref().unwrap(); + inspector.create_local_session() } pub fn poll_event_loop( @@ -261,6 +267,28 @@ impl MainWorker { pub async fn run_event_loop(&mut self) -> Result<(), AnyError> { poll_fn(|cx| self.poll_event_loop(cx)).await } + + /// A utility function that runs provided future concurrently with the event loop. + /// + /// Useful when using a local inspector session. + pub async fn with_event_loop<'a, T>( + &mut self, + mut fut: Pin<Box<dyn Future<Output = T> + 'a>>, + ) -> T { + loop { + tokio::select! { + result = &mut fut => { + return result; + } + _ = self.run_event_loop() => { + // A zero delay is long enough to yield the thread in order to prevent the loop from + // running hot for messages that are taking longer to resolve like for example an + // evaluation of top level await. + tokio::time::sleep(tokio::time::Duration::from_millis(0)).await; + } + }; + } + } } impl Drop for MainWorker { |