Skip to content
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

fix wasmType error in optional type #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading