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
|
@@ -94,6 +94,30 @@ export class BoundMethod extends Frame {
|
|
|
94
94
|
isMutating() {
|
|
95
95
|
return this.effect.mutating;
|
|
96
96
|
}
|
|
97
|
+
/** The frame this method was bound to, before any functional-update copy. */
|
|
98
|
+
receiverFrame() {
|
|
99
|
+
return this.receiverTarget;
|
|
100
|
+
}
|
|
101
|
+
/** Whether the handle this method was reached through claims mutability. */
|
|
102
|
+
hasMutableReceiver() {
|
|
103
|
+
return this.mutable;
|
|
104
|
+
}
|
|
105
|
+
/** The shared closure this binding runs. */
|
|
106
|
+
methodBody() {
|
|
107
|
+
return this.method;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The scope a call would run the body in, with the receiver capability a
|
|
111
|
+
* call would select, without running the body (`access-footprint.ts`). The
|
|
112
|
+
* selection is the real one: a functional update's scope reads its instance
|
|
113
|
+
* copy as receiver, so a walk over it sees writes land on the copy.
|
|
114
|
+
*/
|
|
115
|
+
scopeForCall(argument) {
|
|
116
|
+
const state = this.receiverStateForCall();
|
|
117
|
+
if (state instanceof Frame)
|
|
118
|
+
return state;
|
|
119
|
+
return this.method.scopeForCall(argument, Frame.nil, state);
|
|
120
|
+
}
|
|
97
121
|
call(argument, _parameter = Frame.nil) {
|
|
98
122
|
const state = this.receiverStateForCall();
|
|
99
123
|
if (state instanceof Frame)
|
|
@@ -103,6 +127,20 @@ export class BoundMethod extends Frame {
|
|
|
103
127
|
// the lookup order, and a shared body's captured scope is whichever instance
|
|
104
128
|
// was built last, so a method would read another instance's fields.
|
|
105
129
|
const result = this.method.call(argument, Frame.nil, state);
|
|
130
|
+
return this.answer(result, state);
|
|
131
|
+
}
|
|
132
|
+
/** `call`, one statement at a time (`FrameLazy.steps`). */
|
|
133
|
+
*steps(argument, _parameter = Frame.nil) {
|
|
134
|
+
const state = this.receiverStateForCall();
|
|
135
|
+
if (state instanceof Frame) {
|
|
136
|
+
yield state;
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
const result = yield* this.method.steps(argument, Frame.nil, state);
|
|
140
|
+
return this.answer(result, state);
|
|
141
|
+
}
|
|
142
|
+
/** What a call answers once the body has: the receiver, or the value. */
|
|
143
|
+
answer(result, state) {
|
|
106
144
|
// A mutating method answers its receiver, unless it answered an error. A
|
|
107
145
|
// diagnostic failure such as `$!.name-missing` carries the same flag.
|
|
108
146
|
// Preserve referent checks for programmatic reference wrappers (011.20).
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Frame } from "./frame.js";
|
|
2
|
+
import { FrameArray } from "./frame-array.js";
|
|
3
|
+
import { FrameCount } from "./frame-count.js";
|
|
4
|
+
import { FrameLazy } from "./frame-lazy.js";
|
|
5
|
+
import { FrameString } from "./frame-string.js";
|
|
6
|
+
import { BoundMethod } from "./bound-method.js";
|
|
7
|
+
import { dispatch, } from "./event-dispatch.js";
|
|
8
|
+
/**
|
|
9
|
+
* The dispatcher as a host-namespace value, so an HC program can hand its
|
|
10
|
+
* own closures to `dispatch` and observe what each policy does to them
|
|
11
|
+
* (occurrence/figure 015.24j: the VACT witness has to reach the dispatcher
|
|
12
|
+
* under test from the testdoc that constructs its state).
|
|
13
|
+
*
|
|
14
|
+
* A harness that installs `dispatchNamespace()` under `DISPATCH_KEY` in its
|
|
15
|
+
* host namespace gives HC source four callables:
|
|
16
|
+
*
|
|
17
|
+
* $$.dispatch.sequential [e₁, e₂, …]
|
|
18
|
+
* $$.dispatch.certified-sequential [e₁, e₂, …]
|
|
19
|
+
* $$.dispatch.interleave [e₁, e₂, …]
|
|
20
|
+
* $$.dispatch.footprint [e₁, e₂, …]
|
|
21
|
+
*
|
|
22
|
+
* Each `eᵢ` is a bound closure or bound method, called with nil, or a pair
|
|
23
|
+
* `[callable, argument]`. The answer is one aggregate: its elements are the
|
|
24
|
+
* events' results in program order, and its properties the schedule the
|
|
25
|
+
* policy chose, `trace` as `“i:n …”` (the nth statement of event i, in the
|
|
26
|
+
* order they ran), `waited` as each event's indexes waited for, and
|
|
27
|
+
* `escapes` as the count of shared frames each event wrote outside its
|
|
28
|
+
* potential footprint, which the guard keeps at zero under `footprint`. The
|
|
29
|
+
* HC state the events touch is the program's own, so the program reads the
|
|
30
|
+
* final state itself, with its own observer, after the dispatch answers.
|
|
31
|
+
*
|
|
32
|
+
* This is a bridge, not a language feature: the policies are host values
|
|
33
|
+
* reached through `$$`, which a bare name never searches, and nothing here
|
|
34
|
+
* changes how HC evaluates, and it grants no authority: every event is a
|
|
35
|
+
* closure the program already holds, run with the authority it already has.
|
|
36
|
+
* It is installed in every program the CLI runs (`cli/hc.ts`), in every
|
|
37
|
+
* `HCLang` session and so the playground (`lib/execute/hc-lang.ts`), and by
|
|
38
|
+
* the companion Deno test; a host that does not install it has no
|
|
39
|
+
* `dispatch`, and `$$.dispatch` answers `$!.name-missing` there.
|
|
40
|
+
*
|
|
41
|
+
* @module
|
|
42
|
+
*/
|
|
43
|
+
/** The key the dispatch namespace is bound under in a host namespace. */
|
|
44
|
+
export const DISPATCH_KEY = "dispatch";
|
|
45
|
+
const POLICIES = [
|
|
46
|
+
"sequential",
|
|
47
|
+
"certified-sequential",
|
|
48
|
+
"interleave",
|
|
49
|
+
"footprint",
|
|
50
|
+
];
|
|
51
|
+
/** One policy as a callable host value. */
|
|
52
|
+
class DispatchPolicyFrame extends Frame {
|
|
53
|
+
constructor(policy) {
|
|
54
|
+
super();
|
|
55
|
+
Object.defineProperty(this, "policy", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: policy
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
toString() {
|
|
63
|
+
return `$$.${DISPATCH_KEY}.${this.policy}`;
|
|
64
|
+
}
|
|
65
|
+
/** A host value is itself; evaluating it performs no dispatch. */
|
|
66
|
+
in() {
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
call(argument) {
|
|
70
|
+
const events = eventsOf(argument);
|
|
71
|
+
if (!Array.isArray(events))
|
|
72
|
+
return events;
|
|
73
|
+
return reportFrame(dispatch(events, this.policy));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** The events an HC argument denotes, or the error that refuses it. */
|
|
77
|
+
function eventsOf(argument) {
|
|
78
|
+
const items = argument.referent();
|
|
79
|
+
if (!(items instanceof FrameArray)) {
|
|
80
|
+
return Frame.error(`$!.dispatch-events ${items.className()}`);
|
|
81
|
+
}
|
|
82
|
+
const events = [];
|
|
83
|
+
for (const [index, item] of items.asArray().entries()) {
|
|
84
|
+
const value = item.referent();
|
|
85
|
+
const label = String(index);
|
|
86
|
+
if (isCallable(value)) {
|
|
87
|
+
events.push({ label, target: value });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (value instanceof FrameArray) {
|
|
91
|
+
const [target, argument] = value.asArray();
|
|
92
|
+
const callee = target?.referent();
|
|
93
|
+
if (callee && isCallable(callee)) {
|
|
94
|
+
events.push({ label, target: callee, argument: argument ?? Frame.nil });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return Frame.error(`$!.dispatch-event ${label} ${value.className()}`);
|
|
99
|
+
}
|
|
100
|
+
return events;
|
|
101
|
+
}
|
|
102
|
+
const isCallable = (frame) => frame instanceof FrameLazy || frame instanceof BoundMethod;
|
|
103
|
+
/** The report as one aggregate: results as elements, schedule as properties. */
|
|
104
|
+
function reportFrame(report) {
|
|
105
|
+
const result = new FrameArray(report.outcomes.map((o) => o.result));
|
|
106
|
+
result.set("trace", new FrameString(report.trace.join(" ")));
|
|
107
|
+
result.set("waited", new FrameArray(report.outcomes.map((o) => new FrameArray(o.waitedFor.map((label) => FrameCount.for(label))))));
|
|
108
|
+
result.set("escapes", new FrameArray(report.outcomes.map((o) => FrameCount.for(String(o.escapes.length)))));
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
/** A fresh namespace holding the three policies, for a host to install. */
|
|
112
|
+
export function dispatchNamespace() {
|
|
113
|
+
const namespace = new Frame();
|
|
114
|
+
for (const policy of POLICIES) {
|
|
115
|
+
namespace.set(policy, new DispatchPolicyFrame(policy));
|
|
116
|
+
}
|
|
117
|
+
return namespace;
|
|
118
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { guardAccess, noteWrite, UncertifiedAccess, } from "./reference-tracking.js";
|
|
1
2
|
/** Subscribers registered on one managed value, in registration order. */
|
|
2
3
|
const subscribers = new WeakMap();
|
|
3
4
|
/** Managed values whose callbacks are currently running, to bound reentrancy. */
|
|
@@ -24,19 +25,34 @@ export function installEffectInvoker(invoker) {
|
|
|
24
25
|
* answer it unchanged.
|
|
25
26
|
*/
|
|
26
27
|
export function registerEffect(managed, callback) {
|
|
28
|
+
// Refused before the subscriber list changes, so a refusal registers nothing.
|
|
29
|
+
guardAccess(managed, "write");
|
|
27
30
|
const existing = subscribers.get(managed);
|
|
28
31
|
if (!existing) {
|
|
29
32
|
subscribers.set(managed, [callback]);
|
|
33
|
+
noteWrite(managed);
|
|
30
34
|
return managed;
|
|
31
35
|
}
|
|
32
|
-
if (
|
|
33
|
-
|
|
36
|
+
if (existing.includes(callback))
|
|
37
|
+
return managed;
|
|
38
|
+
existing.push(callback);
|
|
39
|
+
noteWrite(managed);
|
|
34
40
|
return managed;
|
|
35
41
|
}
|
|
36
42
|
/** Whether a value carries any effect-boundary subscribers. */
|
|
37
43
|
export function hasEffectSubscribers(managed) {
|
|
38
44
|
return subscribers.has(managed);
|
|
39
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The callbacks a committed mutation of `managed` would invoke, in order.
|
|
48
|
+
* Read by the footprint walk (`access-footprint.ts`): a write to a managed
|
|
49
|
+
* value runs its callbacks, so their effects belong to the writer's event.
|
|
50
|
+
* The list can change only by a registration, which is a write of `managed`,
|
|
51
|
+
* so a certificate that writes `managed` cannot see it change while it runs.
|
|
52
|
+
*/
|
|
53
|
+
export function effectSubscribersOf(managed) {
|
|
54
|
+
return subscribers.get(managed) ?? [];
|
|
55
|
+
}
|
|
40
56
|
/**
|
|
41
57
|
* Whether `observer` itself is registered on `managed` by an `A -> B` arrow,
|
|
42
58
|
* so a committed mutation of `managed` invokes it. Another callback on the
|
|
@@ -76,6 +92,9 @@ export function notifyEffect(managed) {
|
|
|
76
92
|
invoke(callback, managed);
|
|
77
93
|
}
|
|
78
94
|
catch (error) {
|
|
95
|
+
// A refusal ends the event here: no sibling runs after it.
|
|
96
|
+
if (error instanceof UncertifiedAccess)
|
|
97
|
+
throw error;
|
|
79
98
|
thrown ??= { error };
|
|
80
99
|
}
|
|
81
100
|
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Frame } from "./frame.js";
|
|
2
|
+
import { FrameLazy } from "./frame-lazy.js";
|
|
3
|
+
import { BoundMethod } from "./bound-method.js";
|
|
4
|
+
import { AccessRecord, UncertifiedAccess, versionOf, withAccessRecord, } from "./reference-tracking.js";
|
|
5
|
+
import { footprintsConflict, isOpaque, isState, potentialFootprint, } from "./access-footprint.js";
|
|
6
|
+
/**
|
|
7
|
+
* The guard an admitted event runs under: its certificate, the footprint
|
|
8
|
+
* admission rested on. An opaque event was serialized against everything, so
|
|
9
|
+
* nothing it does can interfere and nothing is refused.
|
|
10
|
+
*/
|
|
11
|
+
function guardFor(certificate) {
|
|
12
|
+
if (isOpaque(certificate))
|
|
13
|
+
return () => true;
|
|
14
|
+
return (frame, access) => !isState(frame) ||
|
|
15
|
+
(access === "write" ? certificate.writes : certificate.reads).has(frame);
|
|
16
|
+
}
|
|
17
|
+
/** The inferred footprint, widened by the caller's declaration if any. */
|
|
18
|
+
function widen(inferred, declared) {
|
|
19
|
+
if (!declared)
|
|
20
|
+
return inferred;
|
|
21
|
+
return {
|
|
22
|
+
reads: new Set([...inferred.reads, ...declared.reads]),
|
|
23
|
+
writes: new Set([...inferred.writes, ...declared.writes]),
|
|
24
|
+
unresolved: [...inferred.unresolved, ...declared.unresolved],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The shared state frames `record` touched that `footprint` did not allow:
|
|
29
|
+
* writes outside its writes, or reads outside its reads. Shared means not
|
|
30
|
+
* created by the event; state means what the walk would count as state.
|
|
31
|
+
*/
|
|
32
|
+
export function accessesOutside(record, footprint, access = "writes") {
|
|
33
|
+
const touched = access === "writes"
|
|
34
|
+
? record.sharedWrites()
|
|
35
|
+
: record.sharedReads();
|
|
36
|
+
const allowed = access === "writes" ? footprint.writes : footprint.reads;
|
|
37
|
+
return touched.filter((frame) => isState(frame) && !allowed.has(frame));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Runs `events` under `policy` and reports what each did and in what order.
|
|
41
|
+
*/
|
|
42
|
+
export function dispatch(events, policy = "footprint") {
|
|
43
|
+
const trace = [];
|
|
44
|
+
const infer = (event) => potentialFootprint(event.target, event.argument ?? Frame.nil);
|
|
45
|
+
const states = events.map((event) => {
|
|
46
|
+
const inferred = infer(event);
|
|
47
|
+
return {
|
|
48
|
+
event,
|
|
49
|
+
record: new AccessRecord(),
|
|
50
|
+
waitedFor: new Set(),
|
|
51
|
+
inferred,
|
|
52
|
+
potential: widen(inferred, event.declared),
|
|
53
|
+
steps: 0,
|
|
54
|
+
done: false,
|
|
55
|
+
result: Frame.nil,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
// A waiting event's names may have been rebound by an event that finished
|
|
59
|
+
// since, so its footprint is read again from its code until it starts.
|
|
60
|
+
const refresh = (state) => {
|
|
61
|
+
if (state.generator)
|
|
62
|
+
return;
|
|
63
|
+
state.inferred = infer(state.event);
|
|
64
|
+
state.potential = widen(state.inferred, state.event.declared);
|
|
65
|
+
};
|
|
66
|
+
const serial = policy === "sequential" || policy === "certified-sequential";
|
|
67
|
+
const guarded = policy === "footprint" || policy === "certified-sequential";
|
|
68
|
+
const mayStart = (index) => {
|
|
69
|
+
if (policy === "interleave")
|
|
70
|
+
return true;
|
|
71
|
+
const state = states[index];
|
|
72
|
+
refresh(state);
|
|
73
|
+
for (let other = 0; other < states.length; other++) {
|
|
74
|
+
if (other === index)
|
|
75
|
+
continue;
|
|
76
|
+
const peer = states[other];
|
|
77
|
+
if (peer.done)
|
|
78
|
+
continue;
|
|
79
|
+
// A later event matters only once it is running.
|
|
80
|
+
if (other > index && !peer.generator)
|
|
81
|
+
continue;
|
|
82
|
+
// An earlier event that has not started holds back every later one:
|
|
83
|
+
// its footprint is read when it starts and may name what a later event
|
|
84
|
+
// would already have written.
|
|
85
|
+
const conflicts = serial || !peer.generator ||
|
|
86
|
+
footprintsConflict(state.potential, peer.potential);
|
|
87
|
+
if (conflicts) {
|
|
88
|
+
state.waitedFor.add(peer.event.label);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
};
|
|
94
|
+
let remaining = states.length;
|
|
95
|
+
while (remaining > 0) {
|
|
96
|
+
let progressed = false;
|
|
97
|
+
for (let index = 0; index < states.length; index++) {
|
|
98
|
+
const state = states[index];
|
|
99
|
+
if (state.done)
|
|
100
|
+
continue;
|
|
101
|
+
if (!state.generator) {
|
|
102
|
+
if (!mayStart(index))
|
|
103
|
+
continue;
|
|
104
|
+
state.generator = stepsOf(state.event.target, state.event.argument ?? Frame.nil);
|
|
105
|
+
// The certificate is frozen with the footprint admission rested on.
|
|
106
|
+
if (guarded)
|
|
107
|
+
state.record.guard = guardFor(state.potential);
|
|
108
|
+
}
|
|
109
|
+
const generator = state.generator;
|
|
110
|
+
progressed = true;
|
|
111
|
+
let step;
|
|
112
|
+
try {
|
|
113
|
+
step = withAccessRecord(state.record, () => generator.next());
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
// Only this event's own guard ends it; a refusal by an enclosing
|
|
117
|
+
// event's guard belongs to that event's dispatcher.
|
|
118
|
+
if (!(error instanceof UncertifiedAccess) || error.record !== state.record)
|
|
119
|
+
throw error;
|
|
120
|
+
// Refused before the access happened: the event ends here, at the
|
|
121
|
+
// statement it was in, with nothing of that access committed.
|
|
122
|
+
state.refused = { frame: error.frame, access: error.access };
|
|
123
|
+
step = {
|
|
124
|
+
done: true,
|
|
125
|
+
value: Frame.error(`$!.uncertified-${error.access}`),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (step.done) {
|
|
129
|
+
state.done = true;
|
|
130
|
+
state.result = step.value;
|
|
131
|
+
remaining--;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
state.steps++;
|
|
135
|
+
trace.push(`${state.event.label}:${state.steps}`);
|
|
136
|
+
}
|
|
137
|
+
// A serial policy runs each event to completion before looking further.
|
|
138
|
+
if (serial && !state.done)
|
|
139
|
+
index--;
|
|
140
|
+
}
|
|
141
|
+
if (!progressed) {
|
|
142
|
+
throw new Error("$!.dispatch-stalled: no event could start");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
policy,
|
|
147
|
+
trace,
|
|
148
|
+
outcomes: states.map((state) => {
|
|
149
|
+
// An opaque event claimed nothing, so nothing it did escapes a claim.
|
|
150
|
+
const opaque = isOpaque(state.potential);
|
|
151
|
+
const declared = state.event.declared;
|
|
152
|
+
return {
|
|
153
|
+
label: state.event.label,
|
|
154
|
+
result: state.result,
|
|
155
|
+
steps: state.steps,
|
|
156
|
+
inferred: state.inferred,
|
|
157
|
+
potential: state.potential,
|
|
158
|
+
actual: state.record,
|
|
159
|
+
escapes: opaque ? [] : accessesOutside(state.record, state.potential),
|
|
160
|
+
readEscapes: opaque
|
|
161
|
+
? []
|
|
162
|
+
: accessesOutside(state.record, state.potential, "reads"),
|
|
163
|
+
undeclared: declared && !isOpaque(declared)
|
|
164
|
+
? accessesOutside(state.record, declared)
|
|
165
|
+
: [],
|
|
166
|
+
waitedFor: [...state.waitedFor],
|
|
167
|
+
refused: state.refused,
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** The statement steps of calling `target`; a callable without a body is one. */
|
|
173
|
+
export function stepsOf(target, argument = Frame.nil) {
|
|
174
|
+
if (target instanceof BoundMethod)
|
|
175
|
+
return target.steps(argument);
|
|
176
|
+
if (target instanceof FrameLazy)
|
|
177
|
+
return target.steps(argument);
|
|
178
|
+
return (function* () {
|
|
179
|
+
const result = target.call(argument, Frame.nil);
|
|
180
|
+
yield result;
|
|
181
|
+
return result;
|
|
182
|
+
})();
|
|
183
|
+
}
|
|
184
|
+
/** The version of `frame` an outcome read, or undefined if it never did. */
|
|
185
|
+
export function versionRead(outcome, frame) {
|
|
186
|
+
return outcome.actual.reads.get(frame);
|
|
187
|
+
}
|
|
188
|
+
/** The version of `frame` an outcome's last write produced, if it wrote it. */
|
|
189
|
+
export function versionWritten(outcome, frame) {
|
|
190
|
+
return outcome.actual.writes.get(frame);
|
|
191
|
+
}
|
|
192
|
+
export { versionOf };
|
|
@@ -8,9 +8,22 @@ import { FrameHandle } from "./frame-handle.js";
|
|
|
8
8
|
import { FrameList } from "./frame-list.js";
|
|
9
9
|
import { FrameNote } from "./frame-note.js";
|
|
10
10
|
import { NilContext } from "./context.js";
|
|
11
|
+
import { guardAccess, noteElements, noteHolding, noteRead, noteWrite, PARENT_KEY, } from "./reference-tracking.js";
|
|
11
12
|
export class FrameArray extends FrameList {
|
|
12
13
|
constructor(data, meta = NilContext) {
|
|
13
14
|
super(data, meta);
|
|
15
|
+
// An aggregate holds its elements from birth (reference-tracking.ts).
|
|
16
|
+
noteElements(this, data);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Assigning the element plane whole releases the elements held before and
|
|
20
|
+
* holds the new ones, element i under the key `i`.
|
|
21
|
+
*/
|
|
22
|
+
adoptData(data) {
|
|
23
|
+
const previous = this.data;
|
|
24
|
+
super.adoptData(data);
|
|
25
|
+
if (previous !== data)
|
|
26
|
+
noteElements(this, data, previous);
|
|
14
27
|
}
|
|
15
28
|
/** Count leaves use the same Star rule at the root and inside an array. */
|
|
16
29
|
static starCount(count, replacement) {
|
|
@@ -78,9 +91,9 @@ export class FrameArray extends FrameList {
|
|
|
78
91
|
if (!node.elements().length)
|
|
79
92
|
return replacement;
|
|
80
93
|
const result = node.copy();
|
|
81
|
-
result.
|
|
94
|
+
result.adoptData(node.itemsFor(result, (item) => node.repeatsTerm(item)
|
|
82
95
|
? go(item.walked().referent())
|
|
83
|
-
: item);
|
|
96
|
+
: item));
|
|
84
97
|
return result;
|
|
85
98
|
};
|
|
86
99
|
return go(this);
|
|
@@ -97,6 +110,7 @@ export class FrameArray extends FrameList {
|
|
|
97
110
|
// of that field rather than a flag dance over the lexical pointer.
|
|
98
111
|
if (this.hasDeclaredParent()) {
|
|
99
112
|
result.parent = this.parent;
|
|
113
|
+
noteHolding(result, PARENT_KEY, this.parent);
|
|
100
114
|
}
|
|
101
115
|
// An aggregate accepts declarations while it is under construction, and
|
|
102
116
|
// says so rather than leaving a name to infer it from the context stack.
|
|
@@ -105,7 +119,7 @@ export class FrameArray extends FrameList {
|
|
|
105
119
|
// does not make it absorb declarations.
|
|
106
120
|
result.declares = true;
|
|
107
121
|
try {
|
|
108
|
-
result.
|
|
122
|
+
result.adoptData(this.array_eval(input, result));
|
|
109
123
|
}
|
|
110
124
|
finally {
|
|
111
125
|
result.declares = false;
|
|
@@ -207,16 +221,28 @@ export class FrameArray extends FrameList {
|
|
|
207
221
|
instanceCopy() {
|
|
208
222
|
return this.copy();
|
|
209
223
|
}
|
|
224
|
+
/** Reading the element plane is a read of this aggregate's state. */
|
|
225
|
+
elements() {
|
|
226
|
+
guardAccess(this, "read");
|
|
227
|
+
noteRead(this);
|
|
228
|
+
return super.elements();
|
|
229
|
+
}
|
|
210
230
|
/** Elements are members too, so a held element can be replaced in place. */
|
|
211
231
|
replaceMember(member, replacement) {
|
|
212
|
-
|
|
213
|
-
|
|
232
|
+
const replaced = super.replaceMember(member, replacement);
|
|
233
|
+
let elementReplaced = false;
|
|
234
|
+
this.data.forEach((item, index) => {
|
|
214
235
|
if (item !== member)
|
|
215
|
-
return
|
|
216
|
-
replaced
|
|
217
|
-
|
|
236
|
+
return;
|
|
237
|
+
if (!replaced && !elementReplaced)
|
|
238
|
+
guardAccess(this, "write");
|
|
239
|
+
this.data[index] = replacement;
|
|
240
|
+
noteHolding(this, String(index), replacement, member);
|
|
241
|
+
elementReplaced = true;
|
|
218
242
|
});
|
|
219
|
-
|
|
243
|
+
if (elementReplaced && !replaced)
|
|
244
|
+
noteWrite(this);
|
|
245
|
+
return replaced || elementReplaced;
|
|
220
246
|
}
|
|
221
247
|
lookup_here(key, origin) {
|
|
222
248
|
if (Frame.isIndexKey(key)) {
|
|
@@ -413,7 +439,10 @@ export class FrameArray extends FrameList {
|
|
|
413
439
|
/** Pushes a non-nil value, whatever application this aggregate declares. */
|
|
414
440
|
collect(value, _context = Frame.nil) {
|
|
415
441
|
if (!value.is.void) {
|
|
442
|
+
guardAccess(this, "write");
|
|
416
443
|
this.data.push(value);
|
|
444
|
+
noteHolding(this, String(this.data.length - 1), value);
|
|
445
|
+
noteWrite(this);
|
|
417
446
|
}
|
|
418
447
|
return this;
|
|
419
448
|
}
|
|
@@ -436,7 +465,9 @@ export class FrameArray extends FrameList {
|
|
|
436
465
|
return this.data.length;
|
|
437
466
|
}
|
|
438
467
|
reset() {
|
|
439
|
-
this
|
|
468
|
+
guardAccess(this, "write");
|
|
469
|
+
this.adoptData([]);
|
|
470
|
+
noteWrite(this);
|
|
440
471
|
}
|
|
441
472
|
}
|
|
442
473
|
Object.defineProperty(FrameArray, "BEGIN_ARRAY", {
|
|
@@ -55,12 +55,7 @@ export class FrameExpr extends FrameList {
|
|
|
55
55
|
}
|
|
56
56
|
let result = Frame.nil;
|
|
57
57
|
for (const run of commaRuns(body)) {
|
|
58
|
-
|
|
59
|
-
result = run.length > 1
|
|
60
|
-
? FrameExpr.commaGroup(run, scope)
|
|
61
|
-
: item instanceof FrameExpr
|
|
62
|
-
? item.evaluate(scope)
|
|
63
|
-
: item.in(scope);
|
|
58
|
+
result = FrameExpr.evaluateRun(run, scope);
|
|
64
59
|
// A sequence stops at the first failing statement, because later
|
|
65
60
|
// statements were written to run after the earlier ones succeeded.
|
|
66
61
|
if (result.isFailedResult())
|
|
@@ -68,6 +63,42 @@ export class FrameExpr extends FrameList {
|
|
|
68
63
|
}
|
|
69
64
|
return result;
|
|
70
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* `evaluateBody`, one statement at a time.
|
|
68
|
+
*
|
|
69
|
+
* The generator yields after each statement of a sequenced body and returns
|
|
70
|
+
* the body's value, with the same stops and the same scope as
|
|
71
|
+
* `evaluateBody`, so a body stepped to completion answers what a called one
|
|
72
|
+
* answers. A body that is not a sequence, or has one term, is one step. The
|
|
73
|
+
* statement is the finest boundary the language defines between two
|
|
74
|
+
* effects of one body, so it is the unit an event dispatcher may interleave
|
|
75
|
+
* at (`event-dispatch.ts`); nothing inside a statement is a boundary.
|
|
76
|
+
*/
|
|
77
|
+
static *stepBody(body, input, sequence = false) {
|
|
78
|
+
const scope = EvaluationScope.from(input);
|
|
79
|
+
if (body.length === 1 || !sequence) {
|
|
80
|
+
const result = FrameExpr.evaluateBody(body, scope, sequence);
|
|
81
|
+
yield result;
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
let result = Frame.nil;
|
|
85
|
+
for (const run of commaRuns(body)) {
|
|
86
|
+
result = FrameExpr.evaluateRun(run, scope);
|
|
87
|
+
yield result;
|
|
88
|
+
if (result.isFailedResult())
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/** One statement of a sequenced body: a comma group, or a single term. */
|
|
94
|
+
static evaluateRun(run, scope) {
|
|
95
|
+
const [item] = run;
|
|
96
|
+
return run.length > 1
|
|
97
|
+
? FrameExpr.commaGroup(run, scope)
|
|
98
|
+
: item instanceof FrameExpr
|
|
99
|
+
? item.evaluate(scope)
|
|
100
|
+
: item.in(scope);
|
|
101
|
+
}
|
|
71
102
|
call(argument, parameter = Frame.nil) {
|
|
72
103
|
return this.in(EvaluationScope.call(argument, parameter));
|
|
73
104
|
}
|
|
@@ -132,6 +132,18 @@ export class FrameLazy extends FrameExpr {
|
|
|
132
132
|
in(input = []) {
|
|
133
133
|
return this.bind(input);
|
|
134
134
|
}
|
|
135
|
+
/** The scope this bound copy captured; a template no read bound has none. */
|
|
136
|
+
capturedScope() {
|
|
137
|
+
return capturedScopes.get(this) ?? this.legacyCapture();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The scope a call of this closure would evaluate its body in, built as
|
|
141
|
+
* `call` builds it, without running the body. A static walk of the body
|
|
142
|
+
* resolves names against this scope's lookup frames (`access-footprint.ts`).
|
|
143
|
+
*/
|
|
144
|
+
scopeForCall(argument, parameter = Frame.nil, receiverState) {
|
|
145
|
+
return EvaluationScope.call(argument, parameter, this, this.capturedScope(), receiverState);
|
|
146
|
+
}
|
|
135
147
|
/** Returns state only when this literal was evaluated in that invocation. */
|
|
136
148
|
receiverStateForCallback(receiverState) {
|
|
137
149
|
return inlineCallbackReceiverStates.get(this) === receiverState
|
|
@@ -170,6 +182,29 @@ export class FrameLazy extends FrameExpr {
|
|
|
170
182
|
}
|
|
171
183
|
/** Evaluates this closure with optional bound receiver capability. */
|
|
172
184
|
call(argument, parameter = Frame.nil, receiverState) {
|
|
185
|
+
const scope = this.prelude(argument, parameter, receiverState);
|
|
186
|
+
if (scope instanceof Frame)
|
|
187
|
+
return scope;
|
|
188
|
+
const result = FrameExpr.evaluateBody(this.data, scope, this.is.sequence === true);
|
|
189
|
+
return this.checkReturn(result);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* `call`, one statement at a time (`FrameExpr.stepBody`). The generator
|
|
193
|
+
* yields after each statement and returns what `call` would have answered.
|
|
194
|
+
* A call that never reaches the body, a size operator, an empty body, or a
|
|
195
|
+
* refused argument, is one step.
|
|
196
|
+
*/
|
|
197
|
+
*steps(argument, parameter = Frame.nil, receiverState) {
|
|
198
|
+
const scope = this.prelude(argument, parameter, receiverState);
|
|
199
|
+
if (scope instanceof Frame) {
|
|
200
|
+
yield scope;
|
|
201
|
+
return scope;
|
|
202
|
+
}
|
|
203
|
+
const result = yield* FrameExpr.stepBody(this.data, scope, this.is.sequence === true);
|
|
204
|
+
return this.checkReturn(result);
|
|
205
|
+
}
|
|
206
|
+
/** Everything `call` does before the body: an answer, or the body's scope. */
|
|
207
|
+
prelude(argument, parameter, receiverState) {
|
|
173
208
|
if (argument instanceof FrameSymbol && argument.is.address === true &&
|
|
174
209
|
argument.spelling() === "<>")
|
|
175
210
|
return FrameType.of(this);
|
|
@@ -185,10 +220,7 @@ export class FrameLazy extends FrameExpr {
|
|
|
185
220
|
const prepared = this.prepareArgument(argument);
|
|
186
221
|
if (prepared.is.error)
|
|
187
222
|
return prepared;
|
|
188
|
-
|
|
189
|
-
const scope = EvaluationScope.call(prepared, parameter, this, enclosing, receiverState);
|
|
190
|
-
const result = FrameExpr.evaluateBody(this.data, scope, this.is.sequence === true);
|
|
191
|
-
return this.checkReturn(result);
|
|
223
|
+
return this.scopeForCall(prepared, parameter, receiverState);
|
|
192
224
|
}
|
|
193
225
|
legacyCapture() {
|
|
194
226
|
return this.up && !this.up.is.missing
|
|
@@ -236,7 +236,7 @@ export class FrameList extends Frame {
|
|
|
236
236
|
for (const term of repeated)
|
|
237
237
|
terms.push(term);
|
|
238
238
|
}
|
|
239
|
-
result.
|
|
239
|
+
result.adoptData(terms);
|
|
240
240
|
return result.finishRepetition();
|
|
241
241
|
}
|
|
242
242
|
/**
|
|
@@ -283,9 +283,16 @@ export class FrameList extends Frame {
|
|
|
283
283
|
}
|
|
284
284
|
copy() {
|
|
285
285
|
const clone = super.copy();
|
|
286
|
-
clone.
|
|
286
|
+
clone.adoptData(this.itemsFor(clone, (item) => item));
|
|
287
287
|
return clone;
|
|
288
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Replaces the whole data plane. The one assignment point, so an aggregate
|
|
291
|
+
* can observe the elements it stops and starts holding (`FrameArray`).
|
|
292
|
+
*/
|
|
293
|
+
adoptData(data) {
|
|
294
|
+
this.data = data;
|
|
295
|
+
}
|
|
289
296
|
/**
|
|
290
297
|
* This frame's items as a copy holds them.
|
|
291
298
|
*
|