-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure global type instances are available. #1175
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,75 @@ | ||
// Copyright 2018 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 environemnt that code | ||
// is evaluated in. We use this file to automatically build the runtime type | ||
// library. | ||
|
||
// Modules which will make up part of the global public API surface should be | ||
// imported as namespaces, so when the runtime tpye library is generated they | ||
// can be expressed as a namespace in the type library. | ||
import * as blob from "./blob"; | ||
import * as consoleTypes from "./console"; | ||
import * as domTypes from "./dom_types"; | ||
import * as file from "./file"; | ||
import * as formdata from "./form_data"; | ||
import * as console_ from "./console"; | ||
import * as fetch_ from "./fetch"; | ||
import { Headers } from "./headers"; | ||
import { globalEval } from "./global_eval"; | ||
import { libdeno } from "./libdeno"; | ||
import * as formData from "./form_data"; | ||
import * as fetchTypes from "./fetch"; | ||
import * as headers from "./headers"; | ||
import * as textEncoding from "./text_encoding"; | ||
import * as timers from "./timers"; | ||
import * as urlSearchParams from "./url_search_params"; | ||
import * as domTypes from "./dom_types"; | ||
|
||
// These imports are not exposed and therefore are fine to just import the | ||
// symbols required. | ||
import { globalEval } from "./global_eval"; | ||
import { libdeno } from "./libdeno"; | ||
|
||
// 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. | ||
|
||
// Deno, we only need to declare the enough to compile Deno. | ||
declare global { | ||
const console: console_.Console; | ||
const console: consoleTypes.Console; | ||
const setTimeout: typeof timers.setTimeout; | ||
// tslint:disable-next-line:variable-name | ||
const TextEncoder: typeof textEncoding.TextEncoder; | ||
} | ||
|
||
// A reference to the global object. | ||
export const window = globalEval("this"); | ||
// A self reference to the global object. | ||
window.window = window; | ||
|
||
window.setTimeout = timers.setTimeout; | ||
window.setInterval = timers.setInterval; | ||
window.clearTimeout = timers.clearTimer; | ||
window.clearInterval = timers.clearTimer; | ||
|
||
window.console = new console_.Console(libdeno.print); | ||
window.TextEncoder = textEncoding.TextEncoder; | ||
window.TextDecoder = textEncoding.TextDecoder; | ||
// Globally available functions and object instances. | ||
window.atob = textEncoding.atob; | ||
window.btoa = textEncoding.btoa; | ||
window.fetch = fetchTypes.fetch; | ||
window.clearTimeout = timers.clearTimer; | ||
window.clearInterval = timers.clearTimer; | ||
window.console = new consoleTypes.Console(libdeno.print); | ||
window.setTimeout = timers.setTimeout; | ||
window.setInterval = timers.setInterval; | ||
|
||
// 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 = blob.DenoBlob; | ||
window.File = file.DenoFile; | ||
export type File = file.DenoFile; | ||
window.URLSearchParams = urlSearchParams.URLSearchParams; | ||
export type URLSearchParams = urlSearchParams.URLSearchParams; | ||
|
||
window.fetch = fetch_.fetch; | ||
// 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; | ||
|
||
// using the `as` keyword to mask the internal types when generating the | ||
// runtime library | ||
window.Headers = Headers as domTypes.HeadersConstructor; | ||
window.Blob = blob.DenoBlob; | ||
window.File = file.DenoFile; | ||
window.FormData = formdata.FormData as domTypes.FormDataConstructor; | ||
// While these are classes, they have their global instance types created in | ||
// other type definitions, therefore we do not have to include them here. | ||
window.TextEncoder = textEncoding.TextEncoder; | ||
window.TextDecoder = textEncoding.TextDecoder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doing that won't cause window to miss all properties assigned to it in
globals.ts
? I think we would like to use most of them in REPLThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid question ... However, I'm going to land this anyway because I need the fix and because this is passing the tests. If this indeed breaks
window
properties, we should add tests that nail that behavior down.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't just rebased it against my REPL branch and everything works as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At compile/bundle time this ends up being an
any
, even when you doimport { window } from "./globals.ts";
(because that reference is the sameglobalEval("this");
. At runtime we build the type library which describes what the global scope is, and the ability to access theglobalEval
module isn't possible. I had previously thought about tightening compile time typings ofglobalEval("this");
which could be done, but it would be difficult and not really provide any safety, as most of what we do with it is cast it toany
anyways.