From 77a00ce1fb4ae2523e22b9b84ae09a0200502e38 Mon Sep 17 00:00:00 2001 From: Leo K Date: Tue, 5 Oct 2021 22:38:27 +0200 Subject: [PATCH] chore: various op cleanup (#12329) --- cli/ops/errors.rs | 25 ++++++++++------- cli/ops/runtime_compiler.rs | 29 ++++++++++++-------- core/error.rs | 4 --- core/examples/http_bench_json_ops.rs | 9 +++---- core/ops_builtin.rs | 2 +- core/runtime.rs | 6 +---- ext/broadcast_channel/lib.rs | 6 ++--- ext/crypto/lib.rs | 26 +++++++----------- ext/fetch/lib.rs | 10 +++---- ext/http/lib.rs | 10 +++---- ext/net/io.rs | 19 ++++++------- ext/net/ops.rs | 9 +++---- ext/net/ops_unix.rs | 5 +--- ext/timers/lib.rs | 4 +-- ext/webgpu/buffer.rs | 4 +-- ext/webgpu/queue.rs | 7 ++--- ext/websocket/01_websocket.js | 14 +++++----- ext/websocket/02_websocketstream.js | 12 ++++----- ext/websocket/lib.rs | 26 +++++++++--------- runtime/js/30_os.js | 2 +- runtime/metrics.rs | 2 +- runtime/ops/fs.rs | 2 +- runtime/ops/io.rs | 35 +++++++++++------------- runtime/ops/os.rs | 40 +++++++++------------------- runtime/ops/runtime.rs | 2 +- runtime/ops/signal.rs | 6 ++--- 26 files changed, 131 insertions(+), 185 deletions(-) diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index ea6a5ae5ab..14d21ee84a 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -11,6 +11,7 @@ use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::OpState; use serde::Deserialize; +use serde::Serialize; use std::collections::HashMap; pub fn init(rt: &mut deno_core::JsRuntime) { @@ -27,13 +28,19 @@ struct ApplySourceMap { column_number: i32, } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct AppliedSourceMap { + file_name: String, + line_number: u32, + column_number: u32, +} + fn op_apply_source_map( state: &mut OpState, - args: Value, + args: ApplySourceMap, _: (), -) -> Result { - let args: ApplySourceMap = serde_json::from_value(args)?; - +) -> Result { let mut mappings_map: CachedMaps = HashMap::new(); let ps = state.borrow::().clone(); @@ -46,11 +53,11 @@ fn op_apply_source_map( ps, ); - Ok(json!({ - "fileName": orig_file_name, - "lineNumber": orig_line_number as u32, - "columnNumber": orig_column_number as u32, - })) + Ok(AppliedSourceMap { + file_name: orig_file_name, + line_number: orig_line_number as u32, + column_number: orig_column_number as u32, + }) } fn op_format_diagnostic( diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index e77f4d1e71..d63d97f587 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -14,13 +14,12 @@ use deno_core::error::AnyError; use deno_core::error::Context; use deno_core::parking_lot::Mutex; 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::OpState; use deno_runtime::permissions::Permissions; use import_map::ImportMap; use serde::Deserialize; +use serde::Serialize; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; @@ -50,13 +49,21 @@ struct EmitArgs { sources: Option>>, } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct EmitResult { + diagnostics: crate::diagnostics::Diagnostics, + files: HashMap, + ignored_options: Option, + stats: crate::module_graph::Stats, +} + async fn op_emit( state: Rc>, - args: Value, + args: EmitArgs, _: (), -) -> Result { +) -> Result { deno_runtime::ops::check_unstable2(&state, "Deno.emit"); - let args: EmitArgs = serde_json::from_value(args)?; let root_specifier = args.root_specifier; let ps = state.borrow().borrow::().clone(); let mut runtime_permissions = { @@ -127,10 +134,10 @@ async fn op_emit( })?; result_info.diagnostics.extend_graph_errors(graph_errors); - Ok(json!({ - "diagnostics": result_info.diagnostics, - "files": files, - "ignoredOptions": result_info.maybe_ignored_options, - "stats": result_info.stats, - })) + Ok(EmitResult { + diagnostics: result_info.diagnostics, + files, + ignored_options: result_info.maybe_ignored_options, + stats: result_info.stats, + }) } diff --git a/core/error.rs b/core/error.rs index 087b27c414..be97a90fb8 100644 --- a/core/error.rs +++ b/core/error.rs @@ -67,10 +67,6 @@ 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/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index d273c2c884..5989a472fb 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -1,5 +1,4 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::AsyncRefCell; use deno_core::CancelHandle; @@ -121,7 +120,7 @@ fn create_js_runtime() -> JsRuntime { fn op_listen( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result { log::debug!("listen"); @@ -158,9 +157,8 @@ async fn op_accept( async fn op_read( state: Rc>, rid: ResourceId, - buf: Option, + mut buf: ZeroCopyBuf, ) -> Result { - let mut buf = buf.ok_or_else(null_opbuf)?; log::debug!("read rid={}", rid); let stream = state.borrow().resource_table.get::(rid)?; @@ -171,9 +169,8 @@ async fn op_read( async fn op_write( state: Rc>, rid: ResourceId, - buf: Option, + buf: ZeroCopyBuf, ) -> Result { - let buf = buf.ok_or_else(null_opbuf)?; log::debug!("write rid={}", rid); let stream = state.borrow().resource_table.get::(rid)?; diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs index e1313fa323..3920629603 100644 --- a/core/ops_builtin.rs +++ b/core/ops_builtin.rs @@ -34,7 +34,7 @@ pub(crate) fn init_builtins() -> Extension { /// and string representation as value. pub fn op_resources( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result, AnyError> { let serialized_resources = state diff --git a/core/runtime.rs b/core/runtime.rs index 305052e9ae..e4fe8cd6c5 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1919,11 +1919,7 @@ pub mod tests { #[test] fn test_error_builder() { - fn op_err( - _: &mut OpState, - _: (), - _: Option, - ) -> Result<(), AnyError> { + fn op_err(_: &mut OpState, _: (), _: ()) -> Result<(), AnyError> { Err(custom_error("DOMExceptionOperationError", "abc")) } diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index de6c566679..b499d6fbf3 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -45,8 +45,8 @@ struct Unstable(bool); // --unstable pub fn op_broadcast_subscribe( state: &mut OpState, - _args: (), - _buf: (), + _: (), + _: (), ) -> Result { let unstable = state.borrow::().0; @@ -85,7 +85,7 @@ pub async fn op_broadcast_send( pub async fn op_broadcast_recv( state: Rc>, rid: ResourceId, - _buf: (), + _: (), ) -> Result, AnyError> { let resource = state.borrow().resource_table.get::(rid)?; let bc = state.borrow().borrow::().clone(); diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs index 157a9f04b1..a562eaf01b 100644 --- a/ext/crypto/lib.rs +++ b/ext/crypto/lib.rs @@ -2,7 +2,6 @@ use deno_core::error::custom_error; use deno_core::error::not_supported; -use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::include_js_files; @@ -296,9 +295,8 @@ pub struct SignArg { pub async fn op_crypto_sign_key( _state: Rc>, args: SignArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let data = &*zero_copy; let algorithm = args.algorithm; @@ -451,9 +449,8 @@ pub struct VerifyArg { pub async fn op_crypto_verify_key( _state: Rc>, args: VerifyArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let data = &*zero_copy; let algorithm = args.algorithm; @@ -599,7 +596,7 @@ pub struct ExportKeyArg { pub async fn op_crypto_export_key( _state: Rc>, args: ExportKeyArg, - _zero_copy: Option, + _: (), ) -> Result { let algorithm = args.algorithm; match algorithm { @@ -731,9 +728,8 @@ pub struct DeriveKeyArg { pub async fn op_crypto_derive_bits( _state: Rc>, args: DeriveKeyArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let salt = &*zero_copy; let algorithm = args.algorithm; match algorithm { @@ -798,9 +794,8 @@ pub struct EncryptArg { pub async fn op_crypto_encrypt_key( _state: Rc>, args: EncryptArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let data = &*zero_copy; let algorithm = args.algorithm; @@ -1035,9 +1030,8 @@ pub struct ImportKeyResult { pub async fn op_crypto_import_key( _state: Rc>, args: ImportKeyArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let data = &*zero_copy; let algorithm = args.algorithm; @@ -1359,9 +1353,8 @@ pub struct DecryptArg { pub async fn op_crypto_decrypt_key( _state: Rc>, args: DecryptArg, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let data = &*zero_copy; let algorithm = args.algorithm; @@ -1431,11 +1424,10 @@ pub fn op_crypto_random_uuid( pub async fn op_crypto_subtle_digest( _state: Rc>, algorithm: CryptoHash, - data: Option, + data: ZeroCopyBuf, ) -> Result { - let input = data.ok_or_else(null_opbuf)?; let output = tokio::task::spawn_blocking(move || { - digest::digest(algorithm.into(), &input) + digest::digest(algorithm.into(), &data) .as_ref() .to_vec() .into() diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index dfb1ce059f..ae81d126cb 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use data_url::DataUrl; -use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::futures::Future; @@ -341,10 +340,9 @@ pub async fn op_fetch_send( pub async fn op_fetch_request_write( state: Rc>, rid: ResourceId, - data: Option, + data: ZeroCopyBuf, ) -> Result<(), AnyError> { - let data = data.ok_or_else(null_opbuf)?; - let buf = Vec::from(&*data); + let buf = data.to_vec(); let resource = state .borrow() @@ -362,10 +360,8 @@ pub async fn op_fetch_request_write( pub async fn op_fetch_response_read( state: Rc>, rid: ResourceId, - data: Option, + data: ZeroCopyBuf, ) -> Result { - let data = data.ok_or_else(null_opbuf)?; - let resource = state .borrow() .resource_table diff --git a/ext/http/lib.rs b/ext/http/lib.rs index f1b0c911ae..a4e9085376 100644 --- a/ext/http/lib.rs +++ b/ext/http/lib.rs @@ -1,7 +1,6 @@ // 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::future::poll_fn; @@ -513,10 +512,8 @@ async fn op_http_response_close( async fn op_http_request_read( state: Rc>, rid: ResourceId, - data: Option, + mut data: ZeroCopyBuf, ) -> Result { - let mut data = data.ok_or_else(null_opbuf)?; - let resource = state .borrow() .resource_table @@ -565,9 +562,8 @@ async fn op_http_request_read( async fn op_http_response_write( state: Rc>, rid: ResourceId, - data: Option, + data: ZeroCopyBuf, ) -> Result<(), AnyError> { - let buf = data.ok_or_else(null_opbuf)?; let resource = state .borrow() .resource_table @@ -580,7 +576,7 @@ async fn op_http_response_write( let mut body = RcRef::map(&resource, |r| &r.body).borrow_mut().await; - let mut send_data_fut = body.send_data(Vec::from(&*buf).into()).boxed_local(); + let mut send_data_fut = body.send_data(data.to_vec().into()).boxed_local(); poll_fn(|cx| { let r = send_data_fut.poll_unpin(cx).map_err(AnyError::from); diff --git a/ext/net/io.rs b/ext/net/io.rs index f1403679a6..6a93b8cf6e 100644 --- a/ext/net/io.rs +++ b/ext/net/io.rs @@ -2,7 +2,6 @@ use crate::ops_tls as tls; use deno_core::error::not_supported; -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::op_async; use deno_core::AsyncMutFuture; @@ -166,16 +165,15 @@ impl Resource for UnixStreamResource { async fn op_read_async( state: Rc>, rid: ResourceId, - buf: Option, + mut buf: ZeroCopyBuf, ) -> Result { - let buf = &mut buf.ok_or_else(null_opbuf)?; let resource = state.borrow().resource_table.get_any(rid)?; let nread = if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else { return Err(not_supported()); }; @@ -185,16 +183,15 @@ async fn op_read_async( async fn op_write_async( state: Rc>, rid: ResourceId, - buf: Option, + buf: ZeroCopyBuf, ) -> Result { - let buf = &buf.ok_or_else(null_opbuf)?; let resource = state.borrow().resource_table.get_any(rid)?; let nwritten = if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else { return Err(not_supported()); }; diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 52e513f719..7019a9b1f4 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -7,7 +7,6 @@ use crate::NetPermissions; 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::op_async; @@ -167,9 +166,8 @@ pub(crate) struct ReceiveArgs { async fn receive_udp( state: Rc>, args: ReceiveArgs, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let mut zero_copy = zero_copy.clone(); let rid = args.rid; @@ -197,7 +195,7 @@ async fn receive_udp( async fn op_datagram_receive( state: Rc>, args: ReceiveArgs, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { match args.transport.as_str() { "udp" => receive_udp(state, args, zero_copy).await, @@ -218,12 +216,11 @@ struct SendArgs { async fn op_datagram_send( state: Rc>, args: SendArgs, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result where NP: NetPermissions + 'static, { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let zero_copy = zero_copy.clone(); match args { diff --git a/ext/net/ops_unix.rs b/ext/net/ops_unix.rs index c39252fbf4..20d085a5da 100644 --- a/ext/net/ops_unix.rs +++ b/ext/net/ops_unix.rs @@ -8,7 +8,6 @@ use crate::ops::OpPacket; use crate::ops::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::AsyncRefCell; use deno_core::CancelHandle; @@ -114,10 +113,8 @@ pub(crate) async fn accept_unix( pub(crate) async fn receive_unix_packet( state: Rc>, args: ReceiveArgs, - buf: Option, + mut buf: ZeroCopyBuf, ) -> Result { - let mut buf = buf.ok_or_else(null_opbuf)?; - let rid = args.rid; let resource = state diff --git a/ext/timers/lib.rs b/ext/timers/lib.rs index 384627c57d..d95ac71e8a 100644 --- a/ext/timers/lib.rs +++ b/ext/timers/lib.rs @@ -93,7 +93,7 @@ impl GlobalTimer { pub fn op_global_timer_stop( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result<(), AnyError> { let global_timer = state.borrow_mut::(); @@ -127,7 +127,7 @@ pub fn op_global_timer_start( pub async fn op_global_timer( state: Rc>, - _args: (), + _: (), _: (), ) -> Result<(), AnyError> { let maybe_timer_fut = { diff --git a/ext/webgpu/buffer.rs b/ext/webgpu/buffer.rs index 92afd2ef90..30818194fa 100644 --- a/ext/webgpu/buffer.rs +++ b/ext/webgpu/buffer.rs @@ -1,6 +1,5 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::error::null_opbuf; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::futures::channel::oneshot; @@ -171,9 +170,8 @@ pub struct BufferGetMappedRangeArgs { pub fn op_webgpu_buffer_get_mapped_range( state: &mut OpState, args: BufferGetMappedRangeArgs, - zero_copy: Option, + mut zero_copy: ZeroCopyBuf, ) -> Result { - let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let buffer_resource = state.resource_table.get::(args.buffer_rid)?; diff --git a/ext/webgpu/queue.rs b/ext/webgpu/queue.rs index ddb653fca1..79698e7b8e 100644 --- a/ext/webgpu/queue.rs +++ b/ext/webgpu/queue.rs @@ -2,7 +2,6 @@ use std::num::NonZeroU32; -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::OpState; use deno_core::ResourceId; @@ -77,9 +76,8 @@ pub struct QueueWriteBufferArgs { pub fn op_webgpu_write_buffer( state: &mut OpState, args: QueueWriteBufferArgs, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let buffer_resource = state .resource_table @@ -116,9 +114,8 @@ pub struct QueueWriteTextureArgs { pub fn op_webgpu_write_texture( state: &mut OpState, args: QueueWriteTextureArgs, - zero_copy: Option, + zero_copy: ZeroCopyBuf, ) -> Result { - let zero_copy = zero_copy.ok_or_else(null_opbuf)?; let instance = state.borrow::(); let texture_resource = state .resource_table diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 79e4d923c3..54e05c4085 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -324,10 +324,10 @@ const sendTypedArray = (ta) => { this[_bufferedAmount] += ta.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "binary", - }, ta), + value: ta, + }), () => { this[_bufferedAmount] -= ta.byteLength; }, @@ -348,10 +348,9 @@ const d = core.encode(string); this[_bufferedAmount] += d.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "text", - text: string, + value: string, }), () => { this[_bufferedAmount] -= d.byteLength; @@ -456,8 +455,7 @@ break; } case "ping": { - core.opAsync("op_ws_send", { - rid: this[_rid], + core.opAsync("op_ws_send", this[_rid], { kind: "pong", }); break; diff --git a/ext/websocket/02_websocketstream.js b/ext/websocket/02_websocketstream.js index f7c4d4d0fe..82f3063333 100644 --- a/ext/websocket/02_websocketstream.js +++ b/ext/websocket/02_websocketstream.js @@ -190,15 +190,14 @@ const writable = new WritableStream({ write: async (chunk) => { if (typeof chunk === "string") { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "text", - text: chunk, + value: chunk, }); } else if (chunk instanceof Uint8Array) { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "binary", + value: chunk, }, chunk); } else { throw new TypeError( @@ -257,8 +256,7 @@ break; } case "ping": { - await core.opAsync("op_ws_send", { - rid: this[_rid], + await core.opAsync("op_ws_send", this[_rid], { kind: "pong", }); break; diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index ebb2186d04..32ac4cf038 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::invalid_hostname; -use deno_core::error::null_opbuf; use deno_core::error::AnyError; use deno_core::futures::stream::SplitSink; use deno_core::futures::stream::SplitStream; @@ -309,29 +308,28 @@ where } #[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SendArgs { - rid: ResourceId, - kind: String, - text: Option, +#[serde(tag = "kind", content = "value", rename_all = "camelCase")] +pub enum SendValue { + Text(String), + Binary(ZeroCopyBuf), + Pong, } pub async fn op_ws_send( state: Rc>, - args: SendArgs, - buf: Option, + rid: ResourceId, + value: SendValue, ) -> Result<(), AnyError> { - let msg = match args.kind.as_str() { - "text" => Message::Text(args.text.unwrap()), - "binary" => Message::Binary(buf.ok_or_else(null_opbuf)?.to_vec()), - "pong" => Message::Pong(vec![]), - _ => unreachable!(), + let msg = match value { + SendValue::Text(text) => Message::Text(text), + SendValue::Binary(buf) => Message::Binary(buf.to_vec()), + SendValue::Pong => Message::Pong(vec![]), }; let resource = state .borrow_mut() .resource_table - .get::(args.rid)?; + .get::(rid)?; resource.send(msg).await?; Ok(()) } diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index f026940ca5..15df6f5549 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -49,7 +49,7 @@ } function setEnv(key, value) { - core.opSync("op_set_env", { key, value }); + core.opSync("op_set_env", key, value); } function getEnv(key) { diff --git a/runtime/metrics.rs b/runtime/metrics.rs index 22b037a1ef..bdcf2b84e6 100644 --- a/runtime/metrics.rs +++ b/runtime/metrics.rs @@ -27,7 +27,7 @@ struct MetricsReturn { fn op_metrics( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result { let m = state.borrow::(); diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index 819f3f3ace..3ae3bf0f0f 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -1769,7 +1769,7 @@ async fn op_utime_async( .unwrap() } -fn op_cwd(state: &mut OpState, _args: (), _: ()) -> Result { +fn op_cwd(state: &mut OpState, _: (), _: ()) -> Result { let path = current_dir()?; state .borrow_mut::() diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index 82fe3605cc..0687fc397f 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -1,7 +1,6 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::not_supported; -use deno_core::error::null_opbuf; use deno_core::error::resource_unavailable; use deno_core::error::AnyError; use deno_core::op_async; @@ -387,9 +386,8 @@ impl Resource for StdFileResource { fn op_read_sync( state: &mut OpState, rid: ResourceId, - buf: Option, + mut buf: ZeroCopyBuf, ) -> 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 buf) @@ -402,22 +400,21 @@ fn op_read_sync( async fn op_read_async( state: Rc>, rid: ResourceId, - buf: Option, + mut buf: ZeroCopyBuf, ) -> Result { - let buf = &mut buf.ok_or_else(null_opbuf)?; let resource = state.borrow().resource_table.get_any(rid)?; let nread = if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.read(buf).await? + s.read(&mut buf).await? } else { return Err(not_supported()); }; @@ -427,9 +424,8 @@ async fn op_read_async( fn op_write_sync( state: &mut OpState, rid: ResourceId, - buf: Option, + buf: ZeroCopyBuf, ) -> Result { - let buf = buf.ok_or_else(null_opbuf)?; StdFileResource::with(state, rid, move |r| match r { Ok(std_file) => std_file .write(&buf) @@ -442,20 +438,19 @@ fn op_write_sync( async fn op_write_async( state: Rc>, rid: ResourceId, - buf: Option, + buf: ZeroCopyBuf, ) -> Result { - let buf = &buf.ok_or_else(null_opbuf)?; let resource = state.borrow().resource_table.get_any(rid)?; let nwritten = if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else if let Some(s) = resource.downcast_rc::() { - s.write(buf).await? + s.write(&buf).await? } else { return Err(not_supported()); }; diff --git a/runtime/ops/os.rs b/runtime/ops/os.rs index c9567a7d7d..0a6269ac5a 100644 --- a/runtime/ops/os.rs +++ b/runtime/ops/os.rs @@ -7,7 +7,6 @@ use deno_core::op_sync; use deno_core::url::Url; use deno_core::Extension; use deno_core::OpState; -use serde::Deserialize; use serde::Serialize; use std::collections::HashMap; use std::env; @@ -29,11 +28,7 @@ pub fn init() -> Extension { .build() } -fn op_exec_path( - state: &mut OpState, - _args: (), - _: (), -) -> Result { +fn op_exec_path(state: &mut OpState, _: (), _: ()) -> Result { let current_exe = env::current_exe().unwrap(); state .borrow_mut::() @@ -47,31 +42,24 @@ fn op_exec_path( into_string(path.into_os_string()) } -#[derive(Deserialize)] -pub struct SetEnv { - key: String, - value: String, -} - fn op_set_env( state: &mut OpState, - args: SetEnv, - _: (), + key: String, + value: String, ) -> Result<(), AnyError> { - state.borrow_mut::().env.check(&args.key)?; - let invalid_key = - args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]); - let invalid_value = args.value.contains('\0'); + state.borrow_mut::().env.check(&key)?; + let invalid_key = key.is_empty() || key.contains(&['=', '\0'] as &[char]); + let invalid_value = value.contains('\0'); if invalid_key || invalid_value { return Err(type_error("Key or value contains invalid characters.")); } - env::set_var(args.key, args.value); + env::set_var(key, value); Ok(()) } fn op_env( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result, AnyError> { state.borrow_mut::().env.check_all()?; @@ -113,7 +101,7 @@ fn op_exit(_state: &mut OpState, code: i32, _: ()) -> Result<(), AnyError> { fn op_loadavg( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result<(f64, f64, f64), AnyError> { super::check_unstable(state, "Deno.loadavg"); @@ -124,11 +112,7 @@ fn op_loadavg( } } -fn op_hostname( - state: &mut OpState, - _args: (), - _: (), -) -> Result { +fn op_hostname(state: &mut OpState, _: (), _: ()) -> Result { super::check_unstable(state, "Deno.hostname"); state.borrow_mut::().env.check_all()?; let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string()); @@ -137,7 +121,7 @@ fn op_hostname( fn op_os_release( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result { super::check_unstable(state, "Deno.osRelease"); @@ -161,7 +145,7 @@ struct MemInfo { fn op_system_memory_info( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result, AnyError> { super::check_unstable(state, "Deno.systemMemoryInfo"); diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index 981ec7b912..66dd0de4b8 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -20,7 +20,7 @@ pub fn init(main_module: ModuleSpecifier) -> Extension { fn op_main_module( state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result { let main = state.borrow::().to_string(); diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index eef72f5112..aa419c6c81 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -224,7 +224,7 @@ pub fn op_signal_unbind( #[cfg(not(unix))] pub fn op_signal_bind( _state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result<(), AnyError> { Err(generic_error("not implemented")) @@ -233,7 +233,7 @@ pub fn op_signal_bind( #[cfg(not(unix))] fn op_signal_unbind( _state: &mut OpState, - _args: (), + _: (), _: (), ) -> Result<(), AnyError> { Err(generic_error("not implemented")) @@ -242,7 +242,7 @@ fn op_signal_unbind( #[cfg(not(unix))] async fn op_signal_poll( _state: Rc>, - _args: (), + _: (), _: (), ) -> Result<(), AnyError> { Err(generic_error("not implemented"))