2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-05-31 04:42:44 -04:00
|
|
|
|
2024-01-26 17:46:46 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_delete_env,
|
|
|
|
op_env,
|
|
|
|
op_exec_path,
|
|
|
|
op_exit,
|
|
|
|
op_get_env,
|
2024-05-29 19:16:27 -04:00
|
|
|
op_get_exit_code,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_gid,
|
|
|
|
op_hostname,
|
|
|
|
op_loadavg,
|
|
|
|
op_network_interfaces,
|
|
|
|
op_os_release,
|
|
|
|
op_os_uptime,
|
|
|
|
op_set_env,
|
2024-01-26 17:46:46 -05:00
|
|
|
op_set_exit_code,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_system_memory_info,
|
|
|
|
op_uid,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
Error,
|
2023-05-01 09:30:02 -04:00
|
|
|
FunctionPrototypeBind,
|
2024-06-02 21:29:01 -04:00
|
|
|
NumberIsInteger,
|
|
|
|
RangeError,
|
2023-02-07 14:22:46 -05:00
|
|
|
SymbolFor,
|
2024-05-29 19:16:27 -04:00
|
|
|
TypeError,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import { Event, EventTarget } from "ext:deno_web/02_event.js";
|
|
|
|
|
2023-05-01 09:30:02 -04:00
|
|
|
const windowDispatchEvent = FunctionPrototypeBind(
|
|
|
|
EventTarget.prototype.dispatchEvent,
|
2023-02-07 14:22:46 -05:00
|
|
|
globalThis,
|
|
|
|
);
|
|
|
|
|
|
|
|
function loadavg() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_loadavg();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostname() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_hostname();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function osRelease() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_os_release();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 06:35:38 -05:00
|
|
|
function osUptime() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_os_uptime();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function systemMemoryInfo() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_system_memory_info();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function networkInterfaces() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_network_interfaces();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function gid() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_gid();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function uid() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_uid();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is an internal only method used by the test harness to override the
|
|
|
|
// behavior of exit when the exit sanitizer is enabled.
|
|
|
|
let exitHandler = null;
|
|
|
|
function setExitHandler(fn) {
|
|
|
|
exitHandler = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function exit(code) {
|
|
|
|
// Set exit code first so unload event listeners can override it.
|
|
|
|
if (typeof code === "number") {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_set_exit_code(code);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2024-05-29 19:16:27 -04:00
|
|
|
code = op_get_exit_code();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Dispatches `unload` only when it's not dispatched yet.
|
2023-03-08 23:09:40 -05:00
|
|
|
if (!globalThis[SymbolFor("Deno.isUnloadDispatched")]) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// Invokes the `unload` hooks before exiting
|
|
|
|
// ref: https://github.com/denoland/deno/issues/3603
|
|
|
|
windowDispatchEvent(new Event("unload"));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (exitHandler) {
|
|
|
|
exitHandler(code);
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
op_exit();
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new Error("Code not reachable");
|
|
|
|
}
|
|
|
|
|
2024-05-29 19:16:27 -04:00
|
|
|
function getExitCode() {
|
|
|
|
return op_get_exit_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setExitCode(value) {
|
2024-06-02 21:29:01 -04:00
|
|
|
if (typeof value !== "number") {
|
2024-05-29 19:16:27 -04:00
|
|
|
throw new TypeError(
|
2024-06-02 21:29:01 -04:00
|
|
|
`Exit code must be a number, got: ${value} (${typeof value})`,
|
2024-05-29 19:16:27 -04:00
|
|
|
);
|
|
|
|
}
|
2024-06-02 21:29:01 -04:00
|
|
|
if (!NumberIsInteger(value)) {
|
|
|
|
throw new RangeError(
|
|
|
|
`Exit code must be an integer, got: ${value}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
op_set_exit_code(value);
|
2024-05-29 19:16:27 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function setEnv(key, value) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_set_env(key, value);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEnv(key) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_get_env(key) ?? undefined;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEnv(key) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_delete_env(key);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const env = {
|
|
|
|
get: getEnv,
|
|
|
|
toObject() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_env();
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
|
|
|
set: setEnv,
|
|
|
|
has(key) {
|
|
|
|
return getEnv(key) !== undefined;
|
|
|
|
},
|
|
|
|
delete: deleteEnv,
|
|
|
|
};
|
|
|
|
|
|
|
|
function execPath() {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_exec_path();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
env,
|
|
|
|
execPath,
|
|
|
|
exit,
|
2024-05-29 19:16:27 -04:00
|
|
|
getExitCode,
|
2023-02-07 14:22:46 -05:00
|
|
|
gid,
|
|
|
|
hostname,
|
|
|
|
loadavg,
|
|
|
|
networkInterfaces,
|
|
|
|
osRelease,
|
2023-02-14 06:35:38 -05:00
|
|
|
osUptime,
|
2024-05-29 19:16:27 -04:00
|
|
|
setExitCode,
|
2023-02-07 14:22:46 -05:00
|
|
|
setExitHandler,
|
|
|
|
systemMemoryInfo,
|
|
|
|
uid,
|
|
|
|
};
|