2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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;
|
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
|
|
|
}
|
|
|
|
|
2020-10-26 10:54:27 -04:00
|
|
|
function systemCpuInfo() {
|
2021-04-12 15:55:05 -04:00
|
|
|
const { cores, speed } = core.opSync("op_system_cpu_info");
|
2021-04-05 12:40:24 -04:00
|
|
|
// Map nulls to undefined for compatibility
|
|
|
|
return {
|
|
|
|
cores: cores ?? undefined,
|
|
|
|
speed: speed ?? undefined,
|
|
|
|
};
|
2020-10-26 10:54:27 -04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function exit(code = 0) {
|
2021-01-21 02:44:48 -05:00
|
|
|
// Dispatches `unload` only when it's not dispatched yet.
|
|
|
|
if (!window[Symbol.for("isUnloadDispatched")]) {
|
|
|
|
// Invokes the `unload` hooks before exiting
|
|
|
|
// ref: https://github.com/denoland/deno/issues/3603
|
|
|
|
window.dispatchEvent(new Event("unload"));
|
|
|
|
}
|
2021-02-24 07:55:50 -05:00
|
|
|
|
|
|
|
if (exitHandler) {
|
|
|
|
exitHandler(code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_exit", code);
|
2020-07-19 13:49:44 -04:00
|
|
|
throw new Error("Code not reachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEnv(key, value) {
|
2021-04-12 15:55:05 -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,
|
2021-02-24 07:55:50 -05:00
|
|
|
setExitHandler,
|
2020-07-19 13:49:44 -04:00
|
|
|
exit,
|
|
|
|
osRelease,
|
2020-09-10 04:38:17 -04:00
|
|
|
systemMemoryInfo,
|
2020-10-26 10:54:27 -04:00
|
|
|
systemCpuInfo,
|
2020-07-19 13:49:44 -04:00
|
|
|
hostname,
|
|
|
|
loadavg,
|
|
|
|
};
|
|
|
|
})(this);
|