summaryrefslogtreecommitdiff
path: root/cli/ops/io.rs
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-09-10 09:57:45 -0400
committerGitHub <noreply@github.com>2020-09-10 09:57:45 -0400
commit7c2e7c660804afca823d60e6496aa853f75db16c (patch)
treeb7746b181c1564c6b1abd2e906662f9e6b008417 /cli/ops/io.rs
parent6f70e6e72ba2d5c1de7495adac37c1e4f4e86b24 (diff)
Use gotham-like state for ops (#7385)
Provides a concrete state type that can be dynamically added. This is necessary for op crates. * renames BasicState to OpState * async ops take `Rc<RefCell<OpState>>` * sync ops take `&mut OpState` * removes `OpRegistry`, `OpRouter` traits * `get_error_class_fn` moved to OpState * ResourceTable moved to OpState
Diffstat (limited to 'cli/ops/io.rs')
-rw-r--r--cli/ops/io.rs40
1 files changed, 23 insertions, 17 deletions
diff --git a/cli/ops/io.rs b/cli/ops/io.rs
index 4a8f6d96d..be59ac863 100644
--- a/cli/ops/io.rs
+++ b/cli/ops/io.rs
@@ -1,11 +1,15 @@
+use super::dispatch_minimal::minimal_op;
use super::dispatch_minimal::MinimalOp;
use crate::http_util::HttpBody;
-use crate::state::State;
+use crate::metrics::metrics_op;
use deno_core::BufVec;
use deno_core::ErrBox;
+use deno_core::JsRuntime;
+use deno_core::OpState;
use futures::future::poll_fn;
use futures::future::FutureExt;
use futures::ready;
+use std::cell::RefCell;
use std::collections::HashMap;
use std::pin::Pin;
use std::rc::Rc;
@@ -82,9 +86,9 @@ lazy_static! {
};
}
-pub fn init(s: &Rc<State>) {
- s.register_op_minimal("op_read", op_read);
- s.register_op_minimal("op_write", op_write);
+pub fn init(rt: &mut JsRuntime) {
+ rt.register_op("op_read", metrics_op(minimal_op(op_read)));
+ rt.register_op("op_write", metrics_op(minimal_op(op_write)));
}
pub fn get_stdio() -> (
@@ -233,7 +237,7 @@ impl DenoAsyncRead for StreamResource {
}
pub fn op_read(
- state: Rc<State>,
+ state: Rc<RefCell<OpState>>,
is_sync: bool,
rid: i32,
mut zero_copy: BufVec,
@@ -248,7 +252,7 @@ pub fn op_read(
if is_sync {
MinimalOp::Sync({
// First we look up the rid in the resource table.
- std_file_resource(&state, rid as u32, move |r| match r {
+ std_file_resource(&mut state.borrow_mut(), rid as u32, move |r| match r {
Ok(std_file) => {
use std::io::Read;
std_file
@@ -265,8 +269,9 @@ pub fn op_read(
let mut zero_copy = zero_copy[0].clone();
MinimalOp::Async(
poll_fn(move |cx| {
- let mut resource_table = state.resource_table.borrow_mut();
- let resource_holder = resource_table
+ let mut state = state.borrow_mut();
+ let resource_holder = state
+ .resource_table
.get_mut::<StreamResourceHolder>(rid as u32)
.ok_or_else(ErrBox::bad_resource_id)?;
@@ -352,7 +357,7 @@ impl DenoAsyncWrite for StreamResource {
}
pub fn op_write(
- state: Rc<State>,
+ state: Rc<RefCell<OpState>>,
is_sync: bool,
rid: i32,
zero_copy: BufVec,
@@ -367,7 +372,7 @@ pub fn op_write(
if is_sync {
MinimalOp::Sync({
// First we look up the rid in the resource table.
- std_file_resource(&state, rid as u32, move |r| match r {
+ std_file_resource(&mut state.borrow_mut(), rid as u32, move |r| match r {
Ok(std_file) => {
use std::io::Write;
std_file
@@ -385,8 +390,9 @@ pub fn op_write(
MinimalOp::Async(
async move {
let nwritten = poll_fn(|cx| {
- let mut resource_table = state.resource_table.borrow_mut();
- let resource_holder = resource_table
+ let mut state = state.borrow_mut();
+ let resource_holder = state
+ .resource_table
.get_mut::<StreamResourceHolder>(rid as u32)
.ok_or_else(ErrBox::bad_resource_id)?;
resource_holder.resource.poll_write(cx, &zero_copy)
@@ -398,8 +404,9 @@ pub fn op_write(
// Figure out why it's needed and preferably remove it.
// https://github.com/denoland/deno/issues/3565
poll_fn(|cx| {
- let mut resource_table = state.resource_table.borrow_mut();
- let resource_holder = resource_table
+ let mut state = state.borrow_mut();
+ let resource_holder = state
+ .resource_table
.get_mut::<StreamResourceHolder>(rid as u32)
.ok_or_else(ErrBox::bad_resource_id)?;
resource_holder.resource.poll_flush(cx)
@@ -421,7 +428,7 @@ pub fn op_write(
///
/// Returns ErrorKind::Busy if the resource is being used by another op.
pub fn std_file_resource<F, T>(
- state: &State,
+ state: &mut OpState,
rid: u32,
mut f: F,
) -> Result<T, ErrBox>
@@ -430,8 +437,7 @@ where
FnMut(Result<&mut std::fs::File, &mut StreamResource>) -> Result<T, ErrBox>,
{
// First we look up the rid in the resource table.
- let mut resource_table = state.resource_table.borrow_mut();
- let mut r = resource_table.get_mut::<StreamResourceHolder>(rid);
+ let mut r = state.resource_table.get_mut::<StreamResourceHolder>(rid);
if let Some(ref mut resource_holder) = r {
// Sync write only works for FsFile. It doesn't make sense to do this
// for non-blocking sockets. So we error out if not FsFile.