-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
489 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
Oops, something went wrong.