diff options
Diffstat (limited to 'cli/js/globals.ts')
-rw-r--r-- | cli/js/globals.ts | 97 |
1 files changed, 37 insertions, 60 deletions
diff --git a/cli/js/globals.ts b/cli/js/globals.ts index cb1b17678..e6c1e855c 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -1,15 +1,8 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -// This is a "special" module, in that it define the global runtime scope of -// Deno, and therefore it defines a lot of the runtime environment that code -// is evaluated in. - -// By convention we import those items that are globally exposed as namespaces import * as blob from "./blob.ts"; import * as consoleTypes from "./console.ts"; -import * as csprng from "./get_random_values.ts"; import * as customEvent from "./custom_event.ts"; -import * as Deno from "./deno.ts"; import * as domTypes from "./dom_types.ts"; import * as domFile from "./dom_file.ts"; import * as event from "./event.ts"; @@ -21,7 +14,6 @@ import * as textEncoding from "./text_encoding.ts"; import * as timers from "./timers.ts"; import * as url from "./url.ts"; import * as urlSearchParams from "./url_search_params.ts"; -import * as workerRuntime from "./worker_main.ts"; import * as workers from "./workers.ts"; import * as performanceUtil from "./performance.ts"; import * as request from "./request.ts"; @@ -29,7 +21,6 @@ import * as request from "./request.ts"; // These imports are not exposed and therefore are fine to just import the // symbols required. import { core } from "./core.ts"; -import { internalObject } from "./internals.ts"; // This global augmentation is just enough types to be able to build Deno, // the runtime types are fully defined in `lib.deno_runtime.d.ts`. @@ -111,14 +102,23 @@ declare global { callback: (event: domTypes.Event) => void | null, options?: boolean | domTypes.AddEventListenerOptions | undefined ) => void; - var bootstrapTsCompiler: (() => void) | undefined; + var queueMicrotask: (callback: () => void) => void; var console: consoleTypes.Console; + var location: domTypes.Location; + + // Assigned to `window` global - main runtime var Deno: { core: DenoCore; }; - var bootstrapCompilerRuntime: ((compilerType: string) => void) | undefined; + var onload: ((e: domTypes.Event) => void) | undefined; + var onunload: ((e: domTypes.Event) => void) | undefined; var bootstrapMainRuntime: (() => void) | undefined; - var location: domTypes.Location; + + // Assigned to `self` global - worker runtime and compiler + var bootstrapWorkerRuntime: + | ((name: string) => Promise<void> | void) + | undefined; + var runWorkerMessageLoop: (() => Promise<void> | void) | undefined; var onerror: | (( msg: string, @@ -128,22 +128,20 @@ declare global { e: domTypes.Event ) => boolean | void) | undefined; - var onload: ((e: domTypes.Event) => void) | undefined; + // eslint-disable-next-line @typescript-eslint/no-explicit-any var onmessage: ((e: { data: any }) => Promise<void> | void) | undefined; - var onunload: ((e: domTypes.Event) => void) | undefined; - var queueMicrotask: (callback: () => void) => void; - var bootstrapWasmCompiler: (() => void) | undefined; - var bootstrapWorkerRuntime: (() => Promise<void> | void) | undefined; + // Called in compiler + var workerClose: () => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + var postMessage: (msg: any) => void; + // Assigned to `self` global - compiler + var bootstrapTsCompilerRuntime: (() => void) | undefined; + var bootstrapWasmCompilerRuntime: (() => void) | undefined; /* eslint-enable */ } -// Add internal object to Deno object. -// This is not exposed as part of the Deno types. -// @ts-ignore -Deno[Deno.symbols.internal] = internalObject; - -function writable(value: unknown): PropertyDescriptor { +export function writable(value: unknown): PropertyDescriptor { return { value, writable: true, @@ -152,7 +150,7 @@ function writable(value: unknown): PropertyDescriptor { }; } -function nonEnumerable(value: unknown): PropertyDescriptor { +export function nonEnumerable(value: unknown): PropertyDescriptor { return { value, writable: true, @@ -160,27 +158,28 @@ function nonEnumerable(value: unknown): PropertyDescriptor { }; } -function readOnly(value: unknown): PropertyDescriptor { +export function readOnly(value: unknown): PropertyDescriptor { return { value, enumerable: true }; } -const globalProperties = { - window: readOnly(globalThis), - Deno: readOnly(Deno), +// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope +export const windowOrWorkerGlobalScopeMethods = { atob: writable(textEncoding.atob), btoa: writable(textEncoding.btoa), - fetch: writable(fetchTypes.fetch), - clearTimeout: writable(timers.clearTimeout), clearInterval: writable(timers.clearInterval), - console: writable(new consoleTypes.Console(core.print)), - setTimeout: writable(timers.setTimeout), + clearTimeout: writable(timers.clearTimeout), + fetch: writable(fetchTypes.fetch), + // queueMicrotask is bound in Rust setInterval: writable(timers.setInterval), - onload: writable(undefined), - onunload: writable(undefined), - crypto: readOnly(csprng), + setTimeout: writable(timers.setTimeout) +}; + +// Other properties shared between WindowScope and WorkerGlobalScope +export const windowOrWorkerGlobalScopeProperties = { + console: writable(new consoleTypes.Console(core.print)), Blob: nonEnumerable(blob.DenoBlob), File: nonEnumerable(domFile.DomFileImpl), CustomEvent: nonEnumerable(customEvent.CustomEvent), @@ -195,15 +194,10 @@ const globalProperties = { Request: nonEnumerable(request.Request), Response: nonEnumerable(fetchTypes.Response), performance: writable(new performanceUtil.Performance()), + Worker: nonEnumerable(workers.WorkerImpl) +}; - onmessage: writable(workerRuntime.onmessage), - onerror: writable(workerRuntime.onerror), - - bootstrapWorkerRuntime: nonEnumerable(workerRuntime.bootstrapWorkerRuntime), - workerClose: nonEnumerable(workerRuntime.workerClose), - postMessage: writable(workerRuntime.postMessage), - Worker: nonEnumerable(workers.WorkerImpl), - +export const eventTargetProperties = { [domTypes.eventTargetHost]: nonEnumerable(null), [domTypes.eventTargetListeners]: nonEnumerable({}), [domTypes.eventTargetMode]: nonEnumerable(""), @@ -219,20 +213,3 @@ const globalProperties = { eventTarget.EventTarget.prototype.removeEventListener ) }; - -Object.defineProperties(globalThis, globalProperties); - -// Registers the handler for window.onload function. -globalThis.addEventListener("load", (e: domTypes.Event): void => { - const { onload } = globalThis; - if (typeof onload === "function") { - onload(e); - } -}); -// Registers the handler for window.onunload function. -globalThis.addEventListener("unload", (e: domTypes.Event): void => { - const { onunload } = globalThis; - if (typeof onunload === "function") { - onunload(e); - } -}); |