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.
@@ -1,6 +1,7 @@
1
1
  import { NilContext } from "./context.js";
2
2
  import { renderNested } from "./stringify.js";
3
3
  import { hasEffectSubscribers, notifyEffect } from "./effect-registry.js";
4
+ import { guardAccess, noteCopiedHoldings, noteCreated, noteHolding, noteRead, noteWrite, PARENT_KEY, } from "./reference-tracking.js";
4
5
  /** Visibility levels for logical frame bindings. */
5
6
  export var Visibility;
6
7
  (function (Visibility) {
@@ -101,15 +102,22 @@ export class MetaFrame {
101
102
  const name = this.constructor.name;
102
103
  const id = name + "." + MetaFrame.id_count++;
103
104
  this.id = "$:" + id;
105
+ noteCreated(this);
104
106
  }
105
107
  /**
106
108
  * get_here retrieves a Frame by key from the current context.
107
109
  */
108
110
  get_here(key, _origin = this) {
111
+ // A keyed lookup is a read of this frame's state, a miss included: the
112
+ // miss is this frame's answer that the key is absent, and it decides
113
+ // whether the lookup falls through or matches a pattern. Guarded before
114
+ // anything is resolved, so a refusal sees and changes nothing
115
+ // (reference-tracking.ts).
116
+ guardAccess(this, "read");
117
+ noteRead(this);
109
118
  const exact = this.resolve_here(key, _origin);
110
- if (exact != null) {
119
+ if (exact != null)
111
120
  return exact.value;
112
- }
113
121
  return this.match_here(key);
114
122
  }
115
123
  /** Lets a projection perform another frame's local lookup without its links. */
@@ -268,9 +276,16 @@ export class MetaFrame {
268
276
  this.meta = {};
269
277
  }
270
278
  const previous = this.meta[key];
279
+ // Refused before the write lands, so a refusal changes nothing.
280
+ if (previous !== value)
281
+ guardAccess(this, "write");
271
282
  this.meta[key] = value;
272
- if (previous !== value && hasEffectSubscribers(this)) {
273
- notifyEffect(this);
283
+ noteHolding(this, key, value, previous);
284
+ if (previous !== value) {
285
+ noteWrite(this);
286
+ if (hasEffectSubscribers(this)) {
287
+ notifyEffect(this);
288
+ }
274
289
  }
275
290
  return this;
276
291
  }
@@ -287,7 +302,13 @@ export class MetaFrame {
287
302
  if (this.wouldCreateParentCycle(parent)) {
288
303
  return Frame.error("$!.cyclic-parent .^");
289
304
  }
305
+ guardAccess(this, "write");
306
+ const previous = this.parent;
290
307
  this.parent = parent;
308
+ // The parent edge is a holding too (reference-tracking.ts): it is how a
309
+ // name reaches the parent, though it conveys no authority of its own.
310
+ noteHolding(this, PARENT_KEY, parent, previous && !previous.is.missing ? previous : undefined);
311
+ noteWrite(this);
291
312
  return undefined;
292
313
  }
293
314
  /** Whether assigning parent would attach this frame to a cyclic chain. */
@@ -342,6 +363,10 @@ export class MetaFrame {
342
363
  }
343
364
  /**
344
365
  * match_here checks if a target string matches any key in the current context.
366
+ *
367
+ * A match rewrites the bound value's `source` to the target, which changes
368
+ * what every later reader of that binding sees, so it is a write of this
369
+ * frame: guarded before the value is touched, and noted after.
345
370
  */
346
371
  match_here(target) {
347
372
  let result = Frame.missing;
@@ -352,8 +377,10 @@ export class MetaFrame {
352
377
  if (pattern.test(target)) {
353
378
  result = value;
354
379
  if ("source" in result) {
380
+ guardAccess(this, "write");
355
381
  const sourced = result;
356
382
  sourced.source = target;
383
+ noteWrite(this);
357
384
  }
358
385
  }
359
386
  }
@@ -994,6 +1021,12 @@ export class Frame extends MetaFrame {
994
1021
  clone.meta = this.meta_copy();
995
1022
  clone.is = { ...this.is };
996
1023
  clone.id = `$:${this.className()}.${MetaFrame.id_count++}`;
1024
+ // `Object.create` bypasses the constructor, so the clone registers its
1025
+ // construction and its duplicated holdings here (reference-tracking.ts).
1026
+ noteCreated(clone);
1027
+ if (clone.meta !== NilContext) {
1028
+ noteCopiedHoldings(clone);
1029
+ }
997
1030
  return clone;
998
1031
  }
999
1032
  /**
@@ -1015,14 +1048,20 @@ export class Frame extends MetaFrame {
1015
1048
  let replaced = false;
1016
1049
  for (const [key, value] of this.meta_pairs()) {
1017
1050
  if (value === member) {
1051
+ if (!replaced)
1052
+ guardAccess(this, "write");
1018
1053
  this.meta[key] = replacement;
1054
+ noteHolding(this, key, replacement, member);
1019
1055
  replaced = true;
1020
1056
  }
1021
1057
  }
1022
1058
  // An element-plane commit is a mutation too; a copy-on-write install lands
1023
1059
  // on a fresh copy that carries no subscribers, so it never fires here.
1024
- if (replaced && hasEffectSubscribers(this)) {
1025
- notifyEffect(this);
1060
+ if (replaced) {
1061
+ noteWrite(this);
1062
+ if (hasEffectSubscribers(this)) {
1063
+ notifyEffect(this);
1064
+ }
1026
1065
  }
1027
1066
  return replaced;
1028
1067
  }
@@ -0,0 +1,275 @@
1
+ import { touchesIdentity } from "./effect-marker.js";
2
+ /** The key a declared parent is held under. */
3
+ export const PARENT_KEY = "^";
4
+ const isIndexKey = (key) => /^\d+$/.test(key);
5
+ const kindOf = (key) => key === PARENT_KEY ? "parent" : isIndexKey(key) ? "element" : "member";
6
+ /** Whether a key, by its spelling alone, hands out mutation authority. */
7
+ export const keyGrantsMutation = (key) => key !== PARENT_KEY && (isIndexKey(key) || touchesIdentity(key));
8
+ /** Identity → the bindings that hold it. Keyed weakly by the identity. */
9
+ const holdings = new WeakMap();
10
+ /** Identity → the number of mutations committed on it. */
11
+ const versions = new WeakMap();
12
+ /**
13
+ * What one event actually touched while its record was active.
14
+ *
15
+ * `reads` maps each frame read to the version it was first observed at;
16
+ * `writes` maps each frame written to the version its last commit produced.
17
+ * `created` holds the frames constructed while the record was active, so a
18
+ * consumer can tell an event's own fresh state (a functional-update copy, a
19
+ * call frame) from shared state it reached.
20
+ */
21
+ export class AccessRecord {
22
+ constructor() {
23
+ Object.defineProperty(this, "reads", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: new Map()
28
+ });
29
+ Object.defineProperty(this, "writes", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: new Map()
34
+ });
35
+ Object.defineProperty(this, "created", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: new WeakSet()
40
+ });
41
+ /**
42
+ * The certificate this record's accesses are confined to, if any. While a
43
+ * guard is installed, an access to a shared frame the guard does not allow
44
+ * is refused before it happens (`guardAccess`), so the record can never
45
+ * show an access outside what its guard admits.
46
+ */
47
+ Object.defineProperty(this, "guard", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ }
54
+ /** The frames this record saw written that it did not itself create. */
55
+ sharedWrites() {
56
+ return [...this.writes.keys()].filter((frame) => !this.created.has(frame));
57
+ }
58
+ /** The frames this record saw read that it did not itself create. */
59
+ sharedReads() {
60
+ return [...this.reads.keys()].filter((frame) => !this.created.has(frame));
61
+ }
62
+ }
63
+ /** The records currently receiving access notes, innermost last. */
64
+ const active = [];
65
+ /**
66
+ * An access a guard refused. It is thrown from the commit or read point
67
+ * *before* the access happens, so the state is as it was. It carries the
68
+ * record whose guard refused, so that only the dispatcher that installed that
69
+ * guard handles it: when dispatches nest, an inner dispatcher rethrows a
70
+ * refusal that belongs to an outer event, and the outer event ends there.
71
+ */
72
+ export class UncertifiedAccess extends Error {
73
+ constructor(frame, access, record) {
74
+ super(`$!.uncertified-${access} ${frame.className()}`);
75
+ Object.defineProperty(this, "frame", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: frame
80
+ });
81
+ Object.defineProperty(this, "access", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: access
86
+ });
87
+ Object.defineProperty(this, "record", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: record
92
+ });
93
+ this.name = "UncertifiedAccess";
94
+ }
95
+ }
96
+ /**
97
+ * Refuses `access` to `frame` if any active record's guard does not allow
98
+ * it, naming the outermost such record. Called at every commit point before
99
+ * the mutation lands and at every keyed or indexed lookup (hit or miss)
100
+ * before the value is handed out, so a refusal leaves the state untouched and
101
+ * the value unseen. Costs one length check when no record is active and one
102
+ * `WeakSet` lookup per guarded record otherwise.
103
+ */
104
+ export function guardAccess(frame, access) {
105
+ if (active.length === 0)
106
+ return;
107
+ for (const record of active) {
108
+ if (!record.guard || record.created.has(frame))
109
+ continue;
110
+ if (!record.guard(frame, access)) {
111
+ throw new UncertifiedAccess(frame, access, record);
112
+ }
113
+ }
114
+ }
115
+ /** Runs `work` with `record` receiving every access note it causes. */
116
+ export function withAccessRecord(record, work) {
117
+ active.push(record);
118
+ try {
119
+ return work();
120
+ }
121
+ finally {
122
+ active.pop();
123
+ }
124
+ }
125
+ /** Whether any record is active, so hot paths can skip their note early. */
126
+ export const isTracking = () => active.length > 0;
127
+ /** The number of mutations committed on `frame`; 0 for a never-written one. */
128
+ export function versionOf(frame) {
129
+ return versions.get(frame) ?? 0;
130
+ }
131
+ /**
132
+ * Notes a committed mutation of `frame`: its version advances, and every
133
+ * active record learns the version this write produced.
134
+ */
135
+ export function noteWrite(frame) {
136
+ const next = versionOf(frame) + 1;
137
+ versions.set(frame, next);
138
+ for (const record of active)
139
+ record.writes.set(frame, next);
140
+ }
141
+ /** Notes a read of `frame`, dated by the version current at the first read. */
142
+ export function noteRead(frame) {
143
+ if (active.length === 0)
144
+ return;
145
+ for (const record of active) {
146
+ if (!record.reads.has(frame))
147
+ record.reads.set(frame, versionOf(frame));
148
+ }
149
+ }
150
+ /** Notes that `frame` was constructed, for every active record. */
151
+ export function noteCreated(frame) {
152
+ if (active.length === 0)
153
+ return;
154
+ for (const record of active)
155
+ record.created.add(frame);
156
+ }
157
+ /**
158
+ * Notes that `holder` bound `value` under `key`, replacing `previous`. The
159
+ * key is a member name, an element index, or `PARENT_KEY`.
160
+ *
161
+ * Only an identity-owning value is indexed: an atom or a closure has no
162
+ * identity a handle could claim authority over. Rebinding a key releases the
163
+ * previous identity's holding first, so the index reflects the live bindings.
164
+ */
165
+ export function noteHolding(holder, key, value, previous) {
166
+ const identity = identityOf(value);
167
+ const released = previous === undefined ? undefined : identityOf(previous);
168
+ if (released && released !== identity)
169
+ dropHolding(released, holder, key);
170
+ if (identity)
171
+ addHolding(identity, holder, key);
172
+ }
173
+ /**
174
+ * The identity a stored value holds, if it holds one: the value itself when
175
+ * it owns identity, or the identity behind a handle, which is how an element
176
+ * plane stores a name's value (`[x_]` holds a handle on `x_`).
177
+ */
178
+ function identityOf(value) {
179
+ const target = value.referent();
180
+ return target.ownsIdentity() ? target : undefined;
181
+ }
182
+ /** Notes that `holder` no longer holds `previous` under `key`. */
183
+ export function noteReleased(holder, key, previous) {
184
+ const released = identityOf(previous);
185
+ if (released)
186
+ dropHolding(released, holder, key);
187
+ }
188
+ /**
189
+ * Notes that `holder` now holds `elements` in its element plane, element i
190
+ * under the index key `i`, releasing whatever it held under those indexes
191
+ * before. This is the commit point for every assignment of an aggregate's
192
+ * element plane as a whole; an append goes through `noteHolding` with its
193
+ * index.
194
+ */
195
+ export function noteElements(holder, elements, previous = []) {
196
+ for (let index = 0; index < previous.length; index++) {
197
+ const released = identityOf(previous[index]);
198
+ if (released)
199
+ dropHolding(released, holder, String(index));
200
+ }
201
+ for (let index = 0; index < elements.length; index++) {
202
+ const identity = identityOf(elements[index]);
203
+ if (identity)
204
+ addHolding(identity, holder, String(index));
205
+ }
206
+ }
207
+ /**
208
+ * Indexes the identity-owning bindings `clone` holds: its members, its
209
+ * declared parent, and, when given, its elements. A copy duplicates its
210
+ * source's bindings without committing them through `set`, so the index would
211
+ * otherwise miss every holder that is a copy, functional-update copies
212
+ * included.
213
+ */
214
+ export function noteCopiedHoldings(clone, elements = []) {
215
+ for (const [key, value] of clone.meta_pairs()) {
216
+ const identity = identityOf(value);
217
+ if (identity)
218
+ addHolding(identity, clone, key);
219
+ }
220
+ if (clone.hasDeclaredParent()) {
221
+ const parent = identityOf(clone.parent);
222
+ if (parent)
223
+ addHolding(parent, clone, PARENT_KEY);
224
+ }
225
+ noteElements(clone, elements);
226
+ }
227
+ /**
228
+ * The live bindings that hold `identity`, each graded by its key's marker.
229
+ *
230
+ * The answer is the ART inverse index: where the reference relation goes from
231
+ * holder to value, this goes from value to holders. It reports possession,
232
+ * which is distinct from active use: an event holding `x` mutably is not
233
+ * thereby writing it (see `AccessRecord`).
234
+ */
235
+ export function holdersOf(identity) {
236
+ const records = holdings.get(identity);
237
+ if (!records)
238
+ return [];
239
+ const result = [];
240
+ for (const record of records) {
241
+ result.push({
242
+ holder: record.holder,
243
+ key: record.key,
244
+ kind: kindOf(record.key),
245
+ mutable: keyGrantsMutation(record.key),
246
+ });
247
+ }
248
+ return result;
249
+ }
250
+ /** The holdings of `identity` whose key claims mutation authority. */
251
+ export function mutableHoldersOf(identity) {
252
+ return holdersOf(identity).filter((holding) => holding.mutable);
253
+ }
254
+ function addHolding(identity, holder, key) {
255
+ let records = holdings.get(identity);
256
+ if (!records) {
257
+ records = new Set();
258
+ holdings.set(identity, records);
259
+ }
260
+ for (const record of records) {
261
+ if (record.key === key && record.holder === holder)
262
+ return;
263
+ }
264
+ records.add({ holder, key });
265
+ }
266
+ function dropHolding(identity, holder, key) {
267
+ const records = holdings.get(identity);
268
+ if (!records)
269
+ return;
270
+ for (const record of records) {
271
+ if (record.holder === holder && record.key === key) {
272
+ records.delete(record);
273
+ }
274
+ }
275
+ }
package/esm/lib/frames.js CHANGED
@@ -51,5 +51,11 @@ export { MetaFrame, Visibility } from "./frames/meta-frame.js";
51
51
  export { ScanDisposition, } from "./scan.js";
