From dcf391ffed3850f9026d88b146e156375c4619d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 1 May 2023 17:40:00 +0200 Subject: refactor: migrate async ops to generated wrappers (#18937) Migrates some of existing async ops to generated wrappers introduced in https://github.com/denoland/deno/pull/18887. As a result "core.opAsync2" was removed. I will follow up with more PRs that migrate all the async ops to generated wrappers. --- .../http_bench_json_ops/http_bench_json_ops.js | 32 ++++++++++------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'core/examples/http_bench_json_ops') diff --git a/core/examples/http_bench_json_ops/http_bench_json_ops.js b/core/examples/http_bench_json_ops/http_bench_json_ops.js index 0c3b5be13..beb6c90e4 100644 --- a/core/examples/http_bench_json_ops/http_bench_json_ops.js +++ b/core/examples/http_bench_json_ops/http_bench_json_ops.js @@ -3,7 +3,16 @@ // then write this fixed 'responseBuf'. The point of this benchmark is to // exercise the event loop in a simple yet semi-realistic way. -const { ops, opAsync, opAsync2 } = Deno.core; +// deno-lint-ignore-file camelcase + +const { op_listen } = Deno.core.ops; +const { + op_accept, + op_read_socket, +} = core.generateAsyncOpHandler( + "op_accept", + "op_read_socket", +); const requestBuf = new Uint8Array(64 * 1024); const responseBuf = new Uint8Array( @@ -12,24 +21,10 @@ const responseBuf = new Uint8Array( .map((c) => c.charCodeAt(0)), ); -/** Listens on 0.0.0.0:4570, returns rid. */ -function listen() { - return ops.op_listen(); -} - -/** Accepts a connection, returns rid. */ -function accept(serverRid) { - return opAsync("op_accept", serverRid); -} - -function read(serverRid, buf) { - return opAsync2("op_read_socket", serverRid, buf); -} - async function serve(rid) { try { while (true) { - await read(rid, requestBuf); + await op_read_socket(rid, requestBuf); if (!ops.op_try_write(rid, responseBuf)) { await Deno.core.writeAll(rid, responseBuf); } @@ -41,11 +36,12 @@ async function serve(rid) { } async function main() { - const listenerRid = listen(); + /** Listens on 0.0.0.0:4570, returns rid. */ + const listenerRid = op_listen(); Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4570/\n`); while (true) { - const rid = await accept(listenerRid); + const rid = await op_accept(listenerRid); serve(rid); } } -- cgit v1.2.3