Skip to content

Commit

Permalink
refactor: convert ops to use serde_v8 (denoland#10009)
Browse files Browse the repository at this point in the history
This commit rewrites most of the ops to use "serde_v8" instead
of "json" serialization.
  • Loading branch information
AaronO committed Apr 5, 2021
1 parent 284e6c3 commit 2aed322
Show file tree
Hide file tree
Showing 49 changed files with 840 additions and 980 deletions.
2 changes: 1 addition & 1 deletion op_crates/crypto/01_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
arrayBufferView.byteOffset,
arrayBufferView.byteLength,
);
core.jsonOpSync("op_crypto_get_random_values", {}, ui8);
core.jsonOpSync("op_crypto_get_random_values", null, ui8);
return arrayBufferView;
}

Expand Down
8 changes: 3 additions & 5 deletions op_crates/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::JsRuntime;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
Expand All @@ -29,9 +27,9 @@ pub fn init(isolate: &mut JsRuntime) {

pub fn op_crypto_get_random_values(
state: &mut OpState,
_args: Value,
_args: (),
zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
) -> Result<(), AnyError> {
let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let maybe_seeded_rng = state.try_borrow_mut::<StdRng>();
if let Some(seeded_rng) = maybe_seeded_rng {
Expand All @@ -41,7 +39,7 @@ pub fn op_crypto_get_random_values(
rng.fill(&mut *zero_copy);
}

Ok(json!({}))
Ok(())
}

pub fn get_declaration() -> PathBuf {
Expand Down
22 changes: 11 additions & 11 deletions op_crates/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,29 +884,29 @@
if (body != null) {
zeroCopy = new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
}
return core.jsonOpSync("op_fetch", args, ...(zeroCopy ? [zeroCopy] : []));
return core.jsonOpSync("op_fetch", args, zeroCopy);
}

/**
* @param {{rid: number}} args
* @param {number} rid
* @returns {Promise<{status: number, statusText: string, headers: Record<string,string[]>, url: string, responseRid: number}>}
*/
function opFetchSend(args) {
return core.jsonOpAsync("op_fetch_send", args);
function opFetchSend(rid) {
return core.jsonOpAsync("op_fetch_send", rid);
}

/**
* @param {{rid: number}} args
* @param {number} rid
* @param {Uint8Array} body
* @returns {Promise<void>}
*/
function opFetchRequestWrite(args, body) {
function opFetchRequestWrite(rid, body) {
const zeroCopy = new Uint8Array(
body.buffer,
body.byteOffset,
body.byteLength,
);
return core.jsonOpAsync("op_fetch_request_write", args, zeroCopy);
return core.jsonOpAsync("op_fetch_request_write", rid, zeroCopy);
}

const NULL_BODY_STATUS = [101, 204, 205, 304];
Expand Down Expand Up @@ -1276,7 +1276,7 @@
*/
async write(chunk, controller) {
try {
await opFetchRequestWrite({ rid: requestBodyRid }, chunk);
await opFetchRequestWrite(requestBodyRid, chunk);
} catch (err) {
controller.error(err);
}
Expand All @@ -1288,7 +1288,7 @@
body.pipeTo(writer);
}

return await opFetchSend({ rid: requestRid });
return await opFetchSend(requestRid);
}

/**
Expand Down Expand Up @@ -1400,9 +1400,9 @@
async pull(controller) {
try {
const chunk = new Uint8Array(16 * 1024 + 256);
const { read } = await core.jsonOpAsync(
const read = await core.jsonOpAsync(
"op_fetch_response_read",
{ rid },
rid,
chunk,
);
if (read != 0) {
Expand Down
82 changes: 39 additions & 43 deletions op_crates/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use deno_core::error::AnyError;
use deno_core::futures::Future;
use deno_core::futures::Stream;
use deno_core::futures::StreamExt;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::url::Url;
use deno_core::AsyncRefCell;
use deno_core::CancelFuture;
Expand All @@ -34,6 +32,7 @@ use reqwest::Client;
use reqwest::Method;
use reqwest::Response;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::convert::From;
Expand Down Expand Up @@ -121,11 +120,18 @@ pub struct FetchArgs {
has_body: bool,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchReturn {
request_rid: ResourceId,
request_body_rid: Option<ResourceId>,
}

pub fn op_fetch<FP>(
state: &mut OpState,
args: FetchArgs,
data: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError>
) -> Result<FetchReturn, AnyError>
where
FP: FetchPermissions + 'static,
{
Expand Down Expand Up @@ -164,7 +170,7 @@ where

let mut request = client.request(method, url);

let maybe_request_body_rid = if args.has_body {
let request_body_rid = if args.has_body {
match data {
None => {
// If no body is passed, we return a writer for streaming the body.
Expand Down Expand Up @@ -201,27 +207,31 @@ where
.resource_table
.add(FetchRequestResource(Box::pin(fut)));

Ok(json!({
"requestRid": request_rid,
"requestBodyRid": maybe_request_body_rid
}))
Ok(FetchReturn {
request_rid,
request_body_rid,
})
}

#[derive(Deserialize)]
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchSendArgs {
rid: ResourceId,
pub struct FetchResponse {
status: u16,
status_text: String,
headers: Vec<(String, String)>,
url: String,
response_rid: ResourceId,
}

pub async fn op_fetch_send(
state: Rc<RefCell<OpState>>,
args: FetchSendArgs,
rid: ResourceId,
_data: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
) -> Result<FetchResponse, AnyError> {
let request = state
.borrow_mut()
.resource_table
.take::<FetchRequestResource>(args.rid)
.take::<FetchRequestResource>(rid)
.ok_or_else(bad_resource_id)?;

let request = Rc::try_unwrap(request)
Expand Down Expand Up @@ -266,27 +276,20 @@ pub async fn op_fetch_send(
cancel: CancelHandle::default(),
});

Ok(json!({
"status": status.as_u16(),
"statusText": status.canonical_reason().unwrap_or(""),
"headers": res_headers,
"url": url,
"responseRid": rid,
}))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchRequestWriteArgs {
rid: ResourceId,
Ok(FetchResponse {
status: status.as_u16(),
status_text: status.canonical_reason().unwrap_or("").to_string(),
headers: res_headers,
url,
response_rid: rid,
})
}

pub async fn op_fetch_request_write(
state: Rc<RefCell<OpState>>,
args: FetchRequestWriteArgs,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let rid = args.rid;
) -> Result<(), AnyError> {
let data = data.ok_or_else(null_opbuf)?;
let buf = Vec::from(&*data);

Expand All @@ -299,21 +302,14 @@ pub async fn op_fetch_request_write(
let cancel = RcRef::map(resource, |r| &r.cancel);
body.send(Ok(buf)).or_cancel(cancel).await??;

Ok(json!({}))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchResponseReadArgs {
rid: ResourceId,
Ok(())
}

pub async fn op_fetch_response_read(
state: Rc<RefCell<OpState>>,
args: FetchResponseReadArgs,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let rid = args.rid;
) -> Result<usize, AnyError> {
let data = data.ok_or_else(null_opbuf)?;

let resource = state
Expand All @@ -325,7 +321,7 @@ pub async fn op_fetch_response_read(
let cancel = RcRef::map(resource, |r| &r.cancel);
let mut buf = data.clone();
let read = reader.read(&mut buf).try_or_cancel(cancel).await?;
Ok(json!({ "read": read }))
Ok(read)
}

struct FetchRequestResource(
Expand Down Expand Up @@ -391,7 +387,7 @@ pub fn op_create_http_client<FP>(
state: &mut OpState,
args: CreateHttpClientOptions,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError>
) -> Result<ResourceId, AnyError>
where
FP: FetchPermissions + 'static,
{
Expand All @@ -411,7 +407,7 @@ where
.unwrap();

let rid = state.resource_table.add(HttpClientResource::new(client));
Ok(json!(rid))
Ok(rid)
}

fn get_cert_data(
Expand Down
Loading

0 comments on commit 2aed322

Please sign in to comment.