1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor: rewrite more ops to op2 macro (#20478)

This commit is contained in:
Bartek Iwańczuk 2023-09-14 23:05:18 +02:00 committed by GitHub
parent dc505e905e
commit 5e7435fb80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 65 deletions

View file

@ -608,7 +608,7 @@ Deno.test("Worker with invalid permission arg", function () {
deno: { permissions: { env: "foo" } },
}),
TypeError,
'Error parsing args at position 0: (deno.permissions.env) invalid value: string "foo", expected "inherit" or boolean or string[]',
'(deno.permissions.env) invalid value: string "foo", expected "inherit" or boolean or string[]',
);
});

View file

@ -20,7 +20,7 @@ use deno_core::futures::stream::Peekable;
use deno_core::futures::FutureExt;
use deno_core::futures::StreamExt;
use deno_core::futures::TryFutureExt;
use deno_core::op;
use deno_core::op2;
use deno_core::unsync::spawn;
use deno_core::AsyncRefCell;
use deno_core::AsyncResult;
@ -511,10 +511,11 @@ struct NextRequestResponse(
String,
);
#[op]
#[op2(async)]
#[serde]
async fn op_http_accept(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<Option<NextRequestResponse>, AnyError> {
let conn = state.borrow().resource_table.get::<HttpConnResource>(rid)?;
@ -613,13 +614,13 @@ fn req_headers(
headers
}
#[op]
#[op2(async)]
async fn op_http_write_headers(
state: Rc<RefCell<OpState>>,
rid: u32,
status: u16,
headers: Vec<(ByteString, ByteString)>,
data: Option<StringOrBuffer>,
#[smi] rid: u32,
#[smi] status: u16,
#[serde] headers: Vec<(ByteString, ByteString)>,
#[serde] data: Option<StringOrBuffer>,
) -> Result<(), AnyError> {
let stream = state
.borrow_mut()
@ -683,10 +684,11 @@ async fn op_http_write_headers(
}
}
#[op]
#[op2]
#[serde]
fn op_http_headers(
state: &mut OpState,
rid: u32,
#[smi] rid: u32,
) -> Result<Vec<(ByteString, ByteString)>, AnyError> {
let stream = state.resource_table.get::<HttpStreamResource>(rid)?;
let rd = RcRef::map(&stream, |r| &r.rd)
@ -832,11 +834,11 @@ fn should_compress(headers: &hyper::HeaderMap) -> bool {
.unwrap_or_default()
}
#[op]
#[op2(async)]
async fn op_http_write_resource(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
stream: ResourceId,
#[smi] rid: ResourceId,
#[smi] stream: ResourceId,
) -> Result<(), AnyError> {
let http_stream = state
.borrow()
@ -891,11 +893,11 @@ async fn op_http_write_resource(
Ok(())
}
#[op]
#[op2(async)]
async fn op_http_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: JsBuffer,
#[smi] rid: ResourceId,
#[buffer] buf: JsBuffer,
) -> Result<(), AnyError> {
let stream = state
.borrow()
@ -944,10 +946,10 @@ async fn op_http_write(
/// Gracefully closes the write half of the HTTP stream. Note that this does not
/// remove the HTTP stream resource from the resource table; it still has to be
/// closed with `Deno.core.close()`.
#[op]
#[op2(async)]
async fn op_http_shutdown(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
let stream = state
.borrow()
@ -979,8 +981,11 @@ async fn op_http_shutdown(
Ok(())
}
#[op]
fn op_http_websocket_accept_header(key: String) -> Result<String, AnyError> {
#[op2]
#[string]
fn op_http_websocket_accept_header(
#[string] key: String,
) -> Result<String, AnyError> {
let digest = ring::digest::digest(
&ring::digest::SHA1_FOR_LEGACY_USE_ONLY,
format!("{key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11").as_bytes(),
@ -988,10 +993,11 @@ fn op_http_websocket_accept_header(key: String) -> Result<String, AnyError> {
Ok(base64::encode(digest))
}
#[op]
#[op2(async)]
#[smi]
async fn op_http_upgrade_websocket(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<ResourceId, AnyError> {
let stream = state
.borrow_mut()

View file

@ -18,7 +18,7 @@ use codec::encode_key;
use deno_core::anyhow::Context;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::serde_v8::AnyValue;
use deno_core::serde_v8::BigInt;
use deno_core::ByteString;
@ -95,10 +95,11 @@ impl<DB: Database + 'static> Resource for DatabaseResource<DB> {
}
}
#[op]
#[op2(async)]
#[smi]
async fn op_kv_database_open<DBH>(
state: Rc<RefCell<OpState>>,
path: Option<String>,
#[string] path: Option<String>,
) -> Result<ResourceId, AnyError>
where
DBH: DatabaseHandler + 'static,
@ -234,12 +235,13 @@ type SnapshotReadRange = (
Option<ByteString>,
);
#[op]
#[op2(async)]
#[serde]
async fn op_kv_snapshot_read<DBH>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
ranges: Vec<SnapshotReadRange>,
consistency: V8Consistency,
#[smi] rid: ResourceId,
#[serde] ranges: Vec<SnapshotReadRange>,
#[serde] consistency: V8Consistency,
) -> Result<Vec<Vec<ToV8KvEntry>>, AnyError>
where
DBH: DatabaseHandler + 'static,
@ -315,10 +317,11 @@ impl<QMH: QueueMessageHandle + 'static> Resource for QueueMessageResource<QMH> {
}
}
#[op]
#[op2(async)]
#[serde]
async fn op_kv_dequeue_next_message<DBH>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<(ToJsBuffer, ResourceId), AnyError>
where
DBH: DatabaseHandler + 'static,
@ -339,10 +342,10 @@ where
Ok((payload, handle_rid))
}
#[op]
#[op2(async)]
async fn op_kv_finish_dequeued_message<DBH>(
state: Rc<RefCell<OpState>>,
handle_rid: ResourceId,
#[smi] handle_rid: ResourceId,
success: bool,
) -> Result<(), AnyError>
where
@ -598,13 +601,14 @@ fn decode_selector_and_cursor(
Ok((first_key, last_key))
}
#[op]
#[op2(async)]
#[string]
async fn op_kv_atomic_write<DBH>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
checks: Vec<V8KvCheck>,
mutations: Vec<V8KvMutation>,
enqueues: Vec<V8Enqueue>,
#[smi] rid: ResourceId,
#[serde] checks: Vec<V8KvCheck>,
#[serde] mutations: Vec<V8KvMutation>,
#[serde] enqueues: Vec<V8Enqueue>,
) -> Result<Option<String>, AnyError>
where
DBH: DatabaseHandler + 'static,
@ -697,10 +701,11 @@ where
// (prefix, start, end)
type EncodeCursorRangeSelector = (Option<KvKey>, Option<KvKey>, Option<KvKey>);
#[op]
#[op2]
#[string]
fn op_kv_encode_cursor(
(prefix, start, end): EncodeCursorRangeSelector,
boundary_key: KvKey,
#[serde] (prefix, start, end): EncodeCursorRangeSelector,
#[serde] boundary_key: KvKey,
) -> Result<String, AnyError> {
let selector = RawSelector::from_tuple(prefix, start, end)?;
let boundary_key = encode_v8_key(boundary_key)?;

View file

@ -6,7 +6,7 @@ use std::rc::Rc;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
@ -111,7 +111,8 @@ impl Resource for MessagePortResource {
}
}
#[op]
#[op2]
#[serde]
pub fn op_message_port_create_entangled(
state: &mut OpState,
) -> (ResourceId, ResourceId) {
@ -191,11 +192,11 @@ pub struct JsMessageData {
transferables: Vec<JsTransferable>,
}
#[op]
#[op2]
pub fn op_message_port_post_message(
state: &mut OpState,
rid: ResourceId,
data: JsMessageData,
#[smi] rid: ResourceId,
#[serde] data: JsMessageData,
) -> Result<(), AnyError> {
for js_transferable in &data.transferables {
if let JsTransferable::MessagePort(id) = js_transferable {
@ -210,10 +211,11 @@ pub fn op_message_port_post_message(
resource.port.send(state, data)
}
#[op]
#[op2(async)]
#[serde]
pub async fn op_message_port_recv_message(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<Option<JsMessageData>, AnyError> {
let resource = {
let state = state.borrow();

View file

@ -6,6 +6,7 @@ use crate::web_worker::WebWorkerInternalHandle;
use crate::web_worker::WebWorkerType;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::CancelFuture;
use deno_core::OpState;
@ -27,10 +28,10 @@ deno_core::extension!(
],
);
#[op]
#[op2]
fn op_worker_post_message(
state: &mut OpState,
data: JsMessageData,
#[serde] data: JsMessageData,
) -> Result<(), AnyError> {
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
handle.port.send(state, data)?;
@ -52,7 +53,7 @@ async fn op_worker_recv_message(
.await?
}
#[op]
#[op2(fast)]
fn op_worker_close(state: &mut OpState) {
// Notify parent that we're finished
let mut handle = state.borrow_mut::<WebWorkerInternalHandle>().clone();
@ -60,7 +61,8 @@ fn op_worker_close(state: &mut OpState) {
handle.terminate();
}
#[op]
#[op2]
#[serde]
fn op_worker_get_type(state: &mut OpState) -> WebWorkerType {
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
handle.worker_type

View file

@ -13,7 +13,7 @@ use crate::web_worker::WorkerControlEvent;
use crate::web_worker::WorkerId;
use crate::worker::FormatJsErrorFn;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::serde::Deserialize;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
@ -114,10 +114,11 @@ pub struct CreateWorkerArgs {
}
/// Create worker as the host
#[op]
#[op2]
#[serde]
fn op_create_worker(
state: &mut OpState,
args: CreateWorkerArgs,
#[serde] args: CreateWorkerArgs,
) -> Result<WorkerId, AnyError> {
let specifier = args.specifier.clone();
let maybe_source_code = if args.has_source_code {
@ -221,8 +222,8 @@ fn op_create_worker(
Ok(worker_id)
}
#[op]
fn op_host_terminate_worker(state: &mut OpState, id: WorkerId) {
#[op2]
fn op_host_terminate_worker(state: &mut OpState, #[serde] id: WorkerId) {
if let Some(worker_thread) = state.borrow_mut::<WorkersTable>().remove(&id) {
worker_thread.terminate();
} else {
@ -271,10 +272,11 @@ fn close_channel(
}
/// Get control event from guest worker as host
#[op]
#[op2(async)]
#[serde]
async fn op_host_recv_ctrl(
state: Rc<RefCell<OpState>>,
id: WorkerId,
#[serde] id: WorkerId,
) -> Result<WorkerControlEvent, AnyError> {
let (worker_handle, cancel_handle) = {
let state = state.borrow();
@ -313,10 +315,11 @@ async fn op_host_recv_ctrl(
}
}
#[op]
#[op2(async)]
#[serde]
async fn op_host_recv_message(
state: Rc<RefCell<OpState>>,
id: WorkerId,
#[serde] id: WorkerId,
) -> Result<Option<JsMessageData>, AnyError> {
let (worker_handle, cancel_handle) = {
let s = state.borrow();
@ -351,11 +354,11 @@ async fn op_host_recv_message(
}
/// Post message to guest worker as host
#[op]
#[op2]
fn op_host_post_message(
state: &mut OpState,
id: WorkerId,
data: JsMessageData,
#[serde] id: WorkerId,
#[serde] data: JsMessageData,
) -> Result<(), AnyError> {
if let Some(worker_thread) = state.borrow::<WorkersTable>().get(&id) {
debug!("post message to worker {}", id);