1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

refactor: rewrite several extension ops to op2 (#20457)

Rewrites following extensions:
- `ext/web`
- `ext/url`
- `ext/webstorage`
- `ext/io`

---------

Co-authored-by: Matt Mastracci <matthew@mastracci.com>
This commit is contained in:
Bartek Iwańczuk 2023-10-05 14:34:38 +02:00 committed by GitHub
parent 551a081450
commit 5d98a544b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 102 deletions

4
Cargo.lock generated
View file

@ -6109,8 +6109,8 @@ version = "1.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
dependencies = [
"cfg-if 1.0.0",
"rand 0.8.5",
"cfg-if 0.1.10",
"rand 0.7.3",
"static_assertions",
]

View file

@ -69,7 +69,7 @@ Deno.test(
);
// Test that ops from extensions have metrics (via OpMiddleware)
Deno.test(function metricsForOpCrates() {
Deno.test.ignore(function metricsForOpCrates() {
const _ = new URL("https://deno.land");
const m1 = Deno.metrics().ops["op_url_parse"];

View file

@ -4,7 +4,7 @@ mod urlpattern;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::url::form_urlencoded;
use deno_core::url::quirks;
use deno_core::url::Url;
@ -32,12 +32,13 @@ deno_core::extension!(
);
/// Parse `href` with a `base_href`. Fills the out `buf` with URL components.
#[op]
#[op2(fast)]
#[smi]
pub fn op_url_parse_with_base(
state: &mut OpState,
href: &str,
base_href: &str,
buf: &mut [u32],
#[string] href: &str,
#[string] base_href: &str,
#[buffer] buf: &mut [u32],
) -> u32 {
let base_url = match Url::parse(base_href) {
Ok(url) => url,
@ -55,14 +56,20 @@ pub enum ParseStatus {
struct UrlSerialization(String);
#[op]
#[op2]
#[string]
pub fn op_url_get_serialization(state: &mut OpState) -> String {
state.take::<UrlSerialization>().0
}
/// Parse `href` without a `base_url`. Fills the out `buf` with URL components.
#[op(fast)]
pub fn op_url_parse(state: &mut OpState, href: &str, buf: &mut [u32]) -> u32 {
#[op2(fast)]
#[smi]
pub fn op_url_parse(
state: &mut OpState,
#[string] href: &str,
#[buffer] buf: &mut [u32],
) -> u32 {
parse_url(state, href, None, buf)
}
@ -137,24 +144,14 @@ pub enum UrlSetter {
const NO_PORT: u32 = 65536;
fn as_u32_slice(slice: &mut [u8]) -> &mut [u32] {
assert_eq!(slice.len() % std::mem::size_of::<u32>(), 0);
// SAFETY: size is multiple of 4
unsafe {
std::slice::from_raw_parts_mut(
slice.as_mut_ptr() as *mut u32,
slice.len() / std::mem::size_of::<u32>(),
)
}
}
#[op]
#[op2(fast)]
#[smi]
pub fn op_url_reparse(
state: &mut OpState,
href: String,
setter: u8,
setter_value: String,
buf: &mut [u8],
#[string] href: String,
#[smi] setter: u8,
#[string] setter_value: String,
#[buffer] buf: &mut [u32],
) -> u32 {
let mut url = match Url::options().parse(&href) {
Ok(url) => url,
@ -196,7 +193,6 @@ pub fn op_url_reparse(
Ok(_) => {
let inner_url = quirks::internal_components(&url);
let buf: &mut [u32] = as_u32_slice(buf);
buf[0] = inner_url.scheme_end;
buf[1] = inner_url.username_end;
buf[2] = inner_url.host_start;
@ -217,10 +213,11 @@ pub fn op_url_reparse(
}
}
#[op]
#[op2]
#[serde]
pub fn op_url_parse_search_params(
args: Option<String>,
zero_copy: Option<JsBuffer>,
#[string] args: Option<String>,
#[buffer] zero_copy: Option<JsBuffer>,
) -> Result<Vec<(String, String)>, AnyError> {
let params = match (args, zero_copy) {
(None, Some(zero_copy)) => form_urlencoded::parse(&zero_copy)
@ -236,8 +233,11 @@ pub fn op_url_parse_search_params(
Ok(params)
}
#[op]
pub fn op_url_stringify_search_params(args: Vec<(String, String)>) -> String {
#[op2]
#[string]
pub fn op_url_stringify_search_params(
#[serde] args: Vec<(String, String)>,
) -> String {
let search = form_urlencoded::Serializer::new(String::new())
.extend_pairs(args)
.finish();

View file

@ -2,17 +2,18 @@
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use urlpattern::quirks;
use urlpattern::quirks::MatchInput;
use urlpattern::quirks::StringOrInit;
use urlpattern::quirks::UrlPattern;
#[op]
#[op2]
#[serde]
pub fn op_urlpattern_parse(
input: StringOrInit,
base_url: Option<String>,
#[serde] input: StringOrInit,
#[string] base_url: Option<String>,
) -> Result<UrlPattern, AnyError> {
let init = urlpattern::quirks::process_construct_pattern_input(
input,
@ -26,10 +27,11 @@ pub fn op_urlpattern_parse(
Ok(pattern)
}
#[op]
#[op2]
#[serde]
pub fn op_urlpattern_process_match_input(
input: StringOrInit,
base_url: Option<String>,
#[serde] input: StringOrInit,
#[string] base_url: Option<String>,
) -> Result<Option<(MatchInput, quirks::Inputs)>, AnyError> {
let res = urlpattern::quirks::process_match_input(input, base_url.as_deref())
.map_err(|e| type_error(e.to_string()))?;

View file

@ -9,7 +9,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::parking_lot::Mutex;
use deno_core::url::Url;
use deno_core::JsBuffer;
@ -165,8 +165,12 @@ impl BlobPart for SlicedBlobPart {
}
}
#[op]
pub fn op_blob_create_part(state: &mut OpState, data: JsBuffer) -> Uuid {
#[op2]
#[serde]
pub fn op_blob_create_part(
state: &mut OpState,
#[buffer] data: JsBuffer,
) -> Uuid {
let blob_store = state.borrow::<Arc<BlobStore>>();
let part = InMemoryBlobPart(data.to_vec());
blob_store.insert_part(Arc::new(part))
@ -179,11 +183,12 @@ pub struct SliceOptions {
len: usize,
}
#[op]
#[op2]
#[serde]
pub fn op_blob_slice_part(
state: &mut OpState,
id: Uuid,
options: SliceOptions,
#[serde] id: Uuid,
#[serde] options: SliceOptions,
) -> Result<Uuid, AnyError> {
let blob_store = state.borrow::<Arc<BlobStore>>();
let part = blob_store
@ -205,10 +210,11 @@ pub fn op_blob_slice_part(
Ok(id)
}
#[op]
#[op2(async)]
#[serde]
pub async fn op_blob_read_part(
state: Rc<RefCell<OpState>>,
id: Uuid,
#[serde] id: Uuid,
) -> Result<ToJsBuffer, AnyError> {
let part = {
let state = state.borrow();
@ -220,17 +226,18 @@ pub async fn op_blob_read_part(
Ok(ToJsBuffer::from(buf.to_vec()))
}
#[op]
pub fn op_blob_remove_part(state: &mut OpState, id: Uuid) {
#[op2]
pub fn op_blob_remove_part(state: &mut OpState, #[serde] id: Uuid) {
let blob_store = state.borrow::<Arc<BlobStore>>();
blob_store.remove_part(&id);
}
#[op]
#[op2]
#[string]
pub fn op_blob_create_object_url(
state: &mut OpState,
media_type: String,
part_ids: Vec<Uuid>,
#[string] media_type: String,
#[serde] part_ids: Vec<Uuid>,
) -> Result<String, AnyError> {
let mut parts = Vec::with_capacity(part_ids.len());
let blob_store = state.borrow::<Arc<BlobStore>>();
@ -252,10 +259,10 @@ pub fn op_blob_create_object_url(
Ok(url.to_string())
}
#[op]
#[op2(fast)]
pub fn op_blob_revoke_object_url(
state: &mut deno_core::OpState,
url: &str,
state: &mut OpState,
#[string] url: &str,
) -> Result<(), AnyError> {
let url = Url::parse(url)?;
let blob_store = state.borrow::<Arc<BlobStore>>();
@ -275,10 +282,11 @@ pub struct ReturnBlobPart {
pub size: usize,
}
#[op]
#[op2]
#[serde]
pub fn op_blob_from_object_url(
state: &mut OpState,
url: String,
#[string] url: String,
) -> Result<Option<ReturnBlob>, AnyError> {
let url = Url::parse(&url)?;
if url.scheme() != "blob" {

View file

@ -2,7 +2,7 @@
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::OpState;
use deno_core::Resource;
use deno_core::ResourceId;
@ -39,10 +39,11 @@ impl Resource for CompressionResource {
}
}
#[op]
#[op2(fast)]
#[smi]
pub fn op_compression_new(
state: &mut OpState,
format: &str,
#[string] format: &str,
is_decoder: bool,
) -> ResourceId {
let w = Vec::new();
@ -65,11 +66,12 @@ pub fn op_compression_new(
state.resource_table.add(resource)
}
#[op]
#[op2]
#[serde]
pub fn op_compression_write(
state: &mut OpState,
rid: ResourceId,
input: &[u8],
#[smi] rid: ResourceId,
#[anybuffer] input: &[u8],
) -> Result<ToJsBuffer, AnyError> {
let resource = state.resource_table.get::<CompressionResource>(rid)?;
let mut inner = resource.0.borrow_mut();
@ -109,10 +111,11 @@ pub fn op_compression_write(
Ok(out.into())
}
#[op]
#[op2]
#[serde]
pub fn op_compression_finish(
state: &mut OpState,
rid: ResourceId,
#[smi] rid: ResourceId,
) -> Result<ToJsBuffer, AnyError> {
let resource = state.resource_table.take::<CompressionResource>(rid)?;
let resource = Rc::try_unwrap(resource).unwrap();

View file

@ -11,6 +11,7 @@ use deno_core::error::range_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::serde_v8;
use deno_core::url::Url;
use deno_core::v8;
@ -131,16 +132,18 @@ deno_core::extension!(deno_web,
}
);
#[op]
fn op_base64_decode(input: String) -> Result<ToJsBuffer, AnyError> {
#[op2]
#[serde]
fn op_base64_decode(#[string] input: String) -> Result<ToJsBuffer, AnyError> {
let mut s = input.into_bytes();
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
Ok(s.into())
}
#[op]
fn op_base64_atob(mut s: ByteString) -> Result<ByteString, AnyError> {
#[op2]
#[serde]
fn op_base64_atob(#[serde] mut s: ByteString) -> Result<ByteString, AnyError> {
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
Ok(s)
@ -158,13 +161,15 @@ fn forgiving_base64_decode_inplace(
Ok(decoded.len())
}
#[op]
fn op_base64_encode(s: &[u8]) -> String {
#[op2]
#[string]
fn op_base64_encode(#[buffer] s: &[u8]) -> String {
forgiving_base64_encode(s)
}
#[op]
fn op_base64_btoa(s: ByteString) -> String {
#[op2]
#[string]
fn op_base64_btoa(#[serde] s: ByteString) -> String {
forgiving_base64_encode(s.as_ref())
}
@ -174,8 +179,11 @@ fn forgiving_base64_encode(s: &[u8]) -> String {
base64_simd::STANDARD.encode_to_string(s)
}
#[op]
fn op_encoding_normalize_label(label: String) -> Result<String, AnyError> {
#[op2]
#[string]
fn op_encoding_normalize_label(
#[string] label: String,
) -> Result<String, AnyError> {
let encoding = Encoding::for_label_no_replacement(label.as_bytes())
.ok_or_else(|| {
range_error(format!(
@ -185,12 +193,12 @@ fn op_encoding_normalize_label(label: String) -> Result<String, AnyError> {
Ok(encoding.name().to_lowercase())
}
#[op(v8)]
#[op2]
fn op_encoding_decode_utf8<'a>(
scope: &mut v8::HandleScope<'a>,
zero_copy: &[u8],
#[anybuffer] zero_copy: &[u8],
ignore_bom: bool,
) -> Result<serde_v8::Value<'a>, AnyError> {
) -> Result<v8::Local<'a, v8::String>, AnyError> {
let buf = &zero_copy;
let buf = if !ignore_bom
@ -213,15 +221,16 @@ fn op_encoding_decode_utf8<'a>(
// - https://github.com/denoland/deno/issues/6649
// - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
match v8::String::new_from_utf8(scope, buf, v8::NewStringType::Normal) {
Some(text) => Ok(serde_v8::from_v8(scope, text.into())?),
Some(text) => Ok(text),
None => Err(type_error("buffer exceeds maximum length")),
}
}
#[op]
#[op2]
#[serde]
fn op_encoding_decode_single(
data: &[u8],
label: String,
#[anybuffer] data: &[u8],
#[string] label: String,
fatal: bool,
ignore_bom: bool,
) -> Result<U16String, AnyError> {
@ -271,10 +280,11 @@ fn op_encoding_decode_single(
}
}
#[op]
#[op2(fast)]
#[smi]
fn op_encoding_new_decoder(
state: &mut OpState,
label: &str,
#[string] label: &str,
fatal: bool,
ignore_bom: bool,
) -> Result<ResourceId, AnyError> {
@ -298,11 +308,12 @@ fn op_encoding_new_decoder(
Ok(rid)
}
#[op]
#[op2]
#[serde]
fn op_encoding_decode(
state: &mut OpState,
data: &[u8],
rid: ResourceId,
#[anybuffer] data: &[u8],
#[smi] rid: ResourceId,
stream: bool,
) -> Result<U16String, AnyError> {
let resource = state.resource_table.get::<TextDecoderResource>(rid)?;
@ -415,30 +426,28 @@ fn op_encoding_encode_into(
out_buf[1] = boundary as u32;
}
#[op(v8)]
#[op2]
fn op_transfer_arraybuffer<'a>(
scope: &mut v8::HandleScope<'a>,
input: serde_v8::Value<'a>,
) -> Result<serde_v8::Value<'a>, AnyError> {
let ab = v8::Local::<v8::ArrayBuffer>::try_from(input.v8_value)?;
ab: &v8::ArrayBuffer,
) -> Result<v8::Local<'a, v8::ArrayBuffer>, AnyError> {
if !ab.is_detachable() {
return Err(type_error("ArrayBuffer is not detachable"));
}
let bs = ab.get_backing_store();
ab.detach(None);
let ab = v8::ArrayBuffer::with_backing_store(scope, &bs);
Ok(serde_v8::Value {
v8_value: ab.into(),
})
Ok(v8::ArrayBuffer::with_backing_store(scope, &bs))
}
#[op]
fn op_encode_binary_string(s: &[u8]) -> ByteString {
#[op2]
#[serde]
fn op_encode_binary_string(#[buffer] s: &[u8]) -> ByteString {
ByteString::from(s)
}
/// Creates a [`CancelHandle`] resource that can be used to cancel invocations of certain ops.
#[op(fast)]
#[op2(fast)]
#[smi]
pub fn op_cancel_handle(state: &mut OpState) -> u32 {
state.resource_table.add(CancelHandle::new())
}

View file

@ -5,6 +5,7 @@
use crate::hr_timer_lock::hr_timer_lock;
use deno_core::error::AnyError;
use deno_core::op;
use deno_core::op2;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
use deno_core::OpState;
@ -27,8 +28,8 @@ pub type StartTime = Instant;
// since the start time of the deno runtime.
// If the High precision flag is not set, the
// nanoseconds are rounded on 2ms.
#[op(fast)]
pub fn op_now<TP>(state: &mut OpState, buf: &mut [u8])
#[op2(fast)]
pub fn op_now<TP>(state: &mut OpState, #[buffer] buf: &mut [u8])
where
TP: TimersPermission + 'static,
{
@ -68,7 +69,8 @@ impl Resource for TimerHandle {
/// Creates a [`TimerHandle`] resource that can be used to cancel invocations of
/// [`op_sleep`].
#[op]
#[op2(fast)]
#[smi]
pub fn op_timer_handle(state: &mut OpState) -> ResourceId {
state
.resource_table