Skip to content

Implement non-/nullable type refs in type conversion #2535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9965,8 +9965,14 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.STRINGREF:
case TypeKind.STRINGVIEW_WTF8:
case TypeKind.STRINGVIEW_WTF16:
case TypeKind.STRINGVIEW_ITER: return module.ref_null(type.toRef());
case TypeKind.I31REF: return module.i31_new(module.i32(0));
case TypeKind.STRINGVIEW_ITER: {
// TODO: what if not nullable?
return module.ref_null(type.toRef());
}
case TypeKind.I31REF: {
if (type.is(TypeFlags.NULLABLE)) return module.ref_null(type.toRef());
return module.i31_new(module.i32(0));
}
}
}

Expand Down
48 changes: 35 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import {

import {
TypeRef,
createType
createType,
HeapTypeRef
} from "./module";

import * as binaryen from "./glue/binaryen";

/** Indicates the kind of a type. */
export const enum TypeKind {
/** A 1-bit unsigned integer. */
Expand Down Expand Up @@ -583,7 +586,7 @@ export class Type {
/** Converts this type to its respective type reference. */
toRef(): TypeRef {
switch (this.kind) {
default: assert(false);
default: assert(false); // TODO: Concrete struct, array and signature types
case TypeKind.BOOL:
case TypeKind.I8:
case TypeKind.I16:
Expand All @@ -598,17 +601,36 @@ export class Type {
case TypeKind.F32: return TypeRef.F32;
case TypeKind.F64: return TypeRef.F64;
case TypeKind.V128: return TypeRef.V128;
// TODO: nullable/non-nullable refs have different type refs
case TypeKind.FUNCREF: return TypeRef.Funcref;
case TypeKind.EXTERNREF: return TypeRef.Externref;
case TypeKind.ANYREF: return TypeRef.Anyref;
case TypeKind.EQREF: return TypeRef.Eqref;
case TypeKind.I31REF: return TypeRef.I31ref;
case TypeKind.DATAREF: return TypeRef.Dataref;
case TypeKind.STRINGREF: return TypeRef.Stringref;
case TypeKind.STRINGVIEW_WTF8: return TypeRef.StringviewWTF8;
case TypeKind.STRINGVIEW_WTF16: return TypeRef.StringviewWTF16;
case TypeKind.STRINGVIEW_ITER: return TypeRef.StringviewIter;
case TypeKind.FUNCREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.Func, this.is(TypeFlags.NULLABLE));
}
case TypeKind.EXTERNREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.Ext, this.is(TypeFlags.NULLABLE));
}
case TypeKind.ANYREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.Any, this.is(TypeFlags.NULLABLE));
}
case TypeKind.EQREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.Eq, this.is(TypeFlags.NULLABLE));
}
case TypeKind.I31REF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.I31, this.is(TypeFlags.NULLABLE));
}
case TypeKind.DATAREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.Data, this.is(TypeFlags.NULLABLE));
}
case TypeKind.STRINGREF: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.String, this.is(TypeFlags.NULLABLE));
}
case TypeKind.STRINGVIEW_WTF8: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewWTF8, this.is(TypeFlags.NULLABLE));
}
case TypeKind.STRINGVIEW_WTF16: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewWTF16, this.is(TypeFlags.NULLABLE));
}
case TypeKind.STRINGVIEW_ITER: {
return binaryen._BinaryenTypeFromHeapType(HeapTypeRef.StringviewIter, this.is(TypeFlags.NULLABLE));
}
case TypeKind.VOID: return TypeRef.None;
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/compiler/features/gc.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(global $features/gc/a anyref (ref.null any))
(global $features/gc/b i31ref (ref.null i31))
(global $features/gc/c dataref (ref.null data))
(global $~lib/memory/__data_end i32 (i32.const 60))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16444))
(global $~lib/memory/__heap_base i32 (i32.const 16444))
Expand All @@ -12,10 +14,12 @@
(table $0 1 1 funcref)
(elem $0 (i32.const 1))
(export "a" (global $features/gc/a))
(export "b" (global $features/gc/b))
(export "c" (global $features/gc/c))
(export "memory" (memory $0))
(export "_start" (func $~start))
(func $features/gc/test_i31
(local $ref (ref i31))
(local $ref i31ref)
(local $val i32)
(local $uval i32)
i32.const 123
Expand Down
4 changes: 4 additions & 0 deletions tests/compiler/features/gc.release.wat
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
(module
(type $none_=>_none (func))
(global $features/gc/a anyref (ref.null any))
(global $features/gc/b i31ref (ref.null i31))
(global $features/gc/c dataref (ref.null data))
(memory $0 1)
(data (i32.const 1036) ",")
(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")
(export "a" (global $features/gc/a))
(export "b" (global $features/gc/b))
(export "c" (global $features/gc/c))
(export "memory" (memory $0))
(export "_start" (func $~start))
(func $~start
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/features/gc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ test_i31();
// constant globals

export const a: anyref = null;
// export const b: i31ref = null; // TODO: not yet nullable in Binaryen
// export const c: dataref = null; // TODO: not yet nullable in Binaryen
export const b: i31ref = null;
export const c: dataref = null;