diff options
author | John Gardner <gardnerjohng@gmail.com> | 2020-06-11 13:00:29 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-10 23:00:29 -0400 |
commit | ca5b5ba530eccd1a4ed34bc475250daae489190a (patch) | |
tree | daf31cb89b47943707ca0590174a4fd4750f006c | |
parent | a1b37f177be848ce3c3248b6b835f8999e36afff (diff) |
feat: Add Deno.mainModule (#6180)
-rw-r--r-- | cli/js/lib.deno.unstable.d.ts | 3 | ||||
-rw-r--r-- | cli/js/ops/runtime.ts | 4 | ||||
-rw-r--r-- | cli/js/runtime_main.ts | 2 | ||||
-rw-r--r-- | cli/ops/runtime.rs | 17 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 5 | ||||
-rw-r--r-- | cli/tests/main_module.ts | 3 | ||||
-rw-r--r-- | cli/tests/main_module.ts.out | 2 | ||||
-rw-r--r-- | cli/tests/main_module2.ts | 1 |
8 files changed, 37 insertions, 0 deletions
diff --git a/cli/js/lib.deno.unstable.d.ts b/cli/js/lib.deno.unstable.d.ts index aebd7f964..dc50416fc 100644 --- a/cli/js/lib.deno.unstable.d.ts +++ b/cli/js/lib.deno.unstable.d.ts @@ -1245,4 +1245,7 @@ declare namespace Deno { * Requires `allow-env` permission. */ export function hostname(): string; + + /** **UNSTABLE**: The URL of the file that was originally executed from the command-line. */ + export const mainModule: string; } diff --git a/cli/js/ops/runtime.ts b/cli/js/ops/runtime.ts index 632424327..70addf469 100644 --- a/cli/js/ops/runtime.ts +++ b/cli/js/ops/runtime.ts @@ -21,6 +21,10 @@ export function opStart(): Start { return sendSync("op_start"); } +export function opMainModule(): string { + return sendSync("op_main_module"); +} + export interface Metrics { opsDispatched: number; opsDispatchedSync: number; diff --git a/cli/js/runtime_main.ts b/cli/js/runtime_main.ts index 58405f673..25a6b0f93 100644 --- a/cli/js/runtime_main.ts +++ b/cli/js/runtime_main.ts @@ -9,6 +9,7 @@ import * as denoNs from "./deno.ts"; import * as denoUnstableNs from "./deno_unstable.ts"; +import { opMainModule } from "./ops/runtime.ts"; import { exit } from "./ops/os.ts"; import { readOnly, @@ -106,6 +107,7 @@ export function bootstrapMainRuntime(): void { if (unstableFlag) { Object.defineProperties(globalThis, unstableMethods); Object.defineProperties(globalThis, unstableProperties); + Object.defineProperty(denoNs, "mainModule", getterOnly(opMainModule)); Object.assign(denoNs, denoUnstableNs); } diff --git a/cli/ops/runtime.rs b/cli/ops/runtime.rs index a85ce4011..e47b40792 100644 --- a/cli/ops/runtime.rs +++ b/cli/ops/runtime.rs @@ -6,11 +6,13 @@ use crate::state::State; use crate::version; use crate::DenoSubcommand; use deno_core::CoreIsolate; +use deno_core::ModuleSpecifier; use deno_core::ZeroCopyBuf; use std::env; pub fn init(i: &mut CoreIsolate, s: &State) { i.register_op("op_start", s.stateful_json_op(op_start)); + i.register_op("op_main_module", s.stateful_json_op(op_main_module)); i.register_op("op_metrics", s.stateful_json_op(op_metrics)); } @@ -39,6 +41,21 @@ fn op_start( }))) } +fn op_main_module( + state: &State, + _args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result<JsonOp, OpError> { + let main = &state.borrow().main_module.to_string(); + let main_url = ModuleSpecifier::resolve_url_or_path(&main)?; + if main_url.as_url().scheme() == "file" { + let main_path = std::env::current_dir().unwrap().join(main_url.to_string()); + state.check_read_blind(&main_path, "main_module")?; + } + state.check_unstable("Deno.mainModule"); + Ok(JsonOp::Sync(json!(&main))) +} + fn op_metrics( state: &State, _args: Value, diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index e3f4bd085..9c59e2a74 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1708,6 +1708,11 @@ itest!(import_meta { output: "import_meta.ts.out", }); +itest!(main_module { + args: "run --quiet --unstable --allow-read --reload main_module.ts", + output: "main_module.ts.out", +}); + itest!(lib_ref { args: "run --quiet --unstable --reload lib_ref.ts", output: "lib_ref.ts.out", diff --git a/cli/tests/main_module.ts b/cli/tests/main_module.ts new file mode 100644 index 000000000..19988f4bf --- /dev/null +++ b/cli/tests/main_module.ts @@ -0,0 +1,3 @@ +console.log("main_module", Deno.mainModule); + +import "./main_module2.ts"; diff --git a/cli/tests/main_module.ts.out b/cli/tests/main_module.ts.out new file mode 100644 index 000000000..31adb20cb --- /dev/null +++ b/cli/tests/main_module.ts.out @@ -0,0 +1,2 @@ +main_module2 [WILDCARD]tests/main_module.ts +main_module [WILDCARD]tests/main_module.ts diff --git a/cli/tests/main_module2.ts b/cli/tests/main_module2.ts new file mode 100644 index 000000000..585615c7f --- /dev/null +++ b/cli/tests/main_module2.ts @@ -0,0 +1 @@ +console.log("main_module2", Deno.mainModule); |