From bcb02fd994997df13fe4f41737590b39c467df8c Mon Sep 17 00:00:00 2001 From: Benjamin Fischer <61995275+c4spar@users.noreply.github.com> Date: Wed, 27 Apr 2022 22:34:13 +0200 Subject: [PATCH] refactor(runtime): make `$.mainModule`, `$.args` & `$.startTime` readonly (#32) --- src/cli/lib/bootstrap.ts | 48 +++++++++++++++++++++++++++++++--------- src/cli/lib/worker.ts | 2 -- src/cli/mod.ts | 3 +-- src/runtime/mod.ts | 13 +++++------ 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/cli/lib/bootstrap.ts b/src/cli/lib/bootstrap.ts index 22be5b8..8e622d3 100644 --- a/src/cli/lib/bootstrap.ts +++ b/src/cli/lib/bootstrap.ts @@ -1,8 +1,9 @@ import { $ } from "../../runtime/mod.ts"; +const startTime = Date.now(); + export interface BootstrapOptions { code?: string; - startTime?: number; mainModule?: string; args?: Array | string; base64?: boolean; @@ -14,19 +15,32 @@ export function base64Module(code: string) { } export function stringifyArgs(args: Array) { - return args?.length - ? `$.args = JSON.parse(decodeURIComponent("${ + if (!args?.length) { + return ""; + } + let code = args?.length + ? `const args = JSON.parse(decodeURIComponent("${ encodeURIComponent(JSON.stringify(args)) - }"));` - : ""; + }"));\n` + : "const args = [];\n"; + code += `Object.defineProperty($, "args", { get: () => args });`; + return code; +} + +export function stringifyMainModule(mainModule: string) { + return `Object.defineProperty($, "mainModule", { get: () => "${mainModule}" });`; +} + +export function stringifyStartTime(startTime: number) { + return `Object.defineProperty($, "startTime", { get: () => ${startTime} });`; } export function bootstrap(options: BootstrapOptions): string { const code = [ `import "${new URL("../../../mod.ts", import.meta.url)}";`, "{", - options.startTime ? `$.startTime = ${options.startTime};` : "", - options.mainModule ? `$.mainModule = "${options.mainModule}";` : "", + stringifyStartTime(startTime), + options.mainModule ? stringifyMainModule(options.mainModule) : "", options.verbose !== undefined ? `$.verbose = ${options.verbose};` : "", typeof options.args === "string" ? options.args @@ -78,14 +92,26 @@ export interface ImportModuleOptions { } export async function importModule(options: ImportModuleOptions) { - $.mainModule = options.mainModule; - if (options.args) { - $.args = options.args; - } + const mainModule = options.mainModule; + Object.defineProperty($, "mainModule", { + get: () => mainModule, + }); + + const args = options.args ? [...options.args] : []; + Object.defineProperty($, "args", { + get: () => args, + }); + if (typeof options.verbose !== "undefined") { $.verbose = options.verbose; } + + Object.defineProperty($, "startTime", { + get: (): number => startTime, + }); + await import($.mainModule); + if ($.verbose) { console.log($.bold("time: %ss"), Math.round($.time) / 1000); } diff --git a/src/cli/lib/worker.ts b/src/cli/lib/worker.ts index 35885b3..ff45785 100644 --- a/src/cli/lib/worker.ts +++ b/src/cli/lib/worker.ts @@ -17,7 +17,6 @@ export interface SpawnWorkerOptions extends Omit { export function spawnWorker({ args, - startTime, mainModule, perms, verbose, @@ -25,7 +24,6 @@ export function spawnWorker({ new Worker( bootstrapModule({ args, - startTime, mainModule, verbose, base64: true, diff --git a/src/cli/mod.ts b/src/cli/mod.ts index ef3612c..a082bdf 100755 --- a/src/cli/mod.ts +++ b/src/cli/mod.ts @@ -1,5 +1,5 @@ import { VERSION } from "../../version.ts"; -import { $, path } from "../runtime/mod.ts"; +import { path } from "../runtime/mod.ts"; import { bundleCommand } from "./bundle.ts"; import { compileCommand } from "./compile.ts"; import { @@ -107,7 +107,6 @@ export function dzx() { mainModule, args, verbose, - startTime: $.startTime, }); } else { await importModule({ mainModule, args, verbose }); diff --git a/src/runtime/mod.ts b/src/runtime/mod.ts index 9dbd862..91d047d 100644 --- a/src/runtime/mod.ts +++ b/src/runtime/mod.ts @@ -19,17 +19,17 @@ export { ProcessError } from "./process_error.ts"; export { ProcessOutput } from "./process_output.ts"; export type $ = typeof exec & typeof colors & { - shell: string; - prefix: string; - mainModule: string; + get mainModule(): string; + get args(): Array; get verbose(): number; set verbose(value: boolean | number); + get startTime(): number; + shell: string; + prefix: string; stdout: NonNullable; stderr: NonNullable; - args: Array; quote: typeof shq; throwErrors: boolean; - startTime: number; time: number; }; @@ -43,13 +43,10 @@ Object.setPrototypeOf($, Object.getPrototypeOf(colors)); $._stack = []; $.shell = "/bin/bash"; $.prefix = "set -euo pipefail;"; -$.mainModule = Deno.mainModule; $.stdout = "piped"; $.stderr = "piped"; -$.args = []; $.quote = shq; $.throwErrors = false; -$.startTime = Date.now(); let _verbose = 1; Object.defineProperty($, "verbose", {