From 36e8c8dfd759458aa735f79f5cc7cd346c727914 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 6 Apr 2023 00:14:16 +0200 Subject: 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. --- core/01_core.js | 2 ++ core/lib.deno_core.d.ts | 10 ++++++++++ core/ops_builtin.rs | 23 +++++++++++++++++++++++ core/resources.rs | 12 ++++++++++++ 4 files changed, 47 insertions(+) (limited to 'core') diff --git a/core/01_core.js b/core/01_core.js index c46c30070..ab54316e5 100644 --- a/core/01_core.js +++ b/core/01_core.js @@ -412,6 +412,8 @@ readAll: opAsync.bind(null, "op_read_all"), write: opAsync.bind(null, "op_write"), writeAll: opAsync.bind(null, "op_write_all"), + readSync: (rid, buffer) => ops.op_read_sync(rid, buffer), + writeSync: (rid, buffer) => ops.op_write_sync(rid, buffer), shutdown: opAsync.bind(null, "op_shutdown"), print: (msg, isErr) => ops.op_print(msg, isErr), setMacrotaskCallback: (fn) => ops.op_set_macrotask_callback(fn), diff --git a/core/lib.deno_core.d.ts b/core/lib.deno_core.d.ts index b238bd6b3..7f3ea2a19 100644 --- a/core/lib.deno_core.d.ts +++ b/core/lib.deno_core.d.ts @@ -60,6 +60,16 @@ declare namespace Deno { */ function writeAll(rid: number, buf: Uint8Array): Promise; + /** + * Synchronously read from a (stream) resource that implements readSync(). + */ + function readSync(rid: number, buf: Uint8Array): number; + + /** + * Synchronously write to a (stream) resource that implements writeSync(). + */ + function writeSync(rid: number, buf: Uint8Array): number; + /** * Print a message to stdout or stderr */ 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 { + 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 { + 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>, diff --git a/core/resources.rs b/core/resources.rs index 5bec68481..6ca86e10b 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -154,6 +154,18 @@ pub trait Resource: Any + 'static { }) } + /// The same as [`read_byob()`][Resource::read_byob], but synchronous. + fn read_byob_sync(&self, data: &mut [u8]) -> Result { + _ = data; + Err(not_supported()) + } + + /// The same as [`write()`][Resource::write], but synchronous. + fn write_sync(&self, data: &[u8]) -> Result { + _ = data; + Err(not_supported()) + } + /// The shutdown method can be used to asynchronously close the resource. It /// is not automatically called when the resource is dropped or closed. /// -- cgit v1.2.3