mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
feat: Add Deno.mainModule (#6180)
This commit is contained in:
parent
a1b37f177b
commit
ca5b5ba530
8 changed files with 37 additions and 0 deletions
3
cli/js/lib.deno.unstable.d.ts
vendored
3
cli/js/lib.deno.unstable.d.ts
vendored
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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",
|
||||
|
|
3
cli/tests/main_module.ts
Normal file
3
cli/tests/main_module.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
console.log("main_module", Deno.mainModule);
|
||||
|
||||
import "./main_module2.ts";
|
2
cli/tests/main_module.ts.out
Normal file
2
cli/tests/main_module.ts.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
main_module2 [WILDCARD]tests/main_module.ts
|
||||
main_module [WILDCARD]tests/main_module.ts
|
1
cli/tests/main_module2.ts
Normal file
1
cli/tests/main_module2.ts
Normal file
|
@ -0,0 +1 @@
|
|||
console.log("main_module2", Deno.mainModule);
|
Loading…
Reference in a new issue