From 142449ecab20006c5cfd15462814650596bc034d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 21 Sep 2023 16:08:23 +0200 Subject: [PATCH] refactor: rewrite some ops to op2 macro (#20603) --- cli/tsc/mod.rs | 5 ++--- ext/crypto/lib.rs | 9 ++++----- ext/fetch/lib.rs | 16 ++++++++++------ runtime/examples/extension_with_ops/main.rs | 6 +++--- runtime/ops/http.rs | 13 +++++++------ runtime/ops/os/mod.rs | 16 ++++++++-------- runtime/ops/tty.rs | 5 ++--- runtime/ops/web_worker/sync_fetch.rs | 7 ++++--- 8 files changed, 40 insertions(+), 37 deletions(-) diff --git a/cli/tsc/mod.rs b/cli/tsc/mod.rs index 8ab35bc79de1f8..04e8dec0489a3e 100644 --- a/cli/tsc/mod.rs +++ b/cli/tsc/mod.rs @@ -736,9 +736,8 @@ struct RespondArgs { pub stats: Stats, } -// TODO(bartlomieju): `op2` doesn't support `serde_json::Value` -#[op] -fn op_respond(state: &mut OpState, args: RespondArgs) { +#[op2] +fn op_respond(state: &mut OpState, #[serde] args: RespondArgs) { let state = state.borrow_mut::(); state.maybe_response = Some(args); } diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs index 8b329a7ca167d0..e47cc8f3c1e2be 100644 --- a/ext/crypto/lib.rs +++ b/ext/crypto/lib.rs @@ -8,7 +8,6 @@ use deno_core::error::custom_error; use deno_core::error::not_supported; use deno_core::error::type_error; use deno_core::error::AnyError; -use deno_core::op; use deno_core::op2; use deno_core::ToJsBuffer; @@ -46,7 +45,6 @@ use sha2::Sha512; use signature::RandomizedSigner; use signature::Signer; use signature::Verifier; -use std::convert::TryFrom; use std::num::NonZeroU32; use std::path::PathBuf; @@ -422,10 +420,11 @@ pub struct DeriveKeyArg { info: Option, } -#[op] +#[op2(async)] +#[serde] pub async fn op_crypto_derive_bits( - args: DeriveKeyArg, - zero_copy: Option, + #[serde] args: DeriveKeyArg, + #[buffer] zero_copy: Option, ) -> Result { let algorithm = args.algorithm; match algorithm { diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 4485a3dbe54430..dd3526ff88958f 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -21,6 +21,7 @@ use deno_core::futures::FutureExt; use deno_core::futures::Stream; use deno_core::futures::StreamExt; use deno_core::op; +use deno_core::op2; use deno_core::BufView; use deno_core::WriteOutcome; @@ -411,10 +412,11 @@ pub struct FetchResponse { pub remote_addr_port: Option, } -#[op] +#[op2(async)] +#[serde] pub async fn op_fetch_send( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result { let request = state .borrow_mut() @@ -463,10 +465,11 @@ pub async fn op_fetch_send( }) } -#[op] +#[op2(async)] +#[smi] pub async fn op_fetch_response_upgrade( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result { let raw_response = state .borrow_mut() @@ -811,10 +814,11 @@ fn default_true() -> bool { true } -#[op] +#[op2] +#[smi] pub fn op_fetch_custom_client( state: &mut OpState, - args: CreateHttpClientArgs, + #[serde] args: CreateHttpClientArgs, ) -> Result where FP: FetchPermissions + 'static, diff --git a/runtime/examples/extension_with_ops/main.rs b/runtime/examples/extension_with_ops/main.rs index 1feb4ba2791b9a..2ef562ec932b92 100644 --- a/runtime/examples/extension_with_ops/main.rs +++ b/runtime/examples/extension_with_ops/main.rs @@ -4,7 +4,7 @@ use std::path::Path; use std::rc::Rc; use deno_core::error::AnyError; -use deno_core::op; +use deno_core::op2; use deno_core::FsModuleLoader; use deno_core::ModuleSpecifier; use deno_runtime::permissions::PermissionsContainer; @@ -13,8 +13,8 @@ use deno_runtime::worker::WorkerOptions; deno_core::extension!(hello_runtime, ops = [op_hello]); -#[op] -fn op_hello(text: &str) { +#[op2(fast)] +fn op_hello(#[string] text: &str) { println!("Hello {}!", text); } diff --git a/runtime/ops/http.rs b/runtime/ops/http.rs index 35e181d3e93ecb..07757850cd647b 100644 --- a/runtime/ops/http.rs +++ b/runtime/ops/http.rs @@ -7,7 +7,7 @@ use deno_core::error::bad_resource; use deno_core::error::bad_resource_id; use deno_core::error::custom_error; use deno_core::error::AnyError; -use deno_core::op; +use deno_core::op2; use deno_core::OpState; use deno_core::RcRef; use deno_core::ResourceId; @@ -32,10 +32,11 @@ deno_core::extension!( ops = [op_http_start, op_http_upgrade], ); -#[op] +#[op2(fast)] +#[smi] fn op_http_start( state: &mut OpState, - tcp_stream_rid: ResourceId, + #[smi] tcp_stream_rid: ResourceId, ) -> Result { if let Ok(resource_rc) = state .resource_table @@ -96,11 +97,11 @@ pub struct HttpUpgradeResult { read_buf: ToJsBuffer, } -#[op] +#[op2(async)] +#[serde] async fn op_http_upgrade( state: Rc>, - rid: ResourceId, - _: (), + #[smi] rid: ResourceId, ) -> Result { let stream = state .borrow_mut() diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs index 80f37514f3c7e2..166ccb867dbe24 100644 --- a/runtime/ops/os/mod.rs +++ b/runtime/ops/os/mod.rs @@ -250,9 +250,9 @@ fn op_system_memory_info( Ok(sys_info::mem_info()) } -// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(not(windows))] -#[op] +#[op2] +#[smi] fn op_gid(state: &mut OpState) -> Result, AnyError> { state .borrow_mut::() @@ -264,9 +264,9 @@ fn op_gid(state: &mut OpState) -> Result, AnyError> { } } -// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(windows)] -#[op] +#[op2] +#[smi] fn op_gid(state: &mut OpState) -> Result, AnyError> { state .borrow_mut::() @@ -274,9 +274,9 @@ fn op_gid(state: &mut OpState) -> Result, AnyError> { Ok(None) } -// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(not(windows))] -#[op] +#[op2] +#[smi] fn op_uid(state: &mut OpState) -> Result, AnyError> { state .borrow_mut::() @@ -288,9 +288,9 @@ fn op_uid(state: &mut OpState) -> Result, AnyError> { } } -// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(windows)] -#[op] +#[op2] +#[smi] fn op_uid(state: &mut OpState) -> Result, AnyError> { state .borrow_mut::() diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index 07a7a0b738a5b9..8157a4517ca59e 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -4,7 +4,6 @@ use std::io::Error; use std::io::IsTerminal; use deno_core::error::AnyError; -use deno_core::op; use deno_core::op2; use deno_core::OpState; use deno_core::ResourceHandle; @@ -188,10 +187,10 @@ fn op_isatty(state: &mut OpState, rid: u32) -> Result { }) } -#[op(fast)] +#[op2(fast)] fn op_console_size( state: &mut OpState, - result: &mut [u32], + #[buffer] result: &mut [u32], ) -> Result<(), AnyError> { fn check_console_size( state: &mut OpState, diff --git a/runtime/ops/web_worker/sync_fetch.rs b/runtime/ops/web_worker/sync_fetch.rs index 4d2f4ca5a82ffa..ec79118b2a2dbc 100644 --- a/runtime/ops/web_worker/sync_fetch.rs +++ b/runtime/ops/web_worker/sync_fetch.rs @@ -6,7 +6,7 @@ use crate::web_worker::WebWorkerInternalHandle; use crate::web_worker::WebWorkerType; use deno_core::error::type_error; use deno_core::error::AnyError; -use deno_core::op; +use deno_core::op2; use deno_core::url::Url; use deno_core::OpState; use deno_fetch::data_url::DataUrl; @@ -33,10 +33,11 @@ pub struct SyncFetchScript { script: String, } -#[op] +#[op2] +#[serde] pub fn op_worker_sync_fetch( state: &mut OpState, - scripts: Vec, + #[serde] scripts: Vec, mut loose_mime_checks: bool, ) -> Result, AnyError> { let handle = state.borrow::().clone();