Skip to content

Commit 7bc3b57

Browse files
authored
Implement non-/nullable type refs in type conversion (#2535)
1 parent f159298 commit 7bc3b57

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

Diff for: src/compiler.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -9965,8 +9965,14 @@ export class Compiler extends DiagnosticEmitter {
99659965
case TypeKind.STRINGREF:
99669966
case TypeKind.STRINGVIEW_WTF8:
99679967
case TypeKind.STRINGVIEW_WTF16:
9968-
case TypeKind.STRINGVIEW_ITER: return module.ref_null(type.toRef());
9969-
case TypeKind.I31REF: return module.i31_new(module.i32(0));
9968+
case TypeKind.STRINGVIEW_ITER: {
9969+
// TODO: what if not nullable?
9970+
return module.ref_null(type.toRef());
9971+
}
9972+
case TypeKind.I31REF: {
9973+
if (type.is(TypeFlags.NULLABLE)) return module.ref_null(type.toRef());
9974+
return module.i31_new(module.i32(0));
9975+
}
99709976
}
99719977
}
99729978

Diff for: src/types.ts

+35-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import {
1111

1212
import {
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. */
1821
export 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
}

Diff for: tests/compiler/features/gc.debug.wat

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
44
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
55
(global $features/gc/a anyref (ref.null any))
6+
(global $features/gc/b i31ref (ref.null i31))
7+
(global $features/gc/c dataref (ref.null data))
68
(global $~lib/memory/__data_end i32 (i32.const 60))
79
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16444))
810
(global $~lib/memory/__heap_base i32 (i32.const 16444))
@@ -12,10 +14,12 @@
1214
(table $0 1 1 funcref)
1315
(elem $0 (i32.const 1))
1416
(export "a" (global $features/gc/a))
17+
(export "b" (global $features/gc/b))
18+
(export "c" (global $features/gc/c))
1519
(export "memory" (memory $0))
1620
(export "_start" (func $~start))
1721
(func $features/gc/test_i31
18-
(local $ref (ref i31))
22+
(local $ref i31ref)
1923
(local $val i32)
2024
(local $uval i32)
2125
i32.const 123

Diff for: tests/compiler/features/gc.release.wat

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
(module
22
(type $none_=>_none (func))
33
(global $features/gc/a anyref (ref.null any))
4+
(global $features/gc/b i31ref (ref.null i31))
5+
(global $features/gc/c dataref (ref.null data))
46
(memory $0 1)
57
(data (i32.const 1036) ",")
68
(data (i32.const 1048) "\01\00\00\00\1c\00\00\00f\00e\00a\00t\00u\00r\00e\00s\00/\00g\00c\00.\00t\00s")
79
(export "a" (global $features/gc/a))
10+
(export "b" (global $features/gc/b))
11+
(export "c" (global $features/gc/c))
812
(export "memory" (memory $0))
913
(export "_start" (func $~start))
1014
(func $~start

Diff for: tests/compiler/features/gc.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ test_i31();
1010
// constant globals
1111

1212
export const a: anyref = null;
13-
// export const b: i31ref = null; // TODO: not yet nullable in Binaryen
14-
// export const c: dataref = null; // TODO: not yet nullable in Binaryen
13+
export const b: i31ref = null;
14+
export const c: dataref = null;

0 commit comments

Comments
 (0)