2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
Error,
|
|
|
|
SymbolFor,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-05-19 22:57:05 -04:00
|
|
|
const windowDispatchEvent = window.dispatchEvent.bind(window);
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function loadavg() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_loadavg");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostname() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_hostname");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function osRelease() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_os_release");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-09-10 04:38:17 -04:00
|
|
|
function systemMemoryInfo() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_system_memory_info");
|
2020-09-10 04:38:17 -04:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:39:28 -05:00
|
|
|
function networkInterfaces() {
|
|
|
|
return core.opSync("op_network_interfaces");
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:44:19 -05:00
|
|
|
function getUid() {
|
|
|
|
return core.opSync("op_getuid");
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:55:50 -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;
|
|
|
|
}
|
|
|
|
|
2021-11-27 18:45:38 -05:00
|
|
|
function exit(code) {
|
|
|
|
// Set exit code first so unload event listeners can override it.
|
|
|
|
if (typeof code === "number") {
|
|
|
|
core.opSync("op_set_exit_code", code);
|
|
|
|
} else {
|
|
|
|
code = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:44:48 -05:00
|
|
|
// Dispatches `unload` only when it's not dispatched yet.
|
2021-07-03 18:17:52 -04:00
|
|
|
if (!window[SymbolFor("isUnloadDispatched")]) {
|
2021-01-21 02:44:48 -05:00
|
|
|
// Invokes the `unload` hooks before exiting
|
|
|
|
// ref: https://github.com/denoland/deno/issues/3603
|
2022-05-19 22:57:05 -04:00
|
|
|
windowDispatchEvent(new Event("unload"));
|
2021-01-21 02:44:48 -05:00
|
|
|
}
|
2021-02-24 07:55:50 -05:00
|
|
|
|
|
|
|
if (exitHandler) {
|
|
|
|
exitHandler(code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-27 18:45:38 -05:00
|
|
|
core.opSync("op_exit");
|
2020-07-19 13:49:44 -04:00
|
|
|
throw new Error("Code not reachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEnv(key, value) {
|
2021-10-05 16:38:27 -04:00
|
|
|
core.opSync("op_set_env", key, value);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEnv(key) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_get_env", key) ?? undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEnv(key) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_delete_env", key);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const env = {
|
|
|
|
get: getEnv,
|
|
|
|
toObject() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_env");
|
2020-07-19 13:49:44 -04:00
|
|
|
},
|
|
|
|
set: setEnv,
|
|
|
|
delete: deleteEnv,
|
|
|
|
};
|
|
|
|
|
|
|
|
function execPath() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_exec_path");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.os = {
|
|
|
|
env,
|
|
|
|
execPath,
|
|
|
|
exit,
|
2022-01-31 00:44:19 -05:00
|
|
|
getUid,
|
2020-07-19 13:49:44 -04:00
|
|
|
hostname,
|
|
|
|
loadavg,
|
2022-01-24 04:39:28 -05:00
|
|
|
networkInterfaces,
|
2022-01-31 00:44:19 -05:00
|
|
|
osRelease,
|
|
|
|
setExitHandler,
|
|
|
|
systemMemoryInfo,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|