diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cdp.rs | 474 | ||||
-rw-r--r-- | cli/main.rs | 1 | ||||
-rw-r--r-- | cli/tools/coverage/mod.rs | 29 | ||||
-rw-r--r-- | cli/tools/repl/channel.rs | 9 | ||||
-rw-r--r-- | cli/tools/repl/editor.rs | 81 | ||||
-rw-r--r-- | cli/tools/repl/mod.rs | 5 | ||||
-rw-r--r-- | cli/tools/repl/session.rs | 148 |
7 files changed, 107 insertions, 640 deletions
diff --git a/cli/cdp.rs b/cli/cdp.rs deleted file mode 100644 index 24fb76e10..000000000 --- a/cli/cdp.rs +++ /dev/null @@ -1,474 +0,0 @@ -// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. - -/// https://chromedevtools.github.io/devtools-protocol/tot/ -use deno_core::serde_json; -use deno_core::serde_json::Value; -use serde::Deserialize; -use serde::Serialize; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-awaitPromise -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct AwaitPromiseArgs { - pub promise_object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-awaitPromise -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct AwaitPromiseResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFunctionOnArgs { - pub function_declaration: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_id: Option<RemoteObjectId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub arguments: Option<Vec<CallArgument>>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub user_gesture: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] - pub throw_on_side_effect: Option<bool>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFunctionOnResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-compileScript -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CompileScriptArgs { - pub expression: String, - #[serde(rename = "sourceURL")] - pub source_url: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-compileScript -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CompileScriptResponse { - pub script_id: Option<ScriptId>, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct EvaluateArgs { - pub expression: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde( - rename = "includeCommandLineAPI", - skip_serializing_if = "Option::is_none" - )] - pub include_command_line_api: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub user_gesture: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub throw_on_side_effect: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub timeout: Option<TimeDelta>, - #[serde(skip_serializing_if = "Option::is_none")] - pub disable_breaks: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub repl_mode: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "allowUnsafeEvalBlockedByCSP")] - pub allow_unsafe_eval_blocked_by_csp: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub unique_context_id: Option<String>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct EvaluateResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-getProperties -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct GetPropertiesArgs { - pub object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub own_properties: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub accessor_properties_only: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub non_indexed_properties_only: Option<bool>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-getProperties -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetPropertiesResponse { - pub result: Vec<PropertyDescriptor>, - pub internal_properties: Vec<InternalPropertyDescriptor>, - pub private_properties: Vec<PrivatePropertyDescriptor>, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-globalLexicalScopeNames -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct GlobalLexicalScopeNamesArgs { - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-globalLexicalScopeNames -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GlobalLexicalScopeNamesResponse { - pub names: Vec<String>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-queryObjects -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct QueryObjectsArgs { - pub prototype_object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-queryObjects -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct QueryObjectsResponse { - pub objects: RemoteObject, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-releaseObject -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ReleaseObjectArgs { - pub object_id: RemoteObjectId, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-releaseObjectGroup -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ReleaseObjectGroupArgs { - pub object_group: String, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-runScript -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct RunScriptArgs { - pub script_id: ScriptId, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde( - rename = "includeCommandLineAPI", - skip_serializing_if = "Option::is_none" - )] - pub include_command_line_api: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-runScript -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct RunScriptResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-setAsyncCallStackDepth -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct SetAsyncCallStackDepthArgs { - pub max_depth: u64, -} - -// types - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum RemoteObjectType { - Object, - Function, - Undefined, - String, - Number, - Boolean, - Symbol, - Bigint, -} - -impl std::fmt::Display for RemoteObjectType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&format!("{:?}", self).to_lowercase()) - } -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum RemoteObjectSubType { - Array, - Null, - Node, - Regexp, - Date, - Map, - Set, - Weakmap, - Weakset, - Iterator, - Generator, - Error, - Proxy, - Promise, - Typedarray, - Arraybuffer, - Dataview, - Webassemblymemory, - Wasmvalue, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct RemoteObject { - #[serde(rename = "type")] - pub kind: RemoteObjectType, - pub subtype: Option<RemoteObjectSubType>, - pub class_name: Option<String>, - pub value: Option<Value>, - pub unserializable_value: Option<UnserializableValue>, - pub description: Option<String>, - pub object_id: Option<RemoteObjectId>, - pub preview: Option<ObjectPreview>, - pub custom_preview: Option<CustomPreview>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ObjectPreview -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ObjectPreview { - #[serde(rename = "type")] - pub kind: RemoteObjectType, - pub subtype: Option<RemoteObjectSubType>, - pub description: Option<String>, - pub overflow: bool, - pub properties: Vec<PropertyPreview>, - pub entries: Option<Vec<EntryPreview>>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-PropertyPreview -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PropertyPreview { - pub name: String, - #[serde(rename = "type")] - pub kind: PropertyPreviewType, - pub value: Option<String>, - pub value_preview: Option<ObjectPreview>, - pub subtype: Option<RemoteObjectSubType>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-PropertyPreview -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum PropertyPreviewType { - Object, - Function, - Undefined, - String, - Number, - Boolean, - Symbol, - Accessor, - Bigint, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-EntryPreview -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct EntryPreview { - pub key: Option<ObjectPreview>, - pub value: ObjectPreview, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CustomPreview -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CustomPreview { - pub header: String, - pub body_getter_id: RemoteObjectId, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExceptionDetails -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ExceptionDetails { - pub exception_id: u64, - pub text: String, - pub line_number: u64, - pub column_number: u64, - pub script_id: Option<ScriptId>, - pub url: Option<String>, - pub stack_trace: Option<StackTrace>, - pub exception: Option<RemoteObject>, - pub execution_context_id: Option<ExecutionContextId>, - pub exception_meta_data: Option<serde_json::Map<String, Value>>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-StackTrace -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StackTrace { - pub description: Option<String>, - pub call_frames: Vec<CallFrame>, - pub parent: Option<Box<StackTrace>>, - pub parent_id: Option<StackTraceId>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CallFrame -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFrame { - pub function_name: String, - pub script_id: ScriptId, - pub url: String, - pub line_number: u64, - pub column_number: u64, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-StackTraceId -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StackTraceId { - pub id: String, - pub debugger_id: Option<UniqueDebuggerId>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CallArgument -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CallArgument { - #[serde(skip_serializing_if = "Option::is_none")] - pub value: Option<Value>, - #[serde(skip_serializing_if = "Option::is_none")] - pub unserializable_value: Option<UnserializableValue>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_id: Option<RemoteObjectId>, -} - -impl From<&RemoteObject> for CallArgument { - fn from(obj: &RemoteObject) -> Self { - Self { - value: obj.value.clone(), - unserializable_value: obj.unserializable_value.clone(), - object_id: obj.object_id.clone(), - } - } -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-InternalPropertyDescriptor -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, - pub writable: Option<bool>, - pub get: Option<RemoteObject>, - pub set: Option<RemoteObject>, - pub configurable: bool, - pub enumerable: bool, - pub was_thrown: Option<bool>, - pub is_own: Option<bool>, - pub symbol: Option<RemoteObject>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-InternalPropertyDescriptor -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct InternalPropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-PrivatePropertyDescriptor -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PrivatePropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, - pub get: Option<RemoteObject>, - pub set: Option<RemoteObject>, -} - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObjectId -pub type RemoteObjectId = String; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExecutionContextId -pub type ExecutionContextId = u64; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ScriptId -pub type ScriptId = String; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-TimeDelta -pub type TimeDelta = u64; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-UnserializableValue -pub type UnserializableValue = String; - -/// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-UniqueDebuggerId -pub type UniqueDebuggerId = String; diff --git a/cli/main.rs b/cli/main.rs index f4871228e..c996425c7 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -2,7 +2,6 @@ mod auth_tokens; mod cache; -mod cdp; mod checksum; mod compat; mod config_file; diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs index 6177295a6..1c14859ea 100644 --- a/cli/tools/coverage/mod.rs +++ b/cli/tools/coverage/mod.rs @@ -43,34 +43,22 @@ impl CoverageCollector { } async fn enable_debugger(&mut self) -> Result<(), AnyError> { - self - .session - .post_message::<()>("Debugger.enable", None) - .await?; + self.session.post_message("Debugger.enable", None).await?; Ok(()) } async fn enable_profiler(&mut self) -> Result<(), AnyError> { - self - .session - .post_message::<()>("Profiler.enable", None) - .await?; + self.session.post_message("Profiler.enable", None).await?; Ok(()) } async fn disable_debugger(&mut self) -> Result<(), AnyError> { - self - .session - .post_message::<()>("Debugger.disable", None) - .await?; + self.session.post_message("Debugger.disable", None).await?; Ok(()) } async fn disable_profiler(&mut self) -> Result<(), AnyError> { - self - .session - .post_message::<()>("Profiler.disable", None) - .await?; + self.session.post_message("Profiler.disable", None).await?; Ok(()) } @@ -78,9 +66,10 @@ impl CoverageCollector { &mut self, parameters: StartPreciseCoverageParameters, ) -> Result<StartPreciseCoverageReturnObject, AnyError> { + let parameters_value = serde_json::to_value(parameters)?; let return_value = self .session - .post_message("Profiler.startPreciseCoverage", Some(parameters)) + .post_message("Profiler.startPreciseCoverage", Some(parameters_value)) .await?; let return_object = serde_json::from_value(return_value)?; @@ -93,7 +82,7 @@ impl CoverageCollector { ) -> Result<TakePreciseCoverageReturnObject, AnyError> { let return_value = self .session - .post_message::<()>("Profiler.takePreciseCoverage", None) + .post_message("Profiler.takePreciseCoverage", None) .await?; let return_object = serde_json::from_value(return_value)?; @@ -603,8 +592,8 @@ pub async fn cover_files( })? }; let file = maybe_file.ok_or_else(|| { - anyhow!("Failed to fetch \"{}\" from cache. - Before generating coverage report, run `deno test --coverage` to ensure consistent state.", + anyhow!("Failed to fetch \"{}\" from cache. + Before generating coverage report, run `deno test --coverage` to ensure consistent state.", module_specifier ) })?; diff --git a/cli/tools/repl/channel.rs b/cli/tools/repl/channel.rs index 4f2086fb5..7cc802905 100644 --- a/cli/tools/repl/channel.rs +++ b/cli/tools/repl/channel.rs @@ -2,7 +2,6 @@ use deno_core::anyhow::anyhow; use deno_core::error::AnyError; -use deno_core::serde_json; use deno_core::serde_json::Value; use std::cell::RefCell; use tokio::sync::mpsc::channel; @@ -56,19 +55,17 @@ pub struct RustylineSyncMessageSender { } impl RustylineSyncMessageSender { - pub fn post_message<T: serde::Serialize>( + pub fn post_message( &self, method: &str, - params: Option<T>, + params: Option<Value>, ) -> Result<Value, AnyError> { if let Err(err) = self .message_tx .blocking_send(RustylineSyncMessage::PostMessage { method: method.to_string(), - params: params - .map(|params| serde_json::to_value(params)) - .transpose()?, + params, }) { Err(anyhow!("{}", err)) diff --git a/cli/tools/repl/editor.rs b/cli/tools/repl/editor.rs index 8a327f624..30b9fba87 100644 --- a/cli/tools/repl/editor.rs +++ b/cli/tools/repl/editor.rs @@ -1,13 +1,13 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use crate::cdp; use crate::colors; use deno_ast::swc::parser::error::SyntaxError; use deno_ast::swc::parser::token::Token; use deno_ast::swc::parser::token::Word; use deno_core::error::AnyError; use deno_core::parking_lot::Mutex; -use deno_core::serde_json; +use deno_core::serde_json::json; +use deno_core::serde_json::Value; use rustyline::completion::Completer; use rustyline::error::ReadlineError; use rustyline::highlight::Highlighter; @@ -39,14 +39,20 @@ impl EditorHelper { .sync_sender .post_message( "Runtime.globalLexicalScopeNames", - Some(cdp::GlobalLexicalScopeNamesArgs { - execution_context_id: Some(self.context_id), - }), + Some(json!({ + "executionContextId": self.context_id, + })), ) .unwrap(); - let evaluate_response: cdp::GlobalLexicalScopeNamesResponse = - serde_json::from_value(evaluate_response).unwrap(); - evaluate_response.names + + evaluate_response + .get("names") + .unwrap() + .as_array() + .unwrap() + .iter() + .map(|n| n.as_str().unwrap().to_string()) + .collect() } pub fn get_expression_property_names(&self, expr: &str) -> Vec<String> { @@ -75,8 +81,11 @@ impl EditorHelper { fn get_expression_type(&self, expr: &str) -> Option<String> { self - .evaluate_expression(expr) - .map(|res| res.result.kind.to_string()) + .evaluate_expression(expr)? + .get("result")? + .get("type")? + .as_str() + .map(|s| s.to_string()) } fn get_object_expr_properties( @@ -84,60 +93,44 @@ impl EditorHelper { object_expr: &str, ) -> Option<Vec<String>> { let evaluate_result = self.evaluate_expression(object_expr)?; - let object_id = evaluate_result.result.object_id?; + let object_id = evaluate_result.get("result")?.get("objectId")?; let get_properties_response = self .sync_sender .post_message( "Runtime.getProperties", - Some(cdp::GetPropertiesArgs { - object_id, - own_properties: None, - accessor_properties_only: None, - generate_preview: None, - non_indexed_properties_only: None, - }), + Some(json!({ + "objectId": object_id, + })), ) .ok()?; - let get_properties_response: cdp::GetPropertiesResponse = - serde_json::from_value(get_properties_response).ok()?; + Some( get_properties_response - .result - .into_iter() - .map(|prop| prop.name) + .get("result")? + .as_array() + .unwrap() + .iter() + .map(|r| r.get("name").unwrap().as_str().unwrap().to_string()) .collect(), ) } - fn evaluate_expression(&self, expr: &str) -> Option<cdp::EvaluateResponse> { + fn evaluate_expression(&self, expr: &str) -> Option<Value> { let evaluate_response = self .sync_sender .post_message( "Runtime.evaluate", - Some(cdp::EvaluateArgs { - expression: expr.to_string(), - object_group: None, - include_command_line_api: None, - silent: None, - context_id: Some(self.context_id), - return_by_value: None, - generate_preview: None, - user_gesture: None, - await_promise: None, - throw_on_side_effect: Some(true), - timeout: Some(200), - disable_breaks: None, - repl_mode: None, - allow_unsafe_eval_blocked_by_csp: None, - unique_context_id: None, - }), + Some(json!({ + "contextId": self.context_id, + "expression": expr, + "throwOnSideEffect": true, + "timeout": 200, + })), ) .ok()?; - let evaluate_response: cdp::EvaluateResponse = - serde_json::from_value(evaluate_response).ok()?; - if evaluate_response.exception_details.is_some() { + if evaluate_response.get("exceptionDetails").is_some() { None } else { Some(evaluate_response) diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index b49c641c4..0c9fdbfb0 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -33,7 +33,10 @@ async fn read_line_and_poll( } result = message_handler.recv() => { match result { - Some(RustylineSyncMessage::PostMessage { method, params }) => { + Some(RustylineSyncMessage::PostMessage { + method, + params + }) => { let result = repl_session .post_message_with_event_loop(&method, params) .await; diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index 57109eb1d..5d458189f 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -1,13 +1,12 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use crate::cdp; use crate::colors; use crate::lsp::ReplLanguageServer; use deno_ast::DiagnosticsError; use deno_ast::ImportsNotUsedAsValues; use deno_core::error::AnyError; use deno_core::futures::FutureExt; -use deno_core::serde_json; +use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::LocalInspectorSession; use deno_runtime::worker::MainWorker; @@ -59,7 +58,7 @@ impl std::fmt::Display for EvaluationOutput { struct TsEvaluateResponse { ts_code: String, - value: cdp::EvaluateResponse, + value: Value, } pub struct ReplSession { @@ -76,9 +75,7 @@ impl ReplSession { worker .with_event_loop( - session - .post_message::<()>("Runtime.enable", None) - .boxed_local(), + session.post_message("Runtime.enable", None).boxed_local(), ) .await?; @@ -118,8 +115,9 @@ impl ReplSession { let closed = self .evaluate_expression("(this.closed)") .await? - .result - .value + .get("result") + .unwrap() + .get("value") .unwrap() .as_bool() .unwrap(); @@ -127,10 +125,10 @@ impl ReplSession { Ok(closed) } - pub async fn post_message_with_event_loop<T: serde::Serialize>( + pub async fn post_message_with_event_loop( &mut self, method: &str, - params: Option<T>, + params: Option<Value>, ) -> Result<Value, AnyError> { self .worker @@ -158,24 +156,23 @@ impl ReplSession { match self.evaluate_line_with_object_wrapping(line).await { Ok(evaluate_response) => { - let cdp::EvaluateResponse { - result, - exception_details, - } = evaluate_response.value; + let evaluate_result = evaluate_response.value.get("result").unwrap(); + let evaluate_exception_details = + evaluate_response.value.get("exceptionDetails"); - if exception_details.is_some() { - self.set_last_thrown_error(&result).await?; + if evaluate_exception_details.is_some() { + self.set_last_thrown_error(evaluate_result).await?; } else { self .language_server .commit_text(&evaluate_response.ts_code) .await; - self.set_last_eval_result(&result).await?; + self.set_last_eval_result(evaluate_result).await?; } - let value = self.get_eval_value(&result).await?; - Ok(match exception_details { + let value = self.get_eval_value(evaluate_result).await?; + Ok(match evaluate_exception_details { Some(_) => EvaluationOutput::Error(format!("Uncaught {}", value)), None => EvaluationOutput::Value(value), }) @@ -227,7 +224,7 @@ impl ReplSession { .as_ref() .unwrap() .value - .exception_details + .get("exceptionDetails") .is_some()) { self.evaluate_ts_expression(line).await @@ -238,90 +235,66 @@ impl ReplSession { async fn set_last_thrown_error( &mut self, - error: &cdp::RemoteObject, + error: &Value, ) -> Result<(), AnyError> { self.post_message_with_event_loop( "Runtime.callFunctionOn", - Some(cdp::CallFunctionOnArgs { - function_declaration: "function (object) { Deno[Deno.internal].lastThrownError = object; }".to_string(), - object_id: None, - arguments: Some(vec![error.into()]), - silent: None, - return_by_value: None, - generate_preview: None, - user_gesture: None, - await_promise: None, - execution_context_id: Some(self.context_id), - object_group: None, - throw_on_side_effect: None - }), + Some(json!({ + "executionContextId": self.context_id, + "functionDeclaration": "function (object) { Deno[Deno.internal].lastThrownError = object; }", + "arguments": [ + error, + ], + })), ).await?; Ok(()) } async fn set_last_eval_result( &mut self, - evaluate_result: &cdp::RemoteObject, + evaluate_result: &Value, ) -> Result<(), AnyError> { - self - .post_message_with_event_loop( - "Runtime.callFunctionOn", - Some(cdp::CallFunctionOnArgs { - function_declaration: - "function (object) { Deno[Deno.internal].lastEvalResult = object; }" - .to_string(), - object_id: None, - arguments: Some(vec![evaluate_result.into()]), - silent: None, - return_by_value: None, - generate_preview: None, - user_gesture: None, - await_promise: None, - execution_context_id: Some(self.context_id), - object_group: None, - throw_on_side_effect: None, - }), - ) - .await?; + self.post_message_with_event_loop( + "Runtime.callFunctionOn", + Some(json!({ + "executionContextId": self.context_id, + "functionDeclaration": "function (object) { Deno[Deno.internal].lastEvalResult = object; }", + "arguments": [ + evaluate_result, + ], + })), + ).await?; Ok(()) } pub async fn get_eval_value( &mut self, - evaluate_result: &cdp::RemoteObject, + evaluate_result: &Value, ) -> Result<String, AnyError> { // TODO(caspervonb) we should investigate using previews here but to keep things // consistent with the previous implementation we just get the preview result from // Deno.inspectArgs. let inspect_response = self.post_message_with_event_loop( "Runtime.callFunctionOn", - Some(cdp::CallFunctionOnArgs { - function_declaration: r#"function (object) { + Some(json!({ + "executionContextId": self.context_id, + "functionDeclaration": r#"function (object) { try { return Deno[Deno.internal].inspectArgs(["%o", object], { colors: !Deno.noColor }); } catch (err) { return Deno[Deno.internal].inspectArgs(["%o", err]); } - }"#.to_string(), - object_id: None, - arguments: Some(vec![evaluate_result.into()]), - silent: None, - return_by_value: None, - generate_preview: None, - user_gesture: None, - await_promise: None, - execution_context_id: Some(self.context_id), - object_group: None, - throw_on_side_effect: None - }), + }"#, + "arguments": [ + evaluate_result, + ], + })), ).await?; - let response: cdp::CallFunctionOnResponse = - serde_json::from_value(inspect_response)?; - let value = response.result.value.unwrap(); - let s = value.as_str().unwrap(); + let inspect_result = inspect_response.get("result").unwrap(); + let value = inspect_result.get("value").unwrap().as_str().unwrap(); - Ok(s.to_string()) + Ok(value.to_string()) } async fn evaluate_ts_expression( @@ -371,29 +344,16 @@ impl ReplSession { async fn evaluate_expression( &mut self, expression: &str, - ) -> Result<cdp::EvaluateResponse, AnyError> { + ) -> Result<Value, AnyError> { self .post_message_with_event_loop( "Runtime.evaluate", - Some(cdp::EvaluateArgs { - expression: expression.to_string(), - object_group: None, - include_command_line_api: None, - silent: None, - context_id: Some(self.context_id), - return_by_value: None, - generate_preview: None, - user_gesture: None, - await_promise: None, - throw_on_side_effect: None, - timeout: None, - disable_breaks: None, - repl_mode: Some(true), - allow_unsafe_eval_blocked_by_csp: None, - unique_context_id: None, - }), + Some(json!({ + "expression": expression, + "contextId": self.context_id, + "replMode": true, + })), ) .await - .and_then(|res| serde_json::from_value(res).map_err(|e| e.into())) } } |