mirror of
https://github.com/denoland/deno
synced 2024-10-13 03:22:31 +00:00
refactor(cli/repl): get context id from notification (#7864)
This takes the execution context id from a notification which is sent on Runtime.enable rather than hard-coding it to a magic value.
This commit is contained in:
parent
527628e186
commit
fa80649926
|
@ -826,6 +826,7 @@ pub struct InspectorSession {
|
||||||
v8_session: v8::UniqueRef<v8::inspector::V8InspectorSession>,
|
v8_session: v8::UniqueRef<v8::inspector::V8InspectorSession>,
|
||||||
response_tx_map: HashMap<i32, oneshot::Sender<serde_json::Value>>,
|
response_tx_map: HashMap<i32, oneshot::Sender<serde_json::Value>>,
|
||||||
next_message_id: i32,
|
next_message_id: i32,
|
||||||
|
notification_queue: Vec<Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for InspectorSession {
|
impl Deref for InspectorSession {
|
||||||
|
@ -868,8 +869,12 @@ impl v8::inspector::ChannelImpl for InspectorSession {
|
||||||
|
|
||||||
fn send_notification(
|
fn send_notification(
|
||||||
&mut self,
|
&mut self,
|
||||||
_message: v8::UniquePtr<v8::inspector::StringBuffer>,
|
message: v8::UniquePtr<v8::inspector::StringBuffer>,
|
||||||
) {
|
) {
|
||||||
|
let raw_message = message.unwrap().string().to_string();
|
||||||
|
let message = serde_json::from_str(&raw_message).unwrap();
|
||||||
|
|
||||||
|
self.notification_queue.push(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush_protocol_notifications(&mut self) {}
|
fn flush_protocol_notifications(&mut self) {}
|
||||||
|
@ -890,15 +895,22 @@ impl InspectorSession {
|
||||||
let response_tx_map = HashMap::new();
|
let response_tx_map = HashMap::new();
|
||||||
let next_message_id = 0;
|
let next_message_id = 0;
|
||||||
|
|
||||||
|
let notification_queue = Vec::new();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
v8_channel,
|
v8_channel,
|
||||||
v8_session,
|
v8_session,
|
||||||
response_tx_map,
|
response_tx_map,
|
||||||
next_message_id,
|
next_message_id,
|
||||||
|
notification_queue,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn notifications(&mut self) -> Vec<Value> {
|
||||||
|
self.notification_queue.split_off(0)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn post_message(
|
pub async fn post_message(
|
||||||
&mut self,
|
&mut self,
|
||||||
method: &str,
|
method: &str,
|
||||||
|
|
22
cli/repl.rs
22
cli/repl.rs
|
@ -89,9 +89,6 @@ pub async fn run(
|
||||||
global_state: &GlobalState,
|
global_state: &GlobalState,
|
||||||
mut worker: MainWorker,
|
mut worker: MainWorker,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<(), AnyError> {
|
||||||
// Our inspector is unable to default to the default context id so we have to specify it here.
|
|
||||||
let context_id: u32 = 1;
|
|
||||||
|
|
||||||
let mut session = worker.create_inspector_session();
|
let mut session = worker.create_inspector_session();
|
||||||
|
|
||||||
let history_file = global_state.dir.root.join("deno_history.txt");
|
let history_file = global_state.dir.root.join("deno_history.txt");
|
||||||
|
@ -99,6 +96,25 @@ pub async fn run(
|
||||||
post_message_and_poll(&mut *worker, &mut session, "Runtime.enable", None)
|
post_message_and_poll(&mut *worker, &mut session, "Runtime.enable", None)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// Enabling the runtime domain will always send trigger one executionContextCreated for each
|
||||||
|
// context the inspector knows about so we grab the execution context from that since
|
||||||
|
// our inspector does not support a default context (0 is an invalid context id).
|
||||||
|
let mut context_id: u64 = 0;
|
||||||
|
for notification in session.notifications() {
|
||||||
|
let method = notification.get("method").unwrap().as_str().unwrap();
|
||||||
|
let params = notification.get("params").unwrap();
|
||||||
|
|
||||||
|
if method == "Runtime.executionContextCreated" {
|
||||||
|
context_id = params
|
||||||
|
.get("context")
|
||||||
|
.unwrap()
|
||||||
|
.get("id")
|
||||||
|
.unwrap()
|
||||||
|
.as_u64()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let helper = Helper {
|
let helper = Helper {
|
||||||
validator: MatchingBracketValidator::new(),
|
validator: MatchingBracketValidator::new(),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue