hclang 0.44.0 → 0.44.1
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/esm/cli/hc.js +4 -1
- package/esm/lib/bundle/hc-sources.js +469 -0
- package/esm/lib/frames/access-footprint.js +665 -0
- package/esm/lib/frames/bound-method.js +38 -0
- package/esm/lib/frames/dispatch-host.js +118 -0
- package/esm/lib/frames/effect-registry.js +21 -2
- package/esm/lib/frames/event-dispatch.js +192 -0
- package/esm/lib/frames/frame-array.js +41 -10
- package/esm/lib/frames/frame-expr.js +37 -6
- package/esm/lib/frames/frame-lazy.js +36 -4
- package/esm/lib/frames/frame-list.js +9 -2
- package/esm/lib/frames/frame.js +45 -6
- package/esm/lib/frames/reference-tracking.js +275 -0
- package/esm/lib/frames.js +6 -0
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import { Frame } from "./frame.js";
|
|
2
|
+
import { FrameArray } from "./frame-array.js";
|
|
3
|
+
import { FrameExpr, FrameScoped } from "./frame-expr.js";
|
|
4
|
+
import { FrameGroup } from "./frame-group.js";
|
|
5
|
+
import { FrameHandle } from "./frame-handle.js";
|
|
6
|
+
import { FrameLazy } from "./frame-lazy.js";
|
|
7
|
+
import { FrameSymbol } from "./frame-symbol.js";
|
|
8
|
+
import { BoundMethod } from "./bound-method.js";
|
|
9
|
+
import { touchesIdentity } from "./effect-marker.js";
|
|
10
|
+
import { effectSubscribersOf } from "./effect-registry.js";
|
|
11
|
+
import { AccessRecord, withAccessRecord } from "./reference-tracking.js";
|
|
12
|
+
/** Whether a footprint is unknown, so it must be assumed to touch everything. */
|
|
13
|
+
export const isOpaque = (footprint) => footprint.unresolved.length > 0;
|
|
14
|
+
/** Whether two footprints could interfere: a write in one meets the other. */
|
|
15
|
+
export function footprintsConflict(left, right) {
|
|
16
|
+
if (isOpaque(left) || isOpaque(right))
|
|
17
|
+
return true;
|
|
18
|
+
for (const frame of left.writes) {
|
|
19
|
+
if (right.writes.has(frame) || right.reads.has(frame))
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
for (const frame of right.writes) {
|
|
23
|
+
if (left.reads.has(frame))
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
/** The walk's accumulators and the guards that bound it. */
|
|
29
|
+
class Walk {
|
|
30
|
+
constructor() {
|
|
31
|
+
Object.defineProperty(this, "reads", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: new Set()
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(this, "writes", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true,
|
|
40
|
+
writable: true,
|
|
41
|
+
value: new Set()
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(this, "unresolved", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true,
|
|
46
|
+
writable: true,
|
|
47
|
+
value: []
|
|
48
|
+
});
|
|
49
|
+
/** Bodies already walked, by closure and receiver, so recursion ends. */
|
|
50
|
+
Object.defineProperty(this, "walked", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: new Set()
|
|
55
|
+
});
|
|
56
|
+
/**
|
|
57
|
+
* Frames the walk itself made to stand for the call (`scopeForCall`): the
|
|
58
|
+
* call frame the event will make afresh when it runs, never shared state.
|
|
59
|
+
*/
|
|
60
|
+
Object.defineProperty(this, "scratch", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true,
|
|
64
|
+
value: new AccessRecord()
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(this, "standIns", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
writable: true,
|
|
70
|
+
value: new Set()
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/** Whether a frame is state the event could share with another. */
|
|
74
|
+
shared(target) {
|
|
75
|
+
return isState(target) && !this.scratch.created.has(target);
|
|
76
|
+
}
|
|
77
|
+
/** Reads the identity and everything reachable from it. */
|
|
78
|
+
read(frame) {
|
|
79
|
+
const target = unwrap(frame);
|
|
80
|
+
if (!isState(target) || this.reads.has(target))
|
|
81
|
+
return;
|
|
82
|
+
if (this.scratch.created.has(target)) {
|
|
83
|
+
// A stand-in (a functional update's receiver copy) is not shared, but
|
|
84
|
+
// the members it shares with its original are.
|
|
85
|
+
if (this.standIns.has(target))
|
|
86
|
+
return;
|
|
87
|
+
this.standIns.add(target);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
this.reads.add(target);
|
|
91
|
+
}
|
|
92
|
+
for (const member of membersOf(target))
|
|
93
|
+
this.read(member);
|
|
94
|
+
}
|
|
95
|
+
/** Reads one binding of a frame: the frame, not what it holds. */
|
|
96
|
+
readBinding(frame) {
|
|
97
|
+
const target = unwrap(frame);
|
|
98
|
+
if (this.shared(target))
|
|
99
|
+
this.reads.add(target);
|
|
100
|
+
}
|
|
101
|
+
/** Writes one binding of a frame: the frame, not what it holds. */
|
|
102
|
+
writeBinding(frame) {
|
|
103
|
+
const target = unwrap(frame);
|
|
104
|
+
if (!this.shared(target))
|
|
105
|
+
return;
|
|
106
|
+
this.reads.add(target);
|
|
107
|
+
this.writes.add(target);
|
|
108
|
+
}
|
|
109
|
+
write(frame) {
|
|
110
|
+
const target = unwrap(frame);
|
|
111
|
+
if (!this.shared(target))
|
|
112
|
+
return;
|
|
113
|
+
this.read(target);
|
|
114
|
+
if (this.writes.has(target))
|
|
115
|
+
return;
|
|
116
|
+
this.writes.add(target);
|
|
117
|
+
// A marked key on a writable identity hands out a mutable handle on the
|
|
118
|
+
// member it holds, so a write may continue through it.
|
|
119
|
+
for (const [key, member] of target.meta_pairs()) {
|
|
120
|
+
if (touchesIdentity(key))
|
|
121
|
+
this.write(member);
|
|
122
|
+
}
|
|
123
|
+
// A committed write to a managed value invokes the callbacks an arrow
|
|
124
|
+
// `target -> callback` registered on it, so their effects are this
|
|
125
|
+
// event's: each is walked where it was written, as a closure argument is.
|
|
126
|
+
for (const callback of effectSubscribersOf(target)) {
|
|
127
|
+
walkCallable(this, callback, `${target.className()} ->`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
refuse(what) {
|
|
131
|
+
this.unresolved.push(what);
|
|
132
|
+
}
|
|
133
|
+
firstWalk(body, receiver) {
|
|
134
|
+
const key = `${body.id}|${receiver?.id ?? ""}`;
|
|
135
|
+
if (this.walked.has(key))
|
|
136
|
+
return false;
|
|
137
|
+
this.walked.add(key);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const fresh = { frames: [], mutable: false };
|
|
142
|
+
/**
|
|
143
|
+
* The potential footprint of calling `target` with `argument`.
|
|
144
|
+
*
|
|
145
|
+
* `target` is a `BoundMethod` or a `FrameLazy`; anything else has no code to
|
|
146
|
+
* walk and answers an opaque footprint naming the target.
|
|
147
|
+
*/
|
|
148
|
+
export function potentialFootprint(target, argument = Frame.nil) {
|
|
149
|
+
const walk = new Walk();
|
|
150
|
+
walk.read(argument);
|
|
151
|
+
const argumentMutable = argument instanceof FrameHandle &&
|
|
152
|
+
argument.isMutable();
|
|
153
|
+
if (target instanceof BoundMethod) {
|
|
154
|
+
const scope = withAccessRecord(walk.scratch, () => target.scopeForCall(argument));
|
|
155
|
+
if (scope instanceof Frame) {
|
|
156
|
+
// The call is refused before the body, so it touches nothing.
|
|
157
|
+
return finish(walk);
|
|
158
|
+
}
|
|
159
|
+
const receiver = scope.receiverState?.receiver ?? target.receiverFrame();
|
|
160
|
+
const receiverMutable = target.isMutating() && target.hasMutableReceiver();
|
|
161
|
+
if (receiverMutable)
|
|
162
|
+
walk.write(receiver);
|
|
163
|
+
else
|
|
164
|
+
walk.read(receiver);
|
|
165
|
+
walkBody(walk, target.methodBody(), {
|
|
166
|
+
frames: scope.lookupFrames(),
|
|
167
|
+
receiver,
|
|
168
|
+
receiverMutable,
|
|
169
|
+
isolated: scope.receiverState?.copyOnWrite !== undefined,
|
|
170
|
+
argument,
|
|
171
|
+
argumentMutable,
|
|
172
|
+
locals: new Map(),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else if (target instanceof FrameLazy) {
|
|
176
|
+
const scope = withAccessRecord(walk.scratch, () => target.scopeForCall(argument));
|
|
177
|
+
walkBody(walk, target, {
|
|
178
|
+
frames: scope.lookupFrames(),
|
|
179
|
+
receiverMutable: false,
|
|
180
|
+
isolated: false,
|
|
181
|
+
argument,
|
|
182
|
+
argumentMutable,
|
|
183
|
+
locals: new Map(),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
walk.refuse(`target ${target.className()}`);
|
|
188
|
+
}
|
|
189
|
+
return finish(walk);
|
|
190
|
+
}
|
|
191
|
+
const finish = (walk) => ({
|
|
192
|
+
reads: walk.reads,
|
|
193
|
+
writes: walk.writes,
|
|
194
|
+
unresolved: walk.unresolved,
|
|
195
|
+
});
|
|
196
|
+
/** A handle is transparent; a walk's compound atom is its term. */
|
|
197
|
+
function unwrap(frame) {
|
|
198
|
+
if (frame instanceof FrameHandle)
|
|
199
|
+
return frame.unwrap();
|
|
200
|
+
if (frame instanceof FrameScoped)
|
|
201
|
+
return unwrap(frame.term);
|
|
202
|
+
return frame;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Whether a frame is state an event could share: an aggregate, which owns
|
|
206
|
+
* identity, or a plain scope frame, which holds bindings a `@name` can write.
|
|
207
|
+
* Atoms, closures and parsed expressions are values or code, never state.
|
|
208
|
+
* The dispatcher applies the same test to an event's actual accesses, so a
|
|
209
|
+
* write to a plain scope is as visible after the fact as one to an aggregate.
|
|
210
|
+
*/
|
|
211
|
+
export function isState(frame) {
|
|
212
|
+
if (frame.is.missing || frame.is.void)
|
|
213
|
+
return false;
|
|
214
|
+
return frame.ownsIdentity() || frame.className() === "Frame";
|
|
215
|
+
}
|
|
216
|
+
/** The identities a frame holds: its bindings, elements, and declared parent. */
|
|
217
|
+
function membersOf(frame) {
|
|
218
|
+
const members = frame.meta_pairs().map(([, value]) => value);
|
|
219
|
+
if (frame instanceof FrameArray)
|
|
220
|
+
members.push(...frame.asArray());
|
|
221
|
+
if (frame.hasDeclaredParent())
|
|
222
|
+
members.push(frame.parent);
|
|
223
|
+
return members;
|
|
224
|
+
}
|
|
225
|
+
/** Walks a closure body once per receiver, each statement from a fresh path. */
|
|
226
|
+
function walkBody(walk, body, site) {
|
|
227
|
+
if (!walk.firstWalk(body, site.receiver))
|
|
228
|
+
return;
|
|
229
|
+
for (const term of body.asArray())
|
|
230
|
+
walkStatement(walk, term, site);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Walks one statement. A declaration `.k v` records `k` as a local denoting
|
|
234
|
+
* what `v` denotes and, when `k` is marked, makes those referents writable:
|
|
235
|
+
* the new name is a mutable handle on them.
|
|
236
|
+
*/
|
|
237
|
+
function walkStatement(walk, term, site) {
|
|
238
|
+
const items = term instanceof FrameExpr && term.isPlainExpression()
|
|
239
|
+
? term.asArray()
|
|
240
|
+
: [term];
|
|
241
|
+
const head = items[0] === undefined ? undefined : plainAtom(items[0]);
|
|
242
|
+
if (items.length >= 2 && head?.isName() && isDeclaration(head.source)) {
|
|
243
|
+
const referents = walkPath(walk, items.slice(1), site);
|
|
244
|
+
const mutable = touchesIdentity(head.source);
|
|
245
|
+
if (mutable) {
|
|
246
|
+
for (const referent of referents?.frames ?? [])
|
|
247
|
+
walk.write(referent);
|
|
248
|
+
if (referents === undefined)
|
|
249
|
+
walk.refuse(`.${head.source} <unknown>`);
|
|
250
|
+
}
|
|
251
|
+
site.locals.set(stripMarker(head.source), referents && { frames: referents.frames, mutable });
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
walkPath(walk, items, site);
|
|
255
|
+
}
|
|
256
|
+
const isDeclaration = (source) => !/^\.*$/.test(source) && source !== "^";
|
|
257
|
+
const stripMarker = (key) => touchesIdentity(key) ? key.slice(0, -1) : key;
|
|
258
|
+
/**
|
|
259
|
+
* Walks the terms of one expression left to right, carrying what the path so
|
|
260
|
+
* far denotes, as evaluation carries its accumulator. Answers what the whole
|
|
261
|
+
* path denotes, or undefined when that is not known.
|
|
262
|
+
*/
|
|
263
|
+
function walkPath(walk, terms, site) {
|
|
264
|
+
let current = fresh;
|
|
265
|
+
// Once a term has been refused, the path is already opaque; later names
|
|
266
|
+
// applied to the unknown value add nothing.
|
|
267
|
+
let refused = false;
|
|
268
|
+
const refuse = (what) => {
|
|
269
|
+
if (!refused)
|
|
270
|
+
walk.refuse(what);
|
|
271
|
+
refused = true;
|
|
272
|
+
current = undefined;
|
|
273
|
+
};
|
|
274
|
+
for (let index = 0; index < terms.length; index++) {
|
|
275
|
+
const term = plainAtom(terms[index]);
|
|
276
|
+
const next = terms[index + 1] === undefined
|
|
277
|
+
? undefined
|
|
278
|
+
: plainAtom(terms[index + 1]);
|
|
279
|
+
// A mutable referent hands out the identity itself. When the next term
|
|
280
|
+
// reads a member through it, that member's own spelling decides what is
|
|
281
|
+
// handed out next; otherwise the handle is used or handed on whole, so
|
|
282
|
+
// the referent may be written.
|
|
283
|
+
const handedOn = !(next !== undefined && next.isName());
|
|
284
|
+
if (term.is.operator === true) {
|
|
285
|
+
// An arrow `managed -> callback` registers on its left operand's
|
|
286
|
+
// referent, which changes what every later write of it invokes, so it
|
|
287
|
+
// is a write of that referent, whatever the handle's authority.
|
|
288
|
+
if (isArrow(term) && current !== undefined) {
|
|
289
|
+
for (const frame of current.frames)
|
|
290
|
+
walk.write(frame);
|
|
291
|
+
}
|
|
292
|
+
// An operator combines its operands into a value no name holds.
|
|
293
|
+
current = refused ? undefined : fresh;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (term.isName()) {
|
|
297
|
+
if (current === undefined) {
|
|
298
|
+
if (touchesIdentity(term.source))
|
|
299
|
+
refuse(`.${term.source} <unknown>`);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
current = walkName(walk, term.source, current, handedOn, site);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (term.className() === "FrameAlias") {
|
|
306
|
+
current = walkAlias(walk, term.toString().slice(1), site);
|
|
307
|
+
if (current === undefined)
|
|
308
|
+
refused = true;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (term instanceof FrameSymbol) {
|
|
312
|
+
current = walkSymbol(walk, term, site, handedOn);
|
|
313
|
+
if (current === undefined)
|
|
314
|
+
refused = true;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (term instanceof FrameLazy) {
|
|
318
|
+
// A closure literal in this body runs in this body's scope when called.
|
|
319
|
+
walkBody(walk, term, { ...site, locals: new Map(site.locals) });
|
|
320
|
+
current = fresh;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
if (term instanceof FrameGroup) {
|
|
324
|
+
// `(e)` is the path `e`; `(a, b)` is a fresh tuple of walked values.
|
|
325
|
+
const inner = term.asArray();
|
|
326
|
+
if (inner.length === 1) {
|
|
327
|
+
const only = plainAtom(inner[0]);
|
|
328
|
+
current = walkPath(walk, only instanceof FrameExpr && only.isPlainExpression()
|
|
329
|
+
? only.asArray()
|
|
330
|
+
: [only], site);
|
|
331
|
+
if (current === undefined)
|
|
332
|
+
refused = true;
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
for (const each of inner)
|
|
336
|
+
walkStatement(walk, each, site);
|
|
337
|
+
current = fresh;
|
|
338
|
+
}
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (term instanceof FrameExpr && term.isPlainExpression()) {
|
|
342
|
+
// A tight expression, terms that abut, is one path of its own.
|
|
343
|
+
current = walkPath(walk, term.asArray(), site);
|
|
344
|
+
if (current === undefined)
|
|
345
|
+
refused = true;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (term instanceof FrameArray || term instanceof FrameExpr) {
|
|
349
|
+
// An aggregate literal or a nested expression: its terms are read; the
|
|
350
|
+
// value is fresh.
|
|
351
|
+
for (const inner of term.asArray())
|
|
352
|
+
walkStatement(walk, inner, site);
|
|
353
|
+
current = fresh;
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (isOpaqueTerm(term)) {
|
|
357
|
+
refuse(term.className());
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
// An atom (a count, a string, an echo): a value, not a path.
|
|
361
|
+
current = fresh;
|
|
362
|
+
}
|
|
363
|
+
return current;
|
|
364
|
+
}
|
|
365
|
+
/** Whether an operator term is the effect arrow `->` (`registerEffect`). */
|
|
366
|
+
const isArrow = (term) => term.operator === "->";
|
|
367
|
+
/** A term through the parser's one-term wrappers and a walk's scope wrapper. */
|
|
368
|
+
function plainAtom(term) {
|
|
369
|
+
let atom = term instanceof FrameScoped ? term.term : term;
|
|
370
|
+
while (atom instanceof FrameExpr && atom.isPlainExpression() && atom.size() === 1) {
|
|
371
|
+
atom = atom.asArray()[0];
|
|
372
|
+
if (atom instanceof FrameScoped)
|
|
373
|
+
atom = atom.term;
|
|
374
|
+
}
|
|
375
|
+
return atom;
|
|
376
|
+
}
|
|
377
|
+
/** Terms whose effect cannot be read off the code: they run or reach outside. */
|
|
378
|
+
const isOpaqueTerm = (term) => {
|
|
379
|
+
const name = term.className();
|
|
380
|
+
return name === "FrameImport" || name === "FrameResource" ||
|
|
381
|
+
name === "FrameURI";
|
|
382
|
+
};
|
|
383
|
+
/** A bare name: the argument, the receiver, a local, or a lookup. */
|
|
384
|
+
function walkSymbol(walk, term, site, handedOn) {
|
|
385
|
+
const spelling = term.spelling();
|
|
386
|
+
const name = term.className();
|
|
387
|
+
if (name === "FrameArg" || name === "FrameParam") {
|
|
388
|
+
if (spelling === "_") {
|
|
389
|
+
walkCallable(walk, site.argument, spelling);
|
|
390
|
+
return { frames: [site.argument], mutable: site.argumentMutable };
|
|
391
|
+
}
|
|
392
|
+
if (spelling === "_^" && site.receiver) {
|
|
393
|
+
return { frames: [site.receiver], mutable: site.receiverMutable };
|
|
394
|
+
}
|
|
395
|
+
walk.refuse(spelling);
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
const local = stripMarker(spelling);
|
|
399
|
+
if (site.locals.has(local)) {
|
|
400
|
+
const referents = site.locals.get(local);
|
|
401
|
+
if (referents === undefined) {
|
|
402
|
+
walk.refuse(spelling);
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
const mutable = referents.mutable && touchesIdentity(spelling) &&
|
|
406
|
+
!site.isolated;
|
|
407
|
+
if (mutable && handedOn) {
|
|
408
|
+
for (const referent of referents.frames)
|
|
409
|
+
walk.write(referent);
|
|
410
|
+
}
|
|
411
|
+
return { frames: referents.frames, mutable };
|
|
412
|
+
}
|
|
413
|
+
const found = resolveName(walk, spelling, site.frames);
|
|
414
|
+
if (!found) {
|
|
415
|
+
walk.refuse(spelling);
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
return walkReferent(walk, spelling, found, site, handedOn, true);
|
|
419
|
+
}
|
|
420
|
+
/** A dotted name applied to the path so far: a member, a parent, or a method. */
|
|
421
|
+
function walkName(walk, source, current, handedOn, site) {
|
|
422
|
+
if (/^\.*$/.test(source)) {
|
|
423
|
+
// The parameter ladder reads the call's own parameters, not state.
|
|
424
|
+
return fresh;
|
|
425
|
+
}
|
|
426
|
+
if (source === "^") {
|
|
427
|
+
if (current.frames.length === 0 && site.receiver) {
|
|
428
|
+
// Method-position `.^` re-parents the receiver.
|
|
429
|
+
if (site.receiverMutable)
|
|
430
|
+
walk.write(site.receiver);
|
|
431
|
+
return fresh;
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
frames: current.frames.flatMap((frame) => frame.hasDeclaredParent() ? [frame.parent] : []),
|
|
435
|
+
mutable: current.mutable,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const frames = [];
|
|
439
|
+
let mutable = false;
|
|
440
|
+
for (const frame of current.frames) {
|
|
441
|
+
const member = memberOf(frame, source);
|
|
442
|
+
if (!member) {
|
|
443
|
+
// A pattern binding answers a name no key spells, and rewrites the
|
|
444
|
+
// matched value's source when it does (`MetaFrame.match_here`): the
|
|
445
|
+
// walk does not follow it, so the event is opaque.
|
|
446
|
+
if (patternAnswers(frame, source))
|
|
447
|
+
walk.refuse(`/pattern/ ${source}`);
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const receiver = unwrap(frame);
|
|
451
|
+
const referents = walkReferent(walk, source, { owner: receiver, value: member }, { ...site, receiver, receiverMutable: current.mutable }, handedOn, current.mutable);
|
|
452
|
+
frames.push(...referents.frames);
|
|
453
|
+
mutable ||= referents.mutable;
|
|
454
|
+
}
|
|
455
|
+
return { frames, mutable };
|
|
456
|
+
}
|
|
457
|
+
/** `@name`: a write to the receiver's or the lexical owner's binding. */
|
|
458
|
+
function walkAlias(walk, key, site) {
|
|
459
|
+
if (site.locals.has(key))
|
|
460
|
+
return site.locals.get(key) ?? fresh;
|
|
461
|
+
if (site.receiver) {
|
|
462
|
+
const onReceiver = resolveName(walk, key, [site.receiver]);
|
|
463
|
+
if (onReceiver) {
|
|
464
|
+
// The write lands on the receiver, or on its functional copy.
|
|
465
|
+
if (site.receiverMutable && !site.isolated) {
|
|
466
|
+
walk.writeBinding(onReceiver.owner);
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
walk.readBinding(onReceiver.owner);
|
|
470
|
+
}
|
|
471
|
+
return { frames: [onReceiver.value], mutable: false };
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const lexical = resolveName(walk, key, site.frames.filter((frame) => frame !== site.argument && unwrap(frame) !== site.receiver));
|
|
475
|
+
if (!lexical) {
|
|
476
|
+
walk.refuse(`@${key}`);
|
|
477
|
+
return undefined;
|
|
478
|
+
}
|
|
479
|
+
// Inside a functional update a lexical write meets the copy-on-write
|
|
480
|
+
// boundary and is refused, so it writes nothing.
|
|
481
|
+
if (site.isolated)
|
|
482
|
+
walk.readBinding(lexical.owner);
|
|
483
|
+
else
|
|
484
|
+
walk.writeBinding(lexical.owner);
|
|
485
|
+
return { frames: [lexical.value], mutable: false };
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* What a resolved name contributes. Reading the binding reads its owner. A
|
|
489
|
+
* marked name through a path that holds authority makes its referent writable
|
|
490
|
+
* when it is handed on whole. A closure found by name is code that runs in
|
|
491
|
+
* this event, so its body is walked too: as a method of the receiver when
|
|
492
|
+
* found there, with the authority the path holds, otherwise where it was
|
|
493
|
+
* written.
|
|
494
|
+
*/
|
|
495
|
+
function walkReferent(walk, spelling, found, site, handedOn, pathMutable) {
|
|
496
|
+
walk.readBinding(found.owner);
|
|
497
|
+
const value = unwrap(found.value);
|
|
498
|
+
const marked = touchesIdentity(spelling);
|
|
499
|
+
if (value instanceof FrameLazy) {
|
|
500
|
+
const onReceiver = site.receiver !== undefined &&
|
|
501
|
+
isOnReceiverPath(site.receiver, found.owner);
|
|
502
|
+
// A mutating method writes its receiver only through authority: a bare
|
|
503
|
+
// sibling mutator in a functional call stays functional (class-support).
|
|
504
|
+
const writes = marked && onReceiver && site.receiverMutable &&
|
|
505
|
+
pathMutable && !site.isolated;
|
|
506
|
+
if (writes)
|
|
507
|
+
walk.write(site.receiver);
|
|
508
|
+
const receiver = onReceiver ? site.receiver : undefined;
|
|
509
|
+
walkBody(walk, value, {
|
|
510
|
+
frames: receiver
|
|
511
|
+
? [receiver, ...lexicalChain(value)]
|
|
512
|
+
: lexicalChain(value),
|
|
513
|
+
receiver,
|
|
514
|
+
receiverMutable: writes,
|
|
515
|
+
// A mutating method reached without authority is a functional update.
|
|
516
|
+
isolated: site.isolated || (marked && !writes),
|
|
517
|
+
argument: Frame.nil,
|
|
518
|
+
argumentMutable: false,
|
|
519
|
+
locals: new Map(),
|
|
520
|
+
});
|
|
521
|
+
return fresh;
|
|
522
|
+
}
|
|
523
|
+
if (value instanceof BoundMethod) {
|
|
524
|
+
// A method already bound to a receiver the walk cannot see from here.
|
|
525
|
+
walk.refuse(`${spelling} ${value.className()}`);
|
|
526
|
+
return fresh;
|
|
527
|
+
}
|
|
528
|
+
// An element index hands out the element itself; a marked key hands out a
|
|
529
|
+
// mutable handle; an unmarked key an immutable one. Inside a functional
|
|
530
|
+
// update every write is forked or refused, so nothing shared is written.
|
|
531
|
+
const mutable = (Frame.isIndexKey(spelling) || marked) && !site.isolated;
|
|
532
|
+
if (mutable && handedOn)
|
|
533
|
+
walk.write(value);
|
|
534
|
+
else
|
|
535
|
+
walk.read(value);
|
|
536
|
+
return { frames: [value], mutable };
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* An argument that is code may be called by the body (`.apply {_ ()}`), so
|
|
540
|
+
* its effects are the event's: a closure's body is walked where it was
|
|
541
|
+
* written, and a bound method, whose receiver the walk cannot see from here,
|
|
542
|
+
* makes the event opaque. An argument that is data contributes nothing.
|
|
543
|
+
*/
|
|
544
|
+
function walkCallable(walk, argument, spelling) {
|
|
545
|
+
const value = unwrap(argument);
|
|
546
|
+
if (value instanceof FrameLazy) {
|
|
547
|
+
walkBody(walk, value, {
|
|
548
|
+
frames: lexicalChain(value),
|
|
549
|
+
receiverMutable: false,
|
|
550
|
+
isolated: false,
|
|
551
|
+
argument: Frame.nil,
|
|
552
|
+
argumentMutable: false,
|
|
553
|
+
locals: new Map(),
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
else if (value instanceof BoundMethod) {
|
|
557
|
+
walk.refuse(`${spelling} ${value.className()}`);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/** Whether `owner` is the receiver itself or one of its declared parents. */
|
|
561
|
+
function isOnReceiverPath(receiver, owner) {
|
|
562
|
+
const seen = new Set();
|
|
563
|
+
let current = unwrap(receiver);
|
|
564
|
+
while (current && !current.is.missing && !seen.has(current)) {
|
|
565
|
+
if (current === owner)
|
|
566
|
+
return true;
|
|
567
|
+
seen.add(current);
|
|
568
|
+
current = current.hasDeclaredParent() ? current.parent : undefined;
|
|
569
|
+
}
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
/** The frames a closure found by name resolves its own body's names in. */
|
|
573
|
+
function lexicalChain(closure) {
|
|
574
|
+
const captured = closure.capturedScope();
|
|
575
|
+
if (captured)
|
|
576
|
+
return [...captured.lookupFrames()];
|
|
577
|
+
return closure.up && !closure.up.is.missing ? [closure.up] : [];
|
|
578
|
+
}
|
|
579
|
+
/** The member `frame` holds under `key`: an element by index, else a field. */
|
|
580
|
+
function memberOf(frame, key) {
|
|
581
|
+
const target = unwrap(frame);
|
|
582
|
+
if (Frame.isIndexKey(key)) {
|
|
583
|
+
if (!(target instanceof FrameArray))
|
|
584
|
+
return undefined;
|
|
585
|
+
const element = target.at(Number(key));
|
|
586
|
+
return element.is.missing || element.is.error ? undefined : element;
|
|
587
|
+
}
|
|
588
|
+
const seen = new Set();
|
|
589
|
+
let current = target;
|
|
590
|
+
while (current && !current.is.missing && !seen.has(current)) {
|
|
591
|
+
seen.add(current);
|
|
592
|
+
const binding = current.resolve_here(key, target);
|
|
593
|
+
if (binding && !binding.value.is.error)
|
|
594
|
+
return binding.value;
|
|
595
|
+
current = current.hasDeclaredParent() ? current.parent : undefined;
|
|
596
|
+
}
|
|
597
|
+
return undefined;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Whether a pattern key (`/re/`) on `frame` or a declared parent matches
|
|
601
|
+
* `key`. Such a binding has no HC spelling; a host can install one.
|
|
602
|
+
*/
|
|
603
|
+
function patternAnswers(frame, key) {
|
|
604
|
+
const seen = new Set();
|
|
605
|
+
let current = unwrap(frame);
|
|
606
|
+
while (current && !current.is.missing && !seen.has(current)) {
|
|
607
|
+
seen.add(current);
|
|
608
|
+
for (const pattern of current.meta_keys()) {
|
|
609
|
+
const body = pattern.match(/\/(.*)\//);
|
|
610
|
+
if (body && new RegExp(body[1]).test(key))
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
current = current.hasDeclaredParent() ? current.parent : undefined;
|
|
614
|
+
}
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Resolves a bare name the way a call's lookup would: each lookup frame in
|
|
619
|
+
* order, then its declared parents, then the lexical `up` chain, and the
|
|
620
|
+
* globals last. A handle resolves on its target and parents only (#400).
|
|
621
|
+
*
|
|
622
|
+
* Every frame consulted is read, the ones that miss included: a miss is that
|
|
623
|
+
* frame's answer that the name is absent there, and a binding added to it
|
|
624
|
+
* would change where the name resolves.
|
|
625
|
+
*/
|
|
626
|
+
function resolveName(walk, key, frames) {
|
|
627
|
+
const seen = new Set();
|
|
628
|
+
for (const frame of frames) {
|
|
629
|
+
const found = resolveThrough(walk, key, frame, seen, !(frame instanceof FrameHandle));
|
|
630
|
+
if (found)
|
|
631
|
+
return found;
|
|
632
|
+
}
|
|
633
|
+
const globals = Frame.globals;
|
|
634
|
+
if (globals && !globals.is.missing && !seen.has(globals)) {
|
|
635
|
+
walk.readBinding(globals);
|
|
636
|
+
const binding = globals.resolve_here(key, globals);
|
|
637
|
+
if (binding && !binding.value.is.error) {
|
|
638
|
+
return { owner: globals, value: binding.value };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return undefined;
|
|
642
|
+
}
|
|
643
|
+
function resolveThrough(walk, key, frame, seen, followLexical) {
|
|
644
|
+
const target = unwrap(frame);
|
|
645
|
+
if (target.is.missing || seen.has(target))
|
|
646
|
+
return undefined;
|
|
647
|
+
seen.add(target);
|
|
648
|
+
walk.readBinding(target);
|
|
649
|
+
const binding = target.resolve_here(key, target);
|
|
650
|
+
if (binding) {
|
|
651
|
+
return binding.value.is.error
|
|
652
|
+
? undefined
|
|
653
|
+
: { owner: target, value: binding.value };
|
|
654
|
+
}
|
|
655
|
+
if (target.hasDeclaredParent()) {
|
|
656
|
+
const inherited = resolveThrough(walk, key, target.parent, seen, false);
|
|
657
|
+
if (inherited)
|
|
658
|
+
return inherited;
|
|
659
|
+
}
|
|
660
|
+
if (followLexical && target.up && !target.up.is.missing &&
|
|
661
|
+
target.up !== Frame.globals) {
|
|
662
|
+
return resolveThrough(walk, key, target.up, seen, true);
|
|
663
|
+
}
|
|
664
|
+
return undefined;
|
|
665
|
+
}
|