@tinyactors/node 0.1.0 → 0.2.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/README.md +388 -156
- package/node/index.d.ts +915 -0
- package/node/index.js +44 -0
- package/node/transports.js +60 -0
- package/node/ws.js +152 -0
- package/package.json +22 -17
- package/shared/core.mjs +2966 -0
- package/shared/ecmascript.mjs +670 -0
- package/index.d.ts +0 -1875
- package/index.js +0 -34
- package/lib/actor.js +0 -85
- package/lib/debug/breakpoints.js +0 -349
- package/lib/debug/console.js +0 -149
- package/lib/debug/expressions.js +0 -133
- package/lib/debug/frames.js +0 -119
- package/lib/debug/labels.js +0 -142
- package/lib/debug/macrosteps.js +0 -207
- package/lib/debug/protocol.js +0 -186
- package/lib/debug/sources.js +0 -218
- package/lib/debug/variables.js +0 -265
- package/lib/debug.js +0 -1434
- package/lib/definition.js +0 -139
- package/lib/errors.js +0 -24
- package/lib/events.js +0 -56
- package/lib/image.js +0 -90
- package/lib/loader.js +0 -68
- package/lib/metrics.js +0 -333
- package/lib/native.js +0 -8
- package/lib/otlp.js +0 -332
- package/lib/statechart.js +0 -768
- package/lib/system.js +0 -2183
- package/lib/tracing.js +0 -164
package/lib/debug/protocol.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// The base protocol of the Debug Adapter Protocol: each message is a JSON
|
|
4
|
-
// object after a `Content-Length` header and a blank line. A DebugSession
|
|
5
|
-
// takes and gives message objects, so a transport that frames messages
|
|
6
|
-
// itself (a WebSocket) needs none of this; one that carries bytes (stdio, a
|
|
7
|
-
// TCP socket) reads with DAPReader and writes with encodeDAPMessage. Host
|
|
8
|
-
// neutral: Uint8Array, TextEncoder and TextDecoder only.
|
|
9
|
-
|
|
10
|
-
const { TinyactorsError } = require('../errors');
|
|
11
|
-
|
|
12
|
-
const encoder = new TextEncoder();
|
|
13
|
-
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
14
|
-
const CR = 13;
|
|
15
|
-
const LF = 10;
|
|
16
|
-
|
|
17
|
-
/** One message as the bytes a byte stream carries: the header, then its JSON. */
|
|
18
|
-
function encodeDAPMessage(message) {
|
|
19
|
-
const body = encoder.encode(JSON.stringify(message));
|
|
20
|
-
const header = encoder.encode(`Content-Length: ${body.length}\r\n\r\n`);
|
|
21
|
-
const out = new Uint8Array(header.length + body.length);
|
|
22
|
-
out.set(header, 0);
|
|
23
|
-
out.set(body, header.length);
|
|
24
|
-
return out;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Limits that keep a malformed stream from buffering without end: the
|
|
28
|
-
// headers of one message, and one body.
|
|
29
|
-
const MAX_HEADER_BYTES = 8 * 1024;
|
|
30
|
-
const MAX_BODY_BYTES = 64 * 1024 * 1024;
|
|
31
|
-
const TYPES = new Set(['request', 'response', 'event']);
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Turns a byte stream into messages: push() each chunk as it arrives, and
|
|
35
|
-
* `onMessage` gets every whole message in order. Chunks may split a
|
|
36
|
-
* header, a body or a UTF-8 sequence anywhere; a string chunk may split a
|
|
37
|
-
* surrogate pair.
|
|
38
|
-
*
|
|
39
|
-
* A malformed stream throws `invalid` from push(), and the reader is
|
|
40
|
-
* broken from then on: every later push() throws `invalid` too, since the
|
|
41
|
-
* stream cannot be resynchronized once a length is in doubt. Malformed is:
|
|
42
|
-
* headers that are not UTF-8, longer than 8 KiB without the blank line, or
|
|
43
|
-
* with a line that is not `Name: value`; no Content-Length, a second one, or
|
|
44
|
-
* one that is not a decimal number of at most 64 MiB; a body that is not
|
|
45
|
-
* UTF-8 JSON, or not an object with an integer `seq` and a `type` of
|
|
46
|
-
* "request", "response" or "event". Other header fields are ignored. An
|
|
47
|
-
* exception from `onMessage` is not the stream's fault: it propagates from
|
|
48
|
-
* push(), the reader stays usable, and messages already buffered behind it
|
|
49
|
-
* are delivered by the next push().
|
|
50
|
-
*/
|
|
51
|
-
class DAPReader {
|
|
52
|
-
#onMessage;
|
|
53
|
-
#buffer = new Uint8Array(0);
|
|
54
|
-
#pendingSurrogate = '';
|
|
55
|
-
#broken = null;
|
|
56
|
-
|
|
57
|
-
constructor(onMessage) {
|
|
58
|
-
if (typeof onMessage !== 'function') {
|
|
59
|
-
throw new TinyactorsError('DAPReader: expected a function for each message', 'invalid');
|
|
60
|
-
}
|
|
61
|
-
this.#onMessage = onMessage;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
push(chunk) {
|
|
65
|
-
if (this.#broken !== null) {
|
|
66
|
-
throw new TinyactorsError(`DAP: the stream was malformed earlier (${this.#broken})`, 'invalid');
|
|
67
|
-
}
|
|
68
|
-
this.#append(chunk instanceof Uint8Array ? chunk : this.#encode(String(chunk)));
|
|
69
|
-
for (;;) {
|
|
70
|
-
const message = this.#next();
|
|
71
|
-
if (message === undefined) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
this.#onMessage(message);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// A string chunk as UTF-8, holding back a trailing high surrogate until
|
|
79
|
-
// the chunk with its low half arrives.
|
|
80
|
-
#encode(text) {
|
|
81
|
-
let joined = this.#pendingSurrogate + text;
|
|
82
|
-
this.#pendingSurrogate = '';
|
|
83
|
-
const last = joined.charCodeAt(joined.length - 1);
|
|
84
|
-
if (last >= 0xd800 && last <= 0xdbff) {
|
|
85
|
-
this.#pendingSurrogate = joined.slice(-1);
|
|
86
|
-
joined = joined.slice(0, -1);
|
|
87
|
-
}
|
|
88
|
-
return encoder.encode(joined);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
#append(bytes) {
|
|
92
|
-
const joined = new Uint8Array(this.#buffer.length + bytes.length);
|
|
93
|
-
joined.set(this.#buffer, 0);
|
|
94
|
-
joined.set(bytes, this.#buffer.length);
|
|
95
|
-
this.#buffer = joined;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// The next whole message, taken off the buffer, or undefined while it is
|
|
99
|
-
// still arriving.
|
|
100
|
-
#next() {
|
|
101
|
-
const end = headerEnd(this.#buffer);
|
|
102
|
-
if (end < 0) {
|
|
103
|
-
if (this.#buffer.length > MAX_HEADER_BYTES) {
|
|
104
|
-
this.#fail(`no blank line within ${MAX_HEADER_BYTES} bytes of headers`);
|
|
105
|
-
}
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
if (end > MAX_HEADER_BYTES) {
|
|
109
|
-
this.#fail(`headers longer than ${MAX_HEADER_BYTES} bytes`);
|
|
110
|
-
}
|
|
111
|
-
const length = this.#contentLength(this.#text(this.#buffer.subarray(0, end), 'the headers of a message are not UTF-8'));
|
|
112
|
-
const start = end + 4;
|
|
113
|
-
if (this.#buffer.length < start + length) {
|
|
114
|
-
return undefined;
|
|
115
|
-
}
|
|
116
|
-
const body = this.#buffer.subarray(start, start + length);
|
|
117
|
-
let message;
|
|
118
|
-
try {
|
|
119
|
-
message = JSON.parse(this.#text(body, 'a body is not UTF-8'));
|
|
120
|
-
} catch (error) {
|
|
121
|
-
if (error instanceof TinyactorsError) throw error;
|
|
122
|
-
this.#fail(`a body is not JSON: ${error.message}`);
|
|
123
|
-
}
|
|
124
|
-
if (message === null || typeof message !== 'object' || Array.isArray(message)) {
|
|
125
|
-
this.#fail('a body is not a JSON object');
|
|
126
|
-
}
|
|
127
|
-
if (!Number.isInteger(message.seq)) {
|
|
128
|
-
this.#fail('a message has no integer seq');
|
|
129
|
-
}
|
|
130
|
-
if (!TYPES.has(message.type)) {
|
|
131
|
-
this.#fail(`a message's type is ${JSON.stringify(message.type)}, not "request", "response" or "event"`);
|
|
132
|
-
}
|
|
133
|
-
this.#buffer = this.#buffer.slice(start + length);
|
|
134
|
-
return message;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
#text(bytes, refusal) {
|
|
138
|
-
try {
|
|
139
|
-
return decoder.decode(bytes);
|
|
140
|
-
} catch {
|
|
141
|
-
return this.#fail(refusal);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
#contentLength(headers) {
|
|
146
|
-
let length;
|
|
147
|
-
for (const line of headers.split('\r\n')) {
|
|
148
|
-
const colon = line.indexOf(':');
|
|
149
|
-
if (colon <= 0) {
|
|
150
|
-
this.#fail(`a header line is not "Name: value": ${JSON.stringify(line.slice(0, 80))}`);
|
|
151
|
-
}
|
|
152
|
-
if (line.slice(0, colon).trim().toLowerCase() !== 'content-length') {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
if (length !== undefined) {
|
|
156
|
-
this.#fail('a message has two Content-Length headers');
|
|
157
|
-
}
|
|
158
|
-
const value = line.slice(colon + 1).trim();
|
|
159
|
-
length = /^[0-9]{1,9}$/.test(value) ? Number(value) : NaN;
|
|
160
|
-
if (!(length <= MAX_BODY_BYTES)) {
|
|
161
|
-
this.#fail(`Content-Length ${JSON.stringify(value.slice(0, 20))} is not a decimal number of at most ${MAX_BODY_BYTES}`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (length === undefined) {
|
|
165
|
-
this.#fail('a message has no Content-Length header');
|
|
166
|
-
}
|
|
167
|
-
return length;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
#fail(reason) {
|
|
171
|
-
this.#broken = reason;
|
|
172
|
-
throw new TinyactorsError(`DAP: ${reason}`, 'invalid');
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// The index of the blank line after the headers, or -1.
|
|
177
|
-
function headerEnd(bytes) {
|
|
178
|
-
for (let at = 0; at + 3 < bytes.length; at += 1) {
|
|
179
|
-
if (bytes[at] === CR && bytes[at + 1] === LF && bytes[at + 2] === CR && bytes[at + 3] === LF) {
|
|
180
|
-
return at;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return -1;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
module.exports = { DAPReader, encodeDAPMessage };
|
package/lib/debug/sources.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Where a chart's elements were written (core ADR 0085): statechart()
|
|
4
|
-
// records the file, line and column of the builder call that wrote each
|
|
5
|
-
// state, transition, initial transition and action, define(text, { format:
|
|
6
|
-
// 'scxml', name }) records where in the named document each one is, and
|
|
7
|
-
// Definition.sourceOf() reads one back by core's ID. This is how the
|
|
8
|
-
// debugger gives stack frames a source, maps a line breakpoint to the
|
|
9
|
-
// element on that line, answers breakpointLocations, and puts goto targets
|
|
10
|
-
// on lines.
|
|
11
|
-
//
|
|
12
|
-
// A chart whose spans name no file a client can open (an SCXML document
|
|
13
|
-
// defined without a name, a child chart inline in its parent's <content>,
|
|
14
|
-
// a child fetched from a URI that is not a file) is shown as a virtual
|
|
15
|
-
// source instead (napi ADR 0026): the SCXML the codec writes from the
|
|
16
|
-
// definition, with where it wrote each element (core ADR 0085,
|
|
17
|
-
// ta_definition_encode with its source map), which the client asks for by
|
|
18
|
-
// `sourceReference`. A chart SCXML cannot express (a builder chart whose
|
|
19
|
-
// expressions are functions) and that keeps no spans has neither, and the
|
|
20
|
-
// adapter then works as it did before: frames on line 0, lines unverified.
|
|
21
|
-
|
|
22
|
-
const { core } = require('../native');
|
|
23
|
-
const { Definition } = require('../definition');
|
|
24
|
-
const { TinyactorsError } = require('../errors');
|
|
25
|
-
|
|
26
|
-
const KINDS = ['state', 'transition', 'action'];
|
|
27
|
-
const SCXML = 'application/scxml+xml';
|
|
28
|
-
|
|
29
|
-
// A span's source as a path a client can open, or undefined: a `file:`
|
|
30
|
-
// URL loses its scheme, and a URI of any other scheme is not a file. A
|
|
31
|
-
// Windows drive letter is not a scheme.
|
|
32
|
-
function pathOf(source) {
|
|
33
|
-
if (typeof source !== 'string' || source === '') return undefined;
|
|
34
|
-
if (source.startsWith('file://')) return decodeURIComponent(source.slice('file://'.length));
|
|
35
|
-
if (source.startsWith('file:')) return decodeURIComponent(source.slice('file:'.length));
|
|
36
|
-
return /^[a-zA-Z][a-zA-Z0-9+.-]+:/.test(source) ? undefined : source;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const basename = (path) => path.slice(Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) + 1);
|
|
40
|
-
|
|
41
|
-
// Whether a client's path names the same file as a span's.
|
|
42
|
-
function samePath(left, right) {
|
|
43
|
-
if (left === undefined || right === undefined) return false;
|
|
44
|
-
const a = left.replaceAll('\\', '/');
|
|
45
|
-
const b = right.replaceAll('\\', '/');
|
|
46
|
-
return a === b || a.endsWith(`/${b}`) || b.endsWith(`/${a}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Whether an element is in the DAP Source a client named: a virtual source
|
|
50
|
-
// by its reference, a file by its path.
|
|
51
|
-
function inSource(element, source) {
|
|
52
|
-
const reference = Number(source?.sourceReference ?? 0);
|
|
53
|
-
if (reference > 0) return element.reference === reference;
|
|
54
|
-
return samePath(source?.path, element.path);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
class Sources {
|
|
58
|
-
#system;
|
|
59
|
-
#native;
|
|
60
|
-
#allocate;
|
|
61
|
-
#elements = new Map();
|
|
62
|
-
// Definition → its virtual source: { reference, name, text }, or null
|
|
63
|
-
// when it has none. The reference outlives forget(), so a breakpoint set
|
|
64
|
-
// on it is placed again when the chart is defined again.
|
|
65
|
-
#virtuals = new Map();
|
|
66
|
-
#references = new Map();
|
|
67
|
-
|
|
68
|
-
/** `allocate()` answers a new sourceReference, unique in the session. */
|
|
69
|
-
constructor(system, native, allocate) {
|
|
70
|
-
this.#system = system;
|
|
71
|
-
this.#native = native;
|
|
72
|
-
this.#allocate = allocate;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Where one element was written, or undefined: { path, line, column } in
|
|
77
|
-
* a file, or { reference, name, line, column } in a virtual source, as
|
|
78
|
-
* elements() lists it.
|
|
79
|
-
*/
|
|
80
|
-
place(definition, kind, id) {
|
|
81
|
-
if (id === null || id === undefined) return undefined;
|
|
82
|
-
return this.elements(definition).find((element) => element.kind === kind && element.id === id);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** The DAP Source object for a place. */
|
|
86
|
-
static source(place) {
|
|
87
|
-
if (place.path !== undefined) return { name: basename(place.path), path: place.path };
|
|
88
|
-
return { name: place.name, sourceReference: place.reference, origin: 'SCXML written from the definition' };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Every located state, transition and action of a definition, as { kind,
|
|
93
|
-
* id, path, line, column } or { kind, id, reference, name, line, column },
|
|
94
|
-
* by line.
|
|
95
|
-
*/
|
|
96
|
-
elements(definition) {
|
|
97
|
-
const key = String(definition);
|
|
98
|
-
let list = this.#elements.get(key);
|
|
99
|
-
if (list !== undefined) return list;
|
|
100
|
-
list = this.#inFiles(definition);
|
|
101
|
-
if (list.length === 0) {
|
|
102
|
-
const virtual = this.#virtual(definition);
|
|
103
|
-
list = virtual === null ? [] : virtual.elements;
|
|
104
|
-
}
|
|
105
|
-
list.sort((left, right) => left.line - right.line || (left.column ?? 0) - (right.column ?? 0));
|
|
106
|
-
this.#elements.set(key, list);
|
|
107
|
-
return list;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The files a definition's spans name. */
|
|
111
|
-
files(definition) {
|
|
112
|
-
return [...new Set(this.elements(definition).filter((element) => element.path !== undefined)
|
|
113
|
-
.map((element) => element.path))];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** The DAP Source of a definition's virtual source, or undefined when it has none. */
|
|
117
|
-
virtualSource(definition) {
|
|
118
|
-
const first = this.elements(definition).find((element) => element.reference !== undefined);
|
|
119
|
-
return first === undefined ? undefined : Sources.source(first);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** The text of a virtual source, as the `source` request answers it, or undefined. */
|
|
123
|
-
content(reference) {
|
|
124
|
-
for (const virtual of this.#virtuals.values()) {
|
|
125
|
-
if (virtual !== null && virtual.reference === Number(reference)) {
|
|
126
|
-
return { content: virtual.text, mimeType: SCXML };
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return undefined;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* The element a line breakpoint in `source` (a DAP Source) at `line`
|
|
134
|
-
* stands for: the first element on that line, else the nearest one after
|
|
135
|
-
* it; an action before a transition before a state on the same line, as
|
|
136
|
-
* the innermost element there. Undefined when the source holds none of
|
|
137
|
-
* the definition's elements.
|
|
138
|
-
*/
|
|
139
|
-
locate(definition, source, line) {
|
|
140
|
-
const inFile = this.elements(definition).filter((element) => inSource(element, source));
|
|
141
|
-
const rank = { action: 0, transition: 1, state: 2 };
|
|
142
|
-
const byRank = (list) => [...list].sort((left, right) => rank[left.kind] - rank[right.kind]);
|
|
143
|
-
const on = inFile.filter((element) => element.line === line);
|
|
144
|
-
if (on.length > 0) return byRank(on)[0];
|
|
145
|
-
const after = inFile.filter((element) => element.line > line);
|
|
146
|
-
if (after.length === 0) return undefined;
|
|
147
|
-
const next = after[0].line;
|
|
148
|
-
return byRank(after.filter((element) => element.line === next))[0];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** The elements of `source` (a DAP Source) on the lines from `line` to `endLine`. */
|
|
152
|
-
lines(definition, source, line, endLine = line) {
|
|
153
|
-
return this.elements(definition).filter((element) => inSource(element, source) &&
|
|
154
|
-
element.line >= line && element.line <= endLine);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Forgets what was read of a definition. A chart defined again after it
|
|
159
|
-
* moved is answered by the same definition (core ADR 0077), which then
|
|
160
|
-
* says where the chart is written now (core ADR 0085).
|
|
161
|
-
*/
|
|
162
|
-
forget(definition) {
|
|
163
|
-
this.#elements.delete(String(definition));
|
|
164
|
-
this.#virtuals.delete(String(definition));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// The elements whose spans name a file.
|
|
168
|
-
#inFiles(definition) {
|
|
169
|
-
const facade = new Definition(this.#system, this.#native, definition);
|
|
170
|
-
const list = [];
|
|
171
|
-
for (const [kind, count] of this.#counts(definition)) {
|
|
172
|
-
for (let id = 0; id < count; id += 1) {
|
|
173
|
-
const found = facade.sourceOf(kind, id);
|
|
174
|
-
const path = pathOf(found?.source);
|
|
175
|
-
if (path !== undefined && found.line > 0) list.push({ kind, id, path, line: found.line, column: found.column });
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return list;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
#counts(definition) {
|
|
182
|
-
const counts = core.definition_inspect(this.#native, definition);
|
|
183
|
-
return [['state', counts.state_count], ['transition', counts.transition_count], ['action', counts.action_count]];
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// The chart written as SCXML, with its elements' places in that text, or
|
|
187
|
-
// null when SCXML cannot express it.
|
|
188
|
-
#virtual(definition) {
|
|
189
|
-
const key = String(definition);
|
|
190
|
-
if (this.#virtuals.has(key)) return this.#virtuals.get(key);
|
|
191
|
-
let encoded;
|
|
192
|
-
try {
|
|
193
|
-
encoded = this.#native.encodeDefinition(BigInt(definition));
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (!(error instanceof TinyactorsError && (error.code === 'unsupported' || error.code === 'not-found'))) throw error;
|
|
196
|
-
this.#virtuals.set(key, null);
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
let reference = this.#references.get(key);
|
|
200
|
-
if (reference === undefined) {
|
|
201
|
-
reference = this.#allocate();
|
|
202
|
-
this.#references.set(key, reference);
|
|
203
|
-
}
|
|
204
|
-
const family = core.definition_inspect(this.#native, definition).identity.family.name;
|
|
205
|
-
const name = `${family}.scxml`;
|
|
206
|
-
const elements = [];
|
|
207
|
-
for (const kind of KINDS) {
|
|
208
|
-
encoded.spans[kind].forEach((span, id) => {
|
|
209
|
-
if (span !== null) elements.push({ kind, id, reference, name, line: span.line, column: span.column });
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
const virtual = { reference, name, text: encoded.text, elements };
|
|
213
|
-
this.#virtuals.set(key, virtual);
|
|
214
|
-
return virtual;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
module.exports = { Sources };
|
package/lib/debug/variables.js
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Scopes and variables. Every frame of a thread shows the same seven
|
|
4
|
-
// scopes, because they belong to the actor, not to a frame:
|
|
5
|
-
//
|
|
6
|
-
// | Data model | the live data root, read again on each expansion; writable |
|
|
7
|
-
// | Event | _event of the step the actor is in, or of its last one |
|
|
8
|
-
// | Configuration | the active states as a tree, and what each history state recorded |
|
|
9
|
-
// | Queues | the internal queue, the macrostep's event, and the host mailbox |
|
|
10
|
-
// | Timers | pending delayed sends: event, target, when each is due |
|
|
11
|
-
// | Invocations | invocations, and the thread of an in-system child |
|
|
12
|
-
// | System | _sessionid, _name, the time, the definition, scheduling |
|
|
13
|
-
//
|
|
14
|
-
// A variablesReference is valid until the system runs again (reset()).
|
|
15
|
-
// Reading runs no code of the operator's; writing a Data model variable
|
|
16
|
-
// takes a JSON value (or an expression, with the `evaluate` permission).
|
|
17
|
-
|
|
18
|
-
const { core } = require('../native');
|
|
19
|
-
const { TinyactorsError } = require('../errors');
|
|
20
|
-
const { locationText, valueAt } = require('./expressions');
|
|
21
|
-
|
|
22
|
-
const SCOPES = ['Data model', 'Event', 'Configuration', 'Queues', 'Timers', 'Invocations', 'System'];
|
|
23
|
-
const PREVIEW = 60;
|
|
24
|
-
// The Event scope names _event's fields as SCXML spells them.
|
|
25
|
-
const SCXML_EVENT_NAMES = { sendID: 'sendid', originType: 'origintype' };
|
|
26
|
-
|
|
27
|
-
function typeOf(value) {
|
|
28
|
-
if (value === null) return 'null';
|
|
29
|
-
if (Array.isArray(value)) return 'array';
|
|
30
|
-
if (typeof value === 'function') return 'function';
|
|
31
|
-
return typeof value;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function preview(value) {
|
|
35
|
-
switch (typeof value) {
|
|
36
|
-
case 'undefined': return 'undefined';
|
|
37
|
-
case 'bigint': return `${value}n`;
|
|
38
|
-
case 'function': return `function ${value.name || '(anonymous)'}`;
|
|
39
|
-
case 'symbol': return value.toString();
|
|
40
|
-
default: break;
|
|
41
|
-
}
|
|
42
|
-
let text;
|
|
43
|
-
try {
|
|
44
|
-
text = JSON.stringify(value, (key, inner) => (typeof inner === 'bigint' ? `${inner}n` : inner));
|
|
45
|
-
} catch {
|
|
46
|
-
text = undefined;
|
|
47
|
-
}
|
|
48
|
-
if (text === undefined) text = String(value);
|
|
49
|
-
if (text.length <= PREVIEW) return text;
|
|
50
|
-
if (Array.isArray(value)) return `[…] ${value.length} items`;
|
|
51
|
-
if (value !== null && typeof value === 'object') return `{…} ${Object.keys(value).length} keys`;
|
|
52
|
-
return `${text.slice(0, PREVIEW)}…`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const container = (value) => value !== null && typeof value === 'object' && Object.keys(value).length > 0;
|
|
56
|
-
|
|
57
|
-
class Variables {
|
|
58
|
-
#refs = new Map();
|
|
59
|
-
#next = 0;
|
|
60
|
-
#host;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* `host` gives what the scopes read: `system`, `native`, `labels`,
|
|
64
|
-
* `thread(id)`, `eventOf(thread)`, `mailboxNote(thread, message)`,
|
|
65
|
-
* `threadOfInvocation(invocation)`.
|
|
66
|
-
*/
|
|
67
|
-
constructor(host) {
|
|
68
|
-
this.#host = host;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
reset() {
|
|
72
|
-
this.#refs.clear();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
#add(entry) {
|
|
76
|
-
this.#next += 1;
|
|
77
|
-
this.#refs.set(this.#next, entry);
|
|
78
|
-
return this.#next;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
#entry(reference) {
|
|
82
|
-
const entry = this.#refs.get(Number(reference));
|
|
83
|
-
if (entry === undefined) {
|
|
84
|
-
throw new TinyactorsError(`variablesReference ${reference} is not valid (any more)`, 'invalid');
|
|
85
|
-
}
|
|
86
|
-
return entry;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
scopes(thread) {
|
|
90
|
-
return SCOPES.map((name) => ({
|
|
91
|
-
name,
|
|
92
|
-
variablesReference: this.#add(name === 'Data model' ? { kind: 'data', thread: thread.id, path: [] }
|
|
93
|
-
: { kind: 'scope', thread: thread.id, name }),
|
|
94
|
-
expensive: false,
|
|
95
|
-
...(name === 'Data model' ? { presentationHint: 'locals' } : {}),
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** A value as evaluate answers it: text, and a reference when it has children. */
|
|
100
|
-
present(value, data) {
|
|
101
|
-
const body = { result: preview(value), type: typeOf(value), variablesReference: 0 };
|
|
102
|
-
if (container(value)) {
|
|
103
|
-
body.variablesReference = this.#add(data ?? { kind: 'value', value });
|
|
104
|
-
if (Array.isArray(value)) body.indexedVariables = value.length;
|
|
105
|
-
else body.namedVariables = Object.keys(value).length;
|
|
106
|
-
}
|
|
107
|
-
return body;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The children of a reference, paged by `start` and `count`. */
|
|
111
|
-
variables(reference, { start = 0, count = 0, filter } = {}) {
|
|
112
|
-
const entry = this.#entry(reference);
|
|
113
|
-
let children = this.#children(entry);
|
|
114
|
-
if (filter === 'indexed' || filter === 'named') {
|
|
115
|
-
children = children.filter((child) => (filter === 'indexed') === child.indexed);
|
|
116
|
-
}
|
|
117
|
-
children = children.slice(start);
|
|
118
|
-
if (count > 0) children = children.slice(0, count);
|
|
119
|
-
return children.map((child) => child.variable);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* The data-model location a Variables-pane variable names, as
|
|
124
|
-
* { thread, path }, or undefined for any other variable.
|
|
125
|
-
*/
|
|
126
|
-
location(reference, name) {
|
|
127
|
-
const entry = this.#refs.get(Number(reference));
|
|
128
|
-
if (entry === undefined || entry.kind !== 'data') return undefined;
|
|
129
|
-
const thread = this.#host.thread(entry.thread);
|
|
130
|
-
const parent = valueAt(this.#host.system.inspect(thread.actor).data, entry.path);
|
|
131
|
-
return { thread, path: [...entry.path, keyOf(parent, name)] };
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Writes `value` at a Data model variable; answers the DAP setVariable body. */
|
|
135
|
-
set(reference, name, value) {
|
|
136
|
-
const entry = this.#entry(reference);
|
|
137
|
-
if (entry.kind !== 'data') {
|
|
138
|
-
throw new TinyactorsError('only the Data model can be written; the other scopes are views of core state', 'invalid');
|
|
139
|
-
}
|
|
140
|
-
const thread = this.#host.thread(entry.thread);
|
|
141
|
-
const root = this.#host.system.inspect(thread.actor).data;
|
|
142
|
-
const parent = valueAt(root, entry.path);
|
|
143
|
-
if (parent === null || typeof parent !== 'object') {
|
|
144
|
-
throw new TinyactorsError(`${locationText(entry.path) || 'the data root'} is not an object any more`, 'invalid');
|
|
145
|
-
}
|
|
146
|
-
const key = keyOf(parent, name);
|
|
147
|
-
parent[key] = value;
|
|
148
|
-
const body = this.present(value, { kind: 'data', thread: entry.thread, path: [...entry.path, key] });
|
|
149
|
-
return { value: body.result, type: body.type, variablesReference: body.variablesReference,
|
|
150
|
-
indexedVariables: body.indexedVariables, namedVariables: body.namedVariables };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
#children(entry) {
|
|
154
|
-
switch (entry.kind) {
|
|
155
|
-
case 'data': {
|
|
156
|
-
const thread = this.#host.thread(entry.thread);
|
|
157
|
-
const value = valueAt(this.#host.system.inspect(thread.actor).data, entry.path);
|
|
158
|
-
return entries(value).map(([key, child, indexed]) => this.#variable(key, child, indexed, {
|
|
159
|
-
evaluateName: locationText([...entry.path, key]),
|
|
160
|
-
data: { kind: 'data', thread: entry.thread, path: [...entry.path, key] },
|
|
161
|
-
}));
|
|
162
|
-
}
|
|
163
|
-
case 'value':
|
|
164
|
-
return entries(entry.value).map(([key, child, indexed]) => this.#variable(key, child, indexed, {}));
|
|
165
|
-
default:
|
|
166
|
-
return this.#scope(this.#host.thread(entry.thread), entry.name)
|
|
167
|
-
.map(([key, child]) => this.#variable(key, child, false, {}));
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
#variable(key, value, indexed, { evaluateName, data }) {
|
|
172
|
-
const variable = { name: indexed ? `[${key}]` : String(key), value: preview(value), type: typeOf(value),
|
|
173
|
-
variablesReference: 0 };
|
|
174
|
-
if (evaluateName !== undefined) variable.evaluateName = evaluateName;
|
|
175
|
-
if (container(value)) {
|
|
176
|
-
variable.variablesReference = this.#add(data ?? { kind: 'value', value });
|
|
177
|
-
if (Array.isArray(value)) variable.indexedVariables = value.length;
|
|
178
|
-
else variable.namedVariables = Object.keys(value).length;
|
|
179
|
-
}
|
|
180
|
-
return { variable, indexed };
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// The read-only scopes, as [name, value] pairs.
|
|
184
|
-
#scope(thread, name) {
|
|
185
|
-
const { system } = this.#host;
|
|
186
|
-
const view = system.inspect(thread.actor);
|
|
187
|
-
switch (name) {
|
|
188
|
-
// _event's SCXML names, as the test host's adapter shows them.
|
|
189
|
-
case 'Event': return Object.entries(this.#host.eventOf(thread, view) ?? {})
|
|
190
|
-
.map(([key, value]) => [SCXML_EVENT_NAMES[key] ?? key, value]);
|
|
191
|
-
case 'Configuration': return this.#configuration(thread, view);
|
|
192
|
-
case 'Queues': return [
|
|
193
|
-
['internal', view.internalEvents],
|
|
194
|
-
['current', view.currentEvent],
|
|
195
|
-
['mailbox', system.mailbox(thread.actor).map((message) => this.#host.mailboxNote(message))],
|
|
196
|
-
];
|
|
197
|
-
case 'Timers': return view.delayedSends.map((send, index) => [send.id ?? `(no ID) ${index + 1}`, {
|
|
198
|
-
event: send.event, target: send.destination ?? send.target, due: send.due, in: `${send.due - system.time}ms`,
|
|
199
|
-
data: send.data, submitted: send.submitted,
|
|
200
|
-
}]);
|
|
201
|
-
case 'Invocations': return view.invocations.map((invocation) => {
|
|
202
|
-
const child = this.#host.threadOfInvocation(invocation);
|
|
203
|
-
return [this.#host.labels.invocation(invocation.id), { type: invocation.type, active: invocation.active,
|
|
204
|
-
autoforward: invocation.autoforward,
|
|
205
|
-
thread: child === undefined ? null : `${child.id} ${child.label}` }];
|
|
206
|
-
});
|
|
207
|
-
case 'System': return [
|
|
208
|
-
['_sessionid', String(view.sessionID)],
|
|
209
|
-
['_name', this.#host.documentName(view)],
|
|
210
|
-
['now', system.time],
|
|
211
|
-
['actor', `${view.actor.slot}:${view.actor.generation}`],
|
|
212
|
-
['definition', { ...view.definition, id: String(view.definitionID) }],
|
|
213
|
-
['residency', view.residency],
|
|
214
|
-
['scheduling', view.scheduling],
|
|
215
|
-
['published', view.published],
|
|
216
|
-
['allocatedBytes', view.allocatedBytes],
|
|
217
|
-
];
|
|
218
|
-
default: return [];
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
#configuration(thread, view) {
|
|
223
|
-
const { labels, native } = this.#host;
|
|
224
|
-
const definition = view.definitionID;
|
|
225
|
-
const states = labels.states(definition);
|
|
226
|
-
const tree = {};
|
|
227
|
-
for (const name of view.activeStates) {
|
|
228
|
-
const chain = [];
|
|
229
|
-
let at = states.find((state) => state.name === name);
|
|
230
|
-
while (at !== undefined) {
|
|
231
|
-
chain.unshift(at.name);
|
|
232
|
-
at = at.parent === null ? undefined : states[at.parent];
|
|
233
|
-
}
|
|
234
|
-
let node = tree;
|
|
235
|
-
for (const link of chain) {
|
|
236
|
-
node[link] ??= {};
|
|
237
|
-
node = node[link];
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
const pairs = [['active', tree]];
|
|
241
|
-
const history = {};
|
|
242
|
-
for (const state of states) {
|
|
243
|
-
if (state.kind.startsWith('history')) {
|
|
244
|
-
const recorded = core.actor_history(native, thread.actor.slot, thread.actor.generation, state.id);
|
|
245
|
-
history[state.name] = recorded.map((id) => labels.state(definition, id));
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
if (Object.keys(history).length > 0) pairs.push(['history', history]);
|
|
249
|
-
return pairs;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function entries(value) {
|
|
254
|
-
if (Array.isArray(value)) return value.map((child, index) => [index, child, true]);
|
|
255
|
-
if (value !== null && typeof value === 'object') return Object.entries(value).map(([key, child]) => [key, child, false]);
|
|
256
|
-
return [];
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// The key a variable's name stands for in its container: `[3]` of an array.
|
|
260
|
-
function keyOf(parent, name) {
|
|
261
|
-
const match = /^\[(\d+)\]$/.exec(name);
|
|
262
|
-
return Array.isArray(parent) && match !== null ? Number(match[1]) : name;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
module.exports = { Variables, preview, typeOf };
|