hclang 0.44.0 → 0.45.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/esm/cli/hc.js +4 -1
- package/esm/lib/bundle/hc-sources.js +864 -0
- package/esm/lib/execute/denum-prologue.js +88 -0
- package/esm/lib/execute/hc-eval.js +2 -0
- package/esm/lib/frames/access-footprint.js +665 -0
- package/esm/lib/frames/bound-method.js +38 -0
- package/esm/lib/frames/denum-hash.js +166 -0
- package/esm/lib/frames/denum.js +357 -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 +42 -11
- package/esm/lib/frames/frame-expr.js +40 -9
- package/esm/lib/frames/frame-lazy.js +37 -5
- package/esm/lib/frames/frame-list.js +10 -3
- package/esm/lib/frames/frame-name.js +8 -0
- package/esm/lib/frames/frame-symbol.js +1 -1
- package/esm/lib/frames/frame.js +75 -10
- package/esm/lib/frames/reference-tracking.js +275 -0
- package/esm/lib/frames.js +8 -0
- package/esm/lib/ops/denum.js +120 -0
- package/esm/lib/ops.js +3 -0
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
package/esm/lib/frames/frame.js
CHANGED
|
@@ -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
|
-
|
|
273
|
-
|
|
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
|
}
|
|
@@ -425,6 +452,17 @@ export function inspectFlags(flags) {
|
|
|
425
452
|
* manipulating and interacting with frames.
|
|
426
453
|
*/
|
|
427
454
|
export class Frame extends MetaFrame {
|
|
455
|
+
/**
|
|
456
|
+
* Whether `key` names an observer a Frame answers for itself: one of the four
|
|
457
|
+
* fixed hashes `#`, `##`, `###`, `####`, or a ranked hash `#n`.
|
|
458
|
+
*/
|
|
459
|
+
static isSizeOperator(key) {
|
|
460
|
+
return Frame.SIZE_OPERATORS.includes(key) || Frame.RANKED_HASH.test(key);
|
|
461
|
+
}
|
|
462
|
+
/** The rung n of a ranked hash `#n`, or undefined for any other key. */
|
|
463
|
+
static hashRank(key) {
|
|
464
|
+
return Frame.RANKED_HASH.test(key) ? Number(key.slice(1)) : undefined;
|
|
465
|
+
}
|
|
428
466
|
/** Creates a runtime error value without coupling MetaFrame to FrameNote. */
|
|
429
467
|
static error(source) {
|
|
430
468
|
const error = new (class extends Frame {
|
|
@@ -697,7 +735,7 @@ export class Frame extends MetaFrame {
|
|
|
697
735
|
* Nil has no Count, so no residual or Demi.
|
|
698
736
|
*/
|
|
699
737
|
sizeFor(key) {
|
|
700
|
-
if (!Frame.
|
|
738
|
+
if (!Frame.isSizeOperator(key))
|
|
701
739
|
return Frame.nil;
|
|
702
740
|
const failure = this.observationError();
|
|
703
741
|
if (failure)
|
|
@@ -724,8 +762,10 @@ export class Frame extends MetaFrame {
|
|
|
724
762
|
}
|
|
725
763
|
case Frame.kDEMI:
|
|
726
764
|
return Frame.demi(this);
|
|
727
|
-
default:
|
|
728
|
-
|
|
765
|
+
default: {
|
|
766
|
+
const rank = Frame.hashRank(key);
|
|
767
|
+
return rank === undefined ? Frame.nil : Frame.denum(this, rank);
|
|
768
|
+
}
|
|
729
769
|
}
|
|
730
770
|
}
|
|
731
771
|
/** Nonnegative represented quantity; path tracks recursive containment only. */
|
|
@@ -994,6 +1034,12 @@ export class Frame extends MetaFrame {
|
|
|
994
1034
|
clone.meta = this.meta_copy();
|
|
995
1035
|
clone.is = { ...this.is };
|
|
996
1036
|
clone.id = `$:${this.className()}.${MetaFrame.id_count++}`;
|
|
1037
|
+
// `Object.create` bypasses the constructor, so the clone registers its
|
|
1038
|
+
// construction and its duplicated holdings here (reference-tracking.ts).
|
|
1039
|
+
noteCreated(clone);
|
|
1040
|
+
if (clone.meta !== NilContext) {
|
|
1041
|
+
noteCopiedHoldings(clone);
|
|
1042
|
+
}
|
|
997
1043
|
return clone;
|
|
998
1044
|
}
|
|
999
1045
|
/**
|
|
@@ -1015,14 +1061,20 @@ export class Frame extends MetaFrame {
|
|
|
1015
1061
|
let replaced = false;
|
|
1016
1062
|
for (const [key, value] of this.meta_pairs()) {
|
|
1017
1063
|
if (value === member) {
|
|
1064
|
+
if (!replaced)
|
|
1065
|
+
guardAccess(this, "write");
|
|
1018
1066
|
this.meta[key] = replacement;
|
|
1067
|
+
noteHolding(this, key, replacement, member);
|
|
1019
1068
|
replaced = true;
|
|
1020
1069
|
}
|
|
1021
1070
|
}
|
|
1022
1071
|
// An element-plane commit is a mutation too; a copy-on-write install lands
|
|
1023
1072
|
// on a fresh copy that carries no subscribers, so it never fires here.
|
|
1024
|
-
if (replaced
|
|
1025
|
-
|
|
1073
|
+
if (replaced) {
|
|
1074
|
+
noteWrite(this);
|
|
1075
|
+
if (hasEffectSubscribers(this)) {
|
|
1076
|
+
notifyEffect(this);
|
|
1077
|
+
}
|
|
1026
1078
|
}
|
|
1027
1079
|
return replaced;
|
|
1028
1080
|
}
|
|
@@ -1031,7 +1083,7 @@ export class Frame extends MetaFrame {
|
|
|
1031
1083
|
return undefined;
|
|
1032
1084
|
}
|
|
1033
1085
|
const key = argument.spelling();
|
|
1034
|
-
return Frame.
|
|
1086
|
+
return Frame.isSizeOperator(key) ? key : undefined;
|
|
1035
1087
|
}
|
|
1036
1088
|
/**
|
|
1037
1089
|
* Negation is decided by the symbol's name, never by how it prints.
|
|
@@ -1116,6 +1168,19 @@ Object.defineProperty(Frame, "SIZE_OPERATORS", {
|
|
|
1116
1168
|
Frame.kDEMI,
|
|
1117
1169
|
]
|
|
1118
1170
|
});
|
|
1171
|
+
/**
|
|
1172
|
+
* The ranked hash `.#n` (occurrence/figure 007.32): `#` and decimal digits,
|
|
1173
|
+
* as `.#2`, in canonical decimal (no leading zero, so each rung has one
|
|
1174
|
+
* spelling). Its n is the Cayley–Dickson rung and not a hash count, so it is
|
|
1175
|
+
* not one of {@link SIZE_OPERATORS}, whose spellings are the four fixed
|
|
1176
|
+
* hashes. Bare `.#` stays the Count.
|
|
1177
|
+
*/
|
|
1178
|
+
Object.defineProperty(Frame, "RANKED_HASH", {
|
|
1179
|
+
enumerable: true,
|
|
1180
|
+
configurable: true,
|
|
1181
|
+
writable: true,
|
|
1182
|
+
value: /^#(0|[1-9][0-9]*)$/
|
|
1183
|
+
});
|
|
1119
1184
|
/**
|
|
1120
1185
|
* BEGIN_EXPR is the symbol for the beginning of an expression.
|
|
1121
1186
|
*/
|
|
@@ -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
|
@@ -28,6 +28,8 @@ export { FrameNumeric, NUMERIC_RANK, } from "./frames/frame-numeric.js";
|
|
|
28
28
|
export { FrameInt } from "./frames/frame-int.js";
|
|
29
29
|
export { FrameCount } from "./frames/frame-count.js";
|
|
30
30
|
export { installDemiConstructor, installRouterRecognizer, observeDemi, } from "./frames/fourth-hash.js";
|
|
31
|
+
export { installDenumConstructor, observeDenum, } from "./frames/denum-hash.js";
|
|
32
|
+
export * as denum from "./frames/denum.js";
|
|
31
33
|
export { FrameDecimal } from "./frames/frame-decimal.js";
|
|
32
34
|
export { FrameSequence } from "./frames/frame-sequence.js";
|
|
33
35
|
export { FrameTypedNumber } from "./frames/frame-typed-number.js";
|
|
@@ -51,5 +53,11 @@ export { MetaFrame, Visibility } from "./frames/meta-frame.js";
|
|
|
51
53
|
export { ScanDisposition, } from "./scan.js";
|
|
52
54
|
export { contextEqual, contextString, NilContext, } from "./frames/context.js";
|
|
53
55
|
export { hasEffectSubscribers, installEffectInvoker, isEffectObserver, notifyEffect, registerEffect, } from "./frames/effect-registry.js";
|
|
56
|
+
export { AccessRecord, guardAccess, holdersOf, isTracking, keyGrantsMutation, mutableHoldersOf, PARENT_KEY, UncertifiedAccess, versionOf, withAccessRecord, } from "./frames/reference-tracking.js";
|
|
57
|
+
export { footprintsConflict, isOpaque, isState, potentialFootprint, } from "./frames/access-footprint.js";
|
|
58
|
+
export { accessesOutside, dispatch, stepsOf, versionRead, versionWritten, } from "./frames/event-dispatch.js";
|
|
59
|
+
export { DISPATCH_KEY, dispatchNamespace } from "./frames/dispatch-host.js";
|
|
60
|
+
export { BoundMethod } from "./frames/bound-method.js";
|
|
61
|
+
export { FrameHandle } from "./frames/frame-handle.js";
|
|
54
62
|
export { checkPartitions, partitionPlan, } from "./frames/partition-check.js";
|
|
55
63
|
Frame.globals = Ops;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
|
|
2
|
+
import { Frame } from "../frames/frame.js";
|
|
3
|
+
import { FrameArray } from "../frames/frame-array.js";
|
|
4
|
+
import { FrameCount } from "../frames/frame-count.js";
|
|
5
|
+
import { FrameString } from "../frames/frame-string.js";
|
|
6
|
+
import { add, arity, conjugate, denum, DenumError, multiply, observe, opposite, promote, residual, schemaName, signatureName, subtract, } from "../frames/denum.js";
|
|
7
|
+
/**
|
|
8
|
+
* The operations `denum-engine` performs, selected by the first element of its
|
|
9
|
+
* argument. The argument is `[op, rank, count, loads, rank, count, loads]`: the
|
|
10
|
+
* left Denum state, then the right one for the binary operations.
|
|
11
|
+
*/
|
|
12
|
+
export const DENUM_OPERATIONS = {
|
|
13
|
+
/** Validate a state and answer it as built. */
|
|
14
|
+
build: 0,
|
|
15
|
+
add: 1,
|
|
16
|
+
subtract: 2,
|
|
17
|
+
multiply: 3,
|
|
18
|
+
opposite: 4,
|
|
19
|
+
conjugate: 5,
|
|
20
|
+
/** Promote the left state to the rank of the right state's first slot. */
|
|
21
|
+
promote: 6,
|
|
22
|
+
/** The signed integer coordinate of each direction, an observation. */
|
|
23
|
+
observe: 7,
|
|
24
|
+
/** The native signature names of the left state's channels, as text. */
|
|
25
|
+
names: 8,
|
|
26
|
+
/** The schema name `CD[n]` of the left state's rank, as text. */
|
|
27
|
+
schema: 9,
|
|
28
|
+
/** Positive minus negative load total: the declared residual, an integer. */
|
|
29
|
+
residual: 10,
|
|
30
|
+
};
|
|
31
|
+
const counts = (frame) => {
|
|
32
|
+
const value = frame.referent();
|
|
33
|
+
return value instanceof FrameCount && typeof value.data === "bigint"
|
|
34
|
+
? value.data
|
|
35
|
+
: undefined;
|
|
36
|
+
};
|
|
37
|
+
/** The state a triple of Frames holds, or the contract failure. */
|
|
38
|
+
function readState(rank, count, loads) {
|
|
39
|
+
const r = rank === undefined ? undefined : counts(rank);
|
|
40
|
+
const c = count === undefined ? undefined : counts(count);
|
|
41
|
+
// Base Frame.elements() answers [this] for any non-aggregate, so a bare Count
|
|
42
|
+
// would read as a one-element list: require an aggregate, as the hash reader
|
|
43
|
+
// `storedState` does.
|
|
44
|
+
const list = loads?.referent();
|
|
45
|
+
const elements = list?.ownsIdentity() ? list.elements() : undefined;
|
|
46
|
+
const l = elements?.map(counts);
|
|
47
|
+
if (r === undefined || c === undefined || !l || l.some((x) => x === undefined)) {
|
|
48
|
+
throw new DenumError("$!!.invalid-denum");
|
|
49
|
+
}
|
|
50
|
+
if (r > 5n)
|
|
51
|
+
throw new DenumError("$!!.invalid-denum", `rank ${r}`);
|
|
52
|
+
return denum(Number(r), c, l);
|
|
53
|
+
}
|
|
54
|
+
const stateFrame = (state) => new FrameArray([
|
|
55
|
+
Frame.count(BigInt(state.rank)),
|
|
56
|
+
Frame.count(state.count),
|
|
57
|
+
new FrameArray(state.loads.map((load) => Frame.count(load))),
|
|
58
|
+
]);
|
|
59
|
+
/**
|
|
60
|
+
* `denum-engine`, the one native seam under the HC class in `hc/denum.hc`.
|
|
61
|
+
*
|
|
62
|
+
* It is pure arithmetic over the typed reference in `lib/frames/denum.ts`: it
|
|
63
|
+
* reads Counts and answers fresh data, touches no scope and holds no state,
|
|
64
|
+
* and it refuses anything malformed with a named error rather than guessing.
|
|
65
|
+
* The Denum itself is an HC class; this is only how that class reaches the
|
|
66
|
+
* Cayley–Dickson recurrence without a hand-written table in HC.
|
|
67
|
+
*/
|
|
68
|
+
export const DenumEngine = (_source, block) => {
|
|
69
|
+
const failure = block.referent().observationError();
|
|
70
|
+
if (failure)
|
|
71
|
+
return failure;
|
|
72
|
+
const args = block.referent().elements();
|
|
73
|
+
const op = args[0] === undefined ? undefined : counts(args[0]);
|
|
74
|
+
if (op === undefined)
|
|
75
|
+
return Frame.error("$!!.invalid-denum");
|
|
76
|
+
try {
|
|
77
|
+
const left = readState(args[1], args[2], args[3]);
|
|
78
|
+
switch (Number(op)) {
|
|
79
|
+
case DENUM_OPERATIONS.build:
|
|
80
|
+
return stateFrame(left);
|
|
81
|
+
case DENUM_OPERATIONS.opposite:
|
|
82
|
+
return stateFrame(opposite(left));
|
|
83
|
+
case DENUM_OPERATIONS.conjugate:
|
|
84
|
+
return stateFrame(conjugate(left));
|
|
85
|
+
case DENUM_OPERATIONS.observe:
|
|
86
|
+
return new FrameArray(observe(left).map((c) => Frame.signed(c)));
|
|
87
|
+
case DENUM_OPERATIONS.names:
|
|
88
|
+
return new FrameArray(Array.from({ length: arity(left.rank) }, (_, channel) => new FrameString(signatureName(left.rank, channel))));
|
|
89
|
+
case DENUM_OPERATIONS.residual:
|
|
90
|
+
return Frame.signed(residual(left));
|
|
91
|
+
case DENUM_OPERATIONS.schema:
|
|
92
|
+
return new FrameString(schemaName(left.rank));
|
|
93
|
+
case DENUM_OPERATIONS.promote: {
|
|
94
|
+
const target = args[4] === undefined ? undefined : counts(args[4]);
|
|
95
|
+
if (target === undefined || target > 5n) {
|
|
96
|
+
return Frame.error("$!.denum-rank");
|
|
97
|
+
}
|
|
98
|
+
return stateFrame(promote(left, Number(target)));
|
|
99
|
+
}
|
|
100
|
+
case DENUM_OPERATIONS.add:
|
|
101
|
+
case DENUM_OPERATIONS.subtract:
|
|
102
|
+
case DENUM_OPERATIONS.multiply: {
|
|
103
|
+
const right = readState(args[4], args[5], args[6]);
|
|
104
|
+
const operate = Number(op) === DENUM_OPERATIONS.add
|
|
105
|
+
? add
|
|
106
|
+
: Number(op) === DENUM_OPERATIONS.subtract
|
|
107
|
+
? subtract
|
|
108
|
+
: multiply;
|
|
109
|
+
return stateFrame(operate(left, right));
|
|
110
|
+
}
|
|
111
|
+
default:
|
|
112
|
+
return Frame.error("$!!.invalid-denum");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (error instanceof DenumError)
|
|
117
|
+
return Frame.error(error.kind);
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
};
|
package/esm/lib/ops.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IfElse, IfThen } from "./ops/conditionals.js";
|
|
2
|
+
import { DenumEngine } from "./ops/denum.js";
|
|
2
3
|
import { FrameOps } from "./ops/frame-ops.js";
|
|
3
4
|
import { MapElements, MapMembers, ReduceElements, ReduceMembers, SpliceElements, } from "./ops/iterators.js";
|
|
4
5
|
import { Add, Divide, Equals, GreaterThan, GreaterThanOrEqual, Identical, LessThan, LessThanOrEqual, Modulo, Multiply, Power, Subtract, TypeEquals, } from "./ops/math.js";
|
|
@@ -34,4 +35,6 @@ export const Ops = new FrameOps({
|
|
|
34
35
|
"^": BindType,
|
|
35
36
|
"^^": BindReturn,
|
|
36
37
|
"->": Arrow,
|
|
38
|
+
// The one native seam under `hc/denum.hc`: pure Cayley–Dickson arithmetic.
|
|
39
|
+
"denum-engine": DenumEngine,
|
|
37
40
|
});
|
package/esm/lib/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.
|
|
1
|
+
export const VERSION = "0.45.0";
|