summaryrefslogtreecommitdiff
path: root/cli/js/globals.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js/globals.ts')
-rw-r--r--cli/js/globals.ts297
1 files changed, 162 insertions, 135 deletions
diff --git a/cli/js/globals.ts b/cli/js/globals.ts
index f090afcd4..5754002c0 100644
--- a/cli/js/globals.ts
+++ b/cli/js/globals.ts
@@ -1,13 +1,10 @@
// 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. We use this file to automatically build the runtime type
-// library.
+// is evaluated in.
-// Modules which will make up part of the global public API surface should be
-// imported as namespaces, so when the runtime type library is generated they
-// can be expressed as a namespace in the type library.
-import { window } from "./window.ts";
+// 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";
@@ -31,12 +28,10 @@ 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";
-// During the build process, augmentations to the variable `window` in this
-// file are tracked and created as part of default library that is built into
-// Deno, we only need to declare the enough to compile Deno.
+// 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`.
declare global {
interface CallSite {
getThis(): unknown;
@@ -65,145 +60,177 @@ declare global {
[consoleTypes.customInspect]?(): string;
}
- const console: consoleTypes.Console;
-}
+ interface EvalErrorInfo {
+ isNativeError: boolean;
+ isCompileError: boolean;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ thrown: any;
+ }
-// A self reference to the global object.
-window.window = window;
+ interface DenoCore {
+ print(s: string, isErr?: boolean): void;
+ dispatch(
+ opId: number,
+ control: Uint8Array,
+ zeroCopy?: ArrayBufferView | null
+ ): Uint8Array | null;
+ setAsyncHandler(opId: number, cb: (msg: Uint8Array) => void): void;
+ sharedQueue: {
+ head(): number;
+ numRecords(): number;
+ size(): number;
+ push(buf: Uint8Array): boolean;
+ reset(): void;
+ shift(): Uint8Array | null;
+ };
+
+ ops(): Record<string, number>;
+
+ recv(cb: (opId: number, msg: Uint8Array) => void): void;
+
+ send(
+ opId: number,
+ control: null | ArrayBufferView,
+ data?: ArrayBufferView
+ ): null | Uint8Array;
+
+ shared: SharedArrayBuffer;
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ evalContext(code: string): [any, EvalErrorInfo | null];
+
+ errorToJSON: (e: Error) => string;
+ }
+
+ // Only `var` variables show up in the `globalThis` type when doing a global
+ // scope augmentation.
+ /* eslint-disable no-var */
+ var addEventListener: (
+ type: string,
+ callback: (event: domTypes.Event) => void | null,
+ options?: boolean | domTypes.AddEventListenerOptions | undefined
+ ) => void;
+ var compilerMain: (() => void) | undefined;
+ var console: consoleTypes.Console;
+ var Deno: {
+ core: DenoCore;
+ };
+ var denoMain: (() => void) | undefined;
+ var location: domTypes.Location;
+ var onerror:
+ | ((
+ msg: string,
+ source: string,
+ lineno: number,
+ colno: number,
+ 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 wasmCompilerMain: (() => void) | undefined;
+ var workerMain: (() => Promise<void> | 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;
-// This is the Deno namespace, it is handled differently from other window
-// properties when building the runtime type library, as the whole module
-// is flattened into a single namespace.
-window.Deno = Deno;
-
-// Globally available functions and object instances.
-window.atob = textEncoding.atob;
-window.btoa = textEncoding.btoa;
-window.fetch = fetchTypes.fetch;
-window.clearTimeout = timers.clearTimeout;
-window.clearInterval = timers.clearInterval;
-window.console = new consoleTypes.Console(core.print);
-window.setTimeout = timers.setTimeout;
-window.setInterval = timers.setInterval;
-window.location = (undefined as unknown) as domTypes.Location;
-window.onload = undefined as undefined | Function;
-window.onunload = undefined as undefined | Function;
-// The following Crypto interface implementation is not up to par with the
-// standard https://www.w3.org/TR/WebCryptoAPI/#crypto-interface as it does not
-// yet incorporate the SubtleCrypto interface as its "subtle" property.
-window.crypto = (csprng as unknown) as Crypto;
-// window.queueMicrotask added by hand to self-maintained lib.deno_runtime.d.ts
-
-// When creating the runtime type library, we use modifications to `window` to
-// determine what is in the global namespace. When we put a class in the
-// namespace, we also need its global instance type as well, otherwise users
-// won't be able to refer to instances.
-// We have to export the type aliases, so that TypeScript _knows_ they are
-// being used, which it cannot statically determine within this module.
-window.Blob = blob.DenoBlob;
-export type Blob = domTypes.Blob;
-
-export type Body = domTypes.Body;
-
-window.File = domFile.DomFileImpl as domTypes.DomFileConstructor;
-export type File = domTypes.DomFile;
-
-export type CustomEventInit = domTypes.CustomEventInit;
-window.CustomEvent = customEvent.CustomEvent;
-export type CustomEvent = domTypes.CustomEvent;
-export type EventInit = domTypes.EventInit;
-window.Event = event.Event;
-export type Event = domTypes.Event;
-export type EventListener = domTypes.EventListener;
-window.EventTarget = eventTarget.EventTarget;
-export type EventTarget = domTypes.EventTarget;
-window.URL = url.URL;
-export type URL = url.URL;
-window.URLSearchParams = urlSearchParams.URLSearchParams;
-export type URLSearchParams = domTypes.URLSearchParams;
-
-// Using the `as` keyword to use standard compliant interfaces as the Deno
-// implementations contain some implementation details we wouldn't want to
-// expose in the runtime type library.
-window.Headers = headers.Headers as domTypes.HeadersConstructor;
-export type Headers = domTypes.Headers;
-window.FormData = formData.FormData as domTypes.FormDataConstructor;
-export type FormData = domTypes.FormData;
-
-window.TextEncoder = textEncoding.TextEncoder;
-export type TextEncoder = textEncoding.TextEncoder;
-window.TextDecoder = textEncoding.TextDecoder;
-export type TextDecoder = textEncoding.TextDecoder;
-
-window.Request = request.Request as domTypes.RequestConstructor;
-export type Request = domTypes.Request;
-
-window.Response = fetchTypes.Response;
-export type Response = domTypes.Response;
-
-window.performance = new performanceUtil.Performance();
-
-// This variable functioning correctly depends on `declareAsLet`
-// in //tools/ts_library_builder/main.ts
-window.onmessage = workers.onmessage;
-window.onerror = workers.onerror;
-
-window.workerMain = workers.workerMain;
-window.workerClose = workers.workerClose;
-window.postMessage = workers.postMessage;
-
-window.Worker = workers.WorkerImpl;
-export type Worker = workers.Worker;
-
-window[domTypes.eventTargetHost] = null;
-window[domTypes.eventTargetListeners] = {};
-window[domTypes.eventTargetMode] = "";
-window[domTypes.eventTargetNodeType] = 0;
-window[eventTarget.eventTargetAssignedSlot] = false;
-window[eventTarget.eventTargetHasActivationBehavior] = false;
-window.addEventListener = eventTarget.EventTarget.prototype.addEventListener;
-window.dispatchEvent = eventTarget.EventTarget.prototype.dispatchEvent;
-window.removeEventListener =
- eventTarget.EventTarget.prototype.removeEventListener;
+
+function writable(value: unknown): PropertyDescriptor {
+ return {
+ value,
+ writable: true,
+ enumerable: true,
+ configurable: true
+ };
+}
+
+function nonEnumerable(value: unknown): PropertyDescriptor {
+ return {
+ value,
+ writable: true,
+ configurable: true
+ };
+}
+
+function readOnly(value: unknown): PropertyDescriptor {
+ return {
+ value,
+ enumerable: true
+ };
+}
+
+const globalProperties = {
+ window: readOnly(globalThis),
+ Deno: readOnly(Deno),
+ 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),
+ setInterval: writable(timers.setInterval),
+ onload: writable(undefined),
+ onunload: writable(undefined),
+ crypto: readOnly(csprng),
+ Blob: nonEnumerable(blob.DenoBlob),
+ File: nonEnumerable(domFile.DomFileImpl),
+ CustomEvent: nonEnumerable(customEvent.CustomEvent),
+ Event: nonEnumerable(event.Event),
+ EventTarget: nonEnumerable(eventTarget.EventTarget),
+ URL: nonEnumerable(url.URL),
+ URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParams),
+ Headers: nonEnumerable(headers.Headers),
+ FormData: nonEnumerable(formData.FormData),
+ TextEncoder: nonEnumerable(textEncoding.TextEncoder),
+ TextDecoder: nonEnumerable(textEncoding.TextDecoder),
+ Request: nonEnumerable(request.Request),
+ Response: nonEnumerable(fetchTypes.Response),
+ performance: writable(new performanceUtil.Performance()),
+
+ onmessage: writable(workers.onmessage),
+ onerror: writable(workers.onerror),
+
+ workerMain: nonEnumerable(workers.workerMain),
+ workerClose: nonEnumerable(workers.workerClose),
+ postMessage: writable(workers.postMessage),
+ Worker: nonEnumerable(workers.WorkerImpl),
+
+ [domTypes.eventTargetHost]: nonEnumerable(null),
+ [domTypes.eventTargetListeners]: nonEnumerable({}),
+ [domTypes.eventTargetMode]: nonEnumerable(""),
+ [domTypes.eventTargetNodeType]: nonEnumerable(0),
+ [eventTarget.eventTargetAssignedSlot]: nonEnumerable(false),
+ [eventTarget.eventTargetHasActivationBehavior]: nonEnumerable(false),
+
+ addEventListener: readOnly(
+ eventTarget.EventTarget.prototype.addEventListener
+ ),
+ dispatchEvent: readOnly(eventTarget.EventTarget.prototype.dispatchEvent),
+ removeEventListener: readOnly(
+ eventTarget.EventTarget.prototype.removeEventListener
+ )
+};
+
+Object.defineProperties(globalThis, globalProperties);
// Registers the handler for window.onload function.
-window.addEventListener("load", (e: domTypes.Event): void => {
- const onload = window.onload;
+globalThis.addEventListener("load", (e: domTypes.Event): void => {
+ const { onload } = globalThis;
if (typeof onload === "function") {
onload(e);
}
});
// Registers the handler for window.onunload function.
-window.addEventListener("unload", (e: domTypes.Event): void => {
- const onunload = window.onunload;
+globalThis.addEventListener("unload", (e: domTypes.Event): void => {
+ const { onunload } = globalThis;
if (typeof onunload === "function") {
onunload(e);
}
});
-
-// below are interfaces that are available in TypeScript but
-// have different signatures
-export interface ImportMeta {
- url: string;
- main: boolean;
-}
-
-export interface Crypto {
- readonly subtle: null;
- getRandomValues: <
- T extends
- | Int8Array
- | Uint8Array
- | Uint8ClampedArray
- | Int16Array
- | Uint16Array
- | Int32Array
- | Uint32Array
- >(
- typedArray: T
- ) => T;
-}