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 83116e8 commit 571ac0f
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 488 deletions.
1 change: 0 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub mod deno_dir;
pub mod deno_error;
pub mod diagnostics;
mod disk_cache;
mod dispatch_minimal;
mod file_fetcher;
pub mod flags;
pub mod fmt_errors;
Expand Down
82 changes: 18 additions & 64 deletions cli/dispatch_minimal.rs → cli/ops/dispatch_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
use crate::state::ThreadSafeState;
use deno::Buf;
use deno::CoreOp;
use deno::ErrBox;
use deno::Op;
use deno::OpId;
use deno::PinnedBuf;
use futures::Future;

const OP_READ: OpId = 1;
const OP_WRITE: OpId = 2;

#[derive(Copy, Clone, Debug, PartialEq)]
// This corresponds to RecordMinimal on the TS side.
pub struct Record {
Expand Down Expand Up @@ -72,21 +69,29 @@ fn test_parse_min_record() {
assert_eq!(parse_min_record(&buf), None);
}

pub fn dispatch_minimal(
pub type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;

pub fn wrap_minimal<D>(
d: D,
state: &ThreadSafeState,
op_id: OpId,
mut record: Record,
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;
let min_op = match op_id {
OP_READ => ops::read(record.arg, zero_copy),
OP_WRITE => ops::write(record.arg, zero_copy),
_ => unimplemented!(),
};

let state = state.clone();

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 All @@ -109,54 +114,3 @@ pub fn dispatch_minimal(
Op::Async(fut)
}
}

mod ops {
use crate::deno_error;
use crate::resources;
use crate::tokio_write;
use deno::ErrBox;
use deno::PinnedBuf;
use futures::Future;

type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;

pub fn 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 fn 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)),
),
}
}
}
97 changes: 97 additions & 0 deletions cli/ops/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 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 }
}
}

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 571ac0f

Please sign in to comment.