summaryrefslogtreecommitdiff
path: root/ext/io/lib.rs
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2023-04-06 00:14:16 +0200
committerGitHub <noreply@github.com>2023-04-06 00:14:16 +0200
commit36e8c8dfd759458aa735f79f5cc7cd346c727914 (patch)
treeca8065958c406c02a8daa56f5056a658c813839f /ext/io/lib.rs
parentee15b49845b9cbe1f8ea75372091931fa460ad0d (diff)
feat(core): sync io ops in core (#18603)
This commit adds op_read_sync and op_write_sync to core. These ops are similar to op_read and op_write, but they are synchronous. Just like the async ops, they operate on generic `deno_core::Resource` objects. These now have new `read_byob_sync` and `write_sync` methods, with default implementations throwing "NotSupported" errors, just like the async counterparts. There are no `write_all` or `read` equivalents, because the optimizations they unlock are not useful in synchronous contexts.
Diffstat (limited to 'ext/io/lib.rs')
-rw-r--r--ext/io/lib.rs68
1 files changed, 26 insertions, 42 deletions
diff --git a/ext/io/lib.rs b/ext/io/lib.rs
index 2f2e62c11..69f8c9da5 100644
--- a/ext/io/lib.rs
+++ b/ext/io/lib.rs
@@ -78,7 +78,6 @@ pub static STDERR_HANDLE: Lazy<StdFile> = Lazy::new(|| {
deno_core::extension!(deno_io,
deps = [ deno_web ],
- ops = [op_read_sync, op_write_sync],
esm = [ "12_io.js" ],
options = {
stdio: Option<Stdio>,
@@ -454,7 +453,7 @@ impl StdFileResource {
}
fn with_inner_and_metadata<TResult>(
- self: Rc<Self>,
+ &self,
action: impl FnOnce(
&mut StdFileResourceInner,
&Arc<Mutex<FileMetadata>>,
@@ -471,10 +470,7 @@ impl StdFileResource {
}
}
- async fn with_inner_blocking_task<F, R: Send + 'static>(
- self: Rc<Self>,
- action: F,
- ) -> R
+ async fn with_inner_blocking_task<F, R: Send + 'static>(&self, action: F) -> R
where
F: FnOnce(&mut StdFileResourceInner) -> R + Send + 'static,
{
@@ -540,6 +536,14 @@ impl StdFileResource {
.await
}
+ fn read_byob_sync(&self, buf: &mut [u8]) -> Result<usize, AnyError> {
+ self.with_inner_and_metadata(|inner, _| inner.read(buf).map_err(Into::into))
+ }
+
+ fn write_sync(&self, data: &[u8]) -> Result<usize, AnyError> {
+ self.with_inner_and_metadata(|inner, _| inner.write_and_maybe_flush(data))
+ }
+
fn with_resource<F, R>(
state: &mut OpState,
rid: ResourceId,
@@ -632,7 +636,7 @@ impl Resource for StdFileResource {
Box::pin(async move {
let vec = vec![0; limit];
let buf = BufMutView::from(vec);
- let (nread, buf) = self.read_byob(buf).await?;
+ let (nread, buf) = StdFileResource::read_byob(self, buf).await?;
let mut vec = buf.unwrap_vec();
if vec.len() != nread {
vec.truncate(nread);
@@ -645,17 +649,29 @@ impl Resource for StdFileResource {
self: Rc<Self>,
buf: deno_core::BufMutView,
) -> AsyncResult<(usize, deno_core::BufMutView)> {
- Box::pin(self.read_byob(buf))
+ Box::pin(StdFileResource::read_byob(self, buf))
}
fn write(
self: Rc<Self>,
view: deno_core::BufView,
) -> AsyncResult<deno_core::WriteOutcome> {
- Box::pin(self.write(view))
+ Box::pin(StdFileResource::write(self, view))
}
+
fn write_all(self: Rc<Self>, view: deno_core::BufView) -> AsyncResult<()> {
- Box::pin(self.write_all(view))
+ Box::pin(StdFileResource::write_all(self, view))
+ }
+
+ fn write_sync(&self, data: &[u8]) -> Result<usize, deno_core::anyhow::Error> {
+ StdFileResource::write_sync(self, data)
+ }
+
+ fn read_byob_sync(
+ &self,
+ data: &mut [u8],
+ ) -> Result<usize, deno_core::anyhow::Error> {
+ StdFileResource::read_byob_sync(self, data)
}
#[cfg(unix)]
@@ -684,35 +700,3 @@ pub fn op_print(
})
})
}
-
-#[op(fast)]
-fn op_read_sync(
- state: &mut OpState,
- rid: u32,
- buf: &mut [u8],
-) -> Result<u32, AnyError> {
- StdFileResource::with_resource(state, rid, move |resource| {
- resource.with_inner_and_metadata(|inner, _| {
- inner
- .read(buf)
- .map(|n: usize| n as u32)
- .map_err(AnyError::from)
- })
- })
-}
-
-#[op(fast)]
-fn op_write_sync(
- state: &mut OpState,
- rid: u32,
- buf: &mut [u8],
-) -> Result<u32, AnyError> {
- StdFileResource::with_resource(state, rid, move |resource| {
- resource.with_inner_and_metadata(|inner, _| {
- inner
- .write_and_maybe_flush(buf)
- .map(|nwritten: usize| nwritten as u32)
- .map_err(AnyError::from)
- })
- })
-}