forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.js
More file actions
90 lines (78 loc) · 2.51 KB
/
console.js
File metadata and controls
90 lines (78 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file console.js
* @author Tom Tang <xmader@distributive.network>
* @date June 2023
*/
const { customInspectSymbol, format } = require("util");
/** @typedef {(str: string) => void} WriteFn */
/** @typedef {{ write: WriteFn }} IOWriter */
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console_API
*/
// TODO (Tom Tang): adhere https://console.spec.whatwg.org/
class Console {
/** @type {WriteFn} */
#writeToStdout;
/** @type {WriteFn} */
#writeToStderr;
/**
* Console constructor, form 1
* @overload
* @param {IOWriter} stdout - object with write method
* @param {IOWriter} stderr - object with write method
* @param {boolean=} ignoreErrors - currently unused in PythonMonkey
* @see https://nodejs.org/api/console.html#new-consolestdout-stderr-ignoreerrors
*/
/**
* Console constructor, form 2
* @overload
* @param {ConsoleConstructorOptions} options - options object
* @typedef {object} ConsoleConstructorOptions
* @property {IOWriter} stdout - object with write method
* @property {IOWriter} stderr - object with write method
* @property {boolean=} ignoreErrors - currently unused in PythonMonkey
*/
constructor(stdout, stderr, ignoreErrors)
{
var options;
if (arguments.length === 1)
options = stdout;
else
{
if (typeof ignoreErrors === 'undefined')
ignoreErrors = true;
options = { stdout, stderr, ignoreErrors };
}
this.#writeToStdout = options.stdout.write;
this.#writeToStderr = options.stderr.write;
this.log = (...args) => this.#writeToStdout(this.#formatToStr(...args));
this.debug = (...args) => this.#writeToStdout(this.#formatToStr(...args));
this.info = (...args) => this.#writeToStdout(this.#formatToStr(...args));
this.warn = (...args) => this.#writeToStderr(this.#formatToStr(...args));
this.error = (...args) => this.#writeToStderr(this.#formatToStr(...args));
}
/**
* @return {string}
*/
#formatToStr(...args) {
return format(...args) + "\n"
}
// TODO (Tom Tang): implement more methods
/**
* Re-export the `Console` constructor as global `console.Console`, like in Node.js
*/
get Console() {
return Console
}
/**
* Export the `nodejs.util.inspect.custom` symbol as a static property of `Console`
*/
static customInspectSymbol = customInspectSymbol;
}
if (!globalThis.console) {
globalThis.console = new Console(
python.stdout /* sys.stdout */,
python.stderr /* sys.stderr */
);
}
exports.Console = Console;