diff options
author | Luca Casonato <hello@lcas.dev> | 2023-04-06 00:14:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-06 00:14:16 +0200 |
commit | 36e8c8dfd759458aa735f79f5cc7cd346c727914 (patch) | |
tree | ca8065958c406c02a8daa56f5056a658c813839f /core/ops_builtin.rs | |
parent | ee15b49845b9cbe1f8ea75372091931fa460ad0d (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 'core/ops_builtin.rs')
-rw-r--r-- | core/ops_builtin.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs index 668b44bc3..7f9c48e01 100644 --- a/core/ops_builtin.rs +++ b/core/ops_builtin.rs @@ -33,6 +33,8 @@ crate::extension!( op_read, op_read_all, op_write, + op_read_sync, + op_write_sync, op_write_all, op_shutdown, op_metrics, @@ -279,6 +281,27 @@ async fn op_write( Ok(resp.nwritten() as u32) } +#[op(fast)] +fn op_read_sync( + state: &mut OpState, + rid: ResourceId, + data: &mut [u8], +) -> Result<u32, Error> { + let resource = state.resource_table.get_any(rid)?; + resource.read_byob_sync(data).map(|n| n as u32) +} + +#[op] +fn op_write_sync( + state: &mut OpState, + rid: ResourceId, + data: &[u8], +) -> Result<u32, Error> { + let resource = state.resource_table.get_any(rid)?; + let nwritten = resource.write_sync(data)?; + Ok(nwritten as u32) +} + #[op] async fn op_write_all( state: Rc<RefCell<OpState>>, |