From c4e37285758ff4cc5d388db2d880ed91decb3d29 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 20 Feb 2019 11:42:19 +0900 Subject: [PATCH] remove global_eval.ts (#1813) --- BUILD.gn | 2 +- js/compiler.ts | 5 +---- js/global_eval.ts | 11 ----------- js/globals.ts | 4 +--- js/libdeno.ts | 3 +-- js/location.ts | 2 +- js/mixins/dom_iterable.ts | 6 +----- js/repl.ts | 4 +--- js/window.ts | 10 ++++++++++ js/workers.ts | 6 +----- 10 files changed, 18 insertions(+), 35 deletions(-) delete mode 100644 js/global_eval.ts create mode 100644 js/window.ts diff --git a/BUILD.gn b/BUILD.gn index b1c455a5e2..cda8d1ffbb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -74,7 +74,6 @@ ts_sources = [ "js/files.ts", "js/flatbuffers.ts", "js/form_data.ts", - "js/global_eval.ts", "js/globals.ts", "js/headers.ts", "js/io.ts", @@ -107,6 +106,7 @@ ts_sources = [ "js/url.ts", "js/url_search_params.ts", "js/util.ts", + "js/window.ts", "js/workers.ts", "js/write_file.ts", "js/performance.ts", diff --git a/js/compiler.ts b/js/compiler.ts index 27b97dd2db..27890babdc 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -1,9 +1,9 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import * as ts from "typescript"; import * as msg from "gen/msg_generated"; +import { window } from "./window"; import { assetSourceCode } from "./assets"; import { Console } from "./console"; -import { globalEval } from "./global_eval"; import { libdeno } from "./libdeno"; import * as os from "./os"; import { TextDecoder, TextEncoder } from "./text_encoding"; @@ -15,9 +15,6 @@ const EOL = "\n"; const ASSETS = "$asset$"; const LIB_RUNTIME = `${ASSETS}/lib.deno_runtime.d.ts`; -// A reference to the global scope -const window = globalEval("this"); - // An instance of console const console = new Console(libdeno.print); diff --git a/js/global_eval.ts b/js/global_eval.ts deleted file mode 100644 index b447c99544..0000000000 --- a/js/global_eval.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -/** If you use the eval function indirectly, by invoking it via a reference - * other than eval, as of ECMAScript 5 it works in the global scope rather than - * the local scope. This means, for instance, that function declarations create - * global functions, and that the code being evaluated doesn't have access to - * local variables within the scope where it's being called. - * - * @internal - */ -export const globalEval = eval; diff --git a/js/globals.ts b/js/globals.ts index 935890be4e..9bd0584b2e 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -7,6 +7,7 @@ // 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 { window } from "./window"; import * as blob from "./blob"; import * as consoleTypes from "./console"; import * as customEvent from "./custom_event"; @@ -26,7 +27,6 @@ import * as performanceUtil from "./performance"; // 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 @@ -37,8 +37,6 @@ declare global { const setTimeout: typeof timers.setTimeout; } -// A reference to the global object. -export const window = globalEval("this"); // A self reference to the global object. window.window = window; diff --git a/js/libdeno.ts b/js/libdeno.ts index 79ce4272ac..61e259b54a 100644 --- a/js/libdeno.ts +++ b/js/libdeno.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { globalEval } from "./global_eval"; +import { window } from "./window"; // The libdeno functions are moved so that users can't access them. type MessageCallback = (msg: Uint8Array) => void; @@ -40,5 +40,4 @@ interface Libdeno { errorToJSON: (e: Error) => string; } -const window = globalEval("this"); export const libdeno = window.libdeno as Libdeno; diff --git a/js/location.ts b/js/location.ts index 5e7cb07b45..5d7f44707e 100644 --- a/js/location.ts +++ b/js/location.ts @@ -2,7 +2,7 @@ import { URL } from "./url"; import { notImplemented } from "./util"; import { Location } from "./dom_types"; -import { window } from "./globals"; +import { window } from "./window"; export function setLocation(url: string): void { window.location = new LocationImpl(url); diff --git a/js/mixins/dom_iterable.ts b/js/mixins/dom_iterable.ts index e2fcd2dbd8..ae5a030ce1 100644 --- a/js/mixins/dom_iterable.ts +++ b/js/mixins/dom_iterable.ts @@ -1,12 +1,8 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { DomIterable } from "../dom_types"; -import { globalEval } from "../global_eval"; +import { window } from "../window"; import { requiredArguments } from "../util"; -// if we import it directly from "globals" it will break the unit tests so we -// have to grab a reference to the global scope a different way -const window = globalEval("this"); - // tslint:disable:no-any type Constructor = new (...args: any[]) => T; diff --git a/js/repl.ts b/js/repl.ts index f7ddb2bdf0..2c050bf5bc 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -5,12 +5,10 @@ import { assert } from "./util"; import { close } from "./files"; import * as dispatch from "./dispatch"; import { exit } from "./os"; -import { globalEval } from "./global_eval"; +import { window } from "./window"; import { libdeno } from "./libdeno"; import { formatError } from "./format_error"; -const window = globalEval("this"); - const helpMsg = [ "exit Exit the REPL", "help Print this help message" diff --git a/js/window.ts b/js/window.ts new file mode 100644 index 0000000000..6773ea5b24 --- /dev/null +++ b/js/window.ts @@ -0,0 +1,10 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +// (0, eval) is indirect eval. +// See the links below for details: +// - https://stackoverflow.com/a/14120023 +// - https://tc39.github.io/ecma262/#sec-performeval (spec) +export const window = (0, eval)("this"); +// TODO: The above should be replaced with globalThis +// when the globalThis proposal goes to stage 4 +// See https://github.com/tc39/proposal-global diff --git a/js/workers.ts b/js/workers.ts index fd82e23040..1716f8ef62 100644 --- a/js/workers.ts +++ b/js/workers.ts @@ -3,7 +3,7 @@ import * as dispatch from "./dispatch"; import * as msg from "gen/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert, log } from "./util"; -import { globalEval } from "./global_eval"; +import { window } from "./window"; export async function postMessage(data: Uint8Array): Promise { const builder = flatbuffers.createBuilder(); @@ -53,10 +53,6 @@ export function workerClose(): void { export async function workerMain() { log("workerMain"); - // TODO avoid using globalEval to get Window. But circular imports if getting - // it from globals.ts - const window = globalEval("this"); - while (!isClosing) { const data = await getMessage(); if (data == null) {