52
52
  export { contextEqual, contextString, NilContext, } from "./frames/context.js";
53
53
  export { hasEffectSubscribers, installEffectInvoker, isEffectObserver, notifyEffect, registerEffect, } from "./frames/effect-registry.js";
54
+ export { AccessRecord, guardAccess, holdersOf, isTracking, keyGrantsMutation, mutableHoldersOf, PARENT_KEY, UncertifiedAccess, versionOf, withAccessRecord, } from "./frames/reference-tracking.js";
55
+ export { footprintsConflict, isOpaque, isState, potentialFootprint, } from "./frames/access-footprint.js";
56
+ export { accessesOutside, dispatch, stepsOf, versionRead, versionWritten, } from "./frames/event-dispatch.js";
57
+ export { DISPATCH_KEY, dispatchNamespace } from "./frames/dispatch-host.js";
58
+ export { BoundMethod } from "./frames/bound-method.js";
59
+ export { FrameHandle } from "./frames/frame-handle.js";
54
60
  export { checkPartitions, partitionPlan, } from "./frames/partition-check.js";
55
61
  Frame.globals = Ops;
@@ -1 +1 @@
1
- export const VERSION = "0.44.0";
1
+ export const VERSION = "0.44.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.44.0",
3
+ "version": "0.44.1",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",