summaryrefslogtreecommitdiff
path: root/runtime/ops/io.rs
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/ops/io.rs')
-rw-r--r--runtime/ops/io.rs141
1 files changed, 45 insertions, 96 deletions
diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs
index bda8a51cb..4073342be 100644
--- a/runtime/ops/io.rs
+++ b/runtime/ops/io.rs
@@ -1,13 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-use super::dispatch_minimal::minimal_op;
-use super::dispatch_minimal::MinimalOp;
-use crate::metrics::metrics_op;
use deno_core::error::resource_unavailable;
-use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::error::{bad_resource_id, not_supported};
-use deno_core::futures::future::FutureExt;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
@@ -24,7 +19,6 @@ use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use std::borrow::Cow;
use std::cell::RefCell;
-use std::convert::TryInto;
use std::io::Read;
use std::io::Write;
use std::rc::Rc;
@@ -105,8 +99,12 @@ lazy_static! {
}
pub fn init(rt: &mut JsRuntime) {
- rt.register_op("op_read", metrics_op("op_read", minimal_op(op_read)));
- rt.register_op("op_write", metrics_op("op_write", minimal_op(op_write)));
+ super::reg_buffer_async(rt, "op_read_async", op_read_async);
+ super::reg_buffer_async(rt, "op_write_async", op_write_async);
+
+ super::reg_buffer_sync(rt, "op_read_sync", op_read_sync);
+ super::reg_buffer_sync(rt, "op_write_sync", op_write_sync);
+
super::reg_json_async(rt, "op_shutdown", op_shutdown);
}
@@ -138,10 +136,6 @@ fn get_stdio_stream(
}
}
-fn no_buffer_specified() -> AnyError {
- type_error("no buffer specified")
-}
-
#[cfg(unix)]
use nix::sys::termios;
@@ -526,36 +520,15 @@ impl Resource for StdFileResource {
}
}
-pub fn op_read(
- state: Rc<RefCell<OpState>>,
- is_sync: bool,
- rid: i32,
- bufs: BufVec,
-) -> MinimalOp {
- match bufs.len() {
- 0 => return MinimalOp::Sync(Err(no_buffer_specified())),
- 1 => {}
- _ => panic!("Invalid number of arguments"),
- };
- let buf = bufs.into_iter().next().unwrap();
-
- if is_sync {
- MinimalOp::Sync(op_read_sync(state, rid, buf))
- } else {
- MinimalOp::Async(op_read_async(state, rid, buf).boxed_local())
- }
-}
-
fn op_read_sync(
- state: Rc<RefCell<OpState>>,
- rid: i32,
- mut buf: ZeroCopyBuf,
-) -> Result<i32, AnyError> {
- let rid = rid.try_into().map_err(|_| bad_resource_id())?;
- StdFileResource::with(&mut state.borrow_mut(), rid, move |r| match r {
+ state: &mut OpState,
+ rid: u32,
+ bufs: &mut [ZeroCopyBuf],
+) -> Result<u32, AnyError> {
+ StdFileResource::with(state, rid, move |r| match r {
Ok(std_file) => std_file
- .read(&mut buf)
- .map(|n: usize| n as i32)
+ .read(&mut bufs[0])
+ .map(|n: usize| n as u32)
.map_err(AnyError::from),
Err(_) => Err(not_supported()),
})
@@ -563,65 +536,44 @@ fn op_read_sync(
async fn op_read_async(
state: Rc<RefCell<OpState>>,
- rid: i32,
- mut buf: ZeroCopyBuf,
-) -> Result<i32, AnyError> {
- let rid = rid.try_into().map_err(|_| bad_resource_id())?;
+ rid: u32,
+ mut bufs: BufVec,
+) -> Result<u32, AnyError> {
+ let buf = &mut bufs[0];
let resource = state
.borrow()
.resource_table
.get_any(rid)
.ok_or_else(bad_resource_id)?;
let nread = if let Some(s) = resource.downcast_rc::<ChildStdoutResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<ChildStderrResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsClientStreamResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsServerStreamResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
- s.read(&mut buf).await?
+ s.read(buf).await?
} else {
return Err(not_supported());
};
- Ok(nread as i32)
-}
-
-pub fn op_write(
- state: Rc<RefCell<OpState>>,
- is_sync: bool,
- rid: i32,
- bufs: BufVec,
-) -> MinimalOp {
- match bufs.len() {
- 0 => return MinimalOp::Sync(Err(no_buffer_specified())),
- 1 => {}
- _ => panic!("Invalid number of arguments"),
- };
- let buf = bufs.into_iter().next().unwrap();
-
- if is_sync {
- MinimalOp::Sync(op_write_sync(state, rid, buf))
- } else {
- MinimalOp::Async(op_write_async(state, rid, buf).boxed_local())
- }
+ Ok(nread as u32)
}
fn op_write_sync(
- state: Rc<RefCell<OpState>>,
- rid: i32,
- buf: ZeroCopyBuf,
-) -> Result<i32, AnyError> {
- let rid = rid.try_into().map_err(|_| bad_resource_id())?;
- StdFileResource::with(&mut state.borrow_mut(), rid, move |r| match r {
+ state: &mut OpState,
+ rid: u32,
+ bufs: &mut [ZeroCopyBuf],
+) -> Result<u32, AnyError> {
+ StdFileResource::with(state, rid, move |r| match r {
Ok(std_file) => std_file
- .write(&buf)
- .map(|nwritten: usize| nwritten as i32)
+ .write(&bufs[0])
+ .map(|nwritten: usize| nwritten as u32)
.map_err(AnyError::from),
Err(_) => Err(not_supported()),
})
@@ -629,36 +581,36 @@ fn op_write_sync(
async fn op_write_async(
state: Rc<RefCell<OpState>>,
- rid: i32,
- buf: ZeroCopyBuf,
-) -> Result<i32, AnyError> {
- let rid = rid.try_into().map_err(|_| bad_resource_id())?;
+ rid: u32,
+ bufs: BufVec,
+) -> Result<u32, AnyError> {
+ let buf = &bufs[0];
let resource = state
.borrow()
.resource_table
.get_any(rid)
.ok_or_else(bad_resource_id)?;
let nwritten = if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsClientStreamResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsServerStreamResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
- s.write(&buf).await?
+ s.write(buf).await?
} else {
return Err(not_supported());
};
- Ok(nwritten as i32)
+ Ok(nwritten as u32)
}
#[derive(Deserialize)]
struct ShutdownArgs {
- rid: i32,
+ rid: u32,
}
async fn op_shutdown(
@@ -666,10 +618,7 @@ async fn op_shutdown(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
- let rid = serde_json::from_value::<ShutdownArgs>(args)?
- .rid
- .try_into()
- .map_err(|_| bad_resource_id())?;
+ let rid = serde_json::from_value::<ShutdownArgs>(args)?.rid;
let resource = state
.borrow()
.resource_table