2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-11-12 23:52:59 -05:00
|
|
|
const ops = core.ops;
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
Promise,
|
|
|
|
SafeArrayIterator,
|
|
|
|
} = primordials;
|
2023-05-30 11:34:50 -04:00
|
|
|
|
|
|
|
// WARNING: Keep this in sync with Rust (search for LogLevel)
|
|
|
|
const LogLevel = {
|
|
|
|
Error: 1,
|
|
|
|
Warn: 2,
|
|
|
|
Info: 3,
|
|
|
|
Debug: 4,
|
|
|
|
};
|
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
const logSource = "JS";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
let logLevel_ = null;
|
|
|
|
function logLevel() {
|
|
|
|
if (logLevel_ === null) {
|
|
|
|
logLevel_ = ops.op_bootstrap_log_level() || 3;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-11-12 23:52:59 -05:00
|
|
|
return logLevel_;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function log(...args) {
|
2023-11-12 23:52:59 -05:00
|
|
|
if (logLevel() >= LogLevel.Debug) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// if we destructure `console` off `globalThis` too early, we don't bind to
|
|
|
|
// the right console, therefore we don't log anything out.
|
2023-05-22 14:17:31 -04:00
|
|
|
globalThis.console.error(
|
2023-02-07 14:22:46 -05:00
|
|
|
`DEBUG ${logSource} -`,
|
|
|
|
...new SafeArrayIterator(args),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function createResolvable() {
|
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
promise.resolve = resolve;
|
|
|
|
promise.reject = reject;
|
|
|
|
return promise;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function writable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function nonEnumerable(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function readOnly(value) {
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getterOnly(getter) {
|
|
|
|
return {
|
|
|
|
get: getter,
|
|
|
|
set() {},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
export { createResolvable, getterOnly, log, nonEnumerable, readOnly, writable };
|