@@ -11,9 +11,12 @@ import {
1111
1212import {
1313 TypeRef ,
14- createType
14+ createType ,
15+ HeapTypeRef
1516} from "./module" ;
1617
18+ import * as binaryen from "./glue/binaryen" ;
19+
1720/** Indicates the kind of a type. */
1821export const enum TypeKind {
1922 /** A 1-bit unsigned integer. */
@@ -583,7 +586,7 @@ export class Type {
583586 /** Converts this type to its respective type reference. */
584587 toRef ( ) : TypeRef {
585588 switch ( this . kind ) {
586- default : assert ( false ) ;
589+ default : assert ( false ) ; // TODO: Concrete struct, array and signature types
587590 case TypeKind . BOOL :
588591 case TypeKind . I8 :
589592 case TypeKind . I16 :
@@ -598,17 +601,36 @@ export class Type {
598601 case TypeKind . F32 : return TypeRef . F32 ;
599602 case TypeKind . F64 : return TypeRef . F64 ;
600603 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+ }
612634 case TypeKind . VOID : return TypeRef . None ;
613635 }
614636 }
0 commit comments