diff --git a/core/01_core.js b/core/01_core.js index a13bdc8dd78db..c46c300700c7c 100644 --- a/core/01_core.js +++ b/core/01_core.js @@ -191,12 +191,24 @@ return res; } - function rollPromiseId() { - return nextPromiseId++; + function opAsync2(name, arg0, arg1) { + const id = nextPromiseId++; + let promise = PromisePrototypeThen(setPromise(id), unwrapOpResult); + try { + ops[name](id, arg0, arg1); + } catch (err) { + // Cleanup the just-created promise + getPromise(id); + // Rethrow the error + throw err; + } + promise = handleOpCallTracing(name, id, promise); + promise[promiseIdSymbol] = id; + return promise; } function opAsync(name, ...args) { - const id = rollPromiseId(); + const id = nextPromiseId++; let promise = PromisePrototypeThen(setPromise(id), unwrapOpResult); try { ops[name](id, ...new SafeArrayIterator(args)); @@ -376,6 +388,7 @@ // Extra Deno.core.* exports const core = ObjectAssign(globalThis.Deno.core, { opAsync, + opAsync2, resources, metrics, registerErrorBuilder, 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 5a205188b07dd..0c3b5be13ee8a 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,7 @@ // 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 } = Deno.core; +const { ops, opAsync, opAsync2 } = Deno.core; const requestBuf = new Uint8Array(64 * 1024); const responseBuf = new Uint8Array( @@ -23,7 +23,7 @@ function accept(serverRid) { } function read(serverRid, buf) { - return opAsync("op_read_socket", serverRid, buf); + return opAsync2("op_read_socket", serverRid, buf); } async function serve(rid) { diff --git a/core/runtime.rs b/core/runtime.rs index 787bac972772e..7c73950aa7c55 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -2918,7 +2918,7 @@ pub mod tests { r#" let zero_copy_a = new Uint8Array([0]); - Deno.core.opAsync("op_test", null, zero_copy_a); + Deno.core.opAsync2("op_test", null, zero_copy_a); "#, ) .unwrap(); diff --git a/ext/fs/30_fs.js b/ext/fs/30_fs.js index a8ae7ad3aaa24..50041698058fe 100644 --- a/ext/fs/30_fs.js +++ b/ext/fs/30_fs.js @@ -31,7 +31,7 @@ function chmodSync(path, mode) { } async function chmod(path, mode) { - await core.opAsync("op_chmod_async", pathFromURL(path), mode); + await core.opAsync2("op_chmod_async", pathFromURL(path), mode); } function chownSync( @@ -121,7 +121,7 @@ async function mkdir( path, options, ) { - await core.opAsync("op_mkdir_async", mkdirArgs(path, options)); + await core.opAsync2("op_mkdir_async", mkdirArgs(path, options)); } function readDirSync(path) { @@ -349,7 +349,7 @@ function ftruncateSync(rid, len) { } async function ftruncate(rid, len) { - await core.opAsync("op_ftruncate_async", rid, coerceLen(len)); + await core.opAsync2("op_ftruncate_async", rid, coerceLen(len)); } function truncateSync(path, len) { @@ -357,7 +357,7 @@ function truncateSync(path, len) { } async function truncate(path, len) { - await core.opAsync("op_truncate_async", path, coerceLen(len)); + await core.opAsync2("op_truncate_async", path, coerceLen(len)); } function umask(mask) { @@ -369,7 +369,7 @@ function linkSync(oldpath, newpath) { } async function link(oldpath, newpath) { - await core.opAsync("op_link_async", oldpath, newpath); + await core.opAsync2("op_link_async", oldpath, newpath); } function toUnixTimeFromEpoch(value) { @@ -499,7 +499,7 @@ function flockSync(rid, exclusive) { } async function flock(rid, exclusive) { - await core.opAsync("op_flock_async", rid, exclusive === true); + await core.opAsync2("op_flock_async", rid, exclusive === true); } function funlockSync(rid) { diff --git a/ext/http/01_http.js b/ext/http/01_http.js index 7e648017bf481..5b78bb2f2f0f6 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -316,7 +316,7 @@ function createRespondWith( break; } try { - await core.opAsync("op_http_write", streamRid, value); + await core.opAsync2("op_http_write", streamRid, value); } catch (error) { const connError = httpConn[connErrorSymbol]; if ( diff --git a/ext/node/polyfills/internal/crypto/random.ts b/ext/node/polyfills/internal/crypto/random.ts index 07d91976a6d35..04678b6be1b2f 100644 --- a/ext/node/polyfills/internal/crypto/random.ts +++ b/ext/node/polyfills/internal/crypto/random.ts @@ -92,7 +92,7 @@ export function checkPrime( ); } - core.opAsync(op, candidate, checks).then( + core.opAsync2(op, candidate, checks).then( (result) => { callback?.(null, result); }, diff --git a/ext/web/02_timers.js b/ext/web/02_timers.js index 9bf41316a66de..c224be884e9c1 100644 --- a/ext/web/02_timers.js +++ b/ext/web/02_timers.js @@ -215,7 +215,7 @@ const scheduledTimers = { head: null, tail: null }; */ function runAfterTimeout(cb, millis, timerInfo) { const cancelRid = timerInfo.cancelRid; - const sleepPromise = core.opAsync("op_sleep", millis, cancelRid); + const sleepPromise = core.opAsync2("op_sleep", millis, cancelRid); timerInfo.promiseId = sleepPromise[SymbolFor("Deno.core.internalPromiseId")]; if (!timerInfo.isRef) { core.unrefOp(timerInfo.promiseId); diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 03a6427c2944e..cadd35a50e3bd 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -301,7 +301,7 @@ class WebSocket extends EventTarget { const sendTypedArray = (ta) => { this[_bufferedAmount] += ta.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send_binary", this[_rid], ta), + core.opAsync2("op_ws_send_binary", this[_rid], ta), () => { this[_bufferedAmount] -= ta.byteLength; }, @@ -322,7 +322,7 @@ class WebSocket extends EventTarget { const d = core.encode(string); this[_bufferedAmount] += d.byteLength; PromisePrototypeThen( - core.opAsync("op_ws_send_text", this[_rid], string), + core.opAsync2("op_ws_send_text", this[_rid], string), () => { this[_bufferedAmount] -= d.byteLength; }, @@ -394,7 +394,7 @@ class WebSocket extends EventTarget { async [_eventLoop]() { while (this[_readyState] !== CLOSED) { - const { 0: kind, 1: value } = await core.opAsync( + const { 0: kind, 1: value } = await core.opAsync2( "op_ws_next_event", this[_rid], );