-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathwasi.ts
1316 lines (1094 loc) · 37.7 KB
/
wasi.ts
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import RAL from '../ral';
// We need to move this to a more generic place
import { BigInts } from '../converter';
import { Errno, WasiError } from '../wasi';
const utf8Decoder = RAL().TextDecoder.create('utf-8');
const utf8Encoder = RAL().TextEncoder.create('utf-8');
export interface Memory {
readonly buffer: ArrayBuffer;
readonly raw: Uint8Array;
readonly view: DataView;
alloc: (align: size, size: size) => ptr;
realloc: (ptr: ptr, oldSize: size, align: size, newSize: size) => ptr;
}
export type encodings = 'utf-8' | 'utf-16' | 'latin1+utf-16';
export interface Options {
encoding: encodings;
}
export type alignment = 1 | 2 | 4 | 8;
function align(ptr: ptr, alignment: alignment): ptr {
return Math.ceil(ptr / alignment) * alignment;
}
export type i32 = number;
export type i64 = bigint;
export type f32 = number;
export type f64 = number;
export type wasmType = i32 | i64 | f32 | f64;
export type wasmTypeName = 'i32' | 'i64' | 'f32' | 'f64';
namespace WasmTypes {
const $32 = new DataView(new ArrayBuffer(4));
const $64 = new DataView(new ArrayBuffer(8));
export function reinterpret_i32_as_f32(i32: number): f32 {
$32.setInt32(0, i32, true);
return $32.getFloat32(0, true);
}
export function reinterpret_f32_as_i32(f32: f32): i32 {
$32.setFloat32(0, f32, true);
return $32.getInt32(0, true);
}
export function convert_i64_to_i32(i64: i64): i32 {
return BigInts.asNumber(i64);
}
export function convert_i32_to_i64(i32: i32): i64 {
return BigInt(i32);
}
export function reinterpret_i64_as_f32(i64: i64): f32 {
const i32 = convert_i64_to_i32(i64);
return reinterpret_i32_as_f32(i32);
}
export function reinterpret_f32_as_i64(f32: f32): i64 {
const i32 = reinterpret_f32_as_i32(f32);
return convert_i32_to_i64(i32);
}
export function reinterpret_i64_as_f64(i64: i64): f64 {
$64.setBigInt64(0, i64, true);
return $64.getFloat64(0, true);
}
export function reinterpret_f64_as_i64(f64: f64): i64 {
$64.setFloat64(0, f64, true);
return $64.getBigInt64(0, true);
}
}
export type FlatValuesIter = Iterator<wasmType, wasmType>;
class CoerceValueIter implements Iterator<wasmType, wasmType> {
private index: number;
constructor(private readonly values: FlatValuesIter, private haveFlatTypes: readonly wasmTypeName[], private wantFlatTypes: readonly wasmTypeName[]) {
if (haveFlatTypes.length !== wantFlatTypes.length) {
throw new WasiError(Errno.inval);
}
this.index = 0;
}
next(): IteratorResult<wasmType, wasmType> {
const value = this.values.next();
if (value.done) {
return value;
}
const haveType = this.haveFlatTypes[this.index];
const wantType = this.wantFlatTypes[this.index++];
if (haveType === 'i32' && wantType === 'f32') {
return { done: false, value: WasmTypes.reinterpret_i32_as_f32(value.value as i32) };
} else if (haveType === 'i64' && wantType === 'i32') {
return { done: false, value: WasmTypes.convert_i64_to_i32(value.value as i64) };
} else if (haveType === 'i64' && wantType === 'f32') {
return { done: false, value: WasmTypes.reinterpret_i64_as_f32(value.value as i64) };
} else if (haveType === 'i64' && wantType === 'f64') {
return { done: false, value: WasmTypes.reinterpret_i64_as_f64(value.value as i64) };
} else {
return value;
}
}
}
export interface ComponentModelType<J> {
readonly size: number;
readonly alignment: alignment;
readonly flatTypes: ReadonlyArray<wasmTypeName>;
load(memory: Memory, ptr: ptr, options: Options): J;
liftFlat(memory: Memory, values: Iterator<wasmType, wasmType>, options: Options): J;
alloc(memory: Memory): ptr;
store(memory: Memory, ptr: ptr, value: J, options: Options): void;
lowerFlat(result: wasmType[], memory: Memory, value: J, options: Options): void;
}
export type GenericComponentModelType = ComponentModelType<any>;
export type bool = number;
export const bool: ComponentModelType<boolean> = {
size: 1,
alignment: 1,
flatTypes: ['i32'],
load(memory, ptr: ptr<u8>): boolean {
return memory.view.getUint8(ptr) !== 0;
},
liftFlat(_memory, values): boolean {
const value = values.next().value;
if (value < 0) {
throw new Error(`Invalid bool value ${value}`);
}
return value !== 0;
},
alloc(memory): ptr<u8> {
return memory.alloc(bool.size, bool.alignment);
},
store(memory, ptr: ptr<u8>, value: boolean): void {
memory.view.setUint8(ptr, value ? 1 : 0);
},
lowerFlat(result, _memory, value: boolean): void {
result.push(value ? 1 : 0);
}
};
export type u8 = number;
namespace $u8 {
export const size = 1;
export const alignment: alignment = 1;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
export const LOW_VALUE = 0;
export const HIGH_VALUE = 255;
export function load(memory: Memory, ptr: ptr<u8>): u8 {
return memory.view.getUint8(ptr);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): u8 {
const value = values.next().value;
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u8 value ${value}`);
}
return value as u8;
}
export function alloc(memory: Memory): ptr<u8> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<u8>, value: u8): void {
memory.view.setUint8(ptr, value);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: u8): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u8 value ${value}`);
}
result.push(value);
}
}
export const u8:ComponentModelType<number> = $u8;
export type u16 = number;
namespace $u16 {
export const size = 2;
export const alignment: alignment = 2;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
export const LOW_VALUE = 0;
export const HIGH_VALUE = 65535;
export function load(memory: Memory, ptr: ptr): u16 {
return memory.view.getUint16(ptr, true);
}
export function liftFlat(_memory: Memory, values:FlatValuesIter): u16 {
const value = values.next().value;
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u16 value ${value}`);
}
return value as u16;
}
export function alloc(memory: Memory): ptr<u16> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<u16>, value: u16): void {
memory.view.setUint16(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u16 value ${value}`);
}
result.push(value);
}
}
export const u16: ComponentModelType<number> = $u16;
export type u32 = number;
namespace $u32 {
export const size = 4;
export const alignment: alignment = 4;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
export const LOW_VALUE = 0;
export const HIGH_VALUE = 4294967295; // 2 ^ 32 - 1
export function load(memory: Memory, ptr: ptr): u32 {
return memory.view.getUint32(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): u32 {
const value = values.next().value;
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u32 value ${value}`);
}
return value as u32;
}
export function alloc(memory: Memory): ptr<u32> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<u32>, value: u32): void {
memory.view.setUint32(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u32 value ${value}`);
}
result.push(value);
}
}
export const u32: ComponentModelType<number> = $u32;
export type u64 = bigint;
namespace $u64 {
export const size = 8;
export const alignment: alignment = 8;
export const flatTypes: readonly wasmTypeName[] = ['i64'];
export const LOW_VALUE = 0n;
export const HIGH_VALUE = 18446744073709551615n; // 2 ^ 64 - 1
export function load(memory: Memory, ptr: ptr): u64 {
return memory.view.getBigUint64(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): u64 {
const value = values.next().value;
if (value < LOW_VALUE) {
throw new Error(`Invalid u64 value ${value}`);
}
return value as u64;
}
export function alloc(memory: Memory): ptr<u64> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<u64>, value: u64): void {
memory.view.setBigUint64(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: bigint): void {
if (value < LOW_VALUE) {
throw new Error(`Invalid u64 value ${value}`);
}
result.push(value);
}
}
export const u64: ComponentModelType<bigint> = $u64;
export type s8 = number;
namespace $s8 {
export const size = 1;
export const alignment: alignment = 1;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
const LOW_VALUE = -128;
const HIGH_VALUE = 127;
export function load(memory: Memory, ptr: ptr): s8 {
return memory.view.getInt8(ptr);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): s8 {
const value = values.next().value;
// All int values in the component model are transferred as unsigned
// values. So for signed values we need to convert them back. First
// we check if the value is in range of the corresponding unsigned
// value and the convert it to a signed value.
if (value < $u8.LOW_VALUE || value > $u8.HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid u8 value ${value}`);
}
if (value <= HIGH_VALUE) {
return value as s8;
} else {
return (value as u8) - 256;
}
}
export function alloc(memory: Memory): ptr<s8> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<s8>, value: s8): void {
memory.view.setInt8(ptr, value);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid s8 value ${value}`);
}
result.push((value < 0) ? (value + 256) : value);
}
}
export const s8: ComponentModelType<number> = $s8;
export type s16 = number;
namespace $s16 {
export const size = 2;
export const alignment: alignment = 2;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
const LOW_VALUE = -32768; // -2 ^ 15
const HIGH_VALUE = 32767; // 2 ^ 15 - 1
export function load(memory: Memory, ptr: ptr): s16 {
return memory.view.getInt16(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): s16 {
const value = values.next().value;
if (value < $u16.LOW_VALUE || value > $u16.HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid s16 value ${value}`);
}
return (value <= HIGH_VALUE) ? value as s16 : (value as u16) - 65536;
}
export function alloc(memory: Memory): ptr<s16> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<s16>, value: s16): void {
memory.view.setInt16(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid s16 value ${value}`);
}
result.push((value < 0) ? (value + 65536) : value);
}
}
export const s16: ComponentModelType<number> = $s16;
export type s32 = number;
namespace $s32 {
export const size = 4;
export const alignment: alignment = 4;
export const flatTypes: readonly wasmTypeName[] = ['i32'];
const LOW_VALUE = -2147483648; // -2 ^ 31
const HIGH_VALUE = 2147483647; // 2 ^ 31 - 1
export function load(memory: Memory, ptr: ptr): s32 {
return memory.view.getInt32(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): s32 {
const value = values.next().value;
if (value < $u32.LOW_VALUE || value > $u32.HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid s32 value ${value}`);
}
return (value <= HIGH_VALUE) ? value as s32 : (value as u32) - 4294967296;
}
export function alloc(memory: Memory): ptr<s32> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<s32>, value: s32): void {
memory.view.setInt32(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE || !Number.isInteger(value)) {
throw new Error(`Invalid s32 value ${value}`);
}
result.push((value < 0) ? (value + 4294967296) : value);
}
}
export const s32: ComponentModelType<number> = $s32;
export type s64 = bigint;
namespace $s64 {
export const size = 8;
export const alignment: alignment = 8;
export const flatTypes: readonly wasmTypeName[] = ['i64'];
const LOW_VALUE = -9223372036854775808n; // -2 ^ 63
const HIGH_VALUE = 9223372036854775807n; // 2 ^ 63 - 1
export function load(memory: Memory, ptr: ptr<s64>): s64 {
return memory.view.getBigInt64(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): s64 {
const value = values.next().value;
if (value < $u64.LOW_VALUE) {
throw new Error(`Invalid s64 value ${value}`);
}
return (value <= HIGH_VALUE) ? value as s64 : (value as u64) - 18446744073709551616n;
}
export function alloc(memory: Memory): ptr<s64> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<s64>, value: s64): void {
memory.view.setBigInt64(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: bigint): void {
if (value < LOW_VALUE || value > HIGH_VALUE) {
throw new Error(`Invalid s64 value ${value}`);
}
result.push((value < 0) ? (value + 18446744073709551616n) : value);
}
}
export const s64: ComponentModelType<bigint> = $s64;
export type float32 = number;
namespace $float32 {
export const size = 4;
export const alignment:alignment = 4;
export const flatTypes: readonly wasmTypeName[] = ['f32'];
const LOW_VALUE = -3.4028234663852886e+38;
const HIGH_VALUE = 3.4028234663852886e+38;
const NAN = 0x7fc00000;
export function load(memory: Memory, ptr: ptr): float32 {
return memory.view.getFloat32(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): float32 {
const value = values.next().value;
if (value < LOW_VALUE || value > HIGH_VALUE) {
throw new Error(`Invalid float32 value ${value}`);
}
return value === NAN ? Number.NaN : value as float32;
}
export function alloc(memory: Memory): ptr<float32> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<float32>, value: float32): void {
memory.view.setFloat32(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE) {
throw new Error(`Invalid float32 value ${value}`);
}
result.push(Number.isNaN(value) ? NAN : value);
}
}
export const float32: ComponentModelType<number> = $float32;
export type float64 = number;
namespace $float64 {
export const size = 8;
export const alignment: alignment = 8;
export const flatTypes: readonly wasmTypeName[] = ['f64'];
const LOW_VALUE = -1 * Number.MAX_VALUE;
const HIGH_VALUE = Number.MAX_VALUE;
const NAN = 0x7ff8000000000000;
export function load(memory: Memory, ptr: ptr): float64 {
return memory.view.getFloat64(ptr, true);
}
export function liftFlat(_memory: Memory, values: FlatValuesIter): float64 {
const value = values.next().value;
if (value < LOW_VALUE || value > HIGH_VALUE) {
throw new Error(`Invalid float64 value ${value}`);
}
return value === NAN ? Number.NaN : value as float64;
}
export function alloc(memory: Memory): ptr<float64> {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr<float64>, value: float64): void {
memory.view.setFloat64(ptr, value, true);
}
export function lowerFlat(result: wasmType[], _memory: Memory, value: number): void {
if (value < LOW_VALUE || value > HIGH_VALUE) {
throw new Error(`Invalid float64 value ${value}`);
}
result.push(Number.isNaN(value) ? NAN : value);
}
}
export const float64: ComponentModelType<number> = $float64;
export type byte = u8;
export const byte: ComponentModelType<byte> = {
size: u8.size,
alignment: u8.alignment,
flatTypes: u8.flatTypes,
load: u8.load,
liftFlat: u8.liftFlat,
alloc: u8.alloc,
store: u8.store,
lowerFlat: u8.lowerFlat
};
export type size = u32;
export const size: ComponentModelType<size> = {
size: u32.size,
alignment: u32.alignment,
flatTypes: u32.flatTypes,
load: u32.load,
liftFlat: u32.liftFlat,
alloc: u32.alloc,
store: u32.store,
lowerFlat: u32.lowerFlat
};
export type ptr<_type = ArrayBuffer> = u32;
export const ptr: ComponentModelType<ptr> = {
size: u32.size,
alignment: u32.alignment,
flatTypes: u32.flatTypes,
load: u32.load,
liftFlat: u32.liftFlat,
alloc: u32.alloc,
store: u32.store,
lowerFlat: u32.lowerFlat
};
export interface char {
}
namespace $wstring {
const offsets = {
data: 0,
codeUnits: 4
};
export const size = 8;
export const alignment: alignment = 4;
export const flatTypes: readonly wasmTypeName[] = ['i32', 'i32'];
export function load(memory: Memory, ptr: ptr, options: Options): string {
const view = memory.view;
const dataPtr: ptr = view.getUint32(ptr + offsets.data);
const codeUnits: u32 = view.getUint32(ptr + offsets.codeUnits);
return loadFromRange(memory, dataPtr, codeUnits, options);
}
export function liftFlat(memory: Memory, values: FlatValuesIter, options: Options): string {
const dataPtr: ptr = values.next().value as ptr;
const codeUnits: u32 = values.next().value as u32;
return loadFromRange(memory, dataPtr, codeUnits, options);
}
export function alloc(memory: Memory): ptr {
return memory.alloc(size, alignment);
}
export function store(memory: Memory, ptr: ptr, str: string, options: Options): void {
const [ data, codeUnits ] = storeIntoRange(memory, str, options);
const view = memory.view;
view.setUint32(ptr + offsets.data, data, true);
view.setUint32(ptr + offsets.codeUnits, codeUnits, true);
}
export function lowerFlat(result: wasmType[], memory: Memory, str: string, options: Options): void {
result.push(...storeIntoRange(memory, str, options));
}
function loadFromRange(memory: Memory, data: ptr, codeUnits: u32, options: Options): string {
const encoding = options.encoding;
if (encoding === 'latin1+utf-16') {
throw new Error('latin1+utf-16 encoding not yet supported');
}
if (encoding === 'utf-8') {
const byteLength = codeUnits;
return utf8Decoder.decode(new Uint8Array(memory.buffer, data, byteLength));
} else if (encoding === 'utf-16') {
return String.fromCharCode(...(new Uint16Array(memory.buffer, data, codeUnits)));
} else {
throw new Error('Unsupported encoding');
}
}
function storeIntoRange(memory: Memory, str: string, options: Options): [ptr, u32] {
const { encoding } = options;
if (encoding === 'latin1+utf-16') {
throw new Error('latin1+utf-16 encoding not yet supported');
}
if (encoding === 'utf-8') {
const data = utf8Encoder.encode(str);
const dataPtr = memory.alloc(u8.alignment, data.length);
memory.raw.set(data, dataPtr);
return [dataPtr, data.length];
} else if (encoding === 'utf-16') {
const dataPtr = memory.alloc(u8.alignment, str.length * 2);
const data = new Uint16Array(memory.buffer, dataPtr, str.length);
for (let i = 0; i < str.length; i++) {
data[i] = str.charCodeAt(i);
}
return [dataPtr, data.length];
} else {
throw new Error('Unsupported encoding');
}
}
}
export const wstring: ComponentModelType<string> = $wstring;
interface TypedField {
readonly type: GenericComponentModelType;
readonly offset: number;
}
export interface JRecord {
[key: string]: any;
}
export type JTuple = JType[];
abstract class BaseRecordType<T extends JRecord | JTuple, F extends TypedField> implements ComponentModelType<T> {
private fields: F[];
public readonly size: size;
public readonly alignment: alignment;
public readonly flatTypes: readonly wasmTypeName[];
constructor(fields: F[]) {
this.fields = fields;
this.size = BaseRecordType.size(fields);
this.alignment = BaseRecordType.alignment(fields);
this.flatTypes = BaseRecordType.flatTypes(fields);
}
public load(memory: Memory, ptr: ptr, options: Options): T {
const result: JType[] = [];
for (const field of this.fields) {
const value = field.type.load(memory, ptr + field.offset, options);
result.push(value);
}
return this.create(this.fields, result);
}
public liftFlat(memory: Memory, values: FlatValuesIter, options: Options): T {
const result: JType[] = [];
for (const field of this.fields) {
result.push(field.type.liftFlat(memory, values, options));
}
return this.create(this.fields, result);
}
public alloc(memory: Memory): ptr {
return memory.alloc(this.size, this.alignment);
}
public store(memory: Memory, ptr: ptr, record: T, options: Options): void {
const values = this.elements(record, this.fields);
for (let i = 0; i < this.fields.length; i++) {
const field = this.fields[i];
const value = values[i];
field.type.store(memory, ptr + field.offset, value, options);
}
}
public lowerFlat(result: wasmType[], memory: Memory, record: T, options: Options): void {
const values = this.elements(record, this.fields);
for (let i = 0; i < this.fields.length; i++) {
const field = this.fields[i];
const value = values[i];
field.type.lowerFlat(result, memory, value, options);
}
}
protected abstract create(fields: F[], values: JType[]): T;
protected abstract elements(record: T, fields: F[]): JType[];
private static size(fields: TypedField[]): size {
let result: ptr = 0;
for (const field of fields) {
align(result, field.type.alignment);
result += field.type.size;
}
return result;
}
private static alignment(fields: TypedField[]): alignment {
let result: alignment = 1;
for (const field of fields) {
result = Math.max(result, field.type.alignment) as alignment;
}
return result;
}
private static flatTypes(fields: TypedField[]): readonly wasmTypeName[] {
const result: wasmTypeName[] = [];
for (const field of fields) {
result.push(...field.type.flatTypes);
}
return result;
}
}
interface RecordField extends TypedField {
readonly name: string;
readonly offset: number;
}
namespace RecordField {
export function create(name: string, offset: number, type: GenericComponentModelType): RecordField {
return { name, offset, type };
}
}
export class RecordType<T extends JRecord> extends BaseRecordType<T, RecordField> {
constructor(fields: [string, GenericComponentModelType][]) {
let offset = 0;
const recordFields: RecordField[] = [];
for (const [name, type] of fields) {
offset = align(offset, type.alignment);
recordFields.push(RecordField.create(name, offset, type));
offset += type.size;
}
super(recordFields);
}
protected create(fields: RecordField[], values: JType[]): T {
const result: JRecord = {};
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const value = values[i];
result[field.name] = value;
}
return result as T;
}
protected elements(record: T, fields: RecordField[]): JType[] {
const result: JType[] = [];
for (const field of fields) {
result.push(record[field.name]);
}
return result;
}
}
interface TupleField extends TypedField {
readonly offset: number;
}
namespace TupleField {
export function create(offset: number, type: GenericComponentModelType): TupleField {
return { offset, type };
}
}
export class TupleType<T extends JTuple> extends BaseRecordType<T, TupleField> {
constructor(fields: GenericComponentModelType[]) {
let offset = 0;
const tupleFields: TupleField[] = [];
for (const type of fields) {
offset = align(offset, type.alignment);
tupleFields.push(TupleField.create(offset, type));
offset += type.size;
}
super(tupleFields);
}
protected create(_fields: TupleField[], values: JType[]): T {
return values as JTuple as T;
}
protected elements(record: T, _fields: TupleField[]): JType[] {
return record;
}
}
export interface JFlags {
_flags: u32[];
}
export namespace flags {
export function size(fields: number): size {
if (fields === 0) {
return 0;
} else if (fields <= 8) {
return 1;
} else if (fields <= 16) {
return 2;
} else {
return 4 * num32Flags(fields);
}
}
export function alignment(fields: number): alignment {
if (fields <= 8) {
return 1;
} else if (fields <= 16) {
return 2;
} else {
return 4;
}
}
export function flatTypes(fields: number): 'i32'[] {
return new Array(num32Flags(fields)).fill('i32');
}
export function load(memory: Memory, ptr: ptr<u32[]>, fields: number): u32[] {
const numFlags = num32Flags(fields);
const result: u32[] = new Array(numFlags);
for (let i = 0; i < numFlags; i++) {
result[i] = memory.view.getUint32(ptr, true);
ptr += u32.size;
}
return result;
}
export function liftFlat(_memory: Memory, values: FlatValuesIter, fields: number): u32[] {
const numFlags = num32Flags(fields);
const result: u32[] = new Array(numFlags);
for (let i = 0; i < numFlags; i++) {
result[i] = values.next().value as u32;
}
return result;
}
export function store(memory: Memory, ptr: ptr<u32[]>, flags: u32[]): void {
for (const flag of flags) {
memory.view.setUint32(ptr, flag, true);
ptr += u32.size;
}
}
export function lowerFlat(result: wasmType[], _memory: Memory, flags: u32[]): void {
for (const flag of flags) {
result.push(flag);
}
}
function num32Flags(fields: number): number {
return Math.ceil(fields / 32);
}
}
export interface variantCase {
readonly index: u32;
readonly type: GenericComponentModelType | undefined;
readonly wantFlatTypes: wasmTypeName[] | undefined;
}
export namespace variantCase {
export function create(index: number, type: GenericComponentModelType | undefined): variantCase {
return { index, type, wantFlatTypes: type !== undefined ? [] : undefined };
}
}
export interface JVariantCase {
readonly _caseIndex: u32;
value: JType | undefined;
}
export class VariantType<T extends JVariantCase, I, V> implements ComponentModelType<T> {
private readonly cases: variantCase[];
private readonly ctor: new (caseIndex: I, value: V) => T;
private readonly discriminantType: GenericComponentModelType;
private readonly maxCaseAlignment: alignment;
public readonly size: size;
public readonly alignment: alignment;
public readonly flatTypes: readonly wasmTypeName[];
constructor(variants: (GenericComponentModelType | undefined)[], ctor: new (caseIndex: I, value: V) => T) {
const cases: variantCase[] = [];
for (let i = 0; i < variants.length; i++) {
const type = variants[i];
cases.push(variantCase.create(i, type));
}
this.cases = cases;
this.ctor = ctor;
this.discriminantType = VariantType.discriminantType(cases.length);
this.maxCaseAlignment = VariantType.maxCaseAlignment(cases);
this.size = VariantType.size(this.discriminantType, cases);
this.alignment = VariantType.alignment(this.discriminantType, cases);
this.flatTypes = VariantType.flatTypes(this.discriminantType, cases);
}
public load(memory: Memory, ptr: ptr, options: Options): T {
const caseIndex = this.discriminantType.load(memory, ptr, options);
ptr += this.discriminantType.size;
const caseVariant = this.cases[caseIndex];
if (caseVariant.type === undefined) {
return new this.ctor(caseIndex, undefined as any);
} else {
ptr = align(ptr, this.maxCaseAlignment);
const value = caseVariant.type.load(memory, ptr, options);
return new this.ctor(caseIndex, value);
}
}
public liftFlat(memory: Memory, values: FlatValuesIter, options: Options): T {
const caseIndex = this.discriminantType.liftFlat(memory, values, options);
const caseVariant = this.cases[caseIndex];
if (caseVariant.type === undefined) {
return new this.ctor(caseIndex, undefined as any);
} else {
// The first flat type is the discriminant type. So skip it.
const iter = new CoerceValueIter(values, this.flatTypes.slice(1), caseVariant.wantFlatTypes!);
const value = caseVariant.type.liftFlat(memory, iter, options);
return new this.ctor(caseIndex, value);
}
}
public alloc(memory: Memory): ptr {
return memory.alloc(this.alignment, this.size);
}
public store(memory: Memory, ptr: ptr, value: T, options: Options): void {
this.discriminantType.store(memory, ptr, value._caseIndex, options);
ptr += this.discriminantType.size;
const c = this.cases[value._caseIndex];
if (c.type !== undefined && value !== undefined) {
ptr = align(ptr, this.maxCaseAlignment);
c.type.store(memory, ptr, value, options);
}
}
public lowerFlat(result: wasmType[], memory: Memory, value: T, options: Options): void {
this.discriminantType.lowerFlat(result, memory, value._caseIndex, options);
const c = this.cases[value._caseIndex];
if (c.type !== undefined && value !== undefined) {
const payload: wasmType[] = [];
c.type.lowerFlat(payload, memory, value, options);
// First one is the discriminant type. So skip it.
const wantTypes = this.flatTypes.slice(1);
const haveTypes = c.wantFlatTypes!;
if (wantTypes.length !== haveTypes.length || payload.length !== haveTypes.length) {
throw new WasiError(Errno.inval);
}