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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Intrinsics that represent helpers that implement waitable sets
use super::async_task::AsyncTaskIntrinsic;
use crate::intrinsics::component::ComponentIntrinsic;
use crate::intrinsics::p3::host::HostIntrinsic;
use crate::intrinsics::{Intrinsic, RenderIntrinsicsArgs};
use crate::source::Source;
/// This enum contains intrinsics that enable waitable sets
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum WaitableIntrinsic {
/// The definition of the `WaitableSet` JS class
WaitableSetClass,
/// The definition of the `Waitable` JS class
WaitableClass,
/// Create a new waitable set
///
/// Guest code uses this to create new waitable/pollable groups of events that can be waited on.
/// The waitable set is tied to the implicit current task
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type i32 = number;
/// function waitableSetNew(componentInstanceId: number): i32;
/// ```
///
/// The function returns the index of the waitable set that was created, so it can be used later (e.g. waitableSetWait)
WaitableSetNew,
/// Wait on a given waitable
///
/// Guest code uses this to wait on a waitable that has been already created
/// The waitable set index is relevant to the implicit current task.
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-waitable-setwait
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// function waitableSetWait(
/// componentIdx: i32,
/// isAsync: boolean,
/// memory: i32,
/// waitableSetRep: i32,
/// resultPtr: i32
/// );
/// ```
///
/// The results of the poll should be set in the provided pointer
WaitableSetWait,
/// Poll a given waitable set
///
/// Guest code uses this builtin to poll whether a waitable set is finished or not,
/// yielding to other tasks while doing so.
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-waitable-setpoll
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type i32 = number;
/// function waitableSetPoll(
/// componentIdx: i32,
/// isAsync: boolean,
/// memory: i32,
/// waitableSetRep: i32,
/// resultPtr: i32
/// );
/// ```
///
/// The results of the poll should be set in the provided pointer
WaitableSetPoll,
/// Drop a given waitable set
///
/// Guest code uses this builtin to remove the waitable set in it's entirety from a component instance's tables.
/// The component instance is known via the current task.
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-waitable-setdrop
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type i32 = number;
/// function waitableSetDrop(componentIdx: i32, waitableSetRep: i32);
/// ```
WaitableSetDrop,
/// JS helper function for removing a waitable set
RemoveWaitableSet,
/// Join a given waitable set
///
/// Guest code uses this builtin to add a provided waitable to an existing waitable set.
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-waitablejoin
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// function waitableJoin(componentIdx: i32, waitableSetRep: i32, waitableRep: i32);
/// ```
///
/// If the waitable set index is zero (an otherwise invalid table index), join should *remove* the given waitable from any sets
/// that it may be a part of (of which there should only be one).
WaitableJoin,
}
impl WaitableIntrinsic {
/// Retrieve dependencies for this intrinsic
pub fn deps() -> &'static [&'static Intrinsic] {
&[]
}
/// Retrieve global names for
pub fn get_global_names() -> impl IntoIterator<Item = &'static str> {
[]
}
/// Get the name for the intrinsic
pub fn name(&self) -> &'static str {
match self {
Self::RemoveWaitableSet => "_removeWaitableSet",
Self::WaitableSetNew => "waitableSetNew",
Self::WaitableSetWait => "waitableSetWait",
Self::WaitableSetPoll => "waitableSetPoll",
Self::WaitableSetDrop => "waitableSetDrop",
Self::WaitableJoin => "waitableJoin",
Self::WaitableSetClass => "WaitableSet",
Self::WaitableClass => "Waitable",
}
}
/// Render an intrinsic to a string
pub fn render(&self, output: &mut Source, _render_args: &RenderIntrinsicsArgs<'_>) {
match self {
Self::WaitableSetClass => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_set_class = Self::WaitableSetClass.name();
let async_event_code_enum = Intrinsic::AsyncEventCodeEnum.name();
output.push_str(&format!(
r#"
class {waitable_set_class} {{
#componentIdx;
#waitables = [];
#pendingEvent = null;
#waiting = 0;
target;
constructor(componentIdx) {{
if (componentIdx === undefined) {{ throw new TypeError("missing/invalid component idx"); }}
this.#componentIdx = componentIdx;
this.target = `component [${{this.#componentIdx}}] waitable set`;
}}
componentIdx() {{ return this.#componentIdx; }}
numWaitables() {{ return this.#waitables.length; }}
numWaiting() {{ return this.#waiting; }}
incrementNumWaiting(n) {{ this.#waiting += n ?? 1; }}
decrementNumWaiting(n) {{ this.#waiting -= n ?? 1; }}
targets() {{ return this.#waitables.map(w => w.target); }}
setTarget(tgt) {{ this.target = tgt; }}
shuffleWaitables() {{
this.#waitables = this.#waitables
.map(value => ({{ value, sort: Math.random() }}))
.sort((a, b) => a.sort - b.sort)
.map(({{ value }}) => value);
}}
removeWaitable(waitable) {{
const existing = this.#waitables.find(w => w === waitable);
if (!existing) {{ return undefined; }}
this.#waitables = this.#waitables.filter(w => w !== waitable);
return waitable;
}}
addWaitable(waitable) {{
this.removeWaitable(waitable);
this.#waitables.push(waitable);
}}
hasPendingEvent() {{
{debug_log_fn}('[{waitable_set_class}#hasPendingEvent()] args', {{
componentIdx: this.#componentIdx,
waitableSet: this,
waitableSetTargets: this.targets(),
}});
const waitable = this.#waitables.find(w => w.hasPendingEvent());
return waitable !== undefined;
}}
getPendingEvent() {{
{debug_log_fn}('[{waitable_set_class}#getPendingEvent()] args', {{
componentIdx: this.#componentIdx,
waitableSet: this,
}});
for (const waitable of this.#waitables) {{
if (!waitable.hasPendingEvent()) {{ continue; }}
const event = waitable.getPendingEvent();
{debug_log_fn}('[{waitable_set_class}#getPendingEvent()] found pending event', {{
waitable,
event,
}});
return event;
}}
throw new Error('no waitables had a pending event');
}}
async waitUntil(opts) {{
{debug_log_fn}('[{waitable_set_class}#waitUntil()] args', {{ opts }});
// TODO(threads): this task should be the thread
const {{ readyFn, task, cancellable }} = opts;
let event;
this.incrementNumWaiting();
const keepGoing = await task.suspendUntil({{
readyFn: () => {{
const hasPendingEvent = this.hasPendingEvent();
const ready = readyFn();
return ready && hasPendingEvent;
}},
cancellable,
}});
if (keepGoing) {{
event = this.getPendingEvent();
}} else {{
event = {{
code: {async_event_code_enum}.TASK_CANCELLED,
payload0: 0,
payload1: 0,
}};
}}
this.decrementNumWaiting();
return event;
}}
}}
"#
));
}
Self::WaitableClass => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_class = Self::WaitableClass.name();
let promise_with_resolvers_fn = Intrinsic::PromiseWithResolversPonyfill.name();
output.push_str(&format!(
r#"
class {waitable_class} {{
#componentIdx;
#pendingEventFn = null;
#promise;
#resolve;
#reject;
#waitableSet = null;
#idx = null; // to component-global waitables
target;
constructor(args) {{
const {{ componentIdx, target }} = args;
this.#componentIdx = componentIdx;
this.target = args.target;
this.#resetPromise();
}}
componentIdx() {{ return this.#componentIdx; }}
isInSet() {{ return this.#waitableSet !== null; }}
idx() {{ return this.#idx; }}
setIdx(idx) {{
if (idx === 0) {{ throw new Error("waitable idx cannot be zero"); }}
this.#idx = idx;
}}
setTarget(tgt) {{ this.target = tgt; }}
#resetPromise() {{
const {{ promise, resolve, reject }} = {promise_with_resolvers_fn}()
this.#promise = promise;
this.#resolve = resolve;
this.#reject = reject;
}}
resolve() {{ this.#resolve(); }}
reject(err) {{ this.#reject(err); }}
promise() {{ return this.#promise; }}
hasPendingEvent() {{
// {debug_log_fn}('[{waitable_class}#hasPendingEvent()]', {{
// componentIdx: this.#componentIdx,
// waitable: this,
// waitableSet: this.#waitableSet,
// hasPendingEvent: this.#pendingEventFn !== null,
// }});
return this.#pendingEventFn !== null;
}}
setPendingEvent(fn) {{
{debug_log_fn}('[{waitable_class}#setPendingEvent()] args', {{
waitable: this,
inSet: this.#waitableSet,
}});
this.#pendingEventFn = fn;
}}
getPendingEvent() {{
{debug_log_fn}('[{waitable_class}#getPendingEvent()] args', {{
waitable: this,
inSet: this.#waitableSet,
hasPendingEvent: this.#pendingEventFn !== null,
}});
if (this.#pendingEventFn === null) {{ return null; }}
const eventFn = this.#pendingEventFn;
this.#pendingEventFn = null;
const e = eventFn();
this.#resetPromise();
return e;
}}
join(waitableSet) {{
{debug_log_fn}('[{waitable_class}#join()] args', {{
waitable: this,
waitableSet: waitableSet,
}});
if (this.#waitableSet) {{ this.#waitableSet.removeWaitable(this); }}
if (!waitableSet) {{
this.#waitableSet = null;
return;
}}
waitableSet.addWaitable(this);
this.#waitableSet = waitableSet;
}}
drop() {{
{debug_log_fn}('[{waitable_class}#drop()] args', {{
componentIdx: this.#componentIdx,
waitable: this,
}});
if (this.hasPendingEvent()) {{
throw new Error('waitables with pending events cannot be dropped');
}}
this.join(null);
}}
}}
"#
));
}
Self::WaitableSetNew => {
let debug_log_fn = Intrinsic::DebugLog.name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let waitable_set_class = Self::WaitableSetClass.name();
let waitable_set_new_fn = Self::WaitableSetNew.name();
output.push_str(&format!(r#"
function {waitable_set_new_fn}(componentIdx) {{
{debug_log_fn}('[{waitable_set_new_fn}()] args', {{ componentIdx }});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state) {{throw new Error(`missing async state for component idx [${{componentIdx}}]`); }}
const wset = new {waitable_set_class}(componentIdx);
const rep = state.handles.insert(wset);
if (typeof rep !== 'number') {{ throw new Error(`invalid/missing waitable set rep [${{rep}}]`); }}
{debug_log_fn}('[{waitable_set_new_fn}()] created waitable set', {{ componentIdx, rep }});
return rep;
}}
"#));
}
Self::WaitableSetWait => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_set_wait_fn = Self::WaitableSetWait.name();
let current_task_get_fn =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::GetCurrentTask).name();
let store_event_in_component_memory_fn =
Intrinsic::Host(HostIntrinsic::StoreEventInComponentMemory).name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let waitable_set_class = Self::WaitableSetClass.name();
output.push_str(&format!(r#"
async function {waitable_set_wait_fn}(ctx, waitableSetRep, resultPtr) {{
{debug_log_fn}('[{waitable_set_wait_fn}()] args', {{ ctx, waitableSetRep, resultPtr }});
const {{
componentIdx,
isAsync,
memoryIdx,
getMemoryFn,
}} = ctx;
const taskMeta = {current_task_get_fn}(componentIdx);
if (!taskMeta) {{ throw Error('invalid/missing async task meta'); }}
const task = taskMeta.task;
if (!task) {{ throw Error('invalid/missing async task'); }}
if (task.componentIdx() !== componentIdx) {{
throw Error(`task component [${{task.componentIdx}}] !== executing component [${{componentIdx}}]`);
}}
const memory = getMemoryFn();
const cstate = {get_or_create_async_state_fn}(componentIdx);
const wset = await cstate.handles.get(waitableSetRep);
if (!(wset instanceof {waitable_set_class})) {{
throw new Error(`non-waitable set returned from component state handles @ [${{waitableSetRep}}]`);
}}
const event = await wset.waitUntil({{ readyFn: () => true, task, cancellable: false }});
return {store_event_in_component_memory_fn}({{ memory, ptr: resultPtr, event }});
}}
"#));
}
Self::WaitableSetPoll => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_set_poll_fn = Self::WaitableSetPoll.name();
let current_task_get_fn =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::GetCurrentTask).name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let store_event_in_component_memory_fn =
HostIntrinsic::StoreEventInComponentMemory.name();
let async_event_code_enum = Intrinsic::AsyncEventCodeEnum.name();
output.push_str(&format!(r#"
function {waitable_set_poll_fn}(ctx, waitableSetRep, resultPtr) {{
const {{ componentIdx, memoryIdx, getMemoryFn, isAsync, isCancellable }} = ctx;
{debug_log_fn}('[{waitable_set_poll_fn}()] args', {{
componentIdx,
memoryIdx,
waitableSetRep,
resultPtr,
}});
const taskMeta = {current_task_get_fn}(componentIdx);
if (!taskMeta) {{ throw Error('invalid/missing current task meta'); }}
if (taskMeta.componentIdx !== componentIdx) {{
throw Error('task component idx [' + task.componentIdx + '] != component instance ID [' + componentIdx + ']');
}}
const task = taskMeta.task;
if (!task) {{ throw Error('invalid/missing async task in task meta'); }}
if (task.componentIdx() !== componentIdx) {{
throw Error(`task component idx [${{task.componentIdx()}}] does not match generated [${{componentIdx}}]`);
}}
const cstate = {get_or_create_async_state_fn}(task.componentIdx());
const wset = cstate.handles.get(waitableSetRep);
if (!wset) {{
throw new Error(`missing waitable set [${{waitableSetRep}}] in component [${{componentIdx}}]`);
}}
let event;
const cancelDelivered = task.deliverPendingCancel({{ cancellable: isCancellable }});
if (cancelDelivered) {{
event = {{ code: {async_event_code_enum}.TASK_CANCELLED, payload0: 0, payload1: 0 }};
}} else if (!wset.hasPendingEvent()) {{
event = {{ code: {async_event_code_enum}.NONE, payload0: 0, payload1: 0 }};
}} else {{
event = wset.getPendingEvent();
}}
const eventCode = {store_event_in_component_memory_fn}({{ event, ptr: resultPtr, memory: getMemoryFn() }});
return eventCode;
}}
"#));
}
Self::WaitableSetDrop => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_set_drop_fn = Self::WaitableSetDrop.name();
let current_task_get_fn =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::GetCurrentTask).name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let remove_waitable_set_fn = Self::RemoveWaitableSet.name();
output.push_str(&format!("
function {waitable_set_drop_fn}(componentIdx, waitableSetRep) {{
{debug_log_fn}('[{waitable_set_drop_fn}()] args', {{ componentIdx, waitableSetRep }});
const task = {current_task_get_fn}(componentIdx);
if (!task) {{ throw new Error('invalid/missing async task'); }}
if (task.componentIdx !== componentIdx) {{
throw Error('task component idx [' + task.componentIdx + '] != component instance ID [' + componentIdx + ']');
}}
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state.mayLeave) {{ throw new Error('component instance is not marked as may leave, cannot be cancelled'); }}
{remove_waitable_set_fn}({{ state, waitableSetRep }});
}}
"));
}
Self::RemoveWaitableSet => {
let debug_log_fn = Intrinsic::DebugLog.name();
let remove_waitable_set_fn = Self::RemoveWaitableSet.name();
output.push_str(&format!(r#"
function {remove_waitable_set_fn}(args) {{
{debug_log_fn}('[{remove_waitable_set_fn}()] args', args);
const {{ state, waitableSetRep }} = args;
if (!state) {{ throw new TypeError("missing component state"); }}
if (!waitableSetRep) {{ throw new TypeError("missing component waitableSetRep"); }}
const ws = state.handles.get(waitableSetRep);
if (!ws) {{
throw new Error('cannot remove waitable set: no set present with rep [' + waitableSetRep + ']');
}}
if (ws.hasPendingEvent()) {{
throw new Error('waitable set cannot be removed with pending items remaining');
}}
const waitableSet = state.handles.get(waitableSetRep);
if (ws.numWaitables() > 0) {{
throw new Error('waitable set still contains waitables');
}}
if (ws.numWaiting() > 0) {{
throw new Error('waitable set still has other tasks waiting on it');
}}
state.handles.remove(waitableSetRep);
}}
"#));
}
Self::WaitableJoin => {
let debug_log_fn = Intrinsic::DebugLog.name();
let waitable_join_fn = Self::WaitableJoin.name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
output.push_str(&format!(r#"
function {waitable_join_fn}(componentIdx, waitableRep, waitableSetRep) {{
{debug_log_fn}('[{waitable_join_fn}()] args', {{ componentIdx, waitableSetRep, waitableRep }});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state) {{
throw new Error(`invalid/missing async state for component instance [${{componentIdx}}]`);
}}
if (!state.mayLeave) {{
throw new Error('component instance is not marked as may leave, cannot join waitable');
}}
const waitableObj = state.handles.get(waitableRep);
if (!waitableObj) {{
throw new Error(`missing waitable obj (rep [${{waitableRep}}]), component idx [${{componentIdx}}])`);
}}
const waitable = waitableObj.getWaitable ? waitableObj.getWaitable() : waitableObj;
if (!waitable.join) {{
throw new Error("invalid waitable object, does not have join()");
}}
const waitableSet = waitableSetRep === 0 ? null : state.handles.get(waitableSetRep);
if (waitableSetRep !== 0 && !waitableSet) {{
throw new Error(`missing waitable set [${{waitableSetRep}}] in component idx [${{componentIdx}}]`);
}}
waitable.join(waitableSet);
}}
"#));
}
}
}
}