Skip to content

Commit

Permalink
src: add enum as object field (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: zhenweijin <[email protected]>
  • Loading branch information
kylo5aby authored Dec 21, 2023
1 parent 91ca1a3 commit 183017e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/backend/binaryen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,7 @@ export class WASMGen extends Ts2wasmBackend {
globalVar.name,
varTypeRef,
true,
FunctionalFuncs.getVarDefaultValue(
this.module,
globalVar.type.kind,
),
FunctionalFuncs.getVarDefaultValue(this.module, globalVar.type),
);
}
}
Expand Down
21 changes: 15 additions & 6 deletions src/backend/binaryen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,25 @@ export namespace FunctionalFuncs {

export function getVarDefaultValue(
module: binaryen.Module,
typeKind: ValueTypeKind,
type: ValueType,
defaultValue?: binaryen.ExpressionRef,
): binaryen.ExpressionRef {
if (defaultValue) {
return defaultValue;
}
const typeKind = type.kind;
switch (typeKind) {
case ValueTypeKind.NUMBER:
return defaultValue ? defaultValue : module.f64.const(0);
return module.f64.const(0);
case ValueTypeKind.INT:
case ValueTypeKind.BOOLEAN:
return defaultValue ? defaultValue : module.i32.const(0);
return module.i32.const(0);
case ValueTypeKind.ENUM:
return getVarDefaultValue(module, (<EnumType>type).memberType);
case ValueTypeKind.WASM_I64:
return defaultValue ? defaultValue : module.i64.const(0, 0);
return module.i64.const(0, 0);
case ValueTypeKind.WASM_F32:
return defaultValue ? defaultValue : module.f32.const(0);
return module.f32.const(0);
default:
return getEmptyRef(module);
}
Expand Down Expand Up @@ -2338,14 +2344,17 @@ export namespace FunctionalFuncs {
return ifShapeCompatibal;
}

export function getPredefinedTypeId(type: ValueType) {
export function getPredefinedTypeId(type: ValueType): PredefinedTypeId {
switch (type.kind) {
case ValueTypeKind.UNDEFINED:
case ValueTypeKind.UNION:
case ValueTypeKind.TYPE_PARAMETER:
case ValueTypeKind.ANY: {
return PredefinedTypeId.ANY;
}
case ValueTypeKind.ENUM: {
return getPredefinedTypeId((<EnumType>type).memberType);
}
case ValueTypeKind.NULL:
return PredefinedTypeId.NULL;
case ValueTypeKind.INT:
Expand Down
2 changes: 1 addition & 1 deletion src/backend/binaryen/wasm_type_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ export class WASMTypeGen {
} else if (member.type === MemberType.FIELD) {
let defaultValue = FunctionalFuncs.getVarDefaultValue(
this.wasmComp.module,
member.valueType.kind,
member.valueType,
);
if (member.valueType.kind === ValueTypeKind.ANY) {
defaultValue = FunctionalFuncs.generateDynUndefined(
Expand Down
18 changes: 18 additions & 0 deletions tests/samples/class_field_assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ export function withoutCtor() {
const a = new A12();
return a.b;
}

const enum Count {
ZERO,
ONE,
TWO,
}

class A13 {
count: Count;
constructor() {
this.count = Count.ONE;
}
}

export function withEnum() {
const a = new A13();
return a.count;
}
5 changes: 5 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,11 @@
"name": "withoutCtor",
"args": [],
"result": "0x0:i32"
},
{
"name": "withEnum",
"args": [],
"result": "1:f64"
}
]
},
Expand Down

0 comments on commit 183017e

Please sign in to comment.