1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 00:54:02 -05:00

refactor: rewrite some ops to op2 macro (#20603)

This commit is contained in:
Bartek Iwańczuk 2023-09-21 16:08:23 +02:00 committed by GitHub
parent cf6f649829
commit 142449ecab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 37 deletions

View file

@ -736,9 +736,8 @@ struct RespondArgs {
pub stats: Stats, pub stats: Stats,
} }
// TODO(bartlomieju): `op2` doesn't support `serde_json::Value` #[op2]
#[op] fn op_respond(state: &mut OpState, #[serde] args: RespondArgs) {
fn op_respond(state: &mut OpState, args: RespondArgs) {
let state = state.borrow_mut::<State>(); let state = state.borrow_mut::<State>();
state.maybe_response = Some(args); state.maybe_response = Some(args);
} }

View file

@ -8,7 +8,6 @@ use deno_core::error::custom_error;
use deno_core::error::not_supported; use deno_core::error::not_supported;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2; use deno_core::op2;
use deno_core::ToJsBuffer; use deno_core::ToJsBuffer;
@ -46,7 +45,6 @@ use sha2::Sha512;
use signature::RandomizedSigner; use signature::RandomizedSigner;
use signature::Signer; use signature::Signer;
use signature::Verifier; use signature::Verifier;
use std::convert::TryFrom;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::path::PathBuf; use std::path::PathBuf;
@ -422,10 +420,11 @@ pub struct DeriveKeyArg {
info: Option<JsBuffer>, info: Option<JsBuffer>,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_crypto_derive_bits( pub async fn op_crypto_derive_bits(
args: DeriveKeyArg, #[serde] args: DeriveKeyArg,
zero_copy: Option<JsBuffer>, #[buffer] zero_copy: Option<JsBuffer>,
) -> Result<ToJsBuffer, AnyError> { ) -> Result<ToJsBuffer, AnyError> {
let algorithm = args.algorithm; let algorithm = args.algorithm;
match algorithm { match algorithm {

View file

@ -21,6 +21,7 @@ use deno_core::futures::FutureExt;
use deno_core::futures::Stream; use deno_core::futures::Stream;
use deno_core::futures::StreamExt; use deno_core::futures::StreamExt;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::BufView; use deno_core::BufView;
use deno_core::WriteOutcome; use deno_core::WriteOutcome;
@ -411,10 +412,11 @@ pub struct FetchResponse {
pub remote_addr_port: Option<u16>, pub remote_addr_port: Option<u16>,
} }
#[op] #[op2(async)]
#[serde]
pub async fn op_fetch_send( pub async fn op_fetch_send(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<FetchResponse, AnyError> { ) -> Result<FetchResponse, AnyError> {
let request = state let request = state
.borrow_mut() .borrow_mut()
@ -463,10 +465,11 @@ pub async fn op_fetch_send(
}) })
} }
#[op] #[op2(async)]
#[smi]
pub async fn op_fetch_response_upgrade( pub async fn op_fetch_response_upgrade(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
let raw_response = state let raw_response = state
.borrow_mut() .borrow_mut()
@ -811,10 +814,11 @@ fn default_true() -> bool {
true true
} }
#[op] #[op2]
#[smi]
pub fn op_fetch_custom_client<FP>( pub fn op_fetch_custom_client<FP>(
state: &mut OpState, state: &mut OpState,
args: CreateHttpClientArgs, #[serde] args: CreateHttpClientArgs,
) -> Result<ResourceId, AnyError> ) -> Result<ResourceId, AnyError>
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,

View file

@ -4,7 +4,7 @@ use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op2;
use deno_core::FsModuleLoader; use deno_core::FsModuleLoader;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
@ -13,8 +13,8 @@ use deno_runtime::worker::WorkerOptions;
deno_core::extension!(hello_runtime, ops = [op_hello]); deno_core::extension!(hello_runtime, ops = [op_hello]);
#[op] #[op2(fast)]
fn op_hello(text: &str) { fn op_hello(#[string] text: &str) {
println!("Hello {}!", text); println!("Hello {}!", text);
} }

View file

@ -7,7 +7,7 @@ use deno_core::error::bad_resource;
use deno_core::error::bad_resource_id; use deno_core::error::bad_resource_id;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op2;
use deno_core::OpState; use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::ResourceId; use deno_core::ResourceId;
@ -32,10 +32,11 @@ deno_core::extension!(
ops = [op_http_start, op_http_upgrade], ops = [op_http_start, op_http_upgrade],
); );
#[op] #[op2(fast)]
#[smi]
fn op_http_start( fn op_http_start(
state: &mut OpState, state: &mut OpState,
tcp_stream_rid: ResourceId, #[smi] tcp_stream_rid: ResourceId,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
if let Ok(resource_rc) = state if let Ok(resource_rc) = state
.resource_table .resource_table
@ -96,11 +97,11 @@ pub struct HttpUpgradeResult {
read_buf: ToJsBuffer, read_buf: ToJsBuffer,
} }
#[op] #[op2(async)]
#[serde]
async fn op_http_upgrade( async fn op_http_upgrade(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
_: (),
) -> Result<HttpUpgradeResult, AnyError> { ) -> Result<HttpUpgradeResult, AnyError> {
let stream = state let stream = state
.borrow_mut() .borrow_mut()

View file

@ -250,9 +250,9 @@ fn op_system_memory_info(
Ok(sys_info::mem_info()) Ok(sys_info::mem_info())
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(not(windows))] #[cfg(not(windows))]
#[op] #[op2]
#[smi]
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -264,9 +264,9 @@ fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
} }
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(windows)] #[cfg(windows)]
#[op] #[op2]
#[smi]
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -274,9 +274,9 @@ fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
Ok(None) Ok(None)
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(not(windows))] #[cfg(not(windows))]
#[op] #[op2]
#[smi]
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -288,9 +288,9 @@ fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
} }
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(windows)] #[cfg(windows)]
#[op] #[op2]
#[smi]
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()

