From 6c4da0e429eb47dae6a220c5576a39f137615bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 16 Sep 2020 22:22:43 +0200 Subject: [PATCH] refactor: remove dispatch_json.js from cli/rt and cli/tsc (#7521) Instead use Deno.core.jsonOpSync and Deno.core.jsonOpAsync --- cli/rt/10_dispatch_json.js | 83 --------------------- cli/rt/11_crypto.js | 4 +- cli/rt/11_resources.js | 6 +- cli/rt/11_timers.js | 8 +-- cli/rt/11_url.js | 4 +- cli/rt/11_workers.js | 10 +-- cli/rt/26_fetch.js | 6 +- cli/rt/27_websocket.js | 17 +++-- cli/rt/30_files.js | 10 +-- cli/rt/30_fs.js | 104 ++++++++++++++------------- cli/rt/30_metrics.js | 4 +- cli/rt/30_net.js | 18 +++-- cli/rt/30_os.js | 22 +++--- cli/rt/40_compiler_api.js | 6 +- cli/rt/40_error_stack.js | 6 +- cli/rt/40_fs_events.js | 6 +- cli/rt/40_permissions.js | 8 +-- cli/rt/40_plugins.js | 4 +- cli/rt/40_process.js | 8 +-- cli/rt/40_repl.js | 5 +- cli/rt/40_signals.js | 8 +-- cli/rt/40_tls.js | 10 +-- cli/rt/40_tty.js | 8 +-- cli/rt/99_main.js | 23 ++---- cli/tests/044_bad_resource.ts.out | 3 +- cli/tests/compiler_js_error.ts.out | 3 - cli/tests/unit/buffer_test.ts | 4 +- cli/tests/unit/dispatch_json_test.ts | 20 ------ cli/tsc/10_dispatch_json.js | 84 ---------------------- cli/tsc/99_main_compiler.js | 20 ++---- core/core.js | 28 ++++---- 31 files changed, 177 insertions(+), 373 deletions(-) delete mode 100644 cli/rt/10_dispatch_json.js delete mode 100644 cli/tsc/10_dispatch_json.js diff --git a/cli/rt/10_dispatch_json.js b/cli/rt/10_dispatch_json.js deleted file mode 100644 index 4b244081ac..0000000000 --- a/cli/rt/10_dispatch_json.js +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -((window) => { - const core = window.Deno.core; - const util = window.__bootstrap.util; - // Using an object without a prototype because `Map` was causing GC problems. - const promiseTable = Object.create(null); - let _nextPromiseId = 1; - - function nextPromiseId() { - return _nextPromiseId++; - } - - function decode(ui8) { - return JSON.parse(core.decode(ui8)); - } - - function encode(args) { - return core.encode(JSON.stringify(args)); - } - - function unwrapResponse(res) { - if (res.err != null) { - throw new (core.getErrorClass(res.err.className))(res.err.message); - } - util.assert(res.ok != null); - return res.ok; - } - - function asyncMsgFromRust(resUi8) { - const res = decode(resUi8); - util.assert(res.promiseId != null); - - const promise = promiseTable[res.promiseId]; - util.assert(promise != null); - delete promiseTable[res.promiseId]; - promise.resolve(res); - } - - function sendSync( - opName, - args = {}, - ...zeroCopy - ) { - util.log("sendSync", opName); - const argsUi8 = encode(args); - const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy); - util.assert(resUi8 != null); - const res = decode(resUi8); - util.assert(res.promiseId == null); - return unwrapResponse(res); - } - - async function sendAsync( - opName, - args = {}, - ...zeroCopy - ) { - util.log("sendAsync", opName); - const promiseId = nextPromiseId(); - args = Object.assign(args, { promiseId }); - const promise = util.createResolvable(); - const argsUi8 = encode(args); - const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy); - if (buf != null) { - // Sync result. - const res = decode(buf); - promise.resolve(res); - } else { - // Async result. - promiseTable[promiseId] = promise; - } - - const res = await promise; - return unwrapResponse(res); - } - - window.__bootstrap.dispatchJson = { - asyncMsgFromRust, - sendSync, - sendAsync, - }; -})(this); diff --git a/cli/rt/11_crypto.js b/cli/rt/11_crypto.js index ab4a492009..32b026a784 100644 --- a/cli/rt/11_crypto.js +++ b/cli/rt/11_crypto.js @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; const { assert } = window.__bootstrap.util; function getRandomValues(typedArray) { @@ -12,7 +12,7 @@ typedArray.byteOffset, typedArray.byteLength, ); - sendSync("op_get_random_values", {}, ui8); + core.jsonOpSync("op_get_random_values", {}, ui8); return typedArray; } diff --git a/cli/rt/11_resources.js b/cli/rt/11_resources.js index 247e033cc3..9a1595b78c 100644 --- a/cli/rt/11_resources.js +++ b/cli/rt/11_resources.js @@ -1,10 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const sendSync = window.__bootstrap.dispatchJson.sendSync; + const core = window.Deno.core; function resources() { - const res = sendSync("op_resources"); + const res = core.jsonOpSync("op_resources"); const resources = {}; for (const resourceTuple of res) { resources[resourceTuple[0]] = resourceTuple[1]; @@ -13,7 +13,7 @@ } function close(rid) { - sendSync("op_close", { rid }); + core.jsonOpSync("op_close", { rid }); } window.__bootstrap.resources = { diff --git a/cli/rt/11_timers.js b/cli/rt/11_timers.js index 519a2f461d..ee909dbd96 100644 --- a/cli/rt/11_timers.js +++ b/cli/rt/11_timers.js @@ -2,18 +2,18 @@ ((window) => { const assert = window.__bootstrap.util.assert; - const dispatchJson = window.__bootstrap.dispatchJson; + const core = window.Deno.core; function opStopGlobalTimer() { - dispatchJson.sendSync("op_global_timer_stop"); + core.jsonOpSync("op_global_timer_stop"); } async function opStartGlobalTimer(timeout) { - await dispatchJson.sendAsync("op_global_timer", { timeout }); + await core.jsonOpAsync("op_global_timer", { timeout }); } function opNow() { - return dispatchJson.sendSync("op_now"); + return core.jsonOpSync("op_now"); } // Derived from https://github.com/vadimg/js_bintrees. MIT Licensed. diff --git a/cli/rt/11_url.js b/cli/rt/11_url.js index 86dcbf26d3..e47f710c80 100644 --- a/cli/rt/11_url.js +++ b/cli/rt/11_url.js @@ -1,9 +1,9 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { getRandomValues } = window.__bootstrap.crypto; const { customInspect } = window.__bootstrap.console; - const { sendSync } = window.__bootstrap.dispatchJson; const { isIterable, requiredArguments } = window.__bootstrap.webUtil; /** https://url.spec.whatwg.org/#idna */ @@ -11,7 +11,7 @@ domain, { beStrict = false } = {}, ) { - return sendSync("op_domain_to_ascii", { domain, beStrict }); + return core.jsonOpSync("op_domain_to_ascii", { domain, beStrict }); } function decodeSearchParam(p) { diff --git a/cli/rt/11_workers.js b/cli/rt/11_workers.js index 9a23e3dd88..77ac273f44 100644 --- a/cli/rt/11_workers.js +++ b/cli/rt/11_workers.js @@ -2,8 +2,8 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ ((window) => { + const core = window.Deno.core; const { log } = window.__bootstrap.util; - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; /* import { blobURLMap } from "./web/url.ts"; */ @@ -15,7 +15,7 @@ useDenoNamespace, name, ) { - return sendSync("op_create_worker", { + return core.jsonOpSync("op_create_worker", { specifier, hasSourceCode, sourceCode, @@ -25,15 +25,15 @@ } function hostTerminateWorker(id) { - sendSync("op_host_terminate_worker", { id }); + core.jsonOpSync("op_host_terminate_worker", { id }); } function hostPostMessage(id, data) { - sendSync("op_host_post_message", { id }, data); + core.jsonOpSync("op_host_post_message", { id }, data); } function hostGetMessage(id) { - return sendAsync("op_host_get_message", { id }); + return core.jsonOpAsync("op_host_get_message", { id }); } const encoder = new TextEncoder(); diff --git a/cli/rt/26_fetch.js b/cli/rt/26_fetch.js index 9e34aa8d8f..1c1d8c78f8 100644 --- a/cli/rt/26_fetch.js +++ b/cli/rt/26_fetch.js @@ -1,12 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { notImplemented } = window.__bootstrap.util; const { getHeaderValueParams, isTypedArray } = window.__bootstrap.webUtil; const { Blob, bytesSymbol: blobBytesSymbol } = window.__bootstrap.blob; const { read } = window.__bootstrap.io; const { close } = window.__bootstrap.resources; - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; const Body = window.__bootstrap.body; const { ReadableStream } = window.__bootstrap.streams; const { MultipartBuilder } = window.__bootstrap.multipart; @@ -17,7 +17,7 @@ } function opCreateHttpClient(args) { - return sendSync("op_create_http_client", args); + return core.jsonOpSync("op_create_http_client", args); } class HttpClient { @@ -35,7 +35,7 @@ zeroCopy = new Uint8Array(body.buffer, body.byteOffset, body.byteLength); } - return sendAsync("op_fetch", args, ...(zeroCopy ? [zeroCopy] : [])); + return core.jsonOpAsync("op_fetch", args, ...(zeroCopy ? [zeroCopy] : [])); } const NULL_BODY_STATUS = [101, 204, 205, 304]; diff --git a/cli/rt/27_websocket.js b/cli/rt/27_websocket.js index fdb5333e3a..d7fc031690 100644 --- a/cli/rt/27_websocket.js +++ b/cli/rt/27_websocket.js @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendAsync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; const { close } = window.__bootstrap.resources; const { requiredArguments } = window.__bootstrap.webUtil; const CONNECTING = 0; @@ -47,7 +47,7 @@ ); } - sendAsync("op_ws_create", { + core.jsonOpAsync("op_ws_create", { url: wsURL.href, protocols: protocols.join("; "), }).then((create) => { @@ -57,7 +57,7 @@ this.#protocol = create.protocol; if (this.#readyState === CLOSING) { - sendAsync("op_ws_close", { + core.jsonOpAsync("op_ws_close", { rid: this.#rid, }).then(() => { this.#readyState = CLOSED; @@ -172,7 +172,7 @@ const sendTypedArray = (ta) => { this.#bufferedAmount += ta.size; - sendAsync("op_ws_send", { + core.jsonOpAsync("op_ws_send", { rid: this.#rid, }, ta).then(() => { this.#bufferedAmount -= ta.size; @@ -198,7 +198,7 @@ const encoder = new TextEncoder(); const d = encoder.encode(string); this.#bufferedAmount += d.size; - sendAsync("op_ws_send", { + core.jsonOpAsync("op_ws_send", { rid: this.#rid, text: string, }).then(() => { @@ -228,7 +228,7 @@ } else if (this.#readyState === OPEN) { this.#readyState = CLOSING; - sendAsync("op_ws_close", { + core.jsonOpAsync("op_ws_close", { rid: this.#rid, code, reason, @@ -249,7 +249,10 @@ async #eventLoop() { if (this.#readyState === OPEN) { - const message = await sendAsync("op_ws_next_event", { rid: this.#rid }); + const message = await core.jsonOpAsync( + "op_ws_next_event", + { rid: this.#rid }, + ); if (message.type === "string" || message.type === "binary") { let data; diff --git a/cli/rt/30_files.js b/cli/rt/30_files.js index c0a2bbbb6f..ff6b85ae53 100644 --- a/cli/rt/30_files.js +++ b/cli/rt/30_files.js @@ -1,9 +1,9 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { close } = window.__bootstrap.resources; const { read, readSync, write, writeSync } = window.__bootstrap.io; - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; const { pathFromURL } = window.__bootstrap.util; function seekSync( @@ -11,7 +11,7 @@ offset, whence, ) { - return sendSync("op_seek_sync", { rid, offset, whence }); + return core.jsonOpSync("op_seek_sync", { rid, offset, whence }); } function seek( @@ -19,7 +19,7 @@ offset, whence, ) { - return sendAsync("op_seek_async", { rid, offset, whence }); + return core.jsonOpAsync("op_seek_async", { rid, offset, whence }); } function openSync( @@ -28,7 +28,7 @@ ) { checkOpenOptions(options); const mode = options?.mode; - const rid = sendSync( + const rid = core.jsonOpSync( "op_open_sync", { path: pathFromURL(path), options, mode }, ); @@ -42,7 +42,7 @@ ) { checkOpenOptions(options); const mode = options?.mode; - const rid = await sendAsync( + const rid = await core.jsonOpAsync( "op_open_async", { path: pathFromURL(path), options, mode }, ); diff --git a/cli/rt/30_fs.js b/cli/rt/30_fs.js index df91d767f0..7faa2c392e 100644 --- a/cli/rt/30_fs.js +++ b/cli/rt/30_fs.js @@ -1,16 +1,16 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; const { pathFromURL } = window.__bootstrap.util; const build = window.__bootstrap.build.build; function chmodSync(path, mode) { - sendSync("op_chmod_sync", { path: pathFromURL(path), mode }); + core.jsonOpSync("op_chmod_sync", { path: pathFromURL(path), mode }); } async function chmod(path, mode) { - await sendAsync("op_chmod_async", { path: pathFromURL(path), mode }); + await core.jsonOpAsync("op_chmod_async", { path: pathFromURL(path), mode }); } function chownSync( @@ -18,7 +18,7 @@ uid, gid, ) { - sendSync("op_chown_sync", { path: pathFromURL(path), uid, gid }); + core.jsonOpSync("op_chown_sync", { path: pathFromURL(path), uid, gid }); } async function chown( @@ -26,14 +26,17 @@ uid, gid, ) { - await sendAsync("op_chown_async", { path: pathFromURL(path), uid, gid }); + await core.jsonOpAsync( + "op_chown_async", + { path: pathFromURL(path), uid, gid }, + ); } function copyFileSync( fromPath, toPath, ) { - sendSync("op_copy_file_sync", { + core.jsonOpSync("op_copy_file_sync", { from: pathFromURL(fromPath), to: pathFromURL(toPath), }); @@ -43,34 +46,34 @@ fromPath, toPath, ) { - await sendAsync("op_copy_file_async", { + await core.jsonOpAsync("op_copy_file_async", { from: pathFromURL(fromPath), to: pathFromURL(toPath), }); } function cwd() { - return sendSync("op_cwd"); + return core.jsonOpSync("op_cwd"); } function chdir(directory) { - sendSync("op_chdir", { directory }); + core.jsonOpSync("op_chdir", { directory }); } function makeTempDirSync(options = {}) { - return sendSync("op_make_temp_dir_sync", options); + return core.jsonOpSync("op_make_temp_dir_sync", options); } function makeTempDir(options = {}) { - return sendAsync("op_make_temp_dir_async", options); + return core.jsonOpAsync("op_make_temp_dir_async", options); } function makeTempFileSync(options = {}) { - return sendSync("op_make_temp_file_sync", options); + return core.jsonOpSync("op_make_temp_file_sync", options); } function makeTempFile(options = {}) { - return sendAsync("op_make_temp_file_async", options); + return core.jsonOpAsync("op_make_temp_file_async", options); } function mkdirArgs(path, options) { @@ -87,14 +90,14 @@ } function mkdirSync(path, options) { - sendSync("op_mkdir_sync", mkdirArgs(path, options)); + core.jsonOpSync("op_mkdir_sync", mkdirArgs(path, options)); } async function mkdir( path, options, ) { - await sendAsync("op_mkdir_async", mkdirArgs(path, options)); + await core.jsonOpAsync("op_mkdir_async", mkdirArgs(path, options)); } function res(response) { @@ -102,13 +105,18 @@ } function readDirSync(path) { - return res(sendSync("op_read_dir_sync", { path: pathFromURL(path) }))[ + return res( + core.jsonOpSync("op_read_dir_sync", { path: pathFromURL(path) }), + )[ Symbol.iterator ](); } function readDir(path) { - const array = sendAsync("op_read_dir_async", { path: pathFromURL(path) }) + const array = core.jsonOpAsync( + "op_read_dir_async", + { path: pathFromURL(path) }, + ) .then( res, ); @@ -120,26 +128,26 @@ } function readLinkSync(path) { - return sendSync("op_read_link_sync", { path }); + return core.jsonOpSync("op_read_link_sync", { path }); } function readLink(path) { - return sendAsync("op_read_link_async", { path }); + return core.jsonOpAsync("op_read_link_async", { path }); } function realPathSync(path) { - return sendSync("op_realpath_sync", { path }); + return core.jsonOpSync("op_realpath_sync", { path }); } function realPath(path) { - return sendAsync("op_realpath_async", { path }); + return core.jsonOpAsync("op_realpath_async", { path }); } function removeSync( path, options = {}, ) { - sendSync("op_remove_sync", { + core.jsonOpSync("op_remove_sync", { path: pathFromURL(path), recursive: !!options.recursive, }); @@ -149,18 +157,18 @@ path, options = {}, ) { - await sendAsync("op_remove_async", { + await core.jsonOpAsync("op_remove_async", { path: pathFromURL(path), recursive: !!options.recursive, }); } function renameSync(oldpath, newpath) { - sendSync("op_rename_sync", { oldpath, newpath }); + core.jsonOpSync("op_rename_sync", { oldpath, newpath }); } async function rename(oldpath, newpath) { - await sendAsync("op_rename_async", { oldpath, newpath }); + await core.jsonOpAsync("op_rename_async", { oldpath, newpath }); } function parseFileInfo(response) { @@ -189,15 +197,15 @@ } function fstatSync(rid) { - return parseFileInfo(sendSync("op_fstat_sync", { rid })); + return parseFileInfo(core.jsonOpSync("op_fstat_sync", { rid })); } async function fstat(rid) { - return parseFileInfo(await sendAsync("op_fstat_async", { rid })); + return parseFileInfo(await core.jsonOpAsync("op_fstat_async", { rid })); } async function lstat(path) { - const res = await sendAsync("op_stat_async", { + const res = await core.jsonOpAsync("op_stat_async", { path: pathFromURL(path), lstat: true, }); @@ -205,7 +213,7 @@ } function lstatSync(path) { - const res = sendSync("op_stat_sync", { + const res = core.jsonOpSync("op_stat_sync", { path: pathFromURL(path), lstat: true, }); @@ -213,7 +221,7 @@ } async function stat(path) { - const res = await sendAsync("op_stat_async", { + const res = await core.jsonOpAsync("op_stat_async", { path: pathFromURL(path), lstat: false, }); @@ -221,7 +229,7 @@ } function statSync(path) { - const res = sendSync("op_stat_sync", { + const res = core.jsonOpSync("op_stat_sync", { path: pathFromURL(path), lstat: false, }); @@ -237,31 +245,31 @@ } function ftruncateSync(rid, len) { - sendSync("op_ftruncate_sync", { rid, len: coerceLen(len) }); + core.jsonOpSync("op_ftruncate_sync", { rid, len: coerceLen(len) }); } async function ftruncate(rid, len) { - await sendAsync("op_ftruncate_async", { rid, len: coerceLen(len) }); + await core.jsonOpAsync("op_ftruncate_async", { rid, len: coerceLen(len) }); } function truncateSync(path, len) { - sendSync("op_truncate_sync", { path, len: coerceLen(len) }); + core.jsonOpSync("op_truncate_sync", { path, len: coerceLen(len) }); } async function truncate(path, len) { - await sendAsync("op_truncate_async", { path, len: coerceLen(len) }); + await core.jsonOpAsync("op_truncate_async", { path, len: coerceLen(len) }); } function umask(mask) { - return sendSync("op_umask", { mask }); + return core.jsonOpSync("op_umask", { mask }); } function linkSync(oldpath, newpath) { - sendSync("op_link_sync", { oldpath, newpath }); + core.jsonOpSync("op_link_sync", { oldpath, newpath }); } async function link(oldpath, newpath) { - await sendAsync("op_link_async", { oldpath, newpath }); + await core.jsonOpAsync("op_link_async", { oldpath, newpath }); } function toUnixTimeFromEpoch(value) { @@ -290,7 +298,7 @@ atime, mtime, ) { - sendSync("op_futime_sync", { + core.jsonOpSync("op_futime_sync", { rid, atime: toUnixTimeFromEpoch(atime), mtime: toUnixTimeFromEpoch(mtime), @@ -302,7 +310,7 @@ atime, mtime, ) { - await sendAsync("op_futime_async", { + await core.jsonOpAsync("op_futime_async", { rid, atime: toUnixTimeFromEpoch(atime), mtime: toUnixTimeFromEpoch(mtime), @@ -314,7 +322,7 @@ atime, mtime, ) { - sendSync("op_utime_sync", { + core.jsonOpSync("op_utime_sync", { path, atime: toUnixTimeFromEpoch(atime), mtime: toUnixTimeFromEpoch(mtime), @@ -326,7 +334,7 @@ atime, mtime, ) { - await sendAsync("op_utime_async", { + await core.jsonOpAsync("op_utime_async", { path, atime: toUnixTimeFromEpoch(atime), mtime: toUnixTimeFromEpoch(mtime), @@ -338,7 +346,7 @@ newpath, options, ) { - sendSync("op_symlink_sync", { oldpath, newpath, options }); + core.jsonOpSync("op_symlink_sync", { oldpath, newpath, options }); } async function symlink( @@ -346,23 +354,23 @@ newpath, options, ) { - await sendAsync("op_symlink_async", { oldpath, newpath, options }); + await core.jsonOpAsync("op_symlink_async", { oldpath, newpath, options }); } function fdatasyncSync(rid) { - sendSync("op_fdatasync_sync", { rid }); + core.jsonOpSync("op_fdatasync_sync", { rid }); } async function fdatasync(rid) { - await sendAsync("op_fdatasync_async", { rid }); + await core.jsonOpAsync("op_fdatasync_async", { rid }); } function fsyncSync(rid) { - sendSync("op_fsync_sync", { rid }); + core.jsonOpSync("op_fsync_sync", { rid }); } async function fsync(rid) { - await sendAsync("op_fsync_async", { rid }); + await core.jsonOpAsync("op_fsync_async", { rid }); } window.__bootstrap.fs = { diff --git a/cli/rt/30_metrics.js b/cli/rt/30_metrics.js index 59a76d9102..d44a629cb2 100644 --- a/cli/rt/30_metrics.js +++ b/cli/rt/30_metrics.js @@ -1,10 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; function metrics() { - return sendSync("op_metrics"); + return core.jsonOpSync("op_metrics"); } window.__bootstrap.metrics = { diff --git a/cli/rt/30_net.js b/cli/rt/30_net.js index 78d8b3276a..8c3dcb4ba6 100644 --- a/cli/rt/30_net.js +++ b/cli/rt/30_net.js @@ -1,10 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { errors } = window.__bootstrap.errors; const { read, write } = window.__bootstrap.io; const { close } = window.__bootstrap.resources; - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; const ShutdownMode = { // See http://man7.org/linux/man-pages/man2/shutdown.2.html @@ -18,7 +18,7 @@ }; function shutdown(rid, how) { - sendSync("op_shutdown", { rid, how }); + core.jsonOpSync("op_shutdown", { rid, how }); return Promise.resolve(); } @@ -26,15 +26,15 @@ rid, transport, ) { - return sendAsync("op_accept", { rid, transport }); + return core.jsonOpAsync("op_accept", { rid, transport }); } function opListen(args) { - return sendSync("op_listen", args); + return core.jsonOpSync("op_listen", args); } function opConnect(args) { - return sendAsync("op_connect", args); + return core.jsonOpAsync("op_connect", args); } function opReceive( @@ -42,11 +42,15 @@ transport, zeroCopy, ) { - return sendAsync("op_datagram_receive", { rid, transport }, zeroCopy); + return core.jsonOpAsync( + "op_datagram_receive", + { rid, transport }, + zeroCopy, + ); } function opSend(args, zeroCopy) { - return sendAsync("op_datagram_send", args, zeroCopy); + return core.jsonOpAsync("op_datagram_send", args, zeroCopy); } class Conn { diff --git a/cli/rt/30_os.js b/cli/rt/30_os.js index adf256fc61..892d3bf8bb 100644 --- a/cli/rt/30_os.js +++ b/cli/rt/30_os.js @@ -1,52 +1,52 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const sendSync = window.__bootstrap.dispatchJson.sendSync; + const core = window.Deno.core; function loadavg() { - return sendSync("op_loadavg"); + return core.jsonOpSync("op_loadavg"); } function hostname() { - return sendSync("op_hostname"); + return core.jsonOpSync("op_hostname"); } function osRelease() { - return sendSync("op_os_release"); + return core.jsonOpSync("op_os_release"); } function systemMemoryInfo() { - return sendSync("op_system_memory_info"); + return core.jsonOpSync("op_system_memory_info"); } function exit(code = 0) { - sendSync("op_exit", { code }); + core.jsonOpSync("op_exit", { code }); throw new Error("Code not reachable"); } function setEnv(key, value) { - sendSync("op_set_env", { key, value }); + core.jsonOpSync("op_set_env", { key, value }); } function getEnv(key) { - return sendSync("op_get_env", { key })[0]; + return core.jsonOpSync("op_get_env", { key })[0]; } function deleteEnv(key) { - sendSync("op_delete_env", { key }); + core.jsonOpSync("op_delete_env", { key }); } const env = { get: getEnv, toObject() { - return sendSync("op_env"); + return core.jsonOpSync("op_env"); }, set: setEnv, delete: deleteEnv, }; function execPath() { - return sendSync("op_exec_path"); + return core.jsonOpSync("op_exec_path"); } window.__bootstrap.os = { diff --git a/cli/rt/40_compiler_api.js b/cli/rt/40_compiler_api.js index 8a2aa759a5..ffe58559e2 100644 --- a/cli/rt/40_compiler_api.js +++ b/cli/rt/40_compiler_api.js @@ -3,17 +3,17 @@ // This file contains the runtime APIs which will dispatch work to the internal // compiler within Deno. ((window) => { + const core = window.Deno.core; const util = window.__bootstrap.util; - const { sendAsync } = window.__bootstrap.dispatchJson; function opCompile(request) { - return sendAsync("op_compile", request); + return core.jsonOpAsync("op_compile", request); } function opTranspile( request, ) { - return sendAsync("op_transpile", request); + return core.jsonOpAsync("op_transpile", request); } function checkRelative(specifier) { diff --git a/cli/rt/40_error_stack.js b/cli/rt/40_error_stack.js index 2b7c424757..08647a8d85 100644 --- a/cli/rt/40_error_stack.js +++ b/cli/rt/40_error_stack.js @@ -3,17 +3,17 @@ ((window) => { // Some of the code here is adapted directly from V8 and licensed under a BSD // style license available here: https://github.com/v8/v8/blob/24886f2d1c565287d33d71e4109a53bf0b54b75c/LICENSE.v8 + const core = window.Deno.core; const colors = window.__bootstrap.colors; const assert = window.__bootstrap.util.assert; const internals = window.__bootstrap.internals; - const dispatchJson = window.__bootstrap.dispatchJson; function opFormatDiagnostics(diagnostics) { - return dispatchJson.sendSync("op_format_diagnostic", diagnostics); + return core.jsonOpSync("op_format_diagnostic", diagnostics); } function opApplySourceMap(location) { - const res = dispatchJson.sendSync("op_apply_source_map", location); + const res = core.jsonOpSync("op_apply_source_map", location); return { fileName: res.fileName, lineNumber: res.lineNumber, diff --git a/cli/rt/40_fs_events.js b/cli/rt/40_fs_events.js index b9121c1557..43c0a4b92d 100644 --- a/cli/rt/40_fs_events.js +++ b/cli/rt/40_fs_events.js @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; const { errors } = window.__bootstrap.errors; const { close } = window.__bootstrap.resources; @@ -10,7 +10,7 @@ constructor(paths, options) { const { recursive } = options; - this.#rid = sendSync("op_fs_events_open", { recursive, paths }); + this.#rid = core.jsonOpSync("op_fs_events_open", { recursive, paths }); } get rid() { @@ -19,7 +19,7 @@ async next() { try { - return await sendAsync("op_fs_events_poll", { + return await core.jsonOpAsync("op_fs_events_poll", { rid: this.rid, }); } catch (error) { diff --git a/cli/rt/40_permissions.js b/cli/rt/40_permissions.js index 4aebc94e74..983d0895ad 100644 --- a/cli/rt/40_permissions.js +++ b/cli/rt/40_permissions.js @@ -1,18 +1,18 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; function opQuery(desc) { - return sendSync("op_query_permission", desc).state; + return core.jsonOpSync("op_query_permission", desc).state; } function opRevoke(desc) { - return sendSync("op_revoke_permission", desc).state; + return core.jsonOpSync("op_revoke_permission", desc).state; } function opRequest(desc) { - return sendSync("op_request_permission", desc).state; + return core.jsonOpSync("op_request_permission", desc).state; } class PermissionStatus { diff --git a/cli/rt/40_plugins.js b/cli/rt/40_plugins.js index dda28d6b2f..f5aefd400c 100644 --- a/cli/rt/40_plugins.js +++ b/cli/rt/40_plugins.js @@ -1,10 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; function openPlugin(filename) { - return sendSync("op_open_plugin", { filename }); + return core.jsonOpSync("op_open_plugin", { filename }); } window.__bootstrap.plugins = { diff --git a/cli/rt/40_process.js b/cli/rt/40_process.js index 0e81b8b0f1..e188e9c6ae 100644 --- a/cli/rt/40_process.js +++ b/cli/rt/40_process.js @@ -1,23 +1,23 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { File } = window.__bootstrap.files; const { close } = window.__bootstrap.resources; const { readAll } = window.__bootstrap.buffer; - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; const { assert, pathFromURL } = window.__bootstrap.util; function opKill(pid, signo) { - sendSync("op_kill", { pid, signo }); + core.jsonOpSync("op_kill", { pid, signo }); } function opRunStatus(rid) { - return sendAsync("op_run_status", { rid }); + return core.jsonOpAsync("op_run_status", { rid }); } function opRun(request) { assert(request.cmd.length > 0); - return sendSync("op_run", request); + return core.jsonOpSync("op_run", request); } async function runStatus(rid) { diff --git a/cli/rt/40_repl.js b/cli/rt/40_repl.js index 4966c45be8..8b05aa4c3d 100644 --- a/cli/rt/40_repl.js +++ b/cli/rt/40_repl.js @@ -4,16 +4,15 @@ const core = window.Deno.core; const exit = window.__bootstrap.os.exit; const version = window.__bootstrap.version.version; - const dispatchJson = window.__bootstrap.dispatchJson; const close = window.__bootstrap.resources.close; const inspectArgs = window.__bootstrap.console.inspectArgs; function opStartRepl(historyFile) { - return dispatchJson.sendSync("op_repl_start", { historyFile }); + return core.jsonOpSync("op_repl_start", { historyFile }); } function opReadline(rid, prompt) { - return dispatchJson.sendAsync("op_repl_readline", { rid, prompt }); + return core.jsonOpAsync("op_repl_readline", { rid, prompt }); } function replLog(...args) { diff --git a/cli/rt/40_signals.js b/cli/rt/40_signals.js index ab060598fb..739c963fd9 100644 --- a/cli/rt/40_signals.js +++ b/cli/rt/40_signals.js @@ -1,19 +1,19 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; const { build } = window.__bootstrap.build; function bindSignal(signo) { - return sendSync("op_signal_bind", { signo }); + return core.jsonOpSync("op_signal_bind", { signo }); } function pollSignal(rid) { - return sendAsync("op_signal_poll", { rid }); + return core.jsonOpAsync("op_signal_poll", { rid }); } function unbindSignal(rid) { - sendSync("op_signal_unbind", { rid }); + core.jsonOpSync("op_signal_unbind", { rid }); } // From `kill -l` diff --git a/cli/rt/40_tls.js b/cli/rt/40_tls.js index f4ae551122..d66e0bd012 100644 --- a/cli/rt/40_tls.js +++ b/cli/rt/40_tls.js @@ -1,25 +1,25 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { + const core = window.Deno.core; const { Listener, Conn } = window.__bootstrap.net; - const { sendAsync, sendSync } = window.__bootstrap.dispatchJson; function opConnectTls( args, ) { - return sendAsync("op_connect_tls", args); + return core.jsonOpAsync("op_connect_tls", args); } function opAcceptTLS(rid) { - return sendAsync("op_accept_tls", { rid }); + return core.jsonOpAsync("op_accept_tls", { rid }); } function opListenTls(args) { - return sendSync("op_listen_tls", args); + return core.jsonOpSync("op_listen_tls", args); } function opStartTls(args) { - return sendAsync("op_start_tls", args); + return core.jsonOpAsync("op_start_tls", args); } async function connectTls({ diff --git a/cli/rt/40_tty.js b/cli/rt/40_tty.js index 3bab4f3216..b50b7668ce 100644 --- a/cli/rt/40_tty.js +++ b/cli/rt/40_tty.js @@ -1,18 +1,18 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. ((window) => { - const { sendSync } = window.__bootstrap.dispatchJson; + const core = window.Deno.core; function consoleSize(rid) { - return sendSync("op_console_size", { rid }); + return core.jsonOpSync("op_console_size", { rid }); } function isatty(rid) { - return sendSync("op_isatty", { rid }); + return core.jsonOpSync("op_isatty", { rid }); } function setRaw(rid, mode) { - sendSync("op_set_raw", { rid, mode }); + core.jsonOpSync("op_set_raw", { rid, mode }); } window.__bootstrap.tty = { diff --git a/cli/rt/99_main.js b/cli/rt/99_main.js index 74a7bd2a81..03bd77be49 100644 --- a/cli/rt/99_main.js +++ b/cli/rt/99_main.js @@ -7,7 +7,6 @@ delete Object.prototype.__proto__; const core = Deno.core; const util = window.__bootstrap.util; const eventTarget = window.__bootstrap.eventTarget; - const dispatchJson = window.__bootstrap.dispatchJson; const dispatchMinimal = window.__bootstrap.dispatchMinimal; const build = window.__bootstrap.build; const version = window.__bootstrap.version; @@ -129,29 +128,19 @@ delete Object.prototype.__proto__; } function opPostMessage(data) { - dispatchJson.sendSync("op_worker_post_message", {}, data); + core.jsonOpSync("op_worker_post_message", {}, data); } function opCloseWorker() { - dispatchJson.sendSync("op_worker_close"); + core.jsonOpSync("op_worker_close"); } function opStart() { - return dispatchJson.sendSync("op_start"); + return core.jsonOpSync("op_start"); } function opMainModule() { - return dispatchJson.sendSync("op_main_module"); - } - - function getAsyncHandler(opName) { - switch (opName) { - case "op_write": - case "op_read": - return dispatchMinimal.asyncMsgFromRust; - default: - return dispatchJson.asyncMsgFromRust; - } + return core.jsonOpSync("op_main_module"); } // TODO(bartlomieju): temporary solution, must be fixed when moving @@ -159,7 +148,9 @@ delete Object.prototype.__proto__; function initOps() { const opsMap = core.ops(); for (const [name, opId] of Object.entries(opsMap)) { - core.setAsyncHandler(opId, getAsyncHandler(name)); + if (name === "op_write" || name === "op_read") { + core.setAsyncHandler(opId, dispatchMinimal.asyncMsgFromRust); + } } core.setMacrotaskCallback(timers.handleTimerMacrotask); } diff --git a/cli/tests/044_bad_resource.ts.out b/cli/tests/044_bad_resource.ts.out index 6d8b9da8a2..6f2ac60f83 100644 --- a/cli/tests/044_bad_resource.ts.out +++ b/cli/tests/044_bad_resource.ts.out @@ -1,4 +1,3 @@ [WILDCARD]error: Uncaught BadResource: Bad resource ID - at unwrapResponse ([WILDCARD]dispatch_json.js:[WILDCARD]) - at sendAsync ([WILDCARD]dispatch_json.js:[WILDCARD]) +[WILDCARD] at async main ([WILDCARD]tests/044_bad_resource.ts:[WILDCARD]) diff --git a/cli/tests/compiler_js_error.ts.out b/cli/tests/compiler_js_error.ts.out index 15eb6b22ce..8c6e6e35eb 100644 --- a/cli/tests/compiler_js_error.ts.out +++ b/cli/tests/compiler_js_error.ts.out @@ -2,6 +2,3 @@ Check [WILDCARD]compiler_js_error.ts error: Uncaught Error: Error in TS compiler: Uncaught AssertionError: Unexpected skip of the emit. [WILDCARD] - at unwrapResponse ([WILDCARD]dispatch_json.js:[WILDCARD]) - at sendAsync ([WILDCARD]dispatch_json.js:[WILDCARD]) - at async Object.compile ([WILDCARD]compiler_api.js:[WILDCARD]) diff --git a/cli/tests/unit/buffer_test.ts b/cli/tests/unit/buffer_test.ts index 7a628cae5e..999680be7e 100644 --- a/cli/tests/unit/buffer_test.ts +++ b/cli/tests/unit/buffer_test.ts @@ -329,9 +329,9 @@ unitTest(async function bufferReadFromSync(): Promise { unitTest(async function bufferTestGrow(): Promise { const tmp = new Uint8Array(72); - for (const startLen of [0, 100, 1000, 10000, 100000]) { + for (const startLen of [0, 100, 1000, 10000]) { const xBytes = repeat("x", startLen); - for (const growLen of [0, 100, 1000, 10000, 100000]) { + for (const growLen of [0, 100, 1000, 10000]) { const buf = new Deno.Buffer(xBytes.buffer as ArrayBuffer); // If we read, this affects buf.off, which is good to test. const nread = (await buf.read(tmp)) ?? 0; diff --git a/cli/tests/unit/dispatch_json_test.ts b/cli/tests/unit/dispatch_json_test.ts index 076c9e6f8a..29127a85c7 100644 --- a/cli/tests/unit/dispatch_json_test.ts +++ b/cli/tests/unit/dispatch_json_test.ts @@ -2,28 +2,8 @@ import { assertStrictEquals, unitTest, assertMatch, - unreachable, } from "./test_util.ts"; -const openErrorStackPattern = new RegExp( - `^.* - at unwrapResponse \\(.*dispatch_json\\.js:.*\\) - at sendAsync \\(.*dispatch_json\\.js:.*\\) - at async Object\\.open \\(.*files\\.js:.*\\).*$`, - "ms", -); - -unitTest( - { perms: { read: true } }, - async function sendAsyncStackTrace(): Promise { - await Deno.open("nonexistent.txt") - .then(unreachable) - .catch((error): void => { - assertMatch(error.stack, openErrorStackPattern); - }); - }, -); - declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Deno { diff --git a/cli/tsc/10_dispatch_json.js b/cli/tsc/10_dispatch_json.js deleted file mode 100644 index 7989f55683..0000000000 --- a/cli/tsc/10_dispatch_json.js +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. - -((window) => { - const core = window.Deno.core; - const util = window.__bootstrap.util; - - // Using an object without a prototype because `Map` was causing GC problems. - const promiseTable = Object.create(null); - let _nextPromiseId = 1; - - function nextPromiseId() { - return _nextPromiseId++; - } - - function decode(ui8) { - return JSON.parse(core.decode(ui8)); - } - - function encode(args) { - return core.encode(JSON.stringify(args)); - } - - function unwrapResponse(res) { - if (res.err != null) { - throw new (core.getErrorClass(res.err.className))(res.err.message); - } - util.assert(res.ok != null); - return res.ok; - } - - function asyncMsgFromRust(resUi8) { - const res = decode(resUi8); - util.assert(res.promiseId != null); - - const promise = promiseTable[res.promiseId]; - util.assert(promise != null); - delete promiseTable[res.promiseId]; - promise.resolve(res); - } - - function sendSync( - opName, - args = {}, - ...zeroCopy - ) { - util.log("sendSync", opName); - const argsUi8 = encode(args); - const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy); - util.assert(resUi8 != null); - const res = decode(resUi8); - util.assert(res.promiseId == null); - return unwrapResponse(res); - } - - async function sendAsync( - opName, - args = {}, - ...zeroCopy - ) { - util.log("sendAsync", opName); - const promiseId = nextPromiseId(); - args = Object.assign(args, { promiseId }); - const promise = util.createResolvable(); - const argsUi8 = encode(args); - const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy); - if (buf != null) { - // Sync result. - const res = decode(buf); - promise.resolve(res); - } else { - // Async result. - promiseTable[promiseId] = promise; - } - - const res = await promise; - return unwrapResponse(res); - } - - window.__bootstrap.dispatchJson = { - asyncMsgFromRust, - sendSync, - sendAsync, - }; -})(this); diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 80f7b22336..e5abbd144a 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -17,9 +17,8 @@ delete Object.prototype.__proto__; ((window) => { - const core = Deno.core; + const core = window.Deno.core; const { assert, log, notImplemented } = window.__bootstrap.util; - const dispatchJson = window.__bootstrap.dispatchJson; const util = window.__bootstrap.util; const errorStack = window.__bootstrap.errorStack; const errors = window.__bootstrap.errors.errors; @@ -78,7 +77,7 @@ delete Object.prototype.__proto__; } function opNow() { - const res = dispatchJson.sendSync("op_now"); + const res = core.jsonOpSync("op_now"); return res.seconds * 1e3 + res.subsecNanos / 1e6; } @@ -1252,7 +1251,7 @@ delete Object.prototype.__proto__; } function opCompilerRespond(msg) { - dispatchJson.sendSync("op_compiler_respond", msg); + core.jsonOpSync("op_compiler_respond", msg); } async function tsCompilerOnMessage(msg) { @@ -1292,21 +1291,12 @@ delete Object.prototype.__proto__; } } - // TODO(bartlomieju): temporary solution, must be fixed when moving - // dispatches to separate crates - function initOps() { - const opsMap = core.ops(); - for (const [_name, opId] of Object.entries(opsMap)) { - core.setAsyncHandler(opId, dispatchJson.asyncMsgFromRust); - } - } - function runtimeStart(source) { - initOps(); + core.ops(); // First we send an empty `Start` message to let the privileged side know we // are ready. The response should be a `StartRes` message containing the CLI // args and other info. - const s = dispatchJson.sendSync("op_start"); + const s = core.jsonOpSync("op_start"); util.setLogDebug(s.debugFlag, source); errorStack.setPrepareStackTrace(Error); return s; diff --git a/core/core.js b/core/core.js index b84cb2e99d..b0de55b2c7 100644 --- a/core/core.js +++ b/core/core.js @@ -208,14 +208,14 @@ SharedQueue Binary Layout } function decodeJson(ui8) { - const s = Deno.core.decode(ui8); + const s = core.decode(ui8); return JSON.parse(s); } let nextPromiseId = 1; const promiseTable = {}; - function jsonOpAsync(opName, args, ...zeroCopy) { + async function jsonOpAsync(opName, args = {}, ...zeroCopy) { setAsyncHandler(opsCache[opName], jsonOpAsyncHandler); args.promiseId = nextPromiseId++; @@ -229,31 +229,31 @@ SharedQueue Binary Layout promise.resolve = resolve; promise.reject = reject; promiseTable[args.promiseId] = promise; - return promise; + const res = await promise; + if ("ok" in res) { + return res.ok; + } else { + throw new (getErrorClass(res.err.className))(res.err.message); + } } - function jsonOpSync(opName, args, ...zeroCopy) { + function jsonOpSync(opName, args = {}, ...zeroCopy) { const argsBuf = encodeJson(args); const res = dispatch(opName, argsBuf, ...zeroCopy); const r = decodeJson(res); if ("ok" in r) { return r.ok; } else { - throw r.err; + throw new (getErrorClass(r.err.className))(r.err.message); } } function jsonOpAsyncHandler(buf) { // Json Op. - const msg = decodeJson(buf); - const { ok, err, promiseId } = msg; - const promise = promiseTable[promiseId]; - delete promiseTable[promiseId]; - if (ok) { - promise.resolve(ok); - } else { - promise.reject(err); - } + const res = decodeJson(buf); + const promise = promiseTable[res.promiseId]; + delete promiseTable[res.promiseId]; + promise.resolve(res); } Object.assign(window.Deno.core, {