Skip to content

Commit

Permalink
fix wasmType error in optional type
Browse files Browse the repository at this point in the history
Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu committed Apr 25, 2024
1 parent 93fbb2e commit 7a7f704
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ export class TypeResolver {
}
}
if (!res && tsType.isUnion()) {
res = this.parseUnionType(tsType, typeNode as ts.UnionTypeNode);
res = this.parseUnionType(tsType, typeNode);
}
if (!res && this.isArray(tsType)) {
if (!tsType.typeArguments) {
Expand Down Expand Up @@ -1669,11 +1669,15 @@ export class TypeResolver {

private parseUnionType(
tsUnionType: ts.UnionType,
unionTypeNode?: ts.UnionTypeNode,
unionTypeNode?: ts.Node,
): Type {
const union_type = new TSUnion();
/* 1. get type from typeNode firstly */
if (unionTypeNode && unionTypeNode.types) {
if (
unionTypeNode &&
ts.isUnionTypeNode(unionTypeNode) &&
unionTypeNode.types
) {
for (const typeNode of unionTypeNode.types) {
const type = this.generateNodeType(typeNode);
union_type.addType(type);
Expand All @@ -1683,6 +1687,17 @@ export class TypeResolver {
if (!tsUnionType.types) {
return builtinTypes.get('any')!;
}
if (
unionTypeNode &&
unionTypeNode.kind === ts.SyntaxKind.TypeReference
) {
const tsTypeRawName = this.getTsTypeRawName(unionTypeNode)!;
if (builtinWasmTypes.has(tsTypeRawName)) {
union_type.addType(builtinWasmTypes.get(tsTypeRawName)!);
union_type.addType(builtinTypes.get('undefined')!);
return union_type;
}
}
for (const tsType of tsUnionType.types) {
union_type.addType(this.tsTypeToType(tsType));
}
Expand Down Expand Up @@ -2912,15 +2927,6 @@ export class TypeResolver {
}
}

public static maybeBuiltinWasmType(node: ts.Node) {
const definedTypeName = (node as any).type?.typeName?.escapedText;
if (definedTypeName) {
if (builtinWasmTypes.has(definedTypeName)) {
return builtinWasmTypes.get(definedTypeName)!;
}
}
}

public arrayTypeCheck(node: ts.Node): boolean {
const parentNode = node.parent;
if (
Expand Down
17 changes: 16 additions & 1 deletion tests/samples/wasmType_in_otherType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,19 @@ export function wasmTypeF32AsReturnType(): f32 {

export function wasmTypeF64AsReturnType(): f64 {
return 100.25;
}
}

function wasmTypeInOptionalUnionType_inner(a_param?: i32) {
if (a_param) {
console.log(a_param);
} else {
console.log('undefined');
}
}

export function wasmTypeInOptionalUnionType() {
const a: i32 = 100;
wasmTypeInOptionalUnionType_inner(a);
const b = undefined;
wasmTypeInOptionalUnionType_inner(b);
}
5 changes: 5 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -4825,6 +4825,11 @@
"name": "wasmTypeF64AsReturnType",
"args": [],
"result": "100.25:f64"
},
{
"name": "wasmTypeInOptionalUnionType",
"args": [],
"result": "100\nundefined"
}
]
}
Expand Down

0 comments on commit 7a7f704

Please sign in to comment.