diff options
author | Francesco Borzì <borzifrancesco@gmail.com> | 2018-08-25 21:42:49 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-08-25 15:42:49 -0400 |
commit | 84c38f34eeb2a6f9f6786aba0f5da5eb9efa422b (patch) | |
tree | 5fb5ebec425e2736d08f6043405931109da0f7b9 /js | |
parent | 3bcf7e271f775002ba9c010ad79e321a3888187d (diff) |
Prevent circular imports in ts code (#576)
Diffstat (limited to 'js')
-rw-r--r-- | js/assets.ts | 2 | ||||
-rw-r--r-- | js/compiler.ts | 4 | ||||
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/fetch.ts | 2 | ||||
-rw-r--r-- | js/global-eval.ts | 6 | ||||
-rw-r--r-- | js/globals.ts | 22 | ||||
-rw-r--r-- | js/libdeno.ts | 18 | ||||
-rw-r--r-- | js/main.ts | 2 | ||||
-rw-r--r-- | js/os.ts | 2 | ||||
-rw-r--r-- | js/timers.ts | 2 |
10 files changed, 37 insertions, 25 deletions
diff --git a/js/assets.ts b/js/assets.ts index 3b5691bc7..89b3e2645 100644 --- a/js/assets.ts +++ b/js/assets.ts @@ -10,6 +10,7 @@ import compilerDts from "gen/js/compiler.d.ts!string"; import consoleDts from "gen/js/console.d.ts!string"; import denoDts from "gen/js/deno.d.ts!string"; +import libdenoDts from "gen/js/libdeno.d.ts!string"; import globalsDts from "gen/js/globals.d.ts!string"; import osDts from "gen/js/os.d.ts!string"; import fetchDts from "gen/js/fetch.d.ts!string"; @@ -59,6 +60,7 @@ export const assetSourceCode: { [key: string]: string } = { "compiler.d.ts": compilerDts, "console.d.ts": consoleDts, "deno.d.ts": denoDts, + "libdeno.d.ts": libdenoDts, "globals.d.ts": globalsDts, "os.d.ts": osDts, "fetch.d.ts": fetchDts, diff --git a/js/compiler.ts b/js/compiler.ts index 93cfd34c1..46f8c8121 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -2,7 +2,9 @@ import * as ts from "typescript"; import { assetSourceCode } from "./assets"; import * as deno from "./deno"; -import { libdeno, window, globalEval } from "./globals"; +import { globalEval } from "./global-eval"; +import { libdeno } from "./libdeno"; +import { window } from "./globals"; import * as os from "./os"; import { RawSourceMap } from "./types"; import { assert, log, notImplemented } from "./util"; diff --git a/js/deno.ts b/js/deno.ts index ff9939b1b..f7f6c521e 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,4 +1,4 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. // Public deno module. export { exit, readFileSync, writeFileSync } from "./os"; -export { libdeno } from "./globals"; +export { libdeno } from "./libdeno"; diff --git a/js/fetch.ts b/js/fetch.ts index e6981df37..1c733e066 100644 --- a/js/fetch.ts +++ b/js/fetch.ts @@ -8,7 +8,7 @@ import { notImplemented } from "./util"; import { flatbuffers } from "flatbuffers"; -import { libdeno } from "./globals"; +import { libdeno } from "./libdeno"; import { deno as fbs } from "gen/msg_generated"; import { Headers, diff --git a/js/global-eval.ts b/js/global-eval.ts new file mode 100644 index 000000000..ee37e6f24 --- /dev/null +++ b/js/global-eval.ts @@ -0,0 +1,6 @@ +// 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. +export const globalEval = eval; diff --git a/js/globals.ts b/js/globals.ts index beecbf58d..80e00ea15 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -2,10 +2,11 @@ import { Console } from "./console"; import { exit } from "./os"; -import { RawSourceMap } from "./types"; import * as timers from "./timers"; -import { TextEncoder, TextDecoder } from "./text_encoding"; +import { TextDecoder, TextEncoder } from "./text_encoding"; import * as fetch_ from "./fetch"; +import { libdeno } from "./libdeno"; +import { globalEval } from "./global-eval"; declare global { interface Window { @@ -36,27 +37,10 @@ declare global { // tslint:enable:variable-name } -// 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. -export const globalEval = eval; - // A reference to the global object. export const window = globalEval("this"); window.window = window; -// The libdeno functions are moved so that users can't access them. -type MessageCallback = (msg: Uint8Array) => void; -interface Libdeno { - recv(cb: MessageCallback): void; - send(msg: ArrayBufferView): null | Uint8Array; - print(x: string): void; - mainSource: string; - mainSourceMap: RawSourceMap; -} -export const libdeno = window.libdeno as Libdeno; window.libdeno = null; // import "./url"; diff --git a/js/libdeno.ts b/js/libdeno.ts new file mode 100644 index 000000000..83dc8efa0 --- /dev/null +++ b/js/libdeno.ts @@ -0,0 +1,18 @@ +import { RawSourceMap } from "./types"; +import { globalEval } from "./global-eval"; + +// The libdeno functions are moved so that users can't access them. +type MessageCallback = (msg: Uint8Array) => void; +interface Libdeno { + recv(cb: MessageCallback): void; + + send(msg: ArrayBufferView): null | Uint8Array; + + print(x: string): void; + + mainSource: string; + mainSourceMap: RawSourceMap; +} + +const window = globalEval("this"); +export const libdeno = window.libdeno as Libdeno; diff --git a/js/main.ts b/js/main.ts index 740003049..eb90abb0e 100644 --- a/js/main.ts +++ b/js/main.ts @@ -4,7 +4,7 @@ import { deno as fbs } from "gen/msg_generated"; import { assert, assignCmdId, log, setLogDebug } from "./util"; import * as os from "./os"; import { DenoCompiler } from "./compiler"; -import { libdeno } from "./globals"; +import { libdeno } from "./libdeno"; import * as timers from "./timers"; import { onFetchRes } from "./fetch"; @@ -5,7 +5,7 @@ import { assert } from "./util"; import * as util from "./util"; import { maybeThrowError } from "./errors"; import { flatbuffers } from "flatbuffers"; -import { libdeno } from "./globals"; +import { libdeno } from "./libdeno"; export function exit(exitCode = 0): never { const builder = new flatbuffers.Builder(); diff --git a/js/timers.ts b/js/timers.ts index 3791e079d..43bd199b1 100644 --- a/js/timers.ts +++ b/js/timers.ts @@ -3,7 +3,7 @@ import { assert } from "./util"; import * as util from "./util"; import { deno as fbs } from "gen/msg_generated"; import { flatbuffers } from "flatbuffers"; -import { libdeno } from "./globals"; +import { libdeno } from "./libdeno"; let nextTimerId = 1; |