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;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2022-10-24 10:14:17 -04:00
|
|
|
const { Event } = window.__bootstrap.event;
|
|
|
|
const { EventTarget } = window.__bootstrap.eventTarget;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
Error,
|
|
|
|
SymbolFor,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-10-24 10:14:17 -04:00
|
|
|
const windowDispatchEvent = EventTarget.prototype.dispatchEvent.bind(window);
|
2022-05-19 22:57:05 -04:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function loadavg() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_loadavg();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostname() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_hostname();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function osRelease() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_os_release();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-12-28 08:56:05 -05:00
|
|
|
function createOsUptime(opFn) {
|
|
|
|
return function osUptime() {
|
|
|
|
return opFn();
|
|
|
|
};
|
2022-12-26 18:16:12 -05:00
|
|
|
}
|
|
|
|
|
2020-09-10 04:38:17 -04:00
|
|
|
function systemMemoryInfo() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_system_memory_info();
|
2020-09-10 04:38:17 -04:00
|
|
|
}
|
|
|
|
|
2022-01-24 04:39:28 -05:00
|
|
|
function networkInterfaces() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_network_interfaces();
|
2022-01-24 04:39:28 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:37:48 -04:00
|
|
|
function gid() {
|
|
|
|
return ops.op_gid();
|
2022-05-31 04:42:44 -04:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:37:48 -04:00
|
|
|
function uid() {
|
|
|
|
return ops.op_uid();
|
2022-01-31 00:44:19 -05: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;
|
|
|
|
}
|
|
|
|
|
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") {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_set_exit_code(code);
|
2021-11-27 18:45:38 -05:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_exit();
|
2020-07-19 13:49:44 -04:00
|
|
|
throw new Error("Code not reachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setEnv(key, value) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_set_env(key, value);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEnv(key) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_get_env(key) ?? undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEnv(key) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_delete_env(key);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const env = {
|
|
|
|
get: getEnv,
|
|
|
|
toObject() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_env();
|
2020-07-19 13:49:44 -04:00
|
|
|
},
|
|
|
|
set: setEnv,
|
|
|
|
delete: deleteEnv,
|
|
|
|
};
|
|
|
|
|
|
|
|
function execPath() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_exec_path();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.os = {
|
|
|
|
env,
|
|
|
|
execPath,
|
|
|
|
exit,
|
2022-10-26 16:37:48 -04:00
|
|
|
gid,
|
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,
|
2022-12-28 08:56:05 -05:00
|
|
|
createOsUptime,
|
2022-01-31 00:44:19 -05:00
|
|
|
setExitHandler,
|
|
|
|
systemMemoryInfo,
|
2022-10-26 16:37:48 -04:00
|
|
|
uid,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|