View file

@ -4,7 +4,6 @@ use std::io::Error;
use std::io::IsTerminal; use std::io::IsTerminal;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2; use deno_core::op2;
use deno_core::OpState; use deno_core::OpState;
use deno_core::ResourceHandle; use deno_core::ResourceHandle;
@ -188,10 +187,10 @@ fn op_isatty(state: &mut OpState, rid: u32) -> Result<bool, AnyError> {
}) })
} }
#[op(fast)] #[op2(fast)]
fn op_console_size( fn op_console_size(
state: &mut OpState, state: &mut OpState,
result: &mut [u32], #[buffer] result: &mut [u32],
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
fn check_console_size( fn check_console_size(
state: &mut OpState, state: &mut OpState,

View file

@ -6,7 +6,7 @@ use crate::web_worker::WebWorkerInternalHandle;
use crate::web_worker::WebWorkerType; use crate::web_worker::WebWorkerType;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op2;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::OpState; use deno_core::OpState;
use deno_fetch::data_url::DataUrl; use deno_fetch::data_url::DataUrl;
@ -33,10 +33,11 @@ pub struct SyncFetchScript {
script: String, script: String,
} }
#[op] #[op2]
#[serde]
pub fn op_worker_sync_fetch( pub fn op_worker_sync_fetch(
state: &mut OpState, state: &mut OpState,
scripts: Vec<String>, #[serde] scripts: Vec<String>,
mut loose_mime_checks: bool, mut loose_mime_checks: bool,
) -> Result<Vec<SyncFetchScript>, AnyError> { ) -> Result<Vec<SyncFetchScript>, AnyError> {
let handle = state.borrow::<WebWorkerInternalHandle>().clone(); let handle = state.borrow::<WebWorkerInternalHandle>().clone();