Skip to content

Commit

Permalink
refactor(ops): remove variadic buffers (denoland#9944)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Apr 2, 2021
1 parent adf5761 commit 058579d
Show file tree
Hide file tree
Showing 49 changed files with 332 additions and 385 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ApplySourceMap {
fn op_apply_source_map(
state: &mut OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let args: ApplySourceMap = serde_json::from_value(args)?;

Expand All @@ -56,7 +56,7 @@ fn op_apply_source_map(
fn op_format_diagnostic(
_state: &mut OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let diagnostic: Diagnostics = serde_json::from_value(args)?;
Ok(json!(diagnostic.to_string()))
Expand Down
5 changes: 2 additions & 3 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use deno_core::error::AnyError;
use deno_core::json_op_async;
use deno_core::json_op_sync;
use deno_core::serde_json::Value;
use deno_core::BufVec;
use deno_core::JsRuntime;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
Expand All @@ -18,15 +17,15 @@ use std::rc::Rc;

pub fn reg_json_async<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
F: Fn(Rc<RefCell<OpState>>, Value, BufVec) -> R + 'static,
F: Fn(Rc<RefCell<OpState>>, Value, Option<ZeroCopyBuf>) -> R + 'static,
R: Future<Output = Result<Value, AnyError>> + 'static,
{
rt.register_op(name, metrics_op(name, json_op_async(op_fn)));
}

pub fn reg_json_sync<F>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, AnyError>
F: Fn(&mut OpState, Value, Option<ZeroCopyBuf>) -> Result<Value, AnyError>
+ 'static,
{
rt.register_op(name, metrics_op(name, json_op_sync(op_fn)));
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/runtime_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use deno_core::resolve_url_or_path;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::BufVec;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use deno_runtime::permissions::Permissions;
use serde::Deserialize;
use std::cell::RefCell;
Expand Down Expand Up @@ -54,7 +54,7 @@ struct EmitArgs {
async fn op_emit(
state: Rc<RefCell<OpState>>,
args: Value,
_data: BufVec,
_data: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
deno_runtime::ops::check_unstable2(&state, "Deno.emit");
let args: EmitArgs = serde_json::from_value(args)?;
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ rusty_v8 = "0.21.0"
serde = { version = "1.0.123", features = ["derive"] }
serde_json = { version = "1.0.62", features = ["preserve_order"] }
serde_v8 = { version = "0.1.0", path = "../serde_v8" }
smallvec = "1.6.1"
url = { version = "2.2.0", features = ["serde"] }

[[example]]
Expand Down
4 changes: 4 additions & 0 deletions core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub fn resource_unavailable() -> AnyError {
)
}

pub fn null_opbuf() -> AnyError {
type_error("expected non-null op buffer arg")
}

/// A simple error type that lets the creator specify both the error message and
/// the error class name. This type is private; externally it only ever appears
/// wrapped in an `AnyError`. To retrieve the error class name from a wrapped
Expand Down
2 changes: 1 addition & 1 deletion core/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() {
}

// Write the contents of every buffer to stdout
for buf in zero_copy {
if let Some(buf) = zero_copy {
out.write_all(&buf).unwrap();
}

Expand Down
20 changes: 10 additions & 10 deletions core/examples/http_bench_bin_ops.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::bad_resource_id;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::AsyncRefCell;
use deno_core::BufVec;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
use deno_core::JsRuntime;
Expand Down Expand Up @@ -122,7 +122,7 @@ fn create_js_runtime() -> JsRuntime {
fn op_listen(
state: &mut OpState,
_rid: ResourceId,
_bufs: &mut [ZeroCopyBuf],
_bufs: Option<ZeroCopyBuf>,
) -> Result<u32, AnyError> {
log::debug!("listen");
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
Expand All @@ -136,7 +136,7 @@ fn op_listen(
fn op_close(
state: &mut OpState,
rid: ResourceId,
_bufs: &mut [ZeroCopyBuf],
_bufs: Option<ZeroCopyBuf>,
) -> Result<u32, AnyError> {
log::debug!("close rid={}", rid);
state
Expand All @@ -149,7 +149,7 @@ fn op_close(
async fn op_accept(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
_bufs: BufVec,
_bufs: Option<ZeroCopyBuf>,
) -> Result<u32, AnyError> {
log::debug!("accept rid={}", rid);

Expand All @@ -166,34 +166,34 @@ async fn op_accept(
async fn op_read(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
mut bufs: BufVec,
buf: Option<ZeroCopyBuf>,
) -> Result<u32, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
let mut buf = buf.ok_or_else(null_opbuf)?;
log::debug!("read rid={}", rid);

let stream = state
.borrow()
.resource_table
.get::<TcpStream>(rid)
.ok_or_else(bad_resource_id)?;
let nread = stream.read(&mut bufs[0]).await?;
let nread = stream.read(&mut buf).await?;
Ok(nread as u32)
}

async fn op_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
bufs: BufVec,
buf: Option<ZeroCopyBuf>,
) -> Result<u32, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
let buf = buf.ok_or_else(null_opbuf)?;
log::debug!("write rid={}", rid);

let stream = state
.borrow()
.resource_table
.get::<TcpStream>(rid)
.ok_or_else(bad_resource_id)?;
let nwritten = stream.write(&bufs[0]).await?;
let nwritten = stream.write(&buf).await?;
Ok(nwritten as u32)
}

Expand Down
20 changes: 10 additions & 10 deletions core/examples/http_bench_json_ops.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::bad_resource_id;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::AsyncRefCell;
use deno_core::BufVec;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
use deno_core::JsRuntime;
Expand Down Expand Up @@ -122,7 +122,7 @@ fn create_js_runtime() -> JsRuntime {
fn op_listen(
state: &mut OpState,
_args: (),
_bufs: &mut [ZeroCopyBuf],
_bufs: Option<ZeroCopyBuf>,
) -> Result<ResourceId, AnyError> {
log::debug!("listen");
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
Expand All @@ -136,7 +136,7 @@ fn op_listen(
fn op_close(
state: &mut OpState,
rid: ResourceId,
_buf: &mut [ZeroCopyBuf],
_buf: Option<ZeroCopyBuf>,
) -> Result<(), AnyError> {
log::debug!("close rid={}", rid);
state
Expand All @@ -149,7 +149,7 @@ fn op_close(
async fn op_accept(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
_bufs: BufVec,
_buf: Option<ZeroCopyBuf>,
) -> Result<ResourceId, AnyError> {
log::debug!("accept rid={}", rid);

Expand All @@ -166,34 +166,34 @@ async fn op_accept(
async fn op_read(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
mut bufs: BufVec,
buf: Option<ZeroCopyBuf>,
) -> Result<usize, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
let mut buf = buf.ok_or_else(null_opbuf)?;
log::debug!("read rid={}", rid);

let stream = state
.borrow()
.resource_table
.get::<TcpStream>(rid)
.ok_or_else(bad_resource_id)?;
let nread = stream.read(&mut bufs[0]).await?;
let nread = stream.read(&mut buf).await?;
Ok(nread)
}

async fn op_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
bufs: BufVec,
buf: Option<ZeroCopyBuf>,
) -> Result<usize, AnyError> {
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
let buf = buf.ok_or_else(null_opbuf)?;
log::debug!("write rid={}", rid);

let stream = state
.borrow()
.resource_table
.get::<TcpStream>(rid)
.ok_or_else(bad_resource_id)?;
let nwritten = stream.write(&bufs[0]).await?;
let nwritten = stream.write(&buf).await?;
Ok(nwritten)
}

Expand Down
1 change: 0 additions & 1 deletion core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub use crate::runtime::JsErrorCreateFn;
pub use crate::runtime::JsRuntime;
pub use crate::runtime::RuntimeOptions;
pub use crate::runtime::Snapshot;
pub use crate::zero_copy_buf::BufVec;
pub use crate::zero_copy_buf::ZeroCopyBuf;

pub fn v8_version() -> &'static str {
Expand Down
4 changes: 2 additions & 2 deletions core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Default for OpTable {
pub fn op_resources(
state: &mut OpState,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let serialized_resources: HashMap<u32, String> = state
.resource_table
Expand All @@ -207,7 +207,7 @@ pub fn op_resources(
pub fn op_close(
state: &mut OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let rid = args
.get("rid")
Expand Down
39 changes: 5 additions & 34 deletions core/ops_bin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use crate::error::type_error;
use crate::error::AnyError;
use crate::futures::future::FutureExt;
use crate::serialize_op_result;
use crate::BufVec;
use crate::Op;
use crate::OpFn;
use crate::OpPayload;
Expand Down Expand Up @@ -66,26 +64,13 @@ impl ValueOrVector for u32 {
/// A more complete example is available in the examples directory.
pub fn bin_op_sync<F, R>(op_fn: F) -> Box<OpFn>
where
F: Fn(&mut OpState, u32, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static,
F:
Fn(&mut OpState, u32, Option<ZeroCopyBuf>) -> Result<R, AnyError> + 'static,
R: ValueOrVector,
{
Box::new(move |state, payload, buf| -> Op {
let min_arg: u32 = payload.deserialize().unwrap();
// For sig compat map Option<ZeroCopyBuf> to BufVec
let mut bufs: BufVec = match buf {
Some(b) => vec![b],
None => vec![],
}
.into();
// Bin op buffer arg assert
if bufs.is_empty() {
return Op::Sync(serialize_bin_result::<u32>(
Err(type_error("bin-ops require a non-null buffer arg")),
state,
));
}

let result = op_fn(&mut state.borrow_mut(), min_arg, &mut bufs);
let result = op_fn(&mut state.borrow_mut(), min_arg, buf);
Op::Sync(serialize_bin_result(result, state))
})
}
Expand Down Expand Up @@ -138,7 +123,7 @@ where
/// A more complete example is available in the examples directory.
pub fn bin_op_async<F, R, RV>(op_fn: F) -> Box<OpFn>
where
F: Fn(Rc<RefCell<OpState>>, u32, BufVec) -> R + 'static,
F: Fn(Rc<RefCell<OpState>>, u32, Option<ZeroCopyBuf>) -> R + 'static,
R: Future<Output = Result<RV, AnyError>> + 'static,
RV: ValueOrVector,
{
Expand All @@ -148,21 +133,7 @@ where
b: Option<ZeroCopyBuf>|
-> Op {
let min_arg: u32 = p.deserialize().unwrap();
// For sig compat map Option<ZeroCopyBuf> to BufVec
let bufs: BufVec = match b {
Some(b) => vec![b],
None => vec![],
}
.into();
// Bin op buffer arg assert
if bufs.is_empty() {
return Op::Sync(serialize_bin_result::<u32>(
Err(type_error("bin-ops require a non-null buffer arg")),
state,
));
}

let fut = op_fn(state.clone(), min_arg, bufs)
let fut = op_fn(state.clone(), min_arg, b)
.map(move |result| serialize_bin_result(result, state));
let temp = Box::pin(fut);
Op::Async(temp)
Expand Down

0 comments on commit 058579d

Please sign in to comment.