- Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathtypes.ts
1209 lines (1082 loc) · 37.5 KB
/
types.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
/**
* @fileoverview Mappings from AssemblyScript types to WebAssembly types.
* @license Apache-2.0
*/
import{
CommonNames
}from"./common";
import{
Class,
Program,
DecoratorFlags,
OperatorKind,
Function
}from"./program";
import{
TypeRef,
createType,
HeapTypeRef,
ensureType
}from"./module";
import*asbinaryenfrom"./glue/binaryen";
/** Indicates the kind of a type. */
exportconstenumTypeKind{
/** A 1-bit unsigned integer. */
Bool,
// signed integers
/** An 8-bit signed integer. */
I8,
/** A 16-bit signed integer. */
I16,
/** A 32-bit signed integer. */
I32,
/** A 64-bit signed integer. */
I64,
/** A 32-bit/64-bit signed integer, depending on the target. */
Isize,
// unsigned integers
/** An 8-bit unsigned integer. */
U8,
/** A 16-bit unsigned integer. */
U16,
/** A 32-bit unsigned integer. Also the base of function types. */
U32,
/** A 64-bit unsigned integer. */
U64,
/** A 32-bit/64-bit unsigned integer, depending on the target. Also the base of class types. */
Usize,
// floats
/** A 32-bit float. */
F32,
/** A 64-bit double. */
F64,
// vectors
/** A 128-bit vector. */
V128,
// references (keep in same order as in Binaryen)
/** External reference. */
Extern,
/** Function reference. */
Func,
/** Any reference. */
Any,
/** Equatable reference. */
Eq,
/** Struct reference. */
Struct,
/** Array reference. */
Array,
/** 31-bit integer reference. */
I31,
/** String reference. */
String,
/** WTF8 string view. */
StringviewWTF8,
/** WTF16 string view. */
StringviewWTF16,
/** String iterator. */
StringviewIter,
// other
/** No return type. */
Void
}
/** Indicates capabilities of a type. */
exportconstenumTypeFlags{
None=0,
/** Is a signed type that can represent negative values. */
Signed=1<<0,
/** Is an unsigned type that cannot represent negative values. */
Unsigned=1<<1,
/** Is an integer type. */
Integer=1<<2,
/** Is a floating point type. */
Float=1<<3,
/** Is a varying (in size) type. */
Varying=1<<4,
/** Is smaller than 32-bits. */
Short=1<<5,
/** Is larger than 32-bits. */
Long=1<<6,
/** Is a value type. */
Value=1<<7,
/** Is a reference type (either a class or a function type). */
Reference=1<<8,
/** Is a nullable type. */
Nullable=1<<9,
/** Is a vector type. */
Vector=1<<10,
/** Is an external type. */
External=1<<11,
/** Is a class. */
Class=1<<12,
/** Is a function. */
Function=1<<13
}
/** Represents a resolved type. */
exportclassType{
/** Type kind. */
kind: TypeKind;
/** Type flags. */
flags: TypeFlags;
/** Size in bits. */
size: i32;
/** Underlying class reference, if a class type. */
classReference: Class|null=null;
/** Underlying signature reference, if a function type. */
signatureReference: Signature|null=null;
/** Respective non-nullable type, if nullable. */
private_nonNullableType: Type|null=null;
/** Respective nullable type, if non-nullable. */
private_nullableType: Type|null=null;
/** Cached Binaryen type reference. */
ref: TypeRef=0;
/** Constructs a new resolved type. */
constructor(kind: TypeKind,flags: TypeFlags,size: u32){
this.kind=kind;
this.flags=flags;
this.size=size;
if(!(flags&TypeFlags.Nullable)){
this._nonNullableType=this;
}else{
this._nullableType=this;
}
}
/** Returns the closest int type representing this type. */
getintType(): Type{
if(this==Type.auto)returnthis;// keep auto as a hint
switch(this.kind){
caseTypeKind.Bool:
caseTypeKind.I32:
caseTypeKind.F32: returnType.i32;
caseTypeKind.I8: returnType.i8;
caseTypeKind.I16: returnType.i16;
caseTypeKind.F64:
caseTypeKind.I64: returnType.i64;
caseTypeKind.Isize: returnthis.size==64 ? Type.isize64 : Type.isize32;
caseTypeKind.U8: returnType.u8;
caseTypeKind.U16: returnType.u16;
caseTypeKind.U32: returnType.u32;
caseTypeKind.U64: returnType.u64;
caseTypeKind.Usize: returnthis.size==64 ? Type.usize64 : Type.usize32;
default: returnType.i32;
}
}
/** Substitutes this type with the auto type if this type is void. */
getexceptVoid(): Type{
returnthis.kind==TypeKind.Void ? Type.auto : this;
}
/** Size in bytes. */
getbyteSize(): i32{
// ceiled div by 8
returnthis.size+7>>>3;
}
/** Gets this type's logarithmic alignment in memory. */
getalignLog2(): i32{
return31-clz<i32>(this.byteSize);
}
/** Tests if this type represents a basic value. */
getisValue(): bool{
returnthis.is(TypeFlags.Value);
}
/** Tests if this type represents an integer value. */
getisIntegerValue(): bool{
returnthis.is(TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents a small (< 32 bits) integer value. */
getisShortIntegerValue(): bool{
returnthis.is(TypeFlags.Short|TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents a long (> 32 bits) integer value. */
getisLongIntegerValue(): bool{
returnthis.is(TypeFlags.Long|TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents a signed integer value. */
getisSignedIntegerValue(): bool{
returnthis.is(TypeFlags.Signed|TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents an unsigned integer value. */
getisUnsignedIntegerValue(): bool{
returnthis.is(TypeFlags.Unsigned|TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents a varying (in size) integer value. */
getisVaryingIntegerValue(): bool{
returnthis.is(TypeFlags.Varying|TypeFlags.Integer|TypeFlags.Value);
}
/** Tests if this type represents an integer, including references. */
getisIntegerInclReference(): bool{
returnthis.is(TypeFlags.Integer);
}
/** Tests if this type represents a floating point value. */
getisFloatValue(): bool{
returnthis.is(TypeFlags.Float|TypeFlags.Value);
}
/** Tests if this type represents a numeric (integer or floating point) value. */
getisNumericValue(): bool{
returnthis.isIntegerValue||this.isFloatValue;
}
/** Tests if this type represents a boolean value. */
getisBooleanValue(): bool{
returnthis==Type.bool;
}
/** Tests if this type represents a vector value. */
getisVectorValue(): bool{
returnthis.is(TypeFlags.Vector|TypeFlags.Value);
}
/** Tests if this type represents an internal or external reference. */
getisReference(): bool{
returnthis.is(TypeFlags.Reference);
}
/** Tests if this type represents a nullable internal or external reference. */
getisNullableReference(): bool{
returnthis.is(TypeFlags.Nullable|TypeFlags.Reference);
}
/** Tests if this type represents an internal object. */
getisInternalReference(): bool{
returnthis.is(TypeFlags.Integer|TypeFlags.Reference);
}
/** Tests if this type represents an external object. */
getisExternalReference(): bool{
returnthis.is(TypeFlags.External|TypeFlags.Reference);
}
/** Tests if this type represents a nullable external object. */
getisNullableExternalReference(): bool{
returnthis.is(TypeFlags.Nullable|TypeFlags.External|TypeFlags.Reference);
}
/** Gets the underlying class of this type, if any. */
getClass(): Class|null{
returnthis.isInternalReference
? this.classReference
: null;
}
/** Tests if this type represents a class. */
getisClass(): bool{
returnthis.getClass()!=null;
}
/** Gets the underlying class or wrapper class of this type, if any. */
getClassOrWrapper(program: Program): Class|null{
letclassReference=this.getClass();
if(classReference){
// typical class
returnclassReference;
}else{
letsignatureReference=this.getSignature();
if(signatureReference){
// function wrapper
lettype=signatureReference.type;
letwrapper=assert(program.resolver.resolveClass(program.functionPrototype,[type]));
wrapper.wrappedType=type;
returnwrapper;
}else{
letwrapperClasses=program.wrapperClasses;
if(wrapperClasses.has(this)){
// value wrapper
returnassert(wrapperClasses.get(this));
}
}
}
returnnull;
}
lookupOverload(kind: OperatorKind,program: Program): Function|null{
letclassReference=this.getClassOrWrapper(program);
if(classReference){
returnclassReference.lookupOverload(kind);
}
returnnull;
}
/** Gets the underlying function signature of this type, if any. */
getSignature(): Signature|null{
returnthis.isInternalReference
? this.signatureReference
: null;
}
/** Tests if this type represents a function. */
getisFunction(): bool{
returnthis.getSignature()!=null;
}
/** Tests if this is a managed type that needs GC hooks. */
getisManaged(): bool{
if(this.isInternalReference){
letclassReference=this.classReference;
if(classReference)return!classReference.hasDecorator(DecoratorFlags.Unmanaged);
returnthis.signatureReference!=null;// function references are managed
}
returnfalse;
}
/** Tests if this is a class type explicitly annotated as unmanaged. */
getisUnmanaged(): bool{
letclassReference=this.classReference;
returnclassReference!=null&&classReference.hasDecorator(DecoratorFlags.Unmanaged);
}
getisMemory(): bool{
switch(this.kind){
caseTypeKind.Bool:
caseTypeKind.I8:
caseTypeKind.I16:
caseTypeKind.I32:
caseTypeKind.I64:
caseTypeKind.Isize:
caseTypeKind.U8:
caseTypeKind.U16:
caseTypeKind.U32:
caseTypeKind.U64:
caseTypeKind.Usize:
caseTypeKind.F32:
caseTypeKind.F64:
caseTypeKind.V128: returntrue;
}
returnfalse;
}
/** Gets the corresponding non-nullable type. */
getnonNullableType(): Type{
// Every type has a corresponding non-nullable type
returnassert(this._nonNullableType);
}
/** Gets the corresponding nullable type, if applicable. */
getnullableType(): Type|null{
returnthis.isReference
? this.asNullable()// Every reference type has a corresponding nullable type
: null;// Other types do not have a nullable type
}
/** Computes the sign-extending shift in the target type. */
computeSmallIntegerShift(targetType: Type): i32{
returntargetType.size-this.size;
}
/** Computes the truncating mask in the target type. */
computeSmallIntegerMask(targetType: Type): i32{
letsize=this.size;
if(!this.is(TypeFlags.Unsigned))size-=1;
return~0>>>(targetType.size-size);
}
/** Tests if this type has (all of) the specified flags. */
is(flags: TypeFlags): bool{return(this.flags&flags)==flags;}
/** Tests if this type has any of the specified flags. */
isAny(flags: TypeFlags): bool{return(this.flags&flags)!=0;}
/** Composes the respective nullable type of this type. */
asNullable(): Type{
assert(this.isReference);
letnullableType=this._nullableType;
if(!nullableType){
assert(!this.isNullableReference);
this._nullableType=nullableType=newType(this.kind,this.flags|TypeFlags.Nullable,this.size);
nullableType.classReference=this.classReference;
nullableType.signatureReference=this.signatureReference;
nullableType._nonNullableType=this;
}
returnnullableType;
}
/** Use unsigned type for according size if possible. */
toUnsigned(): Type{
switch(this.kind){
caseTypeKind.I8: returnType.u8;
caseTypeKind.I16: returnType.u16;
caseTypeKind.I32: returnType.u32;
caseTypeKind.I64: returnType.u64;
caseTypeKind.Isize: returnthis.size==64 ? Type.usize64 : Type.usize32;
}
returnthis;
}
/** Tests if this type equals the specified. */
equals(other: Type): bool{
if(this.kind!=other.kind){
returnfalse;
}
if(this.isReference){
letselfSignatureReference=this.signatureReference;
letotherSignatureReference=other.signatureReference;
return(
this.classReference==other.classReference
&&selfSignatureReference==otherSignatureReference
&&this.isNullableReference==other.isNullableReference
);
}
returntrue;
}
/** Tests if a value of this type is assignable to the target type incl. implicit conversion. */
isAssignableTo(target: Type,signednessIsRelevant: bool=false): bool{
letcurrentClass: Class|null;
lettargetClass: Class|null;
letcurrentFunction: Signature|null;
lettargetFunction: Signature|null;
if(this.isReference){
if(target.isReference){
if(!this.isNullableReference||target.isNullableReference){
if(currentClass=this.getClass()){
if(targetClass=target.getClass()){
returncurrentClass.isAssignableTo(targetClass);
}
}elseif(currentFunction=this.getSignature()){
if(targetFunction=target.getSignature()){
returncurrentFunction.isAssignableTo(targetFunction);
}
}elseif(this.isExternalReference){
if(
this.kind==target.kind||
(target.kind==TypeKind.Any&&this.kind!=TypeKind.Extern)
){
returntrue;
}
}
}
}
}elseif(!target.isReference){
if(this.isIntegerValue){
if(target.isIntegerValue){
if(
!signednessIsRelevant||
this.isBooleanValue||// a bool (0 or 1) can be safely assigned to all sorts of integers
this.isSignedIntegerValue==target.isSignedIntegerValue
){
returnthis.size<=target.size;
}
}elseif(target.kind==TypeKind.F32){
returnthis.size<=23;// mantissa bits
}elseif(target.kind==TypeKind.F64){
returnthis.size<=52;// ^
}
}elseif(this.isFloatValue){
if(target.isFloatValue){
returnthis.size<=target.size;
}
}elseif(this.isVectorValue){
if(target.isVectorValue){
returnthis.size==target.size;
}
}
}
returnfalse;
}
/** Tests if a value of this type is assignable to the target type excl. implicit conversion. */
isStrictlyAssignableTo(target: Type,signednessIsRelevant: bool=false): bool{
if(this.isReference)returnthis.isAssignableTo(target);
elseif(target.isReference)returnfalse;
// not dealing with references from here on
if(this.isIntegerValue){
returntarget.isIntegerValue&&target.size==this.size&&(
!signednessIsRelevant||
this.isSignedIntegerValue==target.isSignedIntegerValue
);
}
returnthis.kind==target.kind;
}
/** Tests if this type has a subtype assignable to the target type. */
hasSubtypeAssignableTo(target: Type): bool{
letthisClass=this.getClass();
lettargetClass=target.getClass();
if(!thisClass||!targetClass)returnfalse;// TODO: what about basic types?
returnthisClass.hasSubclassAssignableTo(targetClass);
}
/** Tests if a value of this type can be changed to the target type using `changetype`. */
isChangeableTo(target: Type): bool{
// special in that it allows integer references as well
if(this.is(TypeFlags.Integer)&&target.is(TypeFlags.Integer)){
letsize=this.size;
returnsize==target.size&&(
size>=32||
this.is(TypeFlags.Signed)==target.is(TypeFlags.Signed)
);
}
returnthis.kind==target.kind;
}
/** Tests if this type can extend or implement the given type. */
canExtendOrImplement(base: Type): bool{
// Both must be class types
letthisClass=this.getClass();
letbaseClass=base.getClass();
if(!thisClass||!baseClass)returnfalse;
// Both types must be either managed or unmanaged
if(this.isManaged!=base.isManaged)returnfalse;
// Both types must be either internal or external references
if(this.isInternalReference){
if(!base.isInternalReference)returnfalse;
}elseif(this.isExternalReference){
if(!base.isExternalReference)returnfalse;
}else{
returnfalse;
}
returntrue;
}
/** Computes the common type of a binary-like expression, if any. */
staticcommonType(
/** LHS type. */
left: Type,
/** RHS type. */
right: Type,
/** Contextual type, if any. */
contextualType: Type=Type.auto,
/** Whether signedness is relevant. */
signednessIsRelevant: bool=false
): Type|null{
// Compute LUB of internal reference types (classes)
if(left.isInternalReference){
if(!right.isInternalReference)returnnull;
// Prefer contextual type if meaningful
if(contextualType!=Type.void&&left.isAssignableTo(contextualType)&&right.isAssignableTo(contextualType)){
returncontextualType;
}
letleftClass=left.getClass();
letrightClass=right.getClass();
if(leftClass&&rightClass){
letlubClass=Class.leastUpperBound(leftClass,rightClass);
if(lubClass){
letret=left.is(TypeFlags.Nullable)||right.is(TypeFlags.Nullable) ? lubClass.type.asNullable() : lubClass.type;
returnret;
}
}
}elseif(right.isInternalReference){
returnnull;
}
// TODO: External reference types (needs nullability)
// Otherwise do a trivial check
if(right.isAssignableTo(left,signednessIsRelevant))returnleft;
elseif(left.isAssignableTo(right,signednessIsRelevant))returnright;
returnnull;
}
/** Converts this type's kind to a string. */
kindToString(): string{
switch(this.kind){
caseTypeKind.Bool: returnCommonNames.bool;
caseTypeKind.I8: returnCommonNames.i8;
caseTypeKind.I16: returnCommonNames.i16;
caseTypeKind.I32: returnCommonNames.i32;
caseTypeKind.I64: returnCommonNames.i64;
caseTypeKind.Isize: returnCommonNames.isize;
caseTypeKind.U8: returnCommonNames.u8;
caseTypeKind.U16: returnCommonNames.u16;
caseTypeKind.U32: returnCommonNames.u32;
caseTypeKind.U64: returnCommonNames.u64;
caseTypeKind.Usize: returnCommonNames.usize;
caseTypeKind.F32: returnCommonNames.f32;
caseTypeKind.F64: returnCommonNames.f64;
caseTypeKind.V128: returnCommonNames.v128;
caseTypeKind.Func: returnCommonNames.ref_func;
caseTypeKind.Extern: returnCommonNames.ref_extern;
caseTypeKind.Any: returnCommonNames.ref_any;
caseTypeKind.Eq: returnCommonNames.ref_eq;
caseTypeKind.Struct: returnCommonNames.ref_struct;
caseTypeKind.Array: returnCommonNames.ref_array;
caseTypeKind.I31: returnCommonNames.ref_i31;
caseTypeKind.String: returnCommonNames.ref_string;
caseTypeKind.StringviewWTF8: returnCommonNames.ref_stringview_wtf8;
caseTypeKind.StringviewWTF16: returnCommonNames.ref_stringview_wtf16;
caseTypeKind.StringviewIter: returnCommonNames.ref_stringview_iter;
default: assert(false);
caseTypeKind.Void: returnCommonNames.void_;
}
}
/** Converts this type to a string. */
toString(validWat: bool=false): string{
constnullablePostfix=validWat ? "|null" : " | null";
if(this.isReference){
letclassReference=this.getClass();
if(classReference){
returnthis.isNullableReference
? classReference.internalName+nullablePostfix
: classReference.internalName;
}else{
letsignatureReference=this.getSignature();
if(signatureReference){
returnthis.isNullableReference
? `(${signatureReference.toString(validWat)})${nullablePostfix}`
: signatureReference.toString(validWat);
}else{
returnthis.isNullableReference
? `${this.kindToString()}${nullablePostfix}`
: this.kindToString();
}
}
}
if(this==Type.auto){
return"auto";
}
returnthis.kindToString();
}
// Binaryen specific
/** Converts this type to its respective type reference. */
toRef(): TypeRef{
switch(this.kind){
caseTypeKind.Bool:
caseTypeKind.I8:
caseTypeKind.I16:
caseTypeKind.I32:
caseTypeKind.U8:
caseTypeKind.U16:
caseTypeKind.U32: returnTypeRef.I32;
caseTypeKind.Isize:
caseTypeKind.Usize: if(this.size!=64)returnTypeRef.I32;
caseTypeKind.I64:
caseTypeKind.U64: returnTypeRef.I64;
caseTypeKind.F32: returnTypeRef.F32;
caseTypeKind.F64: returnTypeRef.F64;
caseTypeKind.V128: returnTypeRef.V128;
caseTypeKind.Func: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Func,this.is(TypeFlags.Nullable));
}
caseTypeKind.Extern: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Extern,this.is(TypeFlags.Nullable));
}
caseTypeKind.Any: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Any,this.is(TypeFlags.Nullable));
}
caseTypeKind.Eq: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Eq,this.is(TypeFlags.Nullable));
}
caseTypeKind.Struct: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Struct,this.is(TypeFlags.Nullable));
}
caseTypeKind.Array: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.Array,this.is(TypeFlags.Nullable));
}
caseTypeKind.I31: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.I31,this.is(TypeFlags.Nullable));
}
caseTypeKind.String: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.String,this.is(TypeFlags.Nullable));
}
caseTypeKind.StringviewWTF8: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewWTF8,this.is(TypeFlags.Nullable));
}
caseTypeKind.StringviewWTF16: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewWTF16,this.is(TypeFlags.Nullable));
}
caseTypeKind.StringviewIter: {
returnbinaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewIter,this.is(TypeFlags.Nullable));
}
caseTypeKind.Void: returnTypeRef.None;
}
// TODO: not used yet
assert(false);
returnensureType(this);
}
// Types
/** An 8-bit signed integer. */
staticreadonlyi8: Type=newType(TypeKind.I8,
TypeFlags.Signed|
TypeFlags.Short|
TypeFlags.Integer|
TypeFlags.Value,8
);
/** A 16-bit signed integer. */
staticreadonlyi16: Type=newType(TypeKind.I16,
TypeFlags.Signed|
TypeFlags.Short|
TypeFlags.Integer|
TypeFlags.Value,16
);
/** A 32-bit signed integer. */
staticreadonlyi32: Type=newType(TypeKind.I32,
TypeFlags.Signed|
TypeFlags.Integer|
TypeFlags.Value,32
);
/** A 64-bit signed integer. */
staticreadonlyi64: Type=newType(TypeKind.I64,
TypeFlags.Signed|
TypeFlags.Long|
TypeFlags.Integer|
TypeFlags.Value,64
);
/** A 32-bit signed size. WASM32 only. */
staticreadonlyisize32: Type=newType(TypeKind.Isize,
TypeFlags.Signed|
TypeFlags.Integer|
TypeFlags.Varying|
TypeFlags.Value,32
);
/** A 64-bit signed size. WASM64 only. */
staticreadonlyisize64: Type=newType(TypeKind.Isize,
TypeFlags.Signed|
TypeFlags.Long|
TypeFlags.Integer|
TypeFlags.Varying|
TypeFlags.Value,64
);
/** An 8-bit unsigned integer. */
staticreadonlyu8: Type=newType(TypeKind.U8,
TypeFlags.Unsigned|
TypeFlags.Short|
TypeFlags.Integer|
TypeFlags.Value,8
);
/** A 16-bit unsigned integer. */
staticreadonlyu16: Type=newType(TypeKind.U16,
TypeFlags.Unsigned|
TypeFlags.Short|
TypeFlags.Integer|
TypeFlags.Value,16
);
/** A 32-bit unsigned integer. */
staticreadonlyu32: Type=newType(TypeKind.U32,
TypeFlags.Unsigned|
TypeFlags.Integer|
TypeFlags.Value,32
);
/** A 64-bit unsigned integer. */
staticreadonlyu64: Type=newType(TypeKind.U64,
TypeFlags.Unsigned|
TypeFlags.Long|
TypeFlags.Integer|
TypeFlags.Value,64
);
/** A 32-bit unsigned size. WASM32 only. */
staticreadonlyusize32: Type=newType(TypeKind.Usize,
TypeFlags.Unsigned|
TypeFlags.Integer|
TypeFlags.Varying|
TypeFlags.Value,32
);
/** A 64-bit unsigned size. WASM64 only. */
staticreadonlyusize64: Type=newType(TypeKind.Usize,
TypeFlags.Unsigned|
TypeFlags.Long|
TypeFlags.Integer|
TypeFlags.Varying|
TypeFlags.Value,64
);
/** A 1-bit unsigned integer. */
staticreadonlybool: Type=newType(TypeKind.Bool,
TypeFlags.Unsigned|
TypeFlags.Short|
TypeFlags.Integer|
TypeFlags.Value,1
);
/** A 32-bit float. */
staticreadonlyf32: Type=newType(TypeKind.F32,
TypeFlags.Signed|
TypeFlags.Float|
TypeFlags.Value,32
);
/** A 64-bit float. */
staticreadonlyf64: Type=newType(TypeKind.F64,
TypeFlags.Signed|
TypeFlags.Long|
TypeFlags.Float|
TypeFlags.Value,64
);
/** A 128-bit vector. */
staticreadonlyv128: Type=newType(TypeKind.V128,
TypeFlags.Vector|
TypeFlags.Value,128
);
/** Non-nullable function reference (`ref func`). */
staticreadonlyfunc: Type=newType(TypeKind.Func,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable external reference (`ref extern`). */
staticreadonlyextern: Type=newType(TypeKind.Extern,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable any reference (`ref any`). */
staticreadonlyany: Type=newType(TypeKind.Any,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable equatable reference (`ref eq`). */
staticreadonlyeq: Type=newType(TypeKind.Eq,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable struct reference (`ref struct`). */
staticreadonlystruct: Type=newType(TypeKind.Struct,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable array reference (`ref array`). */
staticreadonlyarray: Type=newType(TypeKind.Array,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable 31-bit integer reference (`ref i31`). */
staticreadonlyi31: Type=newType(TypeKind.I31,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable string reference (`ref string`). */
staticreadonlystring: Type=newType(TypeKind.String,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable WTF8 string view reference (`ref stringview_wtf8`). */
staticreadonlystringview_wtf8: Type=newType(TypeKind.StringviewWTF8,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable WTF16 string view reference (`ref stringview_wtf16`). */
staticreadonlystringview_wtf16: Type=newType(TypeKind.StringviewWTF16,
TypeFlags.External|
TypeFlags.Reference,0
);
/** Non-nullable string iterator reference (`ref stringview_iter`). */
staticreadonlystringview_iter: Type=newType(TypeKind.StringviewIter,
TypeFlags.External|
TypeFlags.Reference,0
);
/** No return type. */
staticreadonlyvoid: Type=newType(TypeKind.Void,TypeFlags.None,0);
/** Alias of i32 indicating type inference of locals and globals with just an initializer. */
staticreadonlyauto: Type=newType(Type.i32.kind,Type.i32.flags,Type.i32.size);
}
/** Converts an array of types to an array of type references. */
exportfunctiontypesToRefs(types: Type[]): TypeRef[]{
letnumTypes=types.length;
letret=newArray<TypeRef>(numTypes);
for(leti=0;i<numTypes;++i){
unchecked(ret[i]=types[i].toRef());
}
returnret;
}
/** Converts an array of types to its combined string representation. */
exportfunctiontypesToString(types: Type[]): string{
letnumTypes=types.length;
if(!numTypes)return"";
letsb=newArray<string>(numTypes);
for(leti=0;i<numTypes;++i){
unchecked(sb[i]=types[i].toString(true));
}
returnsb.join(",");
}
/** Represents a fully resolved function signature. */
exportclassSignature{
/** Construct a new signature. */
publicstaticcreate(
/** The program that created this signature. */
program: Program,
/** Parameter types, if any, excluding `this`. */
parameterTypes: Type[]=[],
/** Return type. */
returnType: Type=Type.void,
/** This type, if an instance signature. */
thisType: Type|null=null,
/** Number of required parameters excluding `this`. Other parameters are considered optional. */
requiredParameters: i32=parameterTypes ? parameterTypes.length : 0,
/** Whether the last parameter is a rest parameter. */
hasRest: bool=false,
): Signature{
// get the usize type, and the type of the signature
letusizeType=program.options.usizeType;
lettype=newType(
usizeType.kind,
usizeType.flags&~TypeFlags.Value|TypeFlags.Reference,
usizeType.size
);
// calculate the properties
letsignatureTypes=program.uniqueSignatures;
letnextId=program.nextSignatureId;
// construct the signature and calculate it's unique key
letsignature=newSignature(program,parameterTypes,returnType,thisType,requiredParameters,hasRest,nextId,type);
letuniqueKey=signature.toString();
// check if it exists, and return it
if(signatureTypes.has(uniqueKey)){
letexisting=assert(signatureTypes.get(uniqueKey));
assert(signature.equals(existing));
returnexisting;
}
// otherwise increment the program's signature id, set the signature reference of the type, and memoize the signature
program.nextSignatureId=nextId+1;
type.signatureReference=signature;
signatureTypes.set(uniqueKey,signature);
returnsignature;
}
/** Constructs a new signature. */
privateconstructor(
/** The program that created this signature. */
publicreadonlyprogram: Program,
/** Parameter types, if any, excluding `this`. */
publicreadonlyparameterTypes: Type[],
/** Return type. */
publicreadonlyreturnType: Type,
/** This type, if an instance signature. */
publicreadonlythisType: Type|null,
/** Number of required parameters excluding `this`. Other parameters are considered optional. */
publicreadonlyrequiredParameters: i32,