diff options
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/30_os.js | 6 | ||||
-rw-r--r-- | runtime/js/40_compiler_api.js | 12 | ||||
-rw-r--r-- | runtime/js/40_files.js | 12 | ||||
-rw-r--r-- | runtime/js/40_fs_events.js | 12 | ||||
-rw-r--r-- | runtime/js/40_permissions.js | 37 | ||||
-rw-r--r-- | runtime/js/40_process.js | 11 | ||||
-rw-r--r-- | runtime/js/40_signals.js | 21 | ||||
-rw-r--r-- | runtime/js/40_testing.js | 46 | ||||
-rw-r--r-- | runtime/js/40_write_file.js | 7 | ||||
-rw-r--r-- | runtime/js/41_prompt.js | 10 | ||||
-rw-r--r-- | runtime/js/99_main.js | 83 |
11 files changed, 175 insertions, 82 deletions
diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index 2d11d6fde..afb5aa4e0 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -3,6 +3,10 @@ ((window) => { const core = window.Deno.core; + const { + Error, + SymbolFor, + } = window.__bootstrap.primordials; function loadavg() { return core.opSync("op_loadavg"); @@ -38,7 +42,7 @@ function exit(code = 0) { // Dispatches `unload` only when it's not dispatched yet. - if (!window[Symbol.for("isUnloadDispatched")]) { + if (!window[SymbolFor("isUnloadDispatched")]) { // Invokes the `unload` hooks before exiting // ref: https://github.com/denoland/deno/issues/3603 window.dispatchEvent(new Event("unload")); diff --git a/runtime/js/40_compiler_api.js b/runtime/js/40_compiler_api.js index 75c21b8dd..4d5cda398 100644 --- a/runtime/js/40_compiler_api.js +++ b/runtime/js/40_compiler_api.js @@ -8,6 +8,11 @@ ((window) => { const core = window.Deno.core; const util = window.__bootstrap.util; + const { + StringPrototypeMatch, + PromiseReject, + TypeError, + } = window.__bootstrap.primordials; /** * @typedef {object} ImportMap @@ -47,7 +52,10 @@ * @returns {string} */ function checkRelative(specifier) { - return specifier.match(/^([\.\/\\]|https?:\/{2}|file:\/{2})/) + return StringPrototypeMatch( + specifier, + /^([\.\/\\]|https?:\/{2}|file:\/{2})/, + ) ? specifier : `./${specifier}`; } @@ -70,7 +78,7 @@ function emit(rootSpecifier, options = {}) { util.log(`Deno.emit`, { rootSpecifier }); if (!rootSpecifier) { - return Promise.reject( + return PromiseReject( new TypeError("A root specifier must be supplied."), ); } diff --git a/runtime/js/40_files.js b/runtime/js/40_files.js index 82cf19ffc..dfd471750 100644 --- a/runtime/js/40_files.js +++ b/runtime/js/40_files.js @@ -6,6 +6,11 @@ const { read, readSync, write, writeSync } = window.__bootstrap.io; const { ftruncate, ftruncateSync, fstat, fstatSync } = window.__bootstrap.fs; const { pathFromURL } = window.__bootstrap.util; + const { + Error, + ObjectValues, + ArrayPrototypeFilter, + } = window.__bootstrap.primordials; function seekSync( rid, @@ -193,7 +198,12 @@ const stderr = new Stderr(); function checkOpenOptions(options) { - if (Object.values(options).filter((val) => val === true).length === 0) { + if ( + ArrayPrototypeFilter( + ObjectValues(options), + (val) => val === true, + ).length === 0 + ) { throw new Error("OpenOptions requires at least one option to be true"); } diff --git a/runtime/js/40_fs_events.js b/runtime/js/40_fs_events.js index 48060e23a..8402cb459 100644 --- a/runtime/js/40_fs_events.js +++ b/runtime/js/40_fs_events.js @@ -4,7 +4,11 @@ ((window) => { const core = window.Deno.core; const { errors } = window.__bootstrap.errors; - + const { + ArrayIsArray, + PromiseResolve, + SymbolAsyncIterator, + } = window.__bootstrap.primordials; class FsWatcher { #rid = 0; @@ -37,14 +41,14 @@ // See https://github.com/denoland/deno/issues/10577 for details return(value) { core.close(this.rid); - return Promise.resolve({ value, done: true }); + return PromiseResolve({ value, done: true }); } close() { core.close(this.rid); } - [Symbol.asyncIterator]() { + [SymbolAsyncIterator]() { return this; } } @@ -53,7 +57,7 @@ paths, options = { recursive: true }, ) { - return new FsWatcher(Array.isArray(paths) ? paths : [paths], options); + return new FsWatcher(ArrayIsArray(paths) ? paths : [paths], options); } window.__bootstrap.fsEvents = { diff --git a/runtime/js/40_permissions.js b/runtime/js/40_permissions.js index 00b1a2974..8e57a5b5a 100644 --- a/runtime/js/40_permissions.js +++ b/runtime/js/40_permissions.js @@ -8,6 +8,18 @@ Deno: { core }, __bootstrap: { webUtil: { illegalConstructorKey } }, } = window; + const { + ArrayPrototypeIncludes, + Map, + MapPrototypeGet, + MapPrototypeHas, + MapPrototypeSet, + FunctionPrototypeCall, + PromiseResolve, + PromiseReject, + SymbolFor, + TypeError, + } = window.__bootstrap.primordials; /** * @typedef StatusCacheValue @@ -81,13 +93,13 @@ dispatchEvent(event) { let dispatched = super.dispatchEvent(event); if (dispatched && this.onchange) { - this.onchange.call(this, event); + FunctionPrototypeCall(this.onchange, this, event); dispatched = !event.defaultPrevented; } return dispatched; } - [Symbol.for("Deno.privateCustomInspect")](inspect) { + [SymbolFor("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ state: this.state, onchange: this.onchange }) }`; @@ -110,8 +122,8 @@ } else if (desc.name === "net" && desc.host) { key += `-${desc.host}`; } - if (statusCache.has(key)) { - const status = statusCache.get(key); + if (MapPrototypeHas(statusCache, key)) { + const status = MapPrototypeGet(statusCache, key); if (status.state !== state) { status.state = state; status.status.dispatchEvent(new Event("change", { cancelable: false })); @@ -121,7 +133,7 @@ /** @type {{ state: Deno.PermissionState; status?: PermissionStatus }} */ const status = { state }; status.status = new PermissionStatus(status, illegalConstructorKey); - statusCache.set(key, status); + MapPrototypeSet(statusCache, key, status); return status.status; } @@ -130,7 +142,8 @@ * @returns {desc is Deno.PermissionDescriptor} */ function isValidDescriptor(desc) { - return desc && desc !== null && permissionNames.includes(desc.name); + return desc && desc !== null && + ArrayPrototypeIncludes(permissionNames, desc.name); } class Permissions { @@ -142,38 +155,38 @@ query(desc) { if (!isValidDescriptor(desc)) { - return Promise.reject( + return PromiseReject( new TypeError( `The provided value "${desc.name}" is not a valid permission name.`, ), ); } const state = opQuery(desc); - return Promise.resolve(cache(desc, state)); + return PromiseResolve(cache(desc, state)); } revoke(desc) { if (!isValidDescriptor(desc)) { - return Promise.reject( + return PromiseReject( new TypeError( `The provided value "${desc.name}" is not a valid permission name.`, ), ); } const state = opRevoke(desc); - return Promise.resolve(cache(desc, state)); + return PromiseResolve(cache(desc, state)); } request(desc) { if (!isValidDescriptor(desc)) { - return Promise.reject( + return PromiseReject( new TypeError( `The provided value "${desc.name}" is not a valid permission name.`, ), ); } const state = opRequest(desc); - return Promise.resolve(cache(desc, state)); + return PromiseResolve(cache(desc, state)); } } diff --git a/runtime/js/40_process.js b/runtime/js/40_process.js index 91e37701a..70a590f36 100644 --- a/runtime/js/40_process.js +++ b/runtime/js/40_process.js @@ -6,6 +6,13 @@ const { File } = window.__bootstrap.files; const { readAll } = window.__bootstrap.io; const { assert, pathFromURL } = window.__bootstrap.util; + const { + ArrayPrototypeMap, + TypeError, + isNaN, + ObjectEntries, + String, + } = window.__bootstrap.primordials; function opKill(pid, signo) { core.opSync("op_kill", { pid, signo }); @@ -102,9 +109,9 @@ cmd[0] = pathFromURL(cmd[0]); } const res = opRun({ - cmd: cmd.map(String), + cmd: ArrayPrototypeMap(cmd, String), cwd, - env: Object.entries(env), + env: ObjectEntries(env), stdin: isRid(stdin) ? "" : stdin, stdout: isRid(stdout) ? "" : stdout, stderr: isRid(stderr) ? "" : stderr, diff --git a/runtime/js/40_signals.js b/runtime/js/40_signals.js index bf6be1263..67c1da313 100644 --- a/runtime/js/40_signals.js +++ b/runtime/js/40_signals.js @@ -5,6 +5,14 @@ const core = window.Deno.core; const { build } = window.__bootstrap.build; const { errors } = window.__bootstrap.errors; + const { + Error, + ObjectAssign, + Promise, + PromisePrototypeThen, + PromiseResolve, + SymbolAsyncIterator, + } = window.__bootstrap.primordials; function bindSignal(signo) { return core.opSync("op_signal_bind", signo); @@ -154,9 +162,9 @@ function setSignals() { if (build.os === "darwin") { - Object.assign(Signal, MacOSSignal); + ObjectAssign(Signal, MacOSSignal); } else { - Object.assign(Signal, LinuxSignal); + ObjectAssign(Signal, LinuxSignal); } } @@ -205,7 +213,7 @@ class SignalStream { #disposed = false; - #pollingPromise = Promise.resolve(false); + #pollingPromise = PromiseResolve(false); #rid = 0; constructor(signo) { @@ -236,7 +244,7 @@ f, g, ) { - return this.#pollingPromise.then((done) => { + const p = PromisePrototypeThen(this.#pollingPromise, (done) => { if (done) { // If pollingPromise returns true, then // this signal stream is finished and the promise API @@ -244,14 +252,15 @@ return new Promise(() => {}); } return; - }).then(f, g); + }); + return PromisePrototypeThen(p, f, g); } async next() { return { done: await this.#pollingPromise, value: undefined }; } - [Symbol.asyncIterator]() { + [SymbolAsyncIterator]() { return this; } diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js index 8abb8a6c8..b1adfa993 100644 --- a/runtime/js/40_testing.js +++ b/runtime/js/40_testing.js @@ -8,6 +8,20 @@ const { Console, inspectArgs } = window.__bootstrap.console; const { metrics } = window.__bootstrap.metrics; const { assert } = window.__bootstrap.util; + const { + ArrayPrototypeFilter, + ArrayPrototypePush, + DateNow, + JSONStringify, + Promise, + TypeError, + StringPrototypeStartsWith, + StringPrototypeEndsWith, + StringPrototypeIncludes, + StringPrototypeSlice, + RegExp, + RegExpPrototypeTest, + } = window.__bootstrap.primordials; // Wrap test function in additional assertion that makes sure // the test case does not leak async "ops" - ie. number of async @@ -58,8 +72,8 @@ finishing test case.`, await fn(); const post = core.resources(); - const preStr = JSON.stringify(pre, null, 2); - const postStr = JSON.stringify(post, null, 2); + const preStr = JSONStringify(pre, null, 2); + const postStr = JSONStringify(post, null, 2); const msg = `Test case is leaking resources. Before: ${preStr} After: ${postStr} @@ -139,7 +153,7 @@ finishing test case.`; testDef.fn = assertExit(testDef.fn); } - tests.push(testDef); + ArrayPrototypePush(tests, testDef); } function postTestMessage(kind, data) { @@ -149,12 +163,17 @@ finishing test case.`; function createTestFilter(filter) { return (def) => { if (filter) { - if (filter.startsWith("/") && filter.endsWith("/")) { - const regex = new RegExp(filter.slice(1, filter.length - 1)); - return regex.test(def.name); + if ( + StringPrototypeStartsWith(filter, "/") && + StringPrototypeEndsWith(filter, "/") + ) { + const regex = new RegExp( + StringPrototypeSlice(filter, 1, filter.length - 1), + ); + return RegExpPrototypeTest(regex, def.name); } - return def.name.includes(filter); + return StringPrototypeIncludes(def.name, filter); } return true; @@ -174,7 +193,7 @@ finishing test case.`; async function runTest({ name, ignore, fn, permissions }) { let token = null; - const time = Date.now(); + const time = DateNow(); try { postTestMessage("wait", { @@ -186,7 +205,7 @@ finishing test case.`; } if (ignore) { - const duration = Date.now() - time; + const duration = DateNow() - time; postTestMessage("result", { name, duration, @@ -198,14 +217,14 @@ finishing test case.`; await fn(); - const duration = Date.now() - time; + const duration = DateNow() - time; postTestMessage("result", { name, duration, result: "ok", }); } catch (error) { - const duration = Date.now() - time; + const duration = DateNow() - time; postTestMessage("result", { name, @@ -230,8 +249,9 @@ finishing test case.`; globalThis.console = new Console(() => {}); } - const only = tests.filter((test) => test.only); - const pending = (only.length > 0 ? only : tests).filter( + const only = ArrayPrototypeFilter(tests, (test) => test.only); + const pending = ArrayPrototypeFilter( + (only.length > 0 ? only : tests), createTestFilter(filter), ); postTestMessage("plan", { diff --git a/runtime/js/40_write_file.js b/runtime/js/40_write_file.js index 4662849a5..def9f6fee 100644 --- a/runtime/js/40_write_file.js +++ b/runtime/js/40_write_file.js @@ -4,6 +4,9 @@ const { stat, statSync, chmod, chmodSync } = window.__bootstrap.fs; const { open, openSync } = window.__bootstrap.files; const { build } = window.__bootstrap.build; + const { + TypedArrayPrototypeSubarray, + } = window.__bootstrap.primordials; function writeFileSync( path, @@ -33,7 +36,7 @@ let nwritten = 0; while (nwritten < data.length) { - nwritten += file.writeSync(data.subarray(nwritten)); + nwritten += file.writeSync(TypedArrayPrototypeSubarray(data, nwritten)); } file.close(); @@ -67,7 +70,7 @@ let nwritten = 0; while (nwritten < data.length) { - nwritten += await file.write(data.subarray(nwritten)); + nwritten += await file.write(TypedArrayPrototypeSubarray(data, nwritten)); } file.close(); diff --git a/runtime/js/41_prompt.js b/runtime/js/41_prompt.js index cb088dafa..e941461de 100644 --- a/runtime/js/41_prompt.js +++ b/runtime/js/41_prompt.js @@ -2,9 +2,11 @@ "use strict"; ((window) => { const { stdin } = window.__bootstrap.files; + const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } = + window.__bootstrap.primordials; const { isatty } = window.__bootstrap.tty; - const LF = "\n".charCodeAt(0); - const CR = "\r".charCodeAt(0); + const LF = StringPrototypeCharCodeAt("\n", 0); + const CR = StringPrototypeCharCodeAt("\r", 0); const core = window.Deno.core; function alert(message = "Alert") { @@ -59,7 +61,7 @@ if (c[0] === LF) { break; } - buf.push(CR); + ArrayPrototypePush(buf, CR); if (n === null || n === 0) { break; } @@ -67,7 +69,7 @@ if (c[0] === LF) { break; } - buf.push(c[0]); + ArrayPrototypePush(buf, c[0]); } return core.decode(new Uint8Array(buf)); } diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 22fd6bd8e..a72b2177b 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -7,6 +7,20 @@ delete Object.prototype.__proto__; ((window) => { const core = Deno.core; + const { + Error, + FunctionPrototypeCall, + FunctionPrototypeBind, + ObjectAssign, + ObjectDefineProperty, + ObjectDefineProperties, + ObjectFreeze, + ObjectSetPrototypeOf, + PromiseResolve, + Symbol, + SymbolFor, + SymbolIterator, + } = window.__bootstrap.primordials; const util = window.__bootstrap.util; const eventTarget = window.__bootstrap.eventTarget; const globalInterfaces = window.__bootstrap.globalInterfaces; @@ -53,15 +67,11 @@ delete Object.prototype.__proto__; windowIsClosing = true; // Push a macrotask to exit after a promise resolve. // This is not perfect, but should be fine for first pass. - Promise.resolve().then(() => - timers.setTimeout.call( - null, - () => { - // This should be fine, since only Window/MainWorker has .close() - os.exit(0); - }, - 0, - ) + PromiseResolve().then(() => + FunctionPrototypeCall(timers.setTimeout, null, () => { + // This should be fine, since only Window/MainWorker has .close() + os.exit(0); + }, 0) ); } } @@ -89,7 +99,7 @@ delete Object.prototype.__proto__; if ( webidl.type(transferOrOptions) === "Object" && transferOrOptions !== undefined && - transferOrOptions[Symbol.iterator] !== undefined + transferOrOptions[SymbolIterator] !== undefined ) { const transfer = webidl.converters["sequence<object>"]( transferOrOptions, @@ -112,7 +122,10 @@ delete Object.prototype.__proto__; async function pollForMessages() { if (!globalDispatchEvent) { - globalDispatchEvent = globalThis.dispatchEvent.bind(globalThis); + globalDispatchEvent = FunctionPrototypeBind( + globalThis.dispatchEvent, + globalThis, + ); } while (!isClosing) { const data = await core.opAsync("op_worker_recv_message"); @@ -249,14 +262,14 @@ delete Object.prototype.__proto__; webidl.illegalConstructor(); } - [Symbol.for("Deno.privateCustomInspect")](inspect) { + [SymbolFor("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({})}`; } } const navigator = webidl.createBranded(Navigator); - Object.defineProperties(Navigator.prototype, { + ObjectDefineProperties(Navigator.prototype, { gpu: { configurable: true, enumerable: true, @@ -272,14 +285,14 @@ delete Object.prototype.__proto__; webidl.illegalConstructor(); } - [Symbol.for("Deno.privateCustomInspect")](inspect) { + [SymbolFor("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({})}`; } } const workerNavigator = webidl.createBranded(WorkerNavigator); - Object.defineProperties(WorkerNavigator.prototype, { + ObjectDefineProperties(WorkerNavigator.prototype, { gpu: { configurable: true, enumerable: true, @@ -470,9 +483,9 @@ delete Object.prototype.__proto__; delete globalThis.bootstrap; util.log("bootstrapMainRuntime"); hasBootstrapped = true; - Object.defineProperties(globalThis, windowOrWorkerGlobalScope); - Object.defineProperties(globalThis, mainRuntimeGlobalProperties); - Object.setPrototypeOf(globalThis, Window.prototype); + ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope); + ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties); + ObjectSetPrototypeOf(globalThis, Window.prototype); const consoleFromDeno = globalThis.console; wrapConsole(consoleFromDeno, consoleFromV8); @@ -482,7 +495,7 @@ delete Object.prototype.__proto__; defineEventHandler(window, "load", null); defineEventHandler(window, "unload", null); - const isUnloadDispatched = Symbol.for("isUnloadDispatched"); + const isUnloadDispatched = SymbolFor("isUnloadDispatched"); // Stores the flag for checking whether unload is dispatched or not. // This prevents the recursive dispatches of unload events. // See https://github.com/denoland/deno/issues/9201. @@ -518,23 +531,23 @@ delete Object.prototype.__proto__; memoryUsage: core.memoryUsage, ...denoNs, }; - Object.defineProperties(finalDenoNs, { + ObjectDefineProperties(finalDenoNs, { pid: util.readOnly(pid), ppid: util.readOnly(ppid), noColor: util.readOnly(noColor), - args: util.readOnly(Object.freeze(args)), + args: util.readOnly(ObjectFreeze(args)), mainModule: util.getterOnly(opMainModule), }); if (unstableFlag) { - Object.assign(finalDenoNs, denoNsUnstable); + ObjectAssign(finalDenoNs, denoNsUnstable); } // Setup `Deno` global - we're actually overriding already existing global // `Deno` with `Deno` namespace from "./deno.ts". - Object.defineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); - Object.freeze(globalThis.Deno.core); - Object.freeze(globalThis.Deno.core.sharedQueue); + ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); + ObjectFreeze(globalThis.Deno.core); + ObjectFreeze(globalThis.Deno.core.sharedQueue); signals.setSignals(); util.log("args", args); @@ -558,9 +571,9 @@ delete Object.prototype.__proto__; delete globalThis.bootstrap; util.log("bootstrapWorkerRuntime"); hasBootstrapped = true; - Object.defineProperties(globalThis, windowOrWorkerGlobalScope); - Object.defineProperties(globalThis, workerRuntimeGlobalProperties); - Object.defineProperties(globalThis, { name: util.readOnly(name) }); + ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope); + ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties); + ObjectDefineProperties(globalThis, { name: util.readOnly(name) }); Object.setPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype); const consoleFromDeno = globalThis.console; @@ -592,19 +605,19 @@ delete Object.prototype.__proto__; }; if (useDenoNamespace) { if (unstableFlag) { - Object.assign(finalDenoNs, denoNsUnstable); + ObjectAssign(finalDenoNs, denoNsUnstable); } - Object.defineProperties(finalDenoNs, { + ObjectDefineProperties(finalDenoNs, { pid: util.readOnly(pid), noColor: util.readOnly(noColor), - args: util.readOnly(Object.freeze(args)), + args: util.readOnly(ObjectFreeze(args)), }); // Setup `Deno` global - we're actually overriding already // existing global `Deno` with `Deno` namespace from "./deno.ts". util.immutableDefine(globalThis, "Deno", finalDenoNs); - Object.freeze(globalThis.Deno); - Object.freeze(globalThis.Deno.core); - Object.freeze(globalThis.Deno.core.sharedQueue); + ObjectFreeze(globalThis.Deno); + ObjectFreeze(globalThis.Deno.core); + ObjectFreeze(globalThis.Deno.core.sharedQueue); signals.setSignals(); } else { delete globalThis.Deno; @@ -612,7 +625,7 @@ delete Object.prototype.__proto__; } } - Object.defineProperties(globalThis, { + ObjectDefineProperties(globalThis, { bootstrap: { value: { mainRuntime: bootstrapMainRuntime, |