1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

refactor: rewrite ops to op2 macro (#20628)

Co-authored-by: Matt Mastracci <matthew@mastracci.com>
This commit is contained in:
Bartek Iwańczuk 2023-09-23 21:33:31 +02:00 committed by GitHub
parent 65dccc389a
commit 68851d6f37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 48 deletions

View file

@ -371,11 +371,11 @@ where
Ok(())
}
#[op]
#[op2(fast)]
pub fn op_fs_stat_sync<P>(
state: &mut OpState,
path: String,
stat_out_buf: &mut [u32],
#[string] path: String,
#[buffer] stat_out_buf: &mut [u32],
) -> Result<(), AnyError>
where
P: FsPermissions + 'static,
@ -414,11 +414,11 @@ where
Ok(SerializableStat::from(stat))
}
#[op]
#[op2(fast)]
pub fn op_fs_lstat_sync<P>(
state: &mut OpState,
path: String,
stat_out_buf: &mut [u32],
#[string] path: String,
#[buffer] stat_out_buf: &mut [u32],
) -> Result<(), AnyError>
where
P: FsPermissions + 'static,
@ -1123,16 +1123,17 @@ where
Ok(())
}
#[op]
#[op2(async)]
#[allow(clippy::too_many_arguments)]
pub async fn op_fs_write_file_async<P>(
state: Rc<RefCell<OpState>>,
path: String,
mode: Option<u32>,
#[string] path: String,
#[smi] mode: Option<u32>,
append: bool,
create: bool,
create_new: bool,
data: JsBuffer,
cancel_rid: Option<ResourceId>,
#[buffer] data: JsBuffer,
#[smi] cancel_rid: Option<ResourceId>,
) -> Result<(), AnyError>
where
P: FsPermissions + 'static,
@ -1189,11 +1190,12 @@ where
Ok(buf.into())
}
#[op]
#[op2(async)]
#[serde]
pub async fn op_fs_read_file_async<P>(
state: Rc<RefCell<OpState>>,
path: String,
cancel_rid: Option<ResourceId>,
#[string] path: String,
#[smi] cancel_rid: Option<ResourceId>,
) -> Result<ToJsBuffer, AnyError>
where
P: FsPermissions + 'static,
@ -1228,10 +1230,11 @@ where
Ok(buf.into())
}
#[op]
#[op2]
#[string]
pub fn op_fs_read_file_text_sync<P>(
state: &mut OpState,
path: String,
#[string] path: String,
) -> Result<String, AnyError>
where
P: FsPermissions + 'static,
@ -1247,11 +1250,12 @@ where
Ok(string_from_utf8_lossy(buf))
}
#[op]
#[op2(async)]
#[string]
pub async fn op_fs_read_file_text_async<P>(
state: Rc<RefCell<OpState>>,
path: String,
cancel_rid: Option<ResourceId>,
#[string] path: String,
#[smi] cancel_rid: Option<ResourceId>,
) -> Result<String, AnyError>
where
P: FsPermissions + 'static,
@ -1375,11 +1379,11 @@ pub async fn op_fs_fsync_async(
Ok(())
}
#[op]
#[op2(fast)]
pub fn op_fs_fstat_sync(
state: &mut OpState,
rid: ResourceId,
stat_out_buf: &mut [u32],
#[smi] rid: ResourceId,
#[buffer] stat_out_buf: &mut [u32],
) -> Result<(), AnyError> {
let file = FileResource::get_file(state, rid)?;
let stat = file.stat_sync()?;

View file

@ -25,7 +25,6 @@ use deno_core::error::AnyError;
use deno_core::futures::TryFutureExt;
use deno_core::op;
use deno_core::op2;
use deno_core::serde_v8;
use deno_core::serde_v8::from_v8;
use deno_core::unsync::spawn;
use deno_core::unsync::JoinHandle;
@ -229,11 +228,11 @@ pub fn op_http_set_promise_complete(#[smi] slab_id: SlabId, status: u16) {
http.complete();
}
#[op(v8)]
#[op2]
pub fn op_http_get_request_method_and_url<'scope, HTTP>(
scope: &mut v8::HandleScope<'scope>,
slab_id: SlabId,
) -> serde_v8::Value<'scope>
#[smi] slab_id: SlabId,
) -> v8::Local<'scope, v8::Array>
where
HTTP: HttpPropertyExtractor,
{
@ -290,10 +289,7 @@ where
};
let vec = [method, authority, path, peer_address, port];
let array = v8::Array::new_with_elements(scope, vec.as_slice());
let array_value: v8::Local<v8::Value> = array.into();
array_value.into()
v8::Array::new_with_elements(scope, vec.as_slice())
}
#[op2]
@ -307,11 +303,11 @@ pub fn op_http_get_request_header(
value.map(|value| value.as_bytes().into())
}
#[op(v8)]
#[op2]
pub fn op_http_get_request_headers<'scope>(
scope: &mut v8::HandleScope<'scope>,
slab_id: SlabId,
) -> serde_v8::Value<'scope> {
#[smi] slab_id: SlabId,
) -> v8::Local<'scope, v8::Array> {
let http = slab_get(slab_id);
let headers = &http.request_parts().headers;
// Two slots for each header key/value pair
@ -372,16 +368,14 @@ pub fn op_http_get_request_headers<'scope>(
);
}
let array = v8::Array::new_with_elements(scope, vec.as_slice());
let array_value: v8::Local<v8::Value> = array.into();
array_value.into()
v8::Array::new_with_elements(scope, vec.as_slice())
}
#[op(fast)]
#[op2(fast)]
#[smi]
pub fn op_http_read_request_body(
state: Rc<RefCell<OpState>>,
slab_id: SlabId,
#[smi] slab_id: SlabId,
) -> ResourceId {
let mut http = slab_get(slab_id);
let rid = if let Some(incoming) = http.take_body() {

View file

@ -10,9 +10,8 @@ use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::channel::mpsc;
use deno_core::futures::StreamExt;
use deno_core::op;
use deno_core::op2;
use deno_core::parking_lot::Mutex;
use deno_core::serde_v8;
use deno_core::OpState;
use std::cell::RefCell;
use std::ffi::CString;
@ -536,13 +535,13 @@ pub unsafe fn weak_local(
value
}
#[op(v8)]
#[op2]
fn op_napi_open<NP, 'scope>(
scope: &mut v8::HandleScope<'scope>,
op_state: &mut OpState,
path: String,
global: serde_v8::Value,
) -> std::result::Result<serde_v8::Value<'scope>, AnyError>
#[string] path: String,
global: v8::Local<'scope, v8::Value>,
) -> std::result::Result<v8::Local<'scope, v8::Value>, AnyError>
where
NP: NapiPermissions + 'static,
{
@ -582,7 +581,7 @@ where
let mut env = Env::new(
isolate_ptr,
v8::Global::new(scope, ctx),
v8::Global::new(scope, global.v8_value),
v8::Global::new(scope, global),
async_work_sender,
tsfn_sender,
cleanup_hooks,
@ -640,7 +639,7 @@ where
// NAPI addons can't be unloaded, so we're going to "forget" the library
// object so it lives till the program exit.
std::mem::forget(library);
return Ok(serde_v8::Value { v8_value: exports });
return Ok(exports);
}
// Initializer callback.
@ -673,5 +672,5 @@ where
// NAPI addons can't be unloaded, so we're going to "forget" the library
// object so it lives till the program exit.
std::mem::forget(library);
Ok(serde_v8::Value { v8_value: exports })
Ok(exports)
}