1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Host invocation tracking owns no runtime-provider state and exposes no task objects.
const CALL_TRACKING = (() => {
const exports = new WeakMap();
const pending = new Set();
let entering = null;
let checkQueued = false;
/** Release private task references and publish one terminal execution outcome. */
function finish(call, outcome) {
pending.delete(call);
call.task = null;
call.resolve(Object.freeze(outcome));
}
/** Check selected guest execution, excluding unrelated calls and host-owned work. */
function checkNow() {
if (pending.size === 0) {
return;
}
const liveRoots = new Set();
if ($STORE.error === null) {
for (const [componentIdx, tasks] of $TASKS) {
if (componentIdx === -1) {
continue;
}
for (const { task } of tasks) {
liveRoots.add(task.getRootTask());
}
}
}
for (const call of pending) {
if ($STORE.stopped) {
finish(call, { status: 'stopped' });
} else if ($STORE.error !== null) {
finish(call, { status: 'trapped', error: $STORE.error });
} else if (call.task?.isErrored() instanceof $RUNTIME_ERROR) {
finish(call, { status: 'trapped', error: call.task.isErrored() });
} else if (call.task ? !liveRoots.has(call.task) : call.resultSettled) {
finish(call, { status: 'completed' });
}
}
}
/** Observe lifecycle changes after the current unwind records any trap. */
function check() {
if (pending.size === 0 || checkQueued) {
return;
}
checkQueued = true;
queueMicrotask(() => {
checkQueued = false;
checkNow();
});
}
/** Observe an invocation promise without assimilating its returned future payload. */
function observe(call, result) {
const settled = () => {
call.resultSettled = true;
check();
};
result.then(settled, settled);
return result;
}
/** Invoke a registered export, preserving its receiver and result representation. */
function invoke(receiver, fn, args) {
const metadata = exports.get(fn);
if (!metadata) {
throw new TypeError('call tracking requires an export from this instance');
}
let resolve;
const finished = new Promise((done) => {
resolve = done;
});
const call = { id: metadata.id, task: null, resolve, resultSettled: false };
const previous = entering;
pending.add(call);
entering = call;
try {
const result = metadata.construct ? Reflect.construct(fn, args) : Reflect.apply(fn, receiver, args);
if (metadata.asynchronous) {
observe(call, result);
} else if (!metadata.future) {
call.resultSettled = true;
}
checkNow();
return Object.freeze({ result, finished });
} catch (error) {
// No handle is returned on a synchronous throw. Guest error handling and
// any background work continue through the ordinary runtime paths.
pending.delete(call);
call.task = null;
throw error;
} finally {
entering = previous;
}
}
const publicAPI = Object.freeze({
/** Start an exported function or resource constructor with its arguments. */
start(fn, ...args) {
return invoke(undefined, fn, args);
},
/** Start an exported resource method with an explicit receiver. */
startWithThis(receiver, fn, ...args) {
return invoke(receiver, fn, args);
},
});
return {
publicAPI,
check,
observe,
/** Register a generated export; identities never cross instance boundaries. */
register(fn, id, construct, asynchronous, future) {
exports.set(fn, { id, construct, asynchronous, future });
},
/** Claim the invocation before argument lowering or reentrant host execution. */
capture(id) {
if (entering?.id !== id) {
return null;
}
const call = entering;
entering = null;
return call;
},
/** Retain only the selected root until its execution and descendants finish. */
attach(call, task) {
if (call && pending.has(call)) {
call.task = task;
}
},
};
})();