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
//! Intrinsics that represent helpers that enable Stream integration
use crate::intrinsics::Intrinsic;
use crate::intrinsics::component::ComponentIntrinsic;
use crate::source::Source;
use super::async_task::AsyncTaskIntrinsic;
/// This enum contains intrinsics that enable Stream
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum AsyncFutureIntrinsic {
/// The definition of the `FutureWritableEnd` JS class
///
/// This class serves as a shared implementation used by writable and readable ends
FutureEndClass,
/// The definition of the `FutureWritableEnd` JS class
FutureWritableEndClass,
/// The definition of the `FutureReadableEnd` JS class
FutureReadableEndClass,
/// Global that stores futures by component instance
///
/// ```ts
/// type i32 = number;
/// type Future = object; // see FutureClass
/// type GlobalFutureMap = Map<i32, Future>;
/// ```
GlobalFutureMap,
/// Create a new future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturenew
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type u32 = number; // >= 0
/// type u64 = bigint; // >= 0
/// function futureNew(typeRep: u32): u64;
/// ```
FutureNew,
/// Read from a future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-futurefuturereadwrite
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type i32 = number;
/// type u32 = number; // >=0
/// type i64 = bigint;
/// type StringEncoding = 'utf8' | 'utf16' | 'compact-utf16'; // see wasmtime_environ::StringEncoding
///
/// function futureRead(
/// componentIdx: i32,
/// memory: i32,
/// realloc: i32,
/// encoding: StringEncoding,
/// isAsync: bool,
/// typeRep: u32,
/// futureRep: u32,
/// ptr: u32,
/// count:u322
/// ): i64;
/// ```
FutureRead,
/// Write to a future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturereadwrite
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type i32 = number;
/// type u32 = number; // >=0
/// type i64 = bigint;
/// type StringEncoding = 'utf8' | 'utf16' | 'compact-utf16'; // see wasmtime_environ::StringEncoding
///
/// function futureWrite(
/// componentIdx: i32,
/// memory: i32,
/// realloc: i32,
/// encoding: StringEncoding,
/// isAsync: bool,
/// typeRep: u32,
/// futureRep: u32,
/// ptr: u32,
/// count:u322
/// ): i64;
/// ```
FutureWrite,
/// Cancel a read to a future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturecancel-readread
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type u32 = number; // >=0
/// type u64 = bigint; // >=0
///
/// function futureCancelRead(futureRep: u32, isAsync: boolean, readerRep: u32): u64;
/// ```
FutureCancelRead,
/// Cancel a write to a future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturecancel-writewrite
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type u32 = number; // >=0
/// type u64 = bigint; // >= 0
///
/// function futureCancelWrite(futureRep: u32, isAsync: boolean, writerRep: u32): u64;
/// ```
FutureCancelWrite,
/// Drop a the readable end of a Future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturedrop-readablewritable
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type u32 = number; // >=0
///
/// function futureDropReadable(futureRep: u32, readerRep: u32): bool;
/// ```
FutureDropReadable,
/// Drop a the writable end of a Future
///
/// See: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#-canon-streamfuturedrop-readablewritable
///
/// # Intrinsic implementation function
///
/// The function that implements this intrinsic has the following definition:
///
/// ```ts
/// type u32 = number; // >=0
///
/// function futureDropWritable(futureRep: u32, writerRep: u32): bool;
/// ```
FutureDropWritable,
}
impl AsyncFutureIntrinsic {
/// Retrieve dependencies for this intrinsic
pub fn deps() -> &'static [&'static Intrinsic] {
&[]
}
/// Retrieve global names for this intrinsic
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::GlobalFutureMap => "FUTURES",
Self::FutureEndClass => "FutureEnd",
Self::FutureWritableEndClass => "FutureWritableEnd",
Self::FutureReadableEndClass => "FutureReadableEnd",
Self::FutureNew => "futureNew",
Self::FutureRead => "futureRead",
Self::FutureWrite => "futureWrite",
Self::FutureDropReadable => "futureDropReadable",
Self::FutureDropWritable => "futureDropWritable",
Self::FutureCancelRead => "futureCancelRead",
Self::FutureCancelWrite => "futureCancelWrite",
}
}
/// Render an intrinsic to a string
pub fn render(&self, output: &mut Source) {
match self {
Self::GlobalFutureMap => {
let global_future_map = Self::GlobalFutureMap.name();
let rep_table_class = Intrinsic::RepTableClass.name();
output.push_str(&format!(
"
const {global_future_map} = new {rep_table_class}();
"
));
}
Self::FutureEndClass => {
let debug_log_fn = Intrinsic::DebugLog.name();
let future_end_class = Self::FutureEndClass.name();
output.push_str(&format!("
class {future_end_class} {{
#elementTypeRep = null;
#componentIdx = null;
constructor(args) {{
{debug_log_fn}('[{future_end_class}#constructor()] args', args);
if (!args?.elementTypeRep || typeof args.elementTypeRep !== 'number') {{
throw new TypeError('missing elementTypeRep [' + args.elementTypeRep + ']');
}}
if (args.elementTypeRep <= 0 || args.elementTypeRep > 2_147_483_647 )) {{
throw new TypeError('invalid elementTypeRep [' + args.elementTypeRep + ']');
}}
this.#elementTypeRep = args.elementTypeRep;
this.#componentIdx = args.componentIdx ??= null;
}}
elementTypeRep() {{ return this.#elementTypeRep; }}
isHostOwned() {{ return this.#componentIdx === -1; }}
}}
"));
}
Self::FutureReadableEndClass | Self::FutureWritableEndClass => {
let debug_log_fn = Intrinsic::DebugLog.name();
let (class_name, future_var_name, js_future_var_type) = match self {
Self::FutureReadableEndClass => (self.name(), "promise", "Promise"),
Self::FutureWritableEndClass => (self.name(), "resolve", "Function"),
_ => unreachable!("impossible future readable end class intrinsic"),
};
let future_end_class = Self::FutureEndClass.name();
output.push_str(&format!("
class {class_name} extends {future_end_class} {{
#copying = false;
#{future_var_name} = null;
#dropped = false;
constructor(args) {{
{debug_log_fn}('[{class_name}#constructor()] args', args);
super(args);
if (!args.{future_var_name} || !(args.{future_var_name} instanceof {js_future_var_type})) {{
throw new TypeError('missing/invalid {future_var_name}, expected {js_future_var_type}');
}}
this.#{future_var_name} = args.{future_var_name};
}}
isCopying() {{ return this.#copying; }}
drop() {{
if (self.#dropped) {{ throw new Error('already dropped'); }}
if (self.#copying) {{ throw new Error('cannot drop while copying'); }}
if (!self.#{future_var_name}) {{ throw new Error('missing/invalid {future_var_name}'); }}
this.#{future_var_name}.close();
super.drop();
self.#dropped = true;
}}
}}
"));
}
// TODO: Futures need a class
Self::FutureNew => {
let debug_log_fn = Intrinsic::DebugLog.name();
let future_new_fn = Self::FutureNew.name();
let future_readable_end_class = Self::FutureReadableEndClass.name();
let future_writable_end_class = Self::FutureWritableEndClass.name();
let global_future_map = Self::GlobalFutureMap.name();
let current_task_get_fn =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::GetCurrentTask).name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
output.push_str(&format!("
function {future_new_fn}(componentIdx, elementTypeRep) {{
{debug_log_fn}('[{future_new_fn}()] args', {{ componentIdx, elementTypeRep }});
const task = {current_task_get_fn}(componentIdx);
if (!task) {{ throw new Error('invalid/missing async task'); }}
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state.mayLeave) {{ throw new Error('component instance is not marked as may leave'); }}
let future = new Promise();
let writeEndWaitableIdx = {global_future_map}.insert(new {future_writable_end_class}({{
isFuture: true,
elementTypeRep,
}}));
let readEndWaitableIdx = {global_future_map}.insert(new {future_readable_end_class}({{
isFuture: true,
elementTypeRep,
}}));
return BigInt(writeEndWaitableIdx) << 32n | BigInt(readEndWaitableIdx);
}}
"));
}
// TODO: fix return from processFn
Self::FutureWrite | Self::FutureRead => {
let debug_log_fn = Intrinsic::DebugLog.name();
let future_write_fn = Self::FutureWrite.name();
let global_future_map = Self::GlobalFutureMap.name();
let global_buffer_mgr = Intrinsic::GlobalBufferManager.name();
let is_write = matches!(self, Self::FutureWrite);
let future_end_class = if is_write {
Self::FutureWritableEndClass.name()
} else {
Self::FutureReadableEndClass.name()
};
let is_borrowed_type = Intrinsic::IsBorrowedType.name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let async_blocked_constant =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::AsyncBlockedConstant).name();
output.push_str(&format!("
function {future_write_fn}(
componentIdx,
memoryIdx,
reallocIdx,
stringEncoding,
isAsync,
futureIdx,
typeIdx,
futureIdx,
ptr,
count,
) {{
{debug_log_fn}('[{future_write_fn}()] args', {{
componentIdx,
memoryIdx,
reallocIdx,
stringEncoding,
isAsync,
futureIdx,
typeIdx,
futureIdx,
ptr,
count,
}});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state.mayLeave) {{ throw new Error('component instance is not marked as may leave'); }}
const future = {global_future_map}.get(futureIdx);
if (!future) {{ throw new Error('missing future'); }}
const futureEnd = {global_future_map}.get(futureEndIdx);
if (!futureEnd) {{ throw new Error('missing future end'); }}
if (!(futureEnd instanceof {future_end_class})) {{ new Error('invalid future end, expected [{future_end_class}]'); }}
if (future.elementTypeRep() !== futureEnd.elementTypeRep()) {{
throw new Error('future type rep [' + future.elementTypeRep() + '] does not match future end [' + futureEnd.elementTypeRep() + ']');
}}
if (futureEnd.isCopying()) {{ throw new Error('future end is copying'); }}
if (futureEnd.isDone()) {{ throw new Error('future end is done'); }}
if ({is_borrowed_type}(componentIdx, future.elementTypeRep())) {{ throw new Error('borrowed types not supported'); }}
const {{ id: bufferID }} = {global_buffer_mgr}.createBuffer({{
componentIdx,
start,
len,
typeIdx,
writable,
readable,
target: `future read/write`,
}});
const processFn = (result) => {{
if (.remaining(bufferID) !== 0) {{
if ({global_buffer_mgr}.remaining(bufferID) === 0 && result != CopyResult.COMPLETED) {{
throw new Error('incomplete copy with future data remanining');
}}
if ({global_buffer_mgr}.remaining(bufferID) !== 0 && result === CopyResult.COMPLETED) {{
throw new Error('remaining data with completed copy');
}}
futureEnd.clearCopying();
if (result === CopyResult.DROPPED || result === CopyResult.FUTURE_WRITE) {{
e.markDone();
}}
return {{ eventCode, index, result }};
}};
futureEnd.copy({{
bufferID,
onCopyDone: (result) => {{
if (result === CopyResult.DROPPED && eventCode === EventCode.FUTURE_WRITE) {{
throw new Error('cannot have a future write when the future is dropped');
}}
futureEnd.setEvent(processFn(result));
}}
}});
if (!isAsync && !e.hasPendingEvent()) {{
// TODO: replace with what block on used to be? wait for?
// await task.blockOn({{ promise: e.waitForPendingEvent(), isAsync: false }});
throw new Error('not implemented');
}}
if (futureEnd.hasPendingEvent()) {{
const {{ code, payload0: index, payload1 }} = futureEnd.getPendingEvent();
if (code !== eventCode || index != 1) {{
throw new Error('invalid event, does not match expected event code');
}}
return payload;
}}
return {async_blocked_constant};
}}
"));
}
Self::FutureCancelRead | Self::FutureCancelWrite => {
let debug_log_fn = Intrinsic::DebugLog.name();
let is_cancel_write = matches!(self, Self::FutureCancelWrite);
let future_end_class = if is_cancel_write {
Self::FutureWritableEndClass.name()
} else {
Self::FutureReadableEndClass.name()
};
let future_cancel_fn = Self::FutureCancelRead.name();
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
let global_future_map = Self::GlobalFutureMap.name();
let async_blocked_const =
Intrinsic::AsyncTask(AsyncTaskIntrinsic::AsyncBlockedConstant).name();
let async_event_code_enum = Intrinsic::AsyncEventCodeEnum.name();
output.push_str(&format!("
async function {future_cancel_fn}(
futureIdx,
isAsync,
futureEndIdx,
) {{
{debug_log_fn}('[{future_cancel_fn}()] args', {{
futureIdx,
isAsync,
futureEndIdx,
}});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state.mayLeave) {{ throw new Error('component instance is not marked as may leave'); }}
const futureEnd = {global_future_map}.get(futureEndIdx);
if (!futureEnd) {{ throw new Error('missing future end with idx [' + futureEndIdx + ']'); }}
if (!(futureEnd instanceof {future_end_class})) {{ throw new Error('invalid future end, expected value of type [{future_end_class}]'); }}
if (futureEnd.elementTypeRep() !== future.elementTypeRep()) {{
throw new Error('future type [' + future.elementTypeRep() + '], does not match future end type [' + futureEnd.elementTypeRep() + ']');
}}
if (!futureEnd.isCopying()) {{ throw new Error('future end is not copying, cannot cancel'); }}
if (!futureEnd.hasPendingEvent()) {{
// TODO: cancel the shared thing (waitable?)
if (!futureEnd.hasPendingEvent()) {{
if (!isAsync) {{
// TODO: repalce with what task.blockOn used to do
// await task.blockOn({{ promise: futureEnd.waitable, isAsync: false }});
throw new Error('not implemented');
}} else {{
return {async_blocked_const};
}}
}}
}}
const {{ code, payload0: index, payload1: payload }} = futureEnd.getPendingEvent();
if (futureEnd.isCopying()) {{ throw new Error('future end is still in copying state'); }}
if (code !== {async_event_code_enum}) {{ throw new Error('unexpected event code [' + code + '], expected [' + {async_event_code_enum} + ']'); }}
if (index !== streamEndIdx) {{ throw new Error('index does not match stream end'); }}
return payload;
}}
"));
}
// TODO: fill in future class impl (check for matching element types)
Self::FutureDropReadable | Self::FutureDropWritable => {
let debug_log_fn = Intrinsic::DebugLog.name();
let future_drop_fn = self.name();
let is_writable = matches!(self, Self::FutureDropWritable);
let global_future_map = Self::GlobalFutureMap.name();
let future_end_class = if is_writable {
Self::FutureWritableEndClass.name()
} else {
Self::FutureReadableEndClass.name()
};
let get_or_create_async_state_fn =
Intrinsic::Component(ComponentIntrinsic::GetOrCreateAsyncState).name();
output.push_str(&format!("
function {future_drop_fn}(
futureIdx,
futureEndIdx,
) {{
{debug_log_fn}('[{future_drop_fn}()] args', {{
futureIdx,
futureEndIdx,
}});
const state = {get_or_create_async_state_fn}(componentIdx);
if (!state.mayLeave) {{ throw new Error('component instance is not marked as may leave'); }}
const future = {global_future_map}.remove(futureIdx);
const futureEnd = {global_future_map}.remove(futureEndIdx);
if (!(futureEnd instanceof {future_end_class}) {{ throw new Error('invalid future end, expected [{future_end_class}]'); }}
if (futureEnd.elementTypeRep() !== future.elementTypeRep()) {{
throw new Error('future type [' + future.elementTypeRep() + '], does not match future end type [' + futureEnd.elementTypeRep() + ']');
}}
futureEnd.drop();
}}
"));
}
}
}
}