Skip to content

Commit

Permalink
add parse phase of wasmstruct
Browse files Browse the repository at this point in the history
Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu committed Jan 22, 2024
1 parent 8281aee commit 1ba25e4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
14 changes: 0 additions & 14 deletions lib/builtin/wasm_lib_type_comment.ts

This file was deleted.

47 changes: 45 additions & 2 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const enum TypeKind {
NULL = 'null',
INTERFACE = 'interface',
UNION = 'unoin',
TUPLE = 'tuple',

WASM_I32 = 'i32',
WASM_I64 = 'i64',
Expand Down Expand Up @@ -859,6 +860,29 @@ export class TSEnum extends Type {
}
}

export class TSTuple extends Type {
typeKind = TypeKind.TUPLE;
private _elements: Type[] = [];

constructor() {
super();
}

get elements(): Array<Type> {
return this._elements;
}

addType(type: Type) {
this._elements.push(type);
}

toString(): string {
const s: string[] = [];
this._elements.forEach((t) => s.push(t.toString()));
return `Tuple[${s.join(' , ')}]`;
}
}

interface TsCustomType {
tsType: ts.Type;
customName: string;
Expand Down Expand Up @@ -1654,7 +1678,8 @@ export class TypeResolver {
(this.isTypeReference(tsType) ||
this.isInterface(tsType) ||
this.isObjectLiteral(tsType) ||
this.isObjectType(tsType))
this.isObjectType(tsType)) &&
tsType.symbol
) {
const decls = tsType.symbol.declarations;
if (decls) {
Expand Down Expand Up @@ -1734,6 +1759,10 @@ export class TypeResolver {
res = this.parseSignature(signature);
}

if (!res && this.isTupleType(tsType)) {
console.log('todo: parse tuple type');
}

if (!res) {
Logger.debug(`Encounter un-processed type: ${tsType.flags}`);
res = new Type();
Expand Down Expand Up @@ -1877,8 +1906,22 @@ export class TypeResolver {
);
}

private isTupleType(tsType: ts.Type) {
if (this.isObject(tsType) && (tsType as any).node) {
const innerNode = (tsType as any).node as ts.Node;
if (innerNode.kind === ts.SyntaxKind.TupleType) {
return true;
}
}
return false;
}

private isArray(type: ts.Type): type is ts.TypeReference {
return this.isTypeReference(type) && type.symbol.name === 'Array';
return (
this.isTypeReference(type) &&
type.symbol &&
type.symbol.name === 'Array'
);
}

private isFunction(type: ts.Type): type is ts.ObjectType {
Expand Down
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,43 @@ export function parseComment(commentStr: string) {
};
return obj;
}
case CommentKind.WASMStruct: {
const structInfoReg = commentStr.match(
/@WASMStruct@<\s*\[([^>]+)\],\s*\[([^>]+)\],\s*([^,]+),\s*([^>]+)>/,
);
if (!structInfoReg || structInfoReg.length !== 5) {
Logger.error('invalid information in WASMArray comment');
return null;
}
const nullabilityKind = structInfoReg[3];
if (
!(
structInfoReg[1]
.split(',')
.every((item) => isPackedTypeKind(item)) &&
structInfoReg[2]
.split(',')
.every((item) => isMutabilityKind(item)) &&
isNullabilityKind(nullabilityKind)
)
) {
Logger.error('typo error in WASMStruct comment');
return null;
}
const packedTypeKindArray = structInfoReg[1]
.split(',')
.map((item) => item.trim());
const mutabilityKindArray = structInfoReg[2]
.split(',')
.map((item) => item.trim());
const obj: WASMStruct = {
packedTypes: packedTypeKindArray as PackedTypeKind[],
mutabilitys: mutabilityKindArray as MutabilityKind[],
nullability: nullabilityKind as NullabilityKind,
baseTypeName: structInfoReg[4],
};
return obj;
}
default: {
Logger.error(`unsupported comment kind ${commentKind}`);
return null;
Expand Down

0 comments on commit 1ba25e4

Please sign in to comment.