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;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
Error,
|
|
|
|
SymbolFor,
|
|
|
|
} = window.__bootstrap.primordials;
|
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
|
|
|
}
|
|
|
|
|
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.
|
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
|
|
|
|
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-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,
|
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-07-19 13:49:44 -04:00
|
|
|
hostname,
|
|
|
|
loadavg,
|
|
|
|
};
|
|
|
|
})(this);
|