@gjsify/node-gi 0.13.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/system.js ADDED
@@ -0,0 +1,116 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // @gjsify/node-gi/system — the GJS `System` module on Node.
3
+ //
4
+ // GJS exposes a built-in `system` module (`import System from 'system'` /
5
+ // `imports.system`) for process identity + lifecycle: `exit`, `gc`, the
6
+ // program-identity accessors (`programInvocationName`/`programPath`/
7
+ // `programArgs`), `version`, plus the introspection/debug helpers
8
+ // (`addressOf`/`refcount`/`breakpoint`/`dumpHeap`/…). On Node these don't
9
+ // exist, so this module re-creates the surface, routed through Node's
10
+ // `process` where there is an equivalent and a safe no-op otherwise.
11
+ //
12
+ // This is the single source of truth for the System surface: the legacy
13
+ // `imports.system` exposed by `@gjsify/node-gi/globals` re-uses this module's
14
+ // default export (no duplicated logic). The gjsify `--app node` build aliases
15
+ // the bare `system` specifier to this module (kept external — `ALIASES_GJS_FOR_NODE`).
16
+ //
17
+ // Reference: GJS's `system` module (refs/gjs/modules/esm/system.js + the native
18
+ // modules/system.cpp). Members with no meaningful Node equivalent
19
+ // (addressOf/refcount/clearDateCaches/dumpHeap/dumpMemoryInfo) are safe no-ops
20
+ // returning the same shape GJS does — they exist so GJS source that calls them
21
+ // keeps running unmodified, not to reproduce SpiderMonkey internals.
22
+
23
+ // `programArgs` mirrors GJS's ARGV: the script arguments, excluding the
24
+ // interpreter (argv[0]) and the script path (argv[1]).
25
+ function readProgramArgs() {
26
+ return typeof process !== 'undefined' && Array.isArray(process.argv) ? process.argv.slice(2) : [];
27
+ }
28
+
29
+ // `programInvocationName` / `programPath` track the running script — Node's
30
+ // `process.argv[1]`.
31
+ function readProgramInvocationName() {
32
+ return (typeof process !== 'undefined' && process.argv[1]) || '';
33
+ }
34
+
35
+ function readProgramPath() {
36
+ return (typeof process !== 'undefined' && process.argv[1]) || null;
37
+ }
38
+
39
+ /** Exit the process (GJS `System.exit`). */
40
+ export function exit(code) {
41
+ if (typeof process !== 'undefined') process.exit(code ?? 0);
42
+ }
43
+
44
+ /** Trigger a garbage collection if the host exposed `globalThis.gc` (`--expose-gc`). */
45
+ export function gc() {
46
+ if (typeof globalThis.gc === 'function') globalThis.gc();
47
+ }
48
+
49
+ /** The SpiderMonkey/mozjs version number — no equivalent on Node, reported as 0. */
50
+ export const version = 0;
51
+
52
+ /** The script arguments (GJS ARGV) — Node `process.argv.slice(2)`. */
53
+ export const programArgs = readProgramArgs();
54
+
55
+ /** The running script's invocation name — Node `process.argv[1]`. */
56
+ export const programInvocationName = readProgramInvocationName();
57
+
58
+ /** The running script's path — Node `process.argv[1]` (or null). */
59
+ export const programPath = readProgramPath();
60
+
61
+ /** Return the address of a JS object as a string. No Node equivalent — stub. */
62
+ export function addressOf() {
63
+ return '0x0';
64
+ }
65
+
66
+ /** Return the address of a GObject as a string. No Node equivalent — stub. */
67
+ export function addressOfGObject() {
68
+ return '0x0';
69
+ }
70
+
71
+ /** Return the refcount of a GObject. No Node equivalent — stub. */
72
+ export function refcount() {
73
+ return 0;
74
+ }
75
+
76
+ /** Trigger a debugger breakpoint. No-op on Node. */
77
+ export function breakpoint() {}
78
+
79
+ /** Clear the Date timezone caches (GJS calls this after a TZ change). No-op on Node. */
80
+ export function clearDateCaches() {}
81
+
82
+ /** Dump the JS heap to a file. No-op on Node. */
83
+ export function dumpHeap() {}
84
+
85
+ /** Dump memory info to a file. No-op on Node. */
86
+ export function dumpMemoryInfo() {}
87
+
88
+ /**
89
+ * The GJS `System` module as a default export — the object shape
90
+ * `import System from 'system'` returns. The program-identity members are live
91
+ * getters (read `process.argv` on access) for fidelity with GJS, where they
92
+ * track the running script.
93
+ */
94
+ const System = {
95
+ exit,
96
+ gc,
97
+ version,
98
+ addressOf,
99
+ addressOfGObject,
100
+ refcount,
101
+ breakpoint,
102
+ clearDateCaches,
103
+ dumpHeap,
104
+ dumpMemoryInfo,
105
+ get programArgs() {
106
+ return readProgramArgs();
107
+ },
108
+ get programInvocationName() {
109
+ return readProgramInvocationName();
110
+ },
111
+ get programPath() {
112
+ return readProgramPath();
113
+ },
114
+ };
115
+
116
+ export default System;