Skip to content

Commit

Permalink
Refactor dispatcher system
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Sep 10, 2019
1 parent acee194 commit baf24e1
Show file tree
Hide file tree
Showing 30 changed files with 2,185 additions and 929 deletions.
12 changes: 6 additions & 6 deletions cli/ops/dispatch_minimal.rs
Expand Up @@ -4,7 +4,6 @@
//! alternative to flatbuffers using a very simple list of int32s to lay out
//! messages. The first i32 is used to determine if a message a flatbuffer
//! message or a "minimal" message.
use crate::state::ThreadSafeState;
use deno::Buf;
use deno::CoreOp;
use deno::ErrBox;
Expand All @@ -13,7 +12,6 @@ use deno::PinnedBuf;
use futures::Future;

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

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

pub fn dispatch(
d: Dispatcher,
_state: &ThreadSafeState,
pub fn wrap_minimal_op<D>(
d: D,
control: &[u8],
zero_copy: Option<PinnedBuf>,
) -> CoreOp {
) -> CoreOp
where
D: FnOnce(i32, Option<PinnedBuf>) -> Box<MinimalOp>,
{
let mut record = parse_min_record(control).unwrap();
let is_sync = record.promise_id == 0;
let rid = record.arg;
Expand Down
101 changes: 70 additions & 31 deletions cli/ops/io.rs
@@ -1,46 +1,85 @@
use super::dispatch_minimal::MinimalOp;
use super::dispatch_minimal::wrap_minimal_op;
use crate::deno_error;
use crate::resources;
use crate::state::DenoOpDispatcher;
use crate::state::ThreadSafeState;
use crate::tokio_read;
use crate::tokio_write;
use deno::CoreOp;
use deno::ErrBox;
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,
};
pub struct OpRead;

match resources::lookup(rid as u32) {
Err(e) => Box::new(futures::future::err(e)),
Ok(resource) => Box::new(
tokio_read::read(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
),
impl DenoOpDispatcher for OpRead {
fn dispatch(
&self,
_state: &ThreadSafeState,
control: &[u8],
buf: Option<PinnedBuf>,
) -> CoreOp {
wrap_minimal_op(
move |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) {
Err(e) => Box::new(futures::future::err(e)),
Ok(resource) => Box::new(
tokio_read::read(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
),
}
},
control,
buf,
)
}

const NAME: &'static str = "read";
}

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,
};
pub struct OpWrite;

match resources::lookup(rid as u32) {
Err(e) => Box::new(futures::future::err(e)),
Ok(resource) => Box::new(
tokio_write::write(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
),
impl DenoOpDispatcher for OpWrite {
fn dispatch(
&self,
_state: &ThreadSafeState,
control: &[u8],
buf: Option<PinnedBuf>,
) -> CoreOp {
wrap_minimal_op(
move |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) {
Err(e) => Box::new(futures::future::err(e)),
Ok(resource) => Box::new(
tokio_write::write(resource, zero_copy)
.map_err(ErrBox::from)
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
),
}
},
control,
buf,
)
}

const NAME: &'static str = "write";
}

0 comments on commit baf24e1

Please sign in to comment.