2020-01-27 21:12:25 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-08-28 11:08:24 -04:00
|
|
|
use super::dispatch_json::Value;
|
2020-01-27 21:12:25 -05:00
|
|
|
use crate::colors;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-27 21:12:25 -05:00
|
|
|
use crate::version;
|
2020-02-04 14:24:33 -05:00
|
|
|
use crate::DenoSubcommand;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-06-10 23:00:29 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::ResourceTable;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-01-27 21:12:25 -05:00
|
|
|
use std::env;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &Rc<State>) {
|
2020-08-28 11:08:24 -04:00
|
|
|
let t = &CoreIsolate::state(i).borrow().resource_table.clone();
|
|
|
|
|
|
|
|
i.register_op("op_start", s.stateful_json_op_sync(t, op_start));
|
|
|
|
i.register_op("op_main_module", s.stateful_json_op_sync(t, op_main_module));
|
|
|
|
i.register_op("op_metrics", s.stateful_json_op_sync(t, op_metrics));
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_start(
|
2020-08-28 11:08:24 -04:00
|
|
|
state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2020-01-27 21:12:25 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-01-27 21:12:25 -05:00
|
|
|
let gs = &state.global_state;
|
2020-02-04 14:24:33 -05:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-02-26 16:11:52 -05:00
|
|
|
// TODO(bartlomieju): `cwd` field is not used in JS, remove?
|
2020-02-04 14:24:33 -05:00
|
|
|
"args": gs.flags.argv.clone(),
|
2020-04-30 11:23:40 -04:00
|
|
|
"cwd": &env::current_dir().unwrap(),
|
2020-01-27 21:12:25 -05:00
|
|
|
"debugFlag": gs.flags.log_level.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
"denoVersion": version::DENO,
|
|
|
|
"noColor": !colors::use_color(),
|
2020-04-30 11:23:40 -04:00
|
|
|
"pid": std::process::id(),
|
2020-07-08 10:35:45 -04:00
|
|
|
"ppid": ppid(),
|
2020-04-30 11:23:40 -04:00
|
|
|
"repl": gs.flags.subcommand == DenoSubcommand::Repl,
|
2020-04-28 12:35:23 -04:00
|
|
|
"target": env!("TARGET"),
|
2020-04-30 11:23:40 -04:00
|
|
|
"tsVersion": version::TYPESCRIPT,
|
|
|
|
"unstableFlag": gs.flags.unstable,
|
|
|
|
"v8Version": version::v8(),
|
|
|
|
"versionFlag": gs.flags.version,
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
|
2020-06-10 23:00:29 -04:00
|
|
|
fn op_main_module(
|
2020-08-28 11:08:24 -04:00
|
|
|
state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2020-06-10 23:00:29 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
let main = &state.main_module.to_string();
|
2020-06-10 23:00:29 -04:00
|
|
|
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")?;
|
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(&main))
|
2020-06-10 23:00:29 -04:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
fn op_metrics(
|
2020-08-28 11:08:24 -04:00
|
|
|
state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2020-02-11 04:04:59 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
let m = &state.metrics.borrow();
|
2020-02-11 04:04:59 -05:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-02-11 11:23:40 -05:00
|
|
|
"opsDispatched": m.ops_dispatched,
|
2020-03-02 13:13:36 -05:00
|
|
|
"opsDispatchedSync": m.ops_dispatched_sync,
|
|
|
|
"opsDispatchedAsync": m.ops_dispatched_async,
|
|
|
|
"opsDispatchedAsyncUnref": m.ops_dispatched_async_unref,
|
2020-02-11 11:23:40 -05:00
|
|
|
"opsCompleted": m.ops_completed,
|
2020-03-02 13:13:36 -05:00
|
|
|
"opsCompletedSync": m.ops_completed_sync,
|
|
|
|
"opsCompletedAsync": m.ops_completed_async,
|
|
|
|
"opsCompletedAsyncUnref": m.ops_completed_async_unref,
|
2020-02-11 11:23:40 -05:00
|
|
|
"bytesSentControl": m.bytes_sent_control,
|
|
|
|
"bytesSentData": m.bytes_sent_data,
|
|
|
|
"bytesReceived": m.bytes_received
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
2020-07-08 10:35:45 -04:00
|
|
|
|
|
|
|
fn ppid() -> Value {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
// Adopted from rustup:
|
|
|
|
// https://github.com/rust-lang/rustup/blob/1.21.1/src/cli/self_update.rs#L1036
|
|
|
|
// Copyright Diggory Blake, the Mozilla Corporation, and rustup contributors.
|
|
|
|
// Licensed under either of
|
|
|
|
// - Apache License, Version 2.0
|
|
|
|
// - MIT license
|
|
|
|
use std::mem;
|
|
|
|
use winapi::shared::minwindef::DWORD;
|
|
|
|
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
|
|
|
|
use winapi::um::processthreadsapi::GetCurrentProcessId;
|
|
|
|
use winapi::um::tlhelp32::{
|
|
|
|
CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32,
|
|
|
|
TH32CS_SNAPPROCESS,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
// Take a snapshot of system processes, one of which is ours
|
|
|
|
// and contains our parent's pid
|
|
|
|
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if snapshot == INVALID_HANDLE_VALUE {
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut entry: PROCESSENTRY32 = mem::zeroed();
|
|
|
|
entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
|
|
|
|
|
|
|
|
// Iterate over system processes looking for ours
|
|
|
|
let success = Process32First(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let this_pid = GetCurrentProcessId();
|
|
|
|
while entry.th32ProcessID != this_pid {
|
|
|
|
let success = Process32Next(snapshot, &mut entry);
|
|
|
|
if success == 0 {
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
return serde_json::to_value(-1).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle(snapshot);
|
|
|
|
|
|
|
|
// FIXME: Using the process ID exposes a race condition
|
|
|
|
// wherein the parent process already exited and the OS
|
|
|
|
// reassigned its ID.
|
|
|
|
let parent_id = entry.th32ParentProcessID;
|
|
|
|
serde_json::to_value(parent_id).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
use std::os::unix::process::parent_id;
|
|
|
|
serde_json::to_value(parent_id()).unwrap()
|
|
|
|
}
|
|
|
|
}
|