diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Cargo.toml | 1 | ||||
-rw-r--r-- | core/error.rs | 4 | ||||
-rw-r--r-- | core/examples/hello_world.rs | 2 | ||||
-rw-r--r-- | core/examples/http_bench_bin_ops.rs | 20 | ||||
-rw-r--r-- | core/examples/http_bench_json_ops.rs | 20 | ||||
-rw-r--r-- | core/lib.rs | 1 | ||||
-rw-r--r-- | core/ops.rs | 4 | ||||
-rw-r--r-- | core/ops_bin.rs | 39 | ||||
-rw-r--r-- | core/ops_json.rs | 26 | ||||
-rw-r--r-- | core/plugin_api.rs | 2 | ||||
-rw-r--r-- | core/zero_copy_buf.rs | 3 |
11 files changed, 39 insertions, 83 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index ecfe4701d..1e23f788e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -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]] diff --git a/core/error.rs b/core/error.rs index 563d8ed5e..a269d637f 100644 --- a/core/error.rs +++ b/core/error.rs @@ -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 diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index 3b63d2bda..11fc5ff0e 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -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(); } diff --git a/core/examples/http_bench_bin_ops.rs b/core/examples/http_bench_bin_ops.rs index 371ec60c3..0ba7b6706 100644 --- a/core/examples/http_bench_bin_ops.rs +++ b/core/examples/http_bench_bin_ops.rs @@ -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; @@ -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(); @@ -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 @@ -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); @@ -166,9 +166,9 @@ 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 @@ -176,16 +176,16 @@ async fn op_read( .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 @@ -193,7 +193,7 @@ async fn op_write( .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) } diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index b1116d757..cb3c64945 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -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; @@ -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(); @@ -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 @@ -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); @@ -166,9 +166,9 @@ 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 @@ -176,16 +176,16 @@ async fn op_read( .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 @@ -193,7 +193,7 @@ async fn op_write( .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) } diff --git a/core/lib.rs b/core/lib.rs index 12ccc1d8a..3ee08689e 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -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 { diff --git a/core/ops.rs b/core/ops.rs index 867bb5210..5bd0928b6 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -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 @@ -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") diff --git a/core/ops_bin.rs b/core/ops_bin.rs index cdd2d630d..b19782449 100644 --- a/core/ops_bin.rs +++ b/core/ops_bin.rs @@ -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; @@ -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)) }) } @@ -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, { @@ -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) diff --git a/core/ops_json.rs b/core/ops_json.rs index ee336830b..21c6b219f 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -2,7 +2,6 @@ use crate::error::AnyError; use crate::serialize_op_result; -use crate::BufVec; use crate::Op; use crate::OpFn; use crate::OpPayload; @@ -39,21 +38,14 @@ use std::rc::Rc; /// A more complete example is available in the examples directory. pub fn json_op_sync<F, V, R>(op_fn: F) -> Box<OpFn> where - F: Fn(&mut OpState, V, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static, + F: Fn(&mut OpState, V, Option<ZeroCopyBuf>) -> Result<R, AnyError> + 'static, V: DeserializeOwned, R: Serialize + 'static, { - Box::new(move |state, payload, buf: Option<ZeroCopyBuf>| -> Op { - // For sig compat map Option<ZeroCopyBuf> to BufVec - let mut bufs: BufVec = match buf { - Some(b) => vec![b], - None => vec![], - } - .into(); - + Box::new(move |state, payload, buf| -> Op { let result = payload .deserialize() - .and_then(|args| op_fn(&mut state.borrow_mut(), args, &mut bufs)); + .and_then(|args| op_fn(&mut state.borrow_mut(), args, buf)); Op::Sync(serialize_op_result(result, state)) }) } @@ -84,26 +76,20 @@ where /// A more complete example is available in the examples directory. pub fn json_op_async<F, V, R, RV>(op_fn: F) -> Box<OpFn> where - F: Fn(Rc<RefCell<OpState>>, V, BufVec) -> R + 'static, + F: Fn(Rc<RefCell<OpState>>, V, Option<ZeroCopyBuf>) -> R + 'static, V: DeserializeOwned, R: Future<Output = Result<RV, AnyError>> + 'static, RV: Serialize + 'static, { let try_dispatch_op = move |state: Rc<RefCell<OpState>>, p: OpPayload, - b: Option<ZeroCopyBuf>| + buf: Option<ZeroCopyBuf>| -> Result<Op, AnyError> { - // For sig compat map Option<ZeroCopyBuf> to BufVec - let bufs: BufVec = match b { - Some(b) => vec![b], - None => vec![], - } - .into(); // Parse args let args = p.deserialize()?; use crate::futures::FutureExt; - let fut = op_fn(state.clone(), args, bufs) + let fut = op_fn(state.clone(), args, buf) .map(move |result| serialize_op_result(result, state)); Ok(Op::Async(Box::pin(fut))) }; diff --git a/core/plugin_api.rs b/core/plugin_api.rs index f91b28403..d1dda160f 100644 --- a/core/plugin_api.rs +++ b/core/plugin_api.rs @@ -15,7 +15,7 @@ pub use crate::ZeroCopyBuf; pub type InitFn = fn(&mut dyn Interface); -pub type DispatchOpFn = fn(&mut dyn Interface, &mut [ZeroCopyBuf]) -> Op; +pub type DispatchOpFn = fn(&mut dyn Interface, Option<ZeroCopyBuf>) -> Op; pub trait Interface { fn register_op(&mut self, name: &str, dispatcher: DispatchOpFn) -> OpId; diff --git a/core/zero_copy_buf.rs b/core/zero_copy_buf.rs index 96baee1ec..75a3caf22 100644 --- a/core/zero_copy_buf.rs +++ b/core/zero_copy_buf.rs @@ -2,12 +2,9 @@ use crate::bindings; use rusty_v8 as v8; -use smallvec::SmallVec; use std::ops::Deref; use std::ops::DerefMut; -pub type BufVec = SmallVec<[ZeroCopyBuf; 2]>; - /// A ZeroCopyBuf encapsulates a slice that's been borrowed from a JavaScript /// ArrayBuffer object. JavaScript objects can normally be garbage collected, /// but the existence of a ZeroCopyBuf inhibits this until it is dropped. It |