Skip to content

Commit

Permalink
feat: Add Deno.mainModule (#6180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed Jun 11, 2020
1 parent a1b37f1 commit ca5b5ba
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cli/js/lib.deno.unstable.d.ts
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions cli/js/ops/runtime.ts
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions cli/js/runtime_main.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions cli/ops/runtime.rs
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/main_module.ts
@@ -0,0 +1,3 @@
console.log("main_module", Deno.mainModule);

import "./main_module2.ts";
2 changes: 2 additions & 0 deletions cli/tests/main_module.ts.out
@@ -0,0 +1,2 @@
main_module2 [WILDCARD]tests/main_module.ts
main_module [WILDCARD]tests/main_module.ts
1 change: 1 addition & 0 deletions cli/tests/main_module2.ts
@@ -0,0 +1 @@
console.log("main_module2", Deno.mainModule);

0 comments on commit ca5b5ba

Please sign in to comment.