deno_runtime/ops/
runtime.rs1use deno_core::op2;
4use deno_core::ModuleSpecifier;
5use deno_core::OpState;
6
7deno_core::extension!(
8 deno_runtime,
9 ops = [op_main_module, op_ppid],
10 options = { main_module: ModuleSpecifier },
11 state = |state, options| {
12 state.put::<ModuleSpecifier>(options.main_module);
13 },
14);
15
16#[op2]
17#[string]
18fn op_main_module(state: &mut OpState) -> String {
19 let main_url = state.borrow::<ModuleSpecifier>();
20 main_url.to_string()
21}
22
23#[op2(fast)]
26#[number]
27pub fn op_ppid() -> i64 {
28 #[cfg(windows)]
29 {
30 use std::mem;
37 use winapi::shared::minwindef::DWORD;
38 use winapi::um::handleapi::CloseHandle;
39 use winapi::um::handleapi::INVALID_HANDLE_VALUE;
40 use winapi::um::processthreadsapi::GetCurrentProcessId;
41 use winapi::um::tlhelp32::CreateToolhelp32Snapshot;
42 use winapi::um::tlhelp32::Process32First;
43 use winapi::um::tlhelp32::Process32Next;
44 use winapi::um::tlhelp32::PROCESSENTRY32;
45 use winapi::um::tlhelp32::TH32CS_SNAPPROCESS;
46 unsafe {
48 let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
51 if snapshot == INVALID_HANDLE_VALUE {
52 return -1;
53 }
54
55 let mut entry: PROCESSENTRY32 = mem::zeroed();
56 entry.dwSize = mem::size_of::<PROCESSENTRY32>() as DWORD;
57
58 let success = Process32First(snapshot, &mut entry);
60 if success == 0 {
61 CloseHandle(snapshot);
62 return -1;
63 }
64
65 let this_pid = GetCurrentProcessId();
66 while entry.th32ProcessID != this_pid {
67 let success = Process32Next(snapshot, &mut entry);
68 if success == 0 {
69 CloseHandle(snapshot);
70 return -1;
71 }
72 }
73 CloseHandle(snapshot);
74
75 let parent_id = entry.th32ParentProcessID;
79 parent_id.into()
80 }
81 }
82 #[cfg(not(windows))]
83 {
84 use std::os::unix::process::parent_id;
85 parent_id().into()
86 }
87}