@@ -11,9 +11,12 @@ import {
11
11
12
12
import {
13
13
TypeRef ,
14
- createType
14
+ createType ,
15
+ HeapTypeRef
15
16
} from "./module" ;
16
17
18
+ import * as binaryen from "./glue/binaryen" ;
19
+
17
20
/** Indicates the kind of a type. */
18
21
export const enum TypeKind {
19
22
/** A 1-bit unsigned integer. */
@@ -583,7 +586,7 @@ export class Type {
583
586
/** Converts this type to its respective type reference. */
584
587
toRef ( ) : TypeRef {
585
588
switch ( this . kind ) {
586
- default : assert ( false ) ;
589
+ default : assert ( false ) ; // TODO: Concrete struct, array and signature types
587
590
case TypeKind . BOOL :
588
591
case TypeKind . I8 :
589
592
case TypeKind . I16 :
@@ -598,17 +601,36 @@ export class Type {
598
601
case TypeKind . F32 : return TypeRef . F32 ;
599
602
case TypeKind . F64 : return TypeRef . F64 ;
600
603
case TypeKind . V128 : return TypeRef . V128 ;
601
- // TODO: nullable/non-nullable refs have different type refs
602
- case TypeKind . FUNCREF : return TypeRef . Funcref ;
603
- case TypeKind . EXTERNREF : return TypeRef . Externref ;
604
- case TypeKind . ANYREF : return TypeRef . Anyref ;
605
- case TypeKind . EQREF : return TypeRef . Eqref ;
606
- case TypeKind . I31REF : return TypeRef . I31ref ;
607
- case TypeKind . DATAREF : return TypeRef . Dataref ;
608
- case TypeKind . STRINGREF : return TypeRef . Stringref ;
609
- case TypeKind . STRINGVIEW_WTF8 : return TypeRef . StringviewWTF8 ;
610
- case TypeKind . STRINGVIEW_WTF16 : return TypeRef . StringviewWTF16 ;
611
- case TypeKind . STRINGVIEW_ITER : return TypeRef . StringviewIter ;
604
+ case TypeKind . FUNCREF : {
605
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . Func , this . is ( TypeFlags . NULLABLE ) ) ;
606
+ }
607
+ case TypeKind . EXTERNREF : {
608
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . Ext , this . is ( TypeFlags . NULLABLE ) ) ;
609
+ }
610
+ case TypeKind . ANYREF : {
611
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . Any , this . is ( TypeFlags . NULLABLE ) ) ;
612
+ }
613
+ case TypeKind . EQREF : {
614
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . Eq , this . is ( TypeFlags . NULLABLE ) ) ;
615
+ }
616
+ case TypeKind . I31REF : {
617
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . I31 , this . is ( TypeFlags . NULLABLE ) ) ;
618
+ }
619
+ case TypeKind . DATAREF : {
620
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . Data , this . is ( TypeFlags . NULLABLE ) ) ;
621
+ }
622
+ case TypeKind . STRINGREF : {
623
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . String , this . is ( TypeFlags . NULLABLE ) ) ;
624
+ }
625
+ case TypeKind . STRINGVIEW_WTF8 : {
626
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . StringviewWTF8 , this . is ( TypeFlags . NULLABLE ) ) ;
627
+ }
628
+ case TypeKind . STRINGVIEW_WTF16 : {
629
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . StringviewWTF16 , this . is ( TypeFlags . NULLABLE ) ) ;
630
+ }
631
+ case TypeKind . STRINGVIEW_ITER : {
632
+ return binaryen . _BinaryenTypeFromHeapType ( HeapTypeRef . StringviewIter , this . is ( TypeFlags . NULLABLE ) ) ;
633
+ }
612
634
case TypeKind . VOID : return TypeRef . None ;
613
635
}
614
636
}
0 commit comments