From 058579da562989ed15c86598053644bbc86c6747 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 2 Apr 2021 15:47:57 +0200 Subject: [PATCH] refactor(ops): remove variadic buffers (#9944) --- Cargo.lock | 1 - cli/ops/errors.rs | 4 +- cli/ops/mod.rs | 5 +- cli/ops/runtime_compiler.rs | 4 +- core/Cargo.toml | 1 - core/error.rs | 4 ++ core/examples/hello_world.rs | 2 +- core/examples/http_bench_bin_ops.rs | 20 +++--- core/examples/http_bench_json_ops.rs | 20 +++--- core/lib.rs | 1 - core/ops.rs | 4 +- core/ops_bin.rs | 39 ++--------- core/ops_json.rs | 26 ++------ core/plugin_api.rs | 2 +- core/zero_copy_buf.rs | 3 - op_crates/crypto/lib.rs | 9 +-- op_crates/fetch/lib.rs | 35 ++++------ op_crates/url/lib.rs | 6 +- op_crates/webgpu/binding.rs | 6 +- op_crates/webgpu/buffer.rs | 16 +++-- op_crates/webgpu/bundle.rs | 29 ++++---- op_crates/webgpu/command_encoder.rs | 26 ++++---- op_crates/webgpu/compute_pass.rs | 26 ++++---- op_crates/webgpu/lib.rs | 8 +-- op_crates/webgpu/pipeline.rs | 8 +-- op_crates/webgpu/queue.rs | 15 +++-- op_crates/webgpu/render_pass.rs | 44 +++++++------ op_crates/webgpu/sampler.rs | 2 +- op_crates/webgpu/shader.rs | 16 +++-- op_crates/webgpu/texture.rs | 4 +- op_crates/websocket/lib.rs | 14 ++-- runtime/ops/fs.rs | 99 ++++++++++++++-------------- runtime/ops/fs_events.rs | 5 +- runtime/ops/io.rs | 22 ++++--- runtime/ops/mod.rs | 10 +-- runtime/ops/net.rs | 32 +++++---- runtime/ops/net_unix.rs | 10 +-- runtime/ops/os.rs | 22 +++---- runtime/ops/permissions.rs | 6 +- runtime/ops/plugin.rs | 12 +--- runtime/ops/process.rs | 7 +- runtime/ops/runtime.rs | 4 +- runtime/ops/signal.rs | 13 ++-- runtime/ops/timers.rs | 21 +++--- runtime/ops/tls.rs | 9 ++- runtime/ops/tty.rs | 6 +- runtime/ops/web_worker.rs | 7 +- runtime/ops/worker_host.rs | 14 ++-- test_plugin/src/lib.rs | 18 +++-- 49 files changed, 332 insertions(+), 385 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00010d2c31f56..fc1d4ca8449c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,7 +576,6 @@ dependencies = [ "serde", "serde_json", "serde_v8", - "smallvec", "tokio", "url", ] diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index 9ab9530137a90..5dc4026f7d16a 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -30,7 +30,7 @@ struct ApplySourceMap { fn op_apply_source_map( state: &mut OpState, args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let args: ApplySourceMap = serde_json::from_value(args)?; @@ -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, ) -> Result { let diagnostic: Diagnostics = serde_json::from_value(args)?; Ok(json!(diagnostic.to_string())) diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index e47722198459d..782b0a201973f 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -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; @@ -18,7 +17,7 @@ use std::rc::Rc; pub fn reg_json_async(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(Rc>, Value, BufVec) -> R + 'static, + F: Fn(Rc>, Value, Option) -> R + 'static, R: Future> + 'static, { rt.register_op(name, metrics_op(name, json_op_async(op_fn))); @@ -26,7 +25,7 @@ where pub fn reg_json_sync(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result + F: Fn(&mut OpState, Value, Option) -> Result + 'static, { rt.register_op(name, metrics_op(name, json_op_sync(op_fn))); diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index 79f378d6dba86..81b400d13ad29 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -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; @@ -54,7 +54,7 @@ struct EmitArgs { async fn op_emit( state: Rc>, args: Value, - _data: BufVec, + _data: Option, ) -> Result { deno_runtime::ops::check_unstable2(&state, "Deno.emit"); let args: EmitArgs = serde_json::from_value(args)?; diff --git a/core/Cargo.toml b/core/Cargo.toml index ecfe4701d6d59..1e23f788ed791 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 563d8ed5ed3ac..a269d637fa19b 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 3b63d2bda059a..11fc5ff0ef475 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 371ec60c30942..0ba7b6706b477 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, ) -> Result { log::debug!("listen"); let addr = "127.0.0.1:4544".parse::().unwrap(); @@ -136,7 +136,7 @@ fn op_listen( fn op_close( state: &mut OpState, rid: ResourceId, - _bufs: &mut [ZeroCopyBuf], + _bufs: Option, ) -> Result { log::debug!("close rid={}", rid); state @@ -149,7 +149,7 @@ fn op_close( async fn op_accept( state: Rc>, rid: ResourceId, - _bufs: BufVec, + _bufs: Option, ) -> Result { log::debug!("accept rid={}", rid); @@ -166,9 +166,9 @@ async fn op_accept( async fn op_read( state: Rc>, rid: ResourceId, - mut bufs: BufVec, + buf: Option, ) -> Result { - 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::(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>, rid: ResourceId, - bufs: BufVec, + buf: Option, ) -> Result { - 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::(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 b1116d757c00d..cb3c64945dd8a 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, ) -> Result { log::debug!("listen"); let addr = "127.0.0.1:4544".parse::().unwrap(); @@ -136,7 +136,7 @@ fn op_listen( fn op_close( state: &mut OpState, rid: ResourceId, - _buf: &mut [ZeroCopyBuf], + _buf: Option, ) -> Result<(), AnyError> { log::debug!("close rid={}", rid); state @@ -149,7 +149,7 @@ fn op_close( async fn op_accept( state: Rc>, rid: ResourceId, - _bufs: BufVec, + _buf: Option, ) -> Result { log::debug!("accept rid={}", rid); @@ -166,9 +166,9 @@ async fn op_accept( async fn op_read( state: Rc>, rid: ResourceId, - mut bufs: BufVec, + buf: Option, ) -> Result { - 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::(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>, rid: ResourceId, - bufs: BufVec, + buf: Option, ) -> Result { - 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::(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 12ccc1d8ad490..3ee08689ebff9 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 867bb521058bd..5bd0928b6e4f5 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, ) -> Result { let serialized_resources: HashMap = 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, ) -> Result { let rid = args .get("rid") diff --git a/core/ops_bin.rs b/core/ops_bin.rs index cdd2d630de87e..b1978244928e0 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(op_fn: F) -> Box where - F: Fn(&mut OpState, u32, &mut [ZeroCopyBuf]) -> Result + 'static, + F: + Fn(&mut OpState, u32, Option) -> Result + 'static, R: ValueOrVector, { Box::new(move |state, payload, buf| -> Op { let min_arg: u32 = payload.deserialize().unwrap(); - // For sig compat map Option 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::( - 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(op_fn: F) -> Box where - F: Fn(Rc>, u32, BufVec) -> R + 'static, + F: Fn(Rc>, u32, Option) -> R + 'static, R: Future> + 'static, RV: ValueOrVector, { @@ -148,21 +133,7 @@ where b: Option| -> Op { let min_arg: u32 = p.deserialize().unwrap(); - // For sig compat map Option 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::( - 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 ee336830be405..21c6b219fc928 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(op_fn: F) -> Box where - F: Fn(&mut OpState, V, &mut [ZeroCopyBuf]) -> Result + 'static, + F: Fn(&mut OpState, V, Option) -> Result + 'static, V: DeserializeOwned, R: Serialize + 'static, { - Box::new(move |state, payload, buf: Option| -> Op { - // For sig compat map Option 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(op_fn: F) -> Box where - F: Fn(Rc>, V, BufVec) -> R + 'static, + F: Fn(Rc>, V, Option) -> R + 'static, V: DeserializeOwned, R: Future> + 'static, RV: Serialize + 'static, { let try_dispatch_op = move |state: Rc>, p: OpPayload, - b: Option| + buf: Option| -> Result { - // For sig compat map Option 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 f91b28403e7d8..d1dda160f498e 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) -> 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 96baee1ec49f0..75a3caf22430c 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 diff --git a/op_crates/crypto/lib.rs b/op_crates/crypto/lib.rs index b8fe3fefb534b..9fc61d871c278 100644 --- a/op_crates/crypto/lib.rs +++ b/op_crates/crypto/lib.rs @@ -2,6 +2,7 @@ #![deny(warnings)] +use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::serde_json::json; use deno_core::serde_json::Value; @@ -29,15 +30,15 @@ pub fn init(isolate: &mut JsRuntime) { pub fn op_crypto_get_random_values( state: &mut OpState, _args: Value, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { - assert_eq!(zero_copy.len(), 1); + let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; let maybe_seeded_rng = state.try_borrow_mut::(); if let Some(seeded_rng) = maybe_seeded_rng { - seeded_rng.fill(&mut *zero_copy[0]); + seeded_rng.fill(&mut *zero_copy); } else { let mut rng = thread_rng(); - rng.fill(&mut *zero_copy[0]); + rng.fill(&mut *zero_copy); } Ok(json!({})) diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index c35c675ad9eef..19f2566c40481 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -4,6 +4,7 @@ use deno_core::error::bad_resource_id; use deno_core::error::generic_error; +use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::futures::Future; @@ -13,7 +14,6 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url::Url; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; @@ -124,7 +124,7 @@ pub struct FetchArgs { pub fn op_fetch( state: &mut OpState, args: FetchArgs, - data: &mut [ZeroCopyBuf], + data: Option, ) -> Result where FP: FetchPermissions + 'static, @@ -165,8 +165,8 @@ where let mut request = client.request(method, url); let maybe_request_body_rid = if args.has_body { - match data.len() { - 0 => { + match data { + None => { // If no body is passed, we return a writer for streaming the body. let (tx, rx) = mpsc::channel::>>(1); request = request.body(Body::wrap_stream(ReceiverStream::new(rx))); @@ -179,12 +179,11 @@ where Some(request_body_rid) } - 1 => { + Some(data) => { // If a body is passed, we use it, and don't return a body for streaming. - request = request.body(Vec::from(&*data[0])); + request = request.body(Vec::from(&*data)); None } - _ => panic!("Invalid number of arguments"), } } else { None @@ -217,7 +216,7 @@ pub struct FetchSendArgs { pub async fn op_fetch_send( state: Rc>, args: FetchSendArgs, - _data: BufVec, + _data: Option, ) -> Result { let request = state .borrow_mut() @@ -285,14 +284,11 @@ pub struct FetchRequestWriteArgs { pub async fn op_fetch_request_write( state: Rc>, args: FetchRequestWriteArgs, - data: BufVec, + data: Option, ) -> Result { let rid = args.rid; - - let buf = match data.len() { - 1 => Vec::from(&*data[0]), - _ => panic!("Invalid number of arguments"), - }; + let data = data.ok_or_else(null_opbuf)?; + let buf = Vec::from(&*data); let resource = state .borrow() @@ -315,13 +311,10 @@ pub struct FetchResponseReadArgs { pub async fn op_fetch_response_read( state: Rc>, args: FetchResponseReadArgs, - data: BufVec, + data: Option, ) -> Result { let rid = args.rid; - - if data.len() != 1 { - panic!("Invalid number of arguments"); - } + let data = data.ok_or_else(null_opbuf)?; let resource = state .borrow() @@ -330,7 +323,7 @@ pub async fn op_fetch_response_read( .ok_or_else(bad_resource_id)?; let mut reader = RcRef::map(&resource, |r| &r.reader).borrow_mut().await; let cancel = RcRef::map(resource, |r| &r.cancel); - let mut buf = data[0].clone(); + let mut buf = data.clone(); let read = reader.read(&mut buf).try_or_cancel(cancel).await?; Ok(json!({ "read": read })) } @@ -397,7 +390,7 @@ pub struct CreateHttpClientOptions { pub fn op_create_http_client( state: &mut OpState, args: CreateHttpClientOptions, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result where FP: FetchPermissions + 'static, diff --git a/op_crates/url/lib.rs b/op_crates/url/lib.rs index 3b55e539c5214..f7615725fd2df 100644 --- a/op_crates/url/lib.rs +++ b/op_crates/url/lib.rs @@ -39,7 +39,7 @@ pub struct UrlParseArgs { pub fn op_url_parse( _state: &mut deno_core::OpState, args: UrlParseArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let base_url = args .base_href @@ -120,7 +120,7 @@ pub fn op_url_parse( pub fn op_url_parse_search_params( _state: &mut deno_core::OpState, args: String, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let search_params: Vec<_> = form_urlencoded::parse(args.as_bytes()) .into_iter() @@ -131,7 +131,7 @@ pub fn op_url_parse_search_params( pub fn op_url_stringify_search_params( _state: &mut deno_core::OpState, args: Vec<(String, String)>, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let search = form_urlencoded::Serializer::new(String::new()) .extend_pairs(args) diff --git a/op_crates/webgpu/binding.rs b/op_crates/webgpu/binding.rs index 512ba1608db79..296a968f1ef19 100644 --- a/op_crates/webgpu/binding.rs +++ b/op_crates/webgpu/binding.rs @@ -82,7 +82,7 @@ pub struct CreateBindGroupLayoutArgs { pub fn op_webgpu_create_bind_group_layout( state: &mut OpState, args: CreateBindGroupLayoutArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -224,7 +224,7 @@ pub struct CreatePipelineLayoutArgs { pub fn op_webgpu_create_pipeline_layout( state: &mut OpState, args: CreatePipelineLayoutArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -287,7 +287,7 @@ pub struct CreateBindGroupArgs { pub fn op_webgpu_create_bind_group( state: &mut OpState, args: CreateBindGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state diff --git a/op_crates/webgpu/buffer.rs b/op_crates/webgpu/buffer.rs index 91a44f214ab74..ade4122d53096 100644 --- a/op_crates/webgpu/buffer.rs +++ b/op_crates/webgpu/buffer.rs @@ -1,14 +1,15 @@ // 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::futures::channel::oneshot; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::OpState; +use deno_core::Resource; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; -use deno_core::{BufVec, Resource}; use serde::Deserialize; use std::borrow::Cow; use std::cell::RefCell; @@ -45,7 +46,7 @@ pub struct CreateBufferArgs { pub fn op_webgpu_create_buffer( state: &mut OpState, args: CreateBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -88,7 +89,7 @@ pub struct BufferGetMapAsyncArgs { pub async fn op_webgpu_buffer_get_map_async( state: Rc>, args: BufferGetMapAsyncArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let (sender, receiver) = oneshot::channel::>(); @@ -177,8 +178,9 @@ pub struct BufferGetMappedRangeArgs { pub fn op_webgpu_buffer_get_mapped_range( state: &mut OpState, args: BufferGetMappedRangeArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { + let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let buffer_resource = state .resource_table @@ -196,7 +198,7 @@ pub fn op_webgpu_buffer_get_mapped_range( let slice = unsafe { std::slice::from_raw_parts_mut(slice_pointer, args.size as usize) }; - zero_copy[0].copy_from_slice(slice); + zero_copy.copy_from_slice(slice); let rid = state .resource_table @@ -217,7 +219,7 @@ pub struct BufferUnmapArgs { pub fn op_webgpu_buffer_unmap( state: &mut OpState, args: BufferUnmapArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { let mapped_resource = state .resource_table @@ -233,7 +235,7 @@ pub fn op_webgpu_buffer_unmap( let slice_pointer = mapped_resource.0; let size = mapped_resource.1; - if let Some(buffer) = zero_copy.get(0) { + if let Some(buffer) = zero_copy { let slice = unsafe { std::slice::from_raw_parts_mut(slice_pointer, size) }; slice.copy_from_slice(&buffer); } diff --git a/op_crates/webgpu/bundle.rs b/op_crates/webgpu/bundle.rs index 406b886ccca9e..58915b1080b58 100644 --- a/op_crates/webgpu/bundle.rs +++ b/op_crates/webgpu/bundle.rs @@ -1,6 +1,7 @@ // 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::serde_json::json; use deno_core::serde_json::Value; @@ -44,7 +45,7 @@ pub struct CreateRenderBundleEncoderArgs { pub fn op_webgpu_create_render_bundle_encoder( state: &mut OpState, args: CreateRenderBundleEncoderArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let device_resource = state .resource_table @@ -100,7 +101,7 @@ pub struct RenderBundleEncoderFinishArgs { pub fn op_webgpu_render_bundle_encoder_finish( state: &mut OpState, args: RenderBundleEncoderFinishArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -143,8 +144,10 @@ pub struct RenderBundleEncoderSetBindGroupArgs { pub fn op_webgpu_render_bundle_encoder_set_bind_group( state: &mut OpState, args: RenderBundleEncoderSetBindGroupArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; + let bind_group_resource = state .resource_table .get::(args.bind_group) @@ -170,7 +173,7 @@ pub fn op_webgpu_render_bundle_encoder_set_bind_group( ); }, None => { - let (prefix, data, suffix) = unsafe { zero_copy[0].align_to::() }; + let (prefix, data, suffix) = unsafe { zero_copy.align_to::() }; assert!(prefix.is_empty()); assert!(suffix.is_empty()); unsafe { @@ -198,7 +201,7 @@ pub struct RenderBundleEncoderPushDebugGroupArgs { pub fn op_webgpu_render_bundle_encoder_push_debug_group( state: &mut OpState, args: RenderBundleEncoderPushDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -225,7 +228,7 @@ pub struct RenderBundleEncoderPopDebugGroupArgs { pub fn op_webgpu_render_bundle_encoder_pop_debug_group( state: &mut OpState, args: RenderBundleEncoderPopDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -251,7 +254,7 @@ pub struct RenderBundleEncoderInsertDebugMarkerArgs { pub fn op_webgpu_render_bundle_encoder_insert_debug_marker( state: &mut OpState, args: RenderBundleEncoderInsertDebugMarkerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -279,7 +282,7 @@ pub struct RenderBundleEncoderSetPipelineArgs { pub fn op_webgpu_render_bundle_encoder_set_pipeline( state: &mut OpState, args: RenderBundleEncoderSetPipelineArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pipeline_resource = state .resource_table @@ -311,7 +314,7 @@ pub struct RenderBundleEncoderSetIndexBufferArgs { pub fn op_webgpu_render_bundle_encoder_set_index_buffer( state: &mut OpState, args: RenderBundleEncoderSetIndexBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -348,7 +351,7 @@ pub struct RenderBundleEncoderSetVertexBufferArgs { pub fn op_webgpu_render_bundle_encoder_set_vertex_buffer( state: &mut OpState, args: RenderBundleEncoderSetVertexBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -383,7 +386,7 @@ pub struct RenderBundleEncoderDrawArgs { pub fn op_webgpu_render_bundle_encoder_draw( state: &mut OpState, args: RenderBundleEncoderDrawArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -415,7 +418,7 @@ pub struct RenderBundleEncoderDrawIndexedArgs { pub fn op_webgpu_render_bundle_encoder_draw_indexed( state: &mut OpState, args: RenderBundleEncoderDrawIndexedArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_bundle_encoder_resource = state .resource_table @@ -445,7 +448,7 @@ pub struct RenderBundleEncoderDrawIndirectArgs { pub fn op_webgpu_render_bundle_encoder_draw_indirect( state: &mut OpState, args: RenderBundleEncoderDrawIndirectArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table diff --git a/op_crates/webgpu/command_encoder.rs b/op_crates/webgpu/command_encoder.rs index 5ca26ccd02964..801682f5652c9 100644 --- a/op_crates/webgpu/command_encoder.rs +++ b/op_crates/webgpu/command_encoder.rs @@ -50,7 +50,7 @@ pub struct CreateCommandEncoderArgs { pub fn op_webgpu_create_command_encoder( state: &mut OpState, args: CreateCommandEncoderArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -116,7 +116,7 @@ pub struct CommandEncoderBeginRenderPassArgs { pub fn op_webgpu_command_encoder_begin_render_pass( state: &mut OpState, args: CommandEncoderBeginRenderPassArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let command_encoder_resource = state .resource_table @@ -251,7 +251,7 @@ pub struct CommandEncoderBeginComputePassArgs { pub fn op_webgpu_command_encoder_begin_compute_pass( state: &mut OpState, args: CommandEncoderBeginComputePassArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let command_encoder_resource = state .resource_table @@ -292,7 +292,7 @@ pub struct CommandEncoderCopyBufferToBufferArgs { pub fn op_webgpu_command_encoder_copy_buffer_to_buffer( state: &mut OpState, args: CommandEncoderCopyBufferToBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -361,7 +361,7 @@ pub struct CommandEncoderCopyBufferToTextureArgs { pub fn op_webgpu_command_encoder_copy_buffer_to_texture( state: &mut OpState, args: CommandEncoderCopyBufferToTextureArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -424,7 +424,7 @@ pub struct CommandEncoderCopyTextureToBufferArgs { pub fn op_webgpu_command_encoder_copy_texture_to_buffer( state: &mut OpState, args: CommandEncoderCopyTextureToBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -486,7 +486,7 @@ pub struct CommandEncoderCopyTextureToTextureArgs { pub fn op_webgpu_command_encoder_copy_texture_to_texture( state: &mut OpState, args: CommandEncoderCopyTextureToTextureArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -550,7 +550,7 @@ pub struct CommandEncoderPushDebugGroupArgs { pub fn op_webgpu_command_encoder_push_debug_group( state: &mut OpState, args: CommandEncoderPushDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -575,7 +575,7 @@ pub struct CommandEncoderPopDebugGroupArgs { pub fn op_webgpu_command_encoder_pop_debug_group( state: &mut OpState, args: CommandEncoderPopDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -599,7 +599,7 @@ pub struct CommandEncoderInsertDebugMarkerArgs { pub fn op_webgpu_command_encoder_insert_debug_marker( state: &mut OpState, args: CommandEncoderInsertDebugMarkerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -627,7 +627,7 @@ pub struct CommandEncoderWriteTimestampArgs { pub fn op_webgpu_command_encoder_write_timestamp( state: &mut OpState, args: CommandEncoderWriteTimestampArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -665,7 +665,7 @@ pub struct CommandEncoderResolveQuerySetArgs { pub fn op_webgpu_command_encoder_resolve_query_set( state: &mut OpState, args: CommandEncoderResolveQuerySetArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let command_encoder_resource = state @@ -706,7 +706,7 @@ pub struct CommandEncoderFinishArgs { pub fn op_webgpu_command_encoder_finish( state: &mut OpState, args: CommandEncoderFinishArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let command_encoder_resource = state .resource_table diff --git a/op_crates/webgpu/compute_pass.rs b/op_crates/webgpu/compute_pass.rs index b9d5b12d3b0de..2e1fb1ac14105 100644 --- a/op_crates/webgpu/compute_pass.rs +++ b/op_crates/webgpu/compute_pass.rs @@ -1,6 +1,7 @@ // 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::serde_json::json; use deno_core::serde_json::Value; @@ -32,7 +33,7 @@ pub struct ComputePassSetPipelineArgs { pub fn op_webgpu_compute_pass_set_pipeline( state: &mut OpState, args: ComputePassSetPipelineArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pipeline_resource = state .resource_table @@ -63,7 +64,7 @@ pub struct ComputePassDispatchArgs { pub fn op_webgpu_compute_pass_dispatch( state: &mut OpState, args: ComputePassDispatchArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -91,7 +92,7 @@ pub struct ComputePassDispatchIndirectArgs { pub fn op_webgpu_compute_pass_dispatch_indirect( state: &mut OpState, args: ComputePassDispatchIndirectArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -122,7 +123,7 @@ pub struct ComputePassBeginPipelineStatisticsQueryArgs { pub fn op_webgpu_compute_pass_begin_pipeline_statistics_query( state: &mut OpState, args: ComputePassBeginPipelineStatisticsQueryArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -153,7 +154,7 @@ pub struct ComputePassEndPipelineStatisticsQueryArgs { pub fn op_webgpu_compute_pass_end_pipeline_statistics_query( state: &mut OpState, args: ComputePassEndPipelineStatisticsQueryArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -180,7 +181,7 @@ pub struct ComputePassWriteTimestampArgs { pub fn op_webgpu_compute_pass_write_timestamp( state: &mut OpState, args: ComputePassWriteTimestampArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -212,7 +213,7 @@ pub struct ComputePassEndPassArgs { pub fn op_webgpu_compute_pass_end_pass( state: &mut OpState, args: ComputePassEndPassArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let command_encoder_resource = state .resource_table @@ -252,7 +253,7 @@ pub struct ComputePassSetBindGroupArgs { pub fn op_webgpu_compute_pass_set_bind_group( state: &mut OpState, args: ComputePassSetBindGroupArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { let bind_group_resource = state .resource_table @@ -271,7 +272,8 @@ pub fn op_webgpu_compute_pass_set_bind_group( match args.dynamic_offsets_data { Some(data) => data.as_ptr(), None => { - let (prefix, data, suffix) = zero_copy[0].align_to::(); + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; + let (prefix, data, suffix) = zero_copy.align_to::(); assert!(prefix.is_empty()); assert!(suffix.is_empty()); data[args.dynamic_offsets_data_start..].as_ptr() @@ -294,7 +296,7 @@ pub struct ComputePassPushDebugGroupArgs { pub fn op_webgpu_compute_pass_push_debug_group( state: &mut OpState, args: ComputePassPushDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -322,7 +324,7 @@ pub struct ComputePassPopDebugGroupArgs { pub fn op_webgpu_compute_pass_pop_debug_group( state: &mut OpState, args: ComputePassPopDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table @@ -346,7 +348,7 @@ pub struct ComputePassInsertDebugMarkerArgs { pub fn op_webgpu_compute_pass_insert_debug_marker( state: &mut OpState, args: ComputePassInsertDebugMarkerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let compute_pass_resource = state .resource_table diff --git a/op_crates/webgpu/lib.rs b/op_crates/webgpu/lib.rs index 5c3b993291a5d..b1c8a631d4db9 100644 --- a/op_crates/webgpu/lib.rs +++ b/op_crates/webgpu/lib.rs @@ -7,9 +7,9 @@ use deno_core::error::{bad_resource_id, not_supported}; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::OpState; +use deno_core::Resource; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; -use deno_core::{BufVec, Resource}; use serde::Deserialize; use std::borrow::Cow; use std::cell::RefCell; @@ -194,7 +194,7 @@ pub struct RequestAdapterArgs { pub async fn op_webgpu_request_adapter( state: Rc>, args: RequestAdapterArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let mut state = state.borrow_mut(); check_unstable(&state, "navigator.gpu.requestAdapter"); @@ -299,7 +299,7 @@ pub struct RequestDeviceArgs { pub async fn op_webgpu_request_device( state: Rc>, args: RequestDeviceArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let mut state = state.borrow_mut(); let adapter_resource = state @@ -472,7 +472,7 @@ pub struct CreateQuerySetArgs { pub fn op_webgpu_create_query_set( state: &mut OpState, args: CreateQuerySetArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let device_resource = state .resource_table diff --git a/op_crates/webgpu/pipeline.rs b/op_crates/webgpu/pipeline.rs index 5e9d4534baba0..10e300a57e1f9 100644 --- a/op_crates/webgpu/pipeline.rs +++ b/op_crates/webgpu/pipeline.rs @@ -162,7 +162,7 @@ pub struct CreateComputePipelineArgs { pub fn op_webgpu_create_compute_pipeline( state: &mut OpState, args: CreateComputePipelineArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -229,7 +229,7 @@ pub struct ComputePipelineGetBindGroupLayoutArgs { pub fn op_webgpu_compute_pipeline_get_bind_group_layout( state: &mut OpState, args: ComputePipelineGetBindGroupLayoutArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let compute_pipeline_resource = state @@ -366,7 +366,7 @@ pub struct CreateRenderPipelineArgs { pub fn op_webgpu_create_render_pipeline( state: &mut OpState, args: CreateRenderPipelineArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -617,7 +617,7 @@ pub struct RenderPipelineGetBindGroupLayoutArgs { pub fn op_webgpu_render_pipeline_get_bind_group_layout( state: &mut OpState, args: RenderPipelineGetBindGroupLayoutArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let render_pipeline_resource = state diff --git a/op_crates/webgpu/queue.rs b/op_crates/webgpu/queue.rs index bbfb782de8388..c96e2a15895de 100644 --- a/op_crates/webgpu/queue.rs +++ b/op_crates/webgpu/queue.rs @@ -1,6 +1,7 @@ // 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::serde_json::json; use deno_core::serde_json::Value; @@ -23,7 +24,7 @@ pub struct QueueSubmitArgs { pub fn op_webgpu_queue_submit( state: &mut OpState, args: QueueSubmitArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let queue_resource = state @@ -69,8 +70,9 @@ pub struct QueueWriteBufferArgs { pub fn op_webgpu_write_buffer( state: &mut OpState, args: QueueWriteBufferArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let buffer_resource = state .resource_table @@ -84,8 +86,8 @@ pub fn op_webgpu_write_buffer( let queue = queue_resource.0; let data = match args.size { - Some(size) => &zero_copy[0][args.data_offset..(args.data_offset + size)], - None => &zero_copy[0][args.data_offset..], + Some(size) => &zero_copy[args.data_offset..(args.data_offset + size)], + None => &zero_copy[args.data_offset..], }; let maybe_err = gfx_select!(queue => instance.queue_write_buffer( queue, @@ -110,8 +112,9 @@ pub struct QueueWriteTextureArgs { pub fn op_webgpu_write_texture( state: &mut OpState, args: QueueWriteTextureArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let texture_resource = state .resource_table @@ -144,7 +147,7 @@ pub fn op_webgpu_write_texture( let maybe_err = gfx_select!(queue => instance.queue_write_texture( queue, &destination, - &*zero_copy[0], + &*zero_copy, &data_layout, &wgpu_types::Extent3d { width: args.size.width.unwrap_or(1), diff --git a/op_crates/webgpu/render_pass.rs b/op_crates/webgpu/render_pass.rs index 5c614024e717f..bf3bd092d5350 100644 --- a/op_crates/webgpu/render_pass.rs +++ b/op_crates/webgpu/render_pass.rs @@ -1,6 +1,7 @@ // 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::serde_json::json; use deno_core::serde_json::Value; @@ -37,7 +38,7 @@ pub struct RenderPassSetViewportArgs { pub fn op_webgpu_render_pass_set_viewport( state: &mut OpState, args: RenderPassSetViewportArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -70,7 +71,7 @@ pub struct RenderPassSetScissorRectArgs { pub fn op_webgpu_render_pass_set_scissor_rect( state: &mut OpState, args: RenderPassSetScissorRectArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -107,7 +108,7 @@ pub struct RenderPassSetBlendColorArgs { pub fn op_webgpu_render_pass_set_blend_color( state: &mut OpState, args: RenderPassSetBlendColorArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -137,7 +138,7 @@ pub struct RenderPassSetStencilReferenceArgs { pub fn op_webgpu_render_pass_set_stencil_reference( state: &mut OpState, args: RenderPassSetStencilReferenceArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -163,7 +164,7 @@ pub struct RenderPassBeginPipelineStatisticsQueryArgs { pub fn op_webgpu_render_pass_begin_pipeline_statistics_query( state: &mut OpState, args: RenderPassBeginPipelineStatisticsQueryArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -194,7 +195,7 @@ pub struct RenderPassEndPipelineStatisticsQueryArgs { pub fn op_webgpu_render_pass_end_pipeline_statistics_query( state: &mut OpState, args: RenderPassEndPipelineStatisticsQueryArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -221,7 +222,7 @@ pub struct RenderPassWriteTimestampArgs { pub fn op_webgpu_render_pass_write_timestamp( state: &mut OpState, args: RenderPassWriteTimestampArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -253,7 +254,7 @@ pub struct RenderPassExecuteBundlesArgs { pub fn op_webgpu_render_pass_execute_bundles( state: &mut OpState, args: RenderPassExecuteBundlesArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let mut render_bundle_ids = vec![]; @@ -291,7 +292,7 @@ pub struct RenderPassEndPassArgs { pub fn op_webgpu_render_pass_end_pass( state: &mut OpState, args: RenderPassEndPassArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let command_encoder_resource = state .resource_table @@ -326,8 +327,9 @@ pub struct RenderPassSetBindGroupArgs { pub fn op_webgpu_render_pass_set_bind_group( state: &mut OpState, args: RenderPassSetBindGroupArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let bind_group_resource = state .resource_table .get::(args.bind_group) @@ -353,7 +355,7 @@ pub fn op_webgpu_render_pass_set_bind_group( ); }, None => { - let (prefix, data, suffix) = unsafe { zero_copy[0].align_to::() }; + let (prefix, data, suffix) = unsafe { zero_copy.align_to::() }; assert!(prefix.is_empty()); assert!(suffix.is_empty()); unsafe { @@ -381,7 +383,7 @@ pub struct RenderPassPushDebugGroupArgs { pub fn op_webgpu_render_pass_push_debug_group( state: &mut OpState, args: RenderPassPushDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -409,7 +411,7 @@ pub struct RenderPassPopDebugGroupArgs { pub fn op_webgpu_render_pass_pop_debug_group( state: &mut OpState, args: RenderPassPopDebugGroupArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -433,7 +435,7 @@ pub struct RenderPassInsertDebugMarkerArgs { pub fn op_webgpu_render_pass_insert_debug_marker( state: &mut OpState, args: RenderPassInsertDebugMarkerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -462,7 +464,7 @@ pub struct RenderPassSetPipelineArgs { pub fn op_webgpu_render_pass_set_pipeline( state: &mut OpState, args: RenderPassSetPipelineArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pipeline_resource = state .resource_table @@ -494,7 +496,7 @@ pub struct RenderPassSetIndexBufferArgs { pub fn op_webgpu_render_pass_set_index_buffer( state: &mut OpState, args: RenderPassSetIndexBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -528,7 +530,7 @@ pub struct RenderPassSetVertexBufferArgs { pub fn op_webgpu_render_pass_set_vertex_buffer( state: &mut OpState, args: RenderPassSetVertexBufferArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -563,7 +565,7 @@ pub struct RenderPassDrawArgs { pub fn op_webgpu_render_pass_draw( state: &mut OpState, args: RenderPassDrawArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -595,7 +597,7 @@ pub struct RenderPassDrawIndexedArgs { pub fn op_webgpu_render_pass_draw_indexed( state: &mut OpState, args: RenderPassDrawIndexedArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let render_pass_resource = state .resource_table @@ -625,7 +627,7 @@ pub struct RenderPassDrawIndirectArgs { pub fn op_webgpu_render_pass_draw_indirect( state: &mut OpState, args: RenderPassDrawIndirectArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table @@ -656,7 +658,7 @@ pub struct RenderPassDrawIndexedIndirectArgs { pub fn op_webgpu_render_pass_draw_indexed_indirect( state: &mut OpState, args: RenderPassDrawIndexedIndirectArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let buffer_resource = state .resource_table diff --git a/op_crates/webgpu/sampler.rs b/op_crates/webgpu/sampler.rs index fdff70b77a5d7..b759d0c117864 100644 --- a/op_crates/webgpu/sampler.rs +++ b/op_crates/webgpu/sampler.rs @@ -82,7 +82,7 @@ pub struct CreateSamplerArgs { pub fn op_webgpu_create_sampler( state: &mut OpState, args: CreateSamplerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state diff --git a/op_crates/webgpu/shader.rs b/op_crates/webgpu/shader.rs index 8a0613862c149..63578ce64a8c9 100644 --- a/op_crates/webgpu/shader.rs +++ b/op_crates/webgpu/shader.rs @@ -1,6 +1,7 @@ // 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::serde_json::json; use deno_core::serde_json::Value; @@ -31,7 +32,7 @@ pub struct CreateShaderModuleArgs { pub fn op_webgpu_create_shader_module( state: &mut OpState, args: CreateShaderModuleArgs, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -45,10 +46,15 @@ pub fn op_webgpu_create_shader_module( wgpu_core::pipeline::ShaderModuleSource::Wgsl(Cow::from(code)) } None => wgpu_core::pipeline::ShaderModuleSource::SpirV(Cow::from(unsafe { - let (prefix, data, suffix) = zero_copy[0].align_to::(); - assert!(prefix.is_empty()); - assert!(suffix.is_empty()); - data + match &zero_copy { + Some(zero_copy) => { + let (prefix, data, suffix) = zero_copy.align_to::(); + assert!(prefix.is_empty()); + assert!(suffix.is_empty()); + data + } + None => return Err(null_opbuf()), + } })), }; diff --git a/op_crates/webgpu/texture.rs b/op_crates/webgpu/texture.rs index 2dcff502779e8..24824215cb457 100644 --- a/op_crates/webgpu/texture.rs +++ b/op_crates/webgpu/texture.rs @@ -147,7 +147,7 @@ pub struct CreateTextureArgs { pub fn op_webgpu_create_texture( state: &mut OpState, args: CreateTextureArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let device_resource = state @@ -209,7 +209,7 @@ pub struct CreateTextureViewArgs { pub fn op_webgpu_create_texture_view( state: &mut OpState, args: CreateTextureViewArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let instance = state.borrow::(); let texture_resource = state diff --git a/op_crates/websocket/lib.rs b/op_crates/websocket/lib.rs index d329894509300..79ddbbee27e92 100644 --- a/op_crates/websocket/lib.rs +++ b/op_crates/websocket/lib.rs @@ -1,6 +1,7 @@ // 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::type_error; use deno_core::error::AnyError; use deno_core::futures::stream::SplitSink; @@ -11,7 +12,6 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::url; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::JsRuntime; @@ -94,7 +94,7 @@ pub struct CheckPermissionArgs { pub fn op_ws_check_permission( state: &mut OpState, args: CheckPermissionArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result where WP: WebSocketPermissions + 'static, @@ -116,7 +116,7 @@ pub struct CreateArgs { pub async fn op_ws_create( state: Rc>, args: CreateArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result where WP: WebSocketPermissions + 'static, @@ -223,11 +223,11 @@ pub struct SendArgs { pub async fn op_ws_send( state: Rc>, args: SendArgs, - bufs: BufVec, + buf: Option, ) -> Result { let msg = match args.kind.as_str() { "text" => Message::Text(args.text.unwrap()), - "binary" => Message::Binary(bufs[0].to_vec()), + "binary" => Message::Binary(buf.ok_or_else(null_opbuf)?.to_vec()), "pong" => Message::Pong(vec![]), _ => unreachable!(), }; @@ -254,7 +254,7 @@ pub struct CloseArgs { pub async fn op_ws_close( state: Rc>, args: CloseArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let rid = args.rid; let msg = Message::Close(args.code.map(|c| CloseFrame { @@ -284,7 +284,7 @@ pub struct NextEventArgs { pub async fn op_ws_next_event( state: Rc>, args: NextEventArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let resource = state .borrow_mut() diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index cb20cf471fcfb..bc166b4ad97f5 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -10,7 +10,6 @@ use deno_core::error::AnyError; 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::RcRef; use deno_core::ResourceId; @@ -183,7 +182,7 @@ fn open_helper( fn op_open_sync( state: &mut OpState, args: OpenArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let (path, open_options) = open_helper(state, args)?; let std_file = open_options.open(path)?; @@ -196,7 +195,7 @@ fn op_open_sync( async fn op_open_async( state: Rc>, args: OpenArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let (path, open_options) = open_helper(&mut state.borrow_mut(), args)?; let tokio_file = tokio::fs::OpenOptions::from(open_options) @@ -235,7 +234,7 @@ fn seek_helper(args: SeekArgs) -> Result<(u32, SeekFrom), AnyError> { fn op_seek_sync( state: &mut OpState, args: SeekArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let (rid, seek_from) = seek_helper(args)?; let pos = StdFileResource::with(state, rid, |r| match r { @@ -250,7 +249,7 @@ fn op_seek_sync( async fn op_seek_async( state: Rc>, args: SeekArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let (rid, seek_from) = seek_helper(args)?; @@ -281,7 +280,7 @@ pub struct FdatasyncArgs { fn op_fdatasync_sync( state: &mut OpState, args: FdatasyncArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let rid = args.rid; StdFileResource::with(state, rid, |r| match r { @@ -294,7 +293,7 @@ fn op_fdatasync_sync( async fn op_fdatasync_async( state: Rc>, args: FdatasyncArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -325,7 +324,7 @@ pub struct FsyncArgs { fn op_fsync_sync( state: &mut OpState, args: FsyncArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let rid = args.rid; StdFileResource::with(state, rid, |r| match r { @@ -338,7 +337,7 @@ fn op_fsync_sync( async fn op_fsync_async( state: Rc>, args: FsyncArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -369,7 +368,7 @@ pub struct FstatArgs { fn op_fstat_sync( state: &mut OpState, args: FstatArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.fstat"); let metadata = StdFileResource::with(state, args.rid, |r| match r { @@ -382,7 +381,7 @@ fn op_fstat_sync( async fn op_fstat_async( state: Rc>, args: FstatArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { super::check_unstable2(&state, "Deno.fstat"); @@ -415,7 +414,7 @@ pub struct UmaskArgs { fn op_umask( state: &mut OpState, args: UmaskArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.umask"); // TODO implement umask for Windows @@ -452,7 +451,7 @@ pub struct ChdirArgs { fn op_chdir( state: &mut OpState, args: ChdirArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let d = PathBuf::from(&args.directory); state.borrow::().read.check(&d)?; @@ -471,7 +470,7 @@ pub struct MkdirArgs { fn op_mkdir_sync( state: &mut OpState, args: MkdirArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode.unwrap_or(0o777) & 0o777; @@ -491,7 +490,7 @@ fn op_mkdir_sync( async fn op_mkdir_async( state: Rc>, args: MkdirArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode.unwrap_or(0o777) & 0o777; @@ -527,7 +526,7 @@ pub struct ChmodArgs { fn op_chmod_sync( state: &mut OpState, args: ChmodArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode & 0o777; @@ -553,7 +552,7 @@ fn op_chmod_sync( async fn op_chmod_async( state: Rc>, args: ChmodArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); let mode = args.mode & 0o777; @@ -595,7 +594,7 @@ pub struct ChownArgs { fn op_chown_sync( state: &mut OpState, args: ChownArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); state.borrow::().write.check(&path)?; @@ -623,7 +622,7 @@ fn op_chown_sync( async fn op_chown_async( state: Rc>, args: ChownArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = Path::new(&args.path).to_path_buf(); @@ -665,7 +664,7 @@ pub struct RemoveArgs { fn op_remove_sync( state: &mut OpState, args: RemoveArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let recursive = args.recursive; @@ -707,7 +706,7 @@ fn op_remove_sync( async fn op_remove_async( state: Rc>, args: RemoveArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let recursive = args.recursive; @@ -763,7 +762,7 @@ pub struct CopyFileArgs { fn op_copy_file_sync( state: &mut OpState, args: CopyFileArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let from = PathBuf::from(&args.from); let to = PathBuf::from(&args.to); @@ -788,7 +787,7 @@ fn op_copy_file_sync( async fn op_copy_file_async( state: Rc>, args: CopyFileArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let from = PathBuf::from(&args.from); let to = PathBuf::from(&args.to); @@ -885,7 +884,7 @@ pub struct StatArgs { fn op_stat_sync( state: &mut OpState, args: StatArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let lstat = args.lstat; @@ -902,7 +901,7 @@ fn op_stat_sync( async fn op_stat_async( state: Rc>, args: StatArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let lstat = args.lstat; @@ -934,7 +933,7 @@ pub struct RealpathArgs { fn op_realpath_sync( state: &mut OpState, args: RealpathArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); @@ -955,7 +954,7 @@ fn op_realpath_sync( async fn op_realpath_async( state: Rc>, args: RealpathArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); @@ -989,7 +988,7 @@ pub struct ReadDirArgs { fn op_read_dir_sync( state: &mut OpState, args: ReadDirArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); @@ -1019,7 +1018,7 @@ fn op_read_dir_sync( async fn op_read_dir_async( state: Rc>, args: ReadDirArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); { @@ -1061,7 +1060,7 @@ pub struct RenameArgs { fn op_rename_sync( state: &mut OpState, args: RenameArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1078,7 +1077,7 @@ fn op_rename_sync( async fn op_rename_async( state: Rc>, args: RenameArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1112,7 +1111,7 @@ pub struct LinkArgs { fn op_link_sync( state: &mut OpState, args: LinkArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1131,7 +1130,7 @@ fn op_link_sync( async fn op_link_async( state: Rc>, args: LinkArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1173,7 +1172,7 @@ pub struct SymlinkOptions { fn op_symlink_sync( state: &mut OpState, args: SymlinkArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1222,7 +1221,7 @@ fn op_symlink_sync( async fn op_symlink_async( state: Rc>, args: SymlinkArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let oldpath = PathBuf::from(&args.oldpath); let newpath = PathBuf::from(&args.newpath); @@ -1280,7 +1279,7 @@ pub struct ReadLinkArgs { fn op_read_link_sync( state: &mut OpState, args: ReadLinkArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); @@ -1295,7 +1294,7 @@ fn op_read_link_sync( async fn op_read_link_async( state: Rc>, args: ReadLinkArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); { @@ -1322,7 +1321,7 @@ pub struct FtruncateArgs { fn op_ftruncate_sync( state: &mut OpState, args: FtruncateArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.ftruncate"); let rid = args.rid; @@ -1337,7 +1336,7 @@ fn op_ftruncate_sync( async fn op_ftruncate_async( state: Rc>, args: FtruncateArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { super::check_unstable2(&state, "Deno.ftruncate"); let rid = args.rid; @@ -1371,7 +1370,7 @@ pub struct TruncateArgs { fn op_truncate_sync( state: &mut OpState, args: TruncateArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let len = args.len; @@ -1387,7 +1386,7 @@ fn op_truncate_sync( async fn op_truncate_async( state: Rc>, args: TruncateArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let path = PathBuf::from(&args.path); let len = args.len; @@ -1461,7 +1460,7 @@ pub struct MakeTempArgs { fn op_make_temp_dir_sync( state: &mut OpState, args: MakeTempArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); @@ -1490,7 +1489,7 @@ fn op_make_temp_dir_sync( async fn op_make_temp_dir_async( state: Rc>, args: MakeTempArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); @@ -1524,7 +1523,7 @@ async fn op_make_temp_dir_async( fn op_make_temp_file_sync( state: &mut OpState, args: MakeTempArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); @@ -1553,7 +1552,7 @@ fn op_make_temp_file_sync( async fn op_make_temp_file_async( state: Rc>, args: MakeTempArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let dir = args.dir.map(|s| PathBuf::from(&s)); let prefix = args.prefix.map(String::from); @@ -1595,7 +1594,7 @@ pub struct FutimeArgs { fn op_futime_sync( state: &mut OpState, args: FutimeArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.futimeSync"); let rid = args.rid; @@ -1618,7 +1617,7 @@ fn op_futime_sync( async fn op_futime_async( state: Rc>, args: FutimeArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { super::check_unstable2(&state, "Deno.futime"); let rid = args.rid; @@ -1667,7 +1666,7 @@ pub struct UtimeArgs { fn op_utime_sync( state: &mut OpState, args: UtimeArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.utime"); @@ -1683,7 +1682,7 @@ fn op_utime_sync( async fn op_utime_async( state: Rc>, args: UtimeArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { super::check_unstable(&state.borrow(), "Deno.utime"); @@ -1704,7 +1703,7 @@ async fn op_utime_async( fn op_cwd( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let path = current_dir()?; state diff --git a/runtime/ops/fs_events.rs b/runtime/ops/fs_events.rs index 9bfaf6d47f174..fed28a3d29a7a 100644 --- a/runtime/ops/fs_events.rs +++ b/runtime/ops/fs_events.rs @@ -6,7 +6,6 @@ use deno_core::error::AnyError; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::OpState; @@ -93,7 +92,7 @@ pub struct OpenArgs { fn op_fs_events_open( state: &mut OpState, args: OpenArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let (sender, receiver) = mpsc::channel::>(16); let sender = std::sync::Mutex::new(sender); @@ -134,7 +133,7 @@ pub struct PollArgs { async fn op_fs_events_poll( state: Rc>, args: PollArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let resource = state .borrow() diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index d87cfcf94b87c..e5a571f811ac7 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -1,5 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::error::null_opbuf; use deno_core::error::resource_unavailable; use deno_core::error::AnyError; use deno_core::error::{bad_resource_id, not_supported}; @@ -7,7 +8,6 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::JsRuntime; @@ -523,11 +523,12 @@ impl Resource for StdFileResource { fn op_read_sync( state: &mut OpState, rid: ResourceId, - bufs: &mut [ZeroCopyBuf], + buf: Option, ) -> Result { + let mut buf = buf.ok_or_else(null_opbuf)?; StdFileResource::with(state, rid, move |r| match r { Ok(std_file) => std_file - .read(&mut bufs[0]) + .read(&mut buf) .map(|n: usize| n as u32) .map_err(AnyError::from), Err(_) => Err(not_supported()), @@ -537,9 +538,9 @@ fn op_read_sync( async fn op_read_async( state: Rc>, rid: ResourceId, - mut bufs: BufVec, + buf: Option, ) -> Result { - let buf = &mut bufs[0]; + let buf = &mut buf.ok_or_else(null_opbuf)?; let resource = state .borrow() .resource_table @@ -568,11 +569,12 @@ async fn op_read_async( fn op_write_sync( state: &mut OpState, rid: ResourceId, - bufs: &mut [ZeroCopyBuf], + buf: Option, ) -> Result { + let buf = buf.ok_or_else(null_opbuf)?; StdFileResource::with(state, rid, move |r| match r { Ok(std_file) => std_file - .write(&bufs[0]) + .write(&buf) .map(|nwritten: usize| nwritten as u32) .map_err(AnyError::from), Err(_) => Err(not_supported()), @@ -582,9 +584,9 @@ fn op_write_sync( async fn op_write_async( state: Rc>, rid: ResourceId, - bufs: BufVec, + buf: Option, ) -> Result { - let buf = &bufs[0]; + let buf = &buf.ok_or_else(null_opbuf)?; let resource = state .borrow() .resource_table @@ -616,7 +618,7 @@ struct ShutdownArgs { async fn op_shutdown( state: Rc>, args: ShutdownArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let resource = state .borrow() diff --git a/runtime/ops/mod.rs b/runtime/ops/mod.rs index 073b17c866735..b497f6a42aefd 100644 --- a/runtime/ops/mod.rs +++ b/runtime/ops/mod.rs @@ -31,7 +31,6 @@ use deno_core::json_op_async; use deno_core::json_op_sync; use deno_core::serde::de::DeserializeOwned; use deno_core::serde::Serialize; -use deno_core::BufVec; use deno_core::JsRuntime; use deno_core::OpState; use deno_core::ValueOrVector; @@ -45,7 +44,7 @@ pub fn reg_json_async( name: &'static str, op_fn: F, ) where - F: Fn(Rc>, V, BufVec) -> R + 'static, + F: Fn(Rc>, V, Option) -> R + 'static, V: DeserializeOwned, R: Future> + 'static, RV: Serialize + 'static, @@ -55,7 +54,7 @@ pub fn reg_json_async( pub fn reg_json_sync(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(&mut OpState, V, &mut [ZeroCopyBuf]) -> Result + 'static, + F: Fn(&mut OpState, V, Option) -> Result + 'static, V: DeserializeOwned, R: Serialize + 'static, { @@ -64,7 +63,7 @@ where pub fn reg_bin_async(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(Rc>, u32, BufVec) -> R + 'static, + F: Fn(Rc>, u32, Option) -> R + 'static, R: Future> + 'static, RV: ValueOrVector, { @@ -73,7 +72,8 @@ where pub fn reg_bin_sync(rt: &mut JsRuntime, name: &'static str, op_fn: F) where - F: Fn(&mut OpState, u32, &mut [ZeroCopyBuf]) -> Result + 'static, + F: + Fn(&mut OpState, u32, Option) -> Result + 'static, R: ValueOrVector, { rt.register_op(name, metrics_op(name, bin_op_sync(op_fn))); diff --git a/runtime/ops/net.rs b/runtime/ops/net.rs index 6ec393bac8d72..7d81fcee050c6 100644 --- a/runtime/ops/net.rs +++ b/runtime/ops/net.rs @@ -6,13 +6,13 @@ use crate::resolve_addr::resolve_addr_sync; use deno_core::error::bad_resource; use deno_core::error::custom_error; use deno_core::error::generic_error; +use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::OpState; @@ -63,7 +63,7 @@ pub(crate) struct AcceptArgs { async fn accept_tcp( state: Rc>, args: AcceptArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -110,13 +110,13 @@ async fn accept_tcp( async fn op_accept( state: Rc>, args: Value, - bufs: BufVec, + _buf: Option, ) -> Result { let args: AcceptArgs = serde_json::from_value(args)?; match args.transport.as_str() { - "tcp" => accept_tcp(state, args, bufs).await, + "tcp" => accept_tcp(state, args, _buf).await, #[cfg(unix)] - "unix" => net_unix::accept_unix(state, args, bufs).await, + "unix" => net_unix::accept_unix(state, args, _buf).await, _ => Err(generic_error(format!( "Unsupported transport protocol {}", args.transport @@ -133,10 +133,10 @@ pub(crate) struct ReceiveArgs { async fn receive_udp( state: Rc>, args: ReceiveArgs, - zero_copy: BufVec, + zero_copy: Option, ) -> Result { - assert_eq!(zero_copy.len(), 1, "Invalid number of arguments"); - let mut zero_copy = zero_copy[0].clone(); + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; + let mut zero_copy = zero_copy.clone(); let rid = args.rid; @@ -164,10 +164,8 @@ async fn receive_udp( async fn op_datagram_receive( state: Rc>, args: Value, - zero_copy: BufVec, + zero_copy: Option, ) -> Result { - assert_eq!(zero_copy.len(), 1, "Invalid number of arguments"); - let args: ReceiveArgs = serde_json::from_value(args)?; match args.transport.as_str() { "udp" => receive_udp(state, args, zero_copy).await, @@ -191,10 +189,10 @@ struct SendArgs { async fn op_datagram_send( state: Rc>, args: Value, - zero_copy: BufVec, + zero_copy: Option, ) -> Result { - assert_eq!(zero_copy.len(), 1, "Invalid number of arguments"); - let zero_copy = zero_copy[0].clone(); + let zero_copy = zero_copy.ok_or_else(null_opbuf)?; + let zero_copy = zero_copy.clone(); match serde_json::from_value(args)? { SendArgs { @@ -260,7 +258,7 @@ struct ConnectArgs { async fn op_connect( state: Rc>, args: Value, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { match serde_json::from_value(args)? { ConnectArgs { @@ -424,7 +422,7 @@ fn listen_udp( fn op_listen( state: &mut OpState, args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let permissions = state.borrow::(); match serde_json::from_value(args)? { @@ -550,7 +548,7 @@ pub struct NameServer { async fn op_dns_resolve( state: Rc>, args: ResolveAddrArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let ResolveAddrArgs { query, diff --git a/runtime/ops/net_unix.rs b/runtime/ops/net_unix.rs index 6d9afb70f82e8..0cc001ab48fbc 100644 --- a/runtime/ops/net_unix.rs +++ b/runtime/ops/net_unix.rs @@ -5,16 +5,17 @@ use crate::ops::net::AcceptArgs; use crate::ops::net::ReceiveArgs; use deno_core::error::bad_resource; use deno_core::error::custom_error; +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::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; +use deno_core::ZeroCopyBuf; use serde::Deserialize; use std::borrow::Cow; use std::cell::RefCell; @@ -63,7 +64,7 @@ pub struct UnixListenArgs { pub(crate) async fn accept_unix( state: Rc>, args: AcceptArgs, - _bufs: BufVec, + _bufs: Option, ) -> Result { let rid = args.rid; @@ -100,12 +101,11 @@ pub(crate) async fn accept_unix( pub(crate) async fn receive_unix_packet( state: Rc>, args: ReceiveArgs, - bufs: BufVec, + buf: Option, ) -> Result { - assert_eq!(bufs.len(), 1, "Invalid number of arguments"); + let mut buf = buf.ok_or_else(null_opbuf)?; let rid = args.rid; - let mut buf = bufs.into_iter().next().unwrap(); let resource = state .borrow() diff --git a/runtime/ops/os.rs b/runtime/ops/os.rs index dec7f9bd6a91a..500c023aa64c4 100644 --- a/runtime/ops/os.rs +++ b/runtime/ops/os.rs @@ -28,7 +28,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) { fn op_exec_path( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let current_exe = env::current_exe().unwrap(); state @@ -51,7 +51,7 @@ pub struct SetEnv { fn op_set_env( state: &mut OpState, args: SetEnv, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { state.borrow::().env.check()?; let invalid_key = @@ -67,7 +67,7 @@ fn op_set_env( fn op_env( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { state.borrow::().env.check()?; let v = env::vars().collect::>(); @@ -82,7 +82,7 @@ pub struct GetEnv { fn op_get_env( state: &mut OpState, args: GetEnv, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { state.borrow::().env.check()?; if args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]) { @@ -103,7 +103,7 @@ pub struct DeleteEnv { fn op_delete_env( state: &mut OpState, args: DeleteEnv, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { state.borrow::().env.check()?; if args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]) { @@ -121,7 +121,7 @@ pub struct Exit { fn op_exit( _state: &mut OpState, args: Exit, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { std::process::exit(args.code) } @@ -129,7 +129,7 @@ fn op_exit( fn op_loadavg( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.loadavg"); state.borrow::().env.check()?; @@ -142,7 +142,7 @@ fn op_loadavg( fn op_hostname( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.hostname"); state.borrow::().env.check()?; @@ -153,7 +153,7 @@ fn op_hostname( fn op_os_release( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.osRelease"); state.borrow::().env.check()?; @@ -164,7 +164,7 @@ fn op_os_release( fn op_system_memory_info( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.systemMemoryInfo"); state.borrow::().env.check()?; @@ -185,7 +185,7 @@ fn op_system_memory_info( fn op_system_cpu_info( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.systemCpuInfo"); state.borrow::().env.check()?; diff --git a/runtime/ops/permissions.rs b/runtime/ops/permissions.rs index 264df50510b9d..61eed6bf4ebff 100644 --- a/runtime/ops/permissions.rs +++ b/runtime/ops/permissions.rs @@ -28,7 +28,7 @@ pub struct PermissionArgs { pub fn op_query_permission( state: &mut OpState, args: PermissionArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let permissions = state.borrow::(); let path = args.path.as_deref(); @@ -59,7 +59,7 @@ pub fn op_query_permission( pub fn op_revoke_permission( state: &mut OpState, args: PermissionArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let permissions = state.borrow_mut::(); let path = args.path.as_deref(); @@ -90,7 +90,7 @@ pub fn op_revoke_permission( pub fn op_request_permission( state: &mut OpState, args: PermissionArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let permissions = state.borrow_mut::(); let path = args.path.as_deref(); diff --git a/runtime/ops/plugin.rs b/runtime/ops/plugin.rs index 7fc59d082d7da..709c5730dec02 100644 --- a/runtime/ops/plugin.rs +++ b/runtime/ops/plugin.rs @@ -6,7 +6,6 @@ use deno_core::futures::prelude::*; use deno_core::plugin_api; use deno_core::serde_json::json; use deno_core::serde_json::Value; -use deno_core::BufVec; use deno_core::JsRuntime; use deno_core::Op; use deno_core::OpAsyncFuture; @@ -38,7 +37,7 @@ pub struct OpenPluginArgs { pub fn op_open_plugin( state: &mut OpState, args: OpenPluginArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let filename = PathBuf::from(&args.filename); @@ -111,16 +110,9 @@ impl<'a> plugin_api::Interface for PluginInterface<'a> { ) -> OpId { let plugin_lib = self.plugin_lib.clone(); let plugin_op_fn: Box = Box::new(move |state_rc, _payload, buf| { - // For sig compat map Option to BufVec - let mut bufs: BufVec = match buf { - Some(b) => vec![b], - None => vec![], - } - .into(); - let mut state = state_rc.borrow_mut(); let mut interface = PluginInterface::new(&mut state, &plugin_lib); - let op = dispatch_op_fn(&mut interface, &mut bufs); + let op = dispatch_op_fn(&mut interface, buf); match op { sync_op @ Op::Sync(..) => sync_op, Op::Async(fut) => Op::Async(PluginOpAsyncFuture::new(&plugin_lib, fut)), diff --git a/runtime/ops/process.rs b/runtime/ops/process.rs index 437cfac76797a..d6b4dcc1ff064 100644 --- a/runtime/ops/process.rs +++ b/runtime/ops/process.rs @@ -12,7 +12,6 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -85,7 +84,7 @@ impl ChildResource { fn op_run( state: &mut OpState, run_args: RunArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { state.borrow::().run.check()?; @@ -185,7 +184,7 @@ pub struct RunStatusArgs { async fn op_run_status( state: Rc>, args: RunStatusArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -280,7 +279,7 @@ struct KillArgs { fn op_kill( state: &mut OpState, args: KillArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.kill"); state.borrow::().run.check()?; diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index 420931b0c7577..9d29671c9c765 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -24,7 +24,7 @@ pub fn init(rt: &mut deno_core::JsRuntime, main_module: ModuleSpecifier) { fn op_main_module( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let main = state.borrow::().to_string(); let main_url = deno_core::resolve_url_or_path(&main)?; @@ -42,7 +42,7 @@ fn op_main_module( fn op_metrics( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let m = state.borrow::(); let combined = m.combined_metrics(); diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index 0113a61899520..ef29ddec70623 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::AnyError; use deno_core::serde_json::Value; -use deno_core::BufVec; use deno_core::OpState; use deno_core::ZeroCopyBuf; use std::cell::RefCell; @@ -70,7 +69,7 @@ pub struct SignalArgs { fn op_signal_bind( state: &mut OpState, args: BindSignalArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.signal"); let resource = SignalStreamResource { @@ -89,7 +88,7 @@ fn op_signal_bind( async fn op_signal_poll( state: Rc>, args: SignalArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { super::check_unstable2(&state, "Deno.signal"); let rid = args.rid; @@ -112,7 +111,7 @@ async fn op_signal_poll( pub fn op_signal_unbind( state: &mut OpState, args: SignalArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.signal"); let rid = args.rid; @@ -127,7 +126,7 @@ pub fn op_signal_unbind( pub fn op_signal_bind( _state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { unimplemented!(); } @@ -136,7 +135,7 @@ pub fn op_signal_bind( fn op_signal_unbind( _state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { unimplemented!(); } @@ -145,7 +144,7 @@ fn op_signal_unbind( async fn op_signal_poll( _state: Rc>, _args: Value, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { unimplemented!(); } diff --git a/runtime/ops/timers.rs b/runtime/ops/timers.rs index 4395b4885c6a0..b283125de2016 100644 --- a/runtime/ops/timers.rs +++ b/runtime/ops/timers.rs @@ -9,7 +9,7 @@ //! calls it) for an entire Isolate. This is what is implemented here. use crate::permissions::Permissions; -use deno_core::error::type_error; +use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::futures; use deno_core::futures::channel::oneshot; @@ -17,7 +17,6 @@ use deno_core::futures::FutureExt; use deno_core::futures::TryFutureExt; 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 serde::Deserialize; @@ -85,7 +84,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) { fn op_global_timer_stop( state: &mut OpState, _args: Value, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let global_timer = state.borrow_mut::(); global_timer.cancel(); @@ -108,7 +107,7 @@ pub struct GlobalTimerArgs { fn op_global_timer_start( state: &mut OpState, args: GlobalTimerArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let val = args.timeout; @@ -121,7 +120,7 @@ fn op_global_timer_start( async fn op_global_timer( state: Rc>, _args: Value, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let maybe_timer_fut = { let mut s = state.borrow_mut(); @@ -141,13 +140,9 @@ async fn op_global_timer( fn op_now( op_state: &mut OpState, _argument: u32, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Result { - match zero_copy.len() { - 0 => return Err(type_error("no buffer specified")), - 1 => {} - _ => return Err(type_error("Invalid number of arguments")), - } + let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; let start_time = op_state.borrow::(); let seconds = start_time.elapsed().as_secs(); @@ -163,7 +158,7 @@ fn op_now( let result = (seconds * 1_000) as f64 + (subsec_nanos / 1_000_000.0); - (&mut zero_copy[0]).copy_from_slice(&result.to_be_bytes()); + (&mut zero_copy).copy_from_slice(&result.to_be_bytes()); Ok(0) } @@ -177,7 +172,7 @@ pub struct SleepArgs { fn op_sleep_sync( state: &mut OpState, args: SleepArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.sleepSync"); sleep(Duration::from_millis(args.millis)); diff --git a/runtime/ops/tls.rs b/runtime/ops/tls.rs index 52c9d322aaa60..00f000a97aaaa 100644 --- a/runtime/ops/tls.rs +++ b/runtime/ops/tls.rs @@ -14,7 +14,6 @@ use deno_core::error::AnyError; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::AsyncRefCell; -use deno_core::BufVec; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::OpState; @@ -97,7 +96,7 @@ struct StartTlsArgs { async fn op_start_tls( state: Rc>, args: StartTlsArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -167,7 +166,7 @@ async fn op_start_tls( async fn op_connect_tls( state: Rc>, args: ConnectTlsArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { { let s = state.borrow(); @@ -307,7 +306,7 @@ pub struct ListenTlsArgs { fn op_listen_tls( state: &mut OpState, args: ListenTlsArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { assert_eq!(args.transport, "tcp"); @@ -357,7 +356,7 @@ pub struct AcceptTlsArgs { async fn op_accept_tls( state: Rc>, args: AcceptTlsArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let rid = args.rid; diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index 33d04620faf7a..6253cc8370591 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -67,7 +67,7 @@ pub struct SetRawArgs { fn op_set_raw( state: &mut OpState, args: SetRawArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.setRaw"); @@ -222,7 +222,7 @@ pub struct IsattyArgs { fn op_isatty( state: &mut OpState, args: IsattyArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { let rid = args.rid; @@ -263,7 +263,7 @@ struct ConsoleSize { fn op_console_size( state: &mut OpState, args: ConsoleSizeArgs, - _zero_copy: &mut [ZeroCopyBuf], + _zero_copy: Option, ) -> Result { super::check_unstable(state, "Deno.consoleSize"); diff --git a/runtime/ops/web_worker.rs b/runtime/ops/web_worker.rs index 17561882ebb1f..7918b97eacd6b 100644 --- a/runtime/ops/web_worker.rs +++ b/runtime/ops/web_worker.rs @@ -2,6 +2,7 @@ use crate::web_worker::WebWorkerHandle; use crate::web_worker::WorkerEvent; +use deno_core::error::null_opbuf; use deno_core::futures::channel::mpsc; use deno_core::serde_json::{json, Value}; @@ -15,9 +16,9 @@ pub fn init( super::reg_json_sync( rt, "op_worker_post_message", - move |_state, _args: Value, bufs| { - assert_eq!(bufs.len(), 1, "Invalid number of arguments"); - let msg_buf: Box<[u8]> = (*bufs[0]).into(); + move |_state, _args: Value, buf| { + let buf = buf.ok_or_else(null_opbuf)?; + let msg_buf: Box<[u8]> = (*buf).into(); sender_ .clone() .try_send(WorkerEvent::Message(msg_buf)) diff --git a/runtime/ops/worker_host.rs b/runtime/ops/worker_host.rs index 424e7a70c1b2c..6891241dd497c 100644 --- a/runtime/ops/worker_host.rs +++ b/runtime/ops/worker_host.rs @@ -14,6 +14,7 @@ use crate::web_worker::WebWorkerHandle; use crate::web_worker::WorkerEvent; use deno_core::error::custom_error; use deno_core::error::generic_error; +use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::error::JsError; use deno_core::futures::channel::mpsc; @@ -23,7 +24,6 @@ use deno_core::serde::Deserialize; use deno_core::serde::Deserializer; use deno_core::serde_json::json; use deno_core::serde_json::Value; -use deno_core::BufVec; use deno_core::ModuleSpecifier; use deno_core::OpState; use deno_core::ZeroCopyBuf; @@ -369,7 +369,7 @@ pub struct CreateWorkerArgs { fn op_create_worker( state: &mut OpState, args: CreateWorkerArgs, - _data: &mut [ZeroCopyBuf], + _data: Option, ) -> Result { let specifier = args.specifier.clone(); let maybe_source_code = if args.has_source_code { @@ -457,7 +457,7 @@ pub struct WorkerArgs { fn op_host_terminate_worker( state: &mut OpState, args: WorkerArgs, - _data: &mut [ZeroCopyBuf], + _data: Option, ) -> Result { let id = args.id as u32; let worker_thread = state @@ -533,7 +533,7 @@ fn try_remove_and_close(state: Rc>, id: u32) { async fn op_host_get_message( state: Rc>, args: WorkerArgs, - _zero_copy: BufVec, + _zero_copy: Option, ) -> Result { let id = args.id as u32; @@ -567,11 +567,11 @@ async fn op_host_get_message( fn op_host_post_message( state: &mut OpState, args: WorkerArgs, - data: &mut [ZeroCopyBuf], + data: Option, ) -> Result { - assert_eq!(data.len(), 1, "Invalid number of arguments"); + let data = data.ok_or_else(null_opbuf)?; let id = args.id as u32; - let msg = Vec::from(&*data[0]).into_boxed_slice(); + let msg = Vec::from(&*data).into_boxed_slice(); debug!("post message to worker {}", id); let worker_thread = state diff --git a/test_plugin/src/lib.rs b/test_plugin/src/lib.rs index c4b0916a43d32..b84dcef48b911 100644 --- a/test_plugin/src/lib.rs +++ b/test_plugin/src/lib.rs @@ -14,15 +14,14 @@ pub fn deno_plugin_init(interface: &mut dyn Interface) { fn op_test_sync( _interface: &mut dyn Interface, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Op { - if !zero_copy.is_empty() { + if zero_copy.is_some() { println!("Hello from plugin."); } - let zero_copy = zero_copy.to_vec(); - for (idx, buf) in zero_copy.iter().enumerate() { + if let Some(buf) = zero_copy { let buf_str = std::str::from_utf8(&buf[..]).unwrap(); - println!("zero_copy[{}]: {}", idx, buf_str); + println!("zero_copy: {}", buf_str); } let result = b"test"; let result_box: Box<[u8]> = Box::new(*result); @@ -31,16 +30,15 @@ fn op_test_sync( fn op_test_async( _interface: &mut dyn Interface, - zero_copy: &mut [ZeroCopyBuf], + zero_copy: Option, ) -> Op { - if !zero_copy.is_empty() { + if zero_copy.is_some() { println!("Hello from plugin."); } - let zero_copy = zero_copy.to_vec(); let fut = async move { - for (idx, buf) in zero_copy.iter().enumerate() { + if let Some(buf) = zero_copy { let buf_str = std::str::from_utf8(&buf[..]).unwrap(); - println!("zero_copy[{}]: {}", idx, buf_str); + println!("zero_copy: {}", buf_str); } let (tx, rx) = futures::channel::oneshot::channel::>(); std::thread::spawn(move || {