Skip to content

Commit

Permalink
implmement in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Aug 18, 2019
1 parent d49676b commit 676d1ff
Show file tree
Hide file tree
Showing 10 changed files with 489 additions and 427 deletions.
14 changes: 10 additions & 4 deletions cli/ops/dispatch_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ fn test_parse_min_record() {
}

pub type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;
pub type Dispatcher = fn(i32, Option<PinnedBuf>) -> Box<MinimalOp>;

pub fn dispatch(
d: Dispatcher,
pub fn wrap_minimal<D>(
d: D,
state: &ThreadSafeState,
control: &[u8],
zero_copy: Option<PinnedBuf>,
) -> CoreOp {
) -> CoreOp
where
D: FnOnce(i32, Option<PinnedBuf>) -> Box<MinimalOp>,
{
let bytes_sent_control = control.len();
let bytes_sent_zero_copy = zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);
let mut record = parse_min_record(control).unwrap();
let is_sync = record.promise_id == 0;

Expand All @@ -86,6 +90,8 @@ pub fn dispatch(
let rid = record.arg;
let min_op = d(rid, zero_copy);

state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);

let fut = Box::new(min_op.then(move |result| -> Result<Buf, ()> {
match result {
Ok(r) => {
Expand Down
116 changes: 85 additions & 31 deletions cli/ops/io.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,97 @@
use super::dispatch_minimal::MinimalOp;
use super::dispatch_minimal::wrap_minimal;
use crate::deno_error;
use crate::resources;
use crate::tokio_write;
use crate::ThreadSafeState;
use deno::CoreOp;
use deno::ErrBox;
use deno::Named;
use deno::OpDispatcher;
use deno::PinnedBuf;
use futures::Future;

pub fn op_read(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
debug!("read rid={}", rid);
let zero_copy = match zero_copy {
None => {
return Box::new(futures::future::err(deno_error::no_buffer_specified()))
}
Some(buf) => buf,
};
match resources::lookup(rid as u32) {
None => Box::new(futures::future::err(deno_error::bad_resource())),
Some(resource) => Box::new(
tokio::io::read(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
),
pub struct ReadDispatcher {
state: ThreadSafeState,
}

impl ReadDispatcher {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}

impl OpDispatcher for ReadDispatcher {
fn dispatch(&self, args: &[u8], buf: Option<PinnedBuf>) -> CoreOp {
wrap_minimal(
|rid, zero_copy| {
debug!("read rid={}", rid);
let zero_copy = match zero_copy {
None => {
return Box::new(futures::future::err(
deno_error::no_buffer_specified(),
))
}
Some(buf) => buf,
};
match resources::lookup(rid as u32) {
None => Box::new(futures::future::err(deno_error::bad_resource())),
Some(resource) => Box::new(
tokio::io::read(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
),
}
},
&self.state,
args,
buf,
)
}
}

impl Named for ReadDispatcher {
const NAME: &'static str = "read";
}

pub struct WriteDispatcher {
state: ThreadSafeState,
}

impl WriteDispatcher {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}

pub fn op_write(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
debug!("write rid={}", rid);
let zero_copy = match zero_copy {
None => {
return Box::new(futures::future::err(deno_error::no_buffer_specified()))
}
Some(buf) => buf,
};
match resources::lookup(rid as u32) {
None => Box::new(futures::future::err(deno_error::bad_resource())),
Some(resource) => Box::new(
tokio_write::write(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
),
impl OpDispatcher for WriteDispatcher {
fn dispatch(&self, args: &[u8], buf: Option<PinnedBuf>) -> CoreOp {
wrap_minimal(
|rid, zero_copy| {
debug!("write rid={}", rid);
let zero_copy = match zero_copy {
None => {
return Box::new(futures::future::err(
deno_error::no_buffer_specified(),
))
}
Some(buf) => buf,
};
match resources::lookup(rid as u32) {
None => Box::new(futures::future::err(deno_error::bad_resource())),
Some(resource) => Box::new(
tokio_write::write(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
),
}
},
&self.state,
args,
buf,
)
}
}

impl Named for WriteDispatcher {
const NAME: &'static str = "write";
}
Loading

0 comments on commit 676d1ff

Please sign in to comment.