Skip to content

Commit

Permalink
support vtable_get and vtable_set
Browse files Browse the repository at this point in the history
  • Loading branch information
yviansu committed Oct 11, 2023
1 parent be2cb6f commit 90d2a18
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/backend/binaryen/wasm_expr_gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import {
SpreadValue,
TemplateExprValue,
EnumerateKeysGetValue,
VTableGetValue,
VTableSetValue,
} from '../../semantics/value.js';
import {
ArrayType,
Expand Down Expand Up @@ -187,6 +189,8 @@ export class WASMExpressionGen {
return this.wasmObjFieldSet(<ShapeSetValue>value);
case SemanticsValueKind.OFFSET_SET:
return this.wasmObjFieldSet(<OffsetSetValue>value);
case SemanticsValueKind.VTABLE_SET:
return this.wasmObjFieldSet(<VTableSetValue>value);
case SemanticsValueKind.NEW_LITERAL_OBJECT:
return this.wasmNewLiteralObj(<NewLiteralObjectValue>value);
case SemanticsValueKind.OBJECT_CAST_OBJECT:
Expand All @@ -200,6 +204,8 @@ export class WASMExpressionGen {
case SemanticsValueKind.OFFSET_GETTER:
case SemanticsValueKind.OFFSET_GET:
return this.wasmObjFieldGet(<OffsetGetValue>value);
case SemanticsValueKind.VTABLE_GET:
return this.wasmObjFieldGet(<VTableGetValue>value);
case SemanticsValueKind.DYNAMIC_GET:
return this.wasmDynamicGet(<DynamicGetValue>value);
case SemanticsValueKind.DYNAMIC_SET:
Expand Down Expand Up @@ -1796,7 +1802,7 @@ export class WASMExpressionGen {
}

private wasmObjFieldSet(
value: ShapeSetValue | OffsetSetValue,
value: ShapeSetValue | OffsetSetValue | VTableSetValue,
rightValue?: SemanticsValue,
) {
const owner = value.owner as VarValue;
Expand Down Expand Up @@ -2387,7 +2393,7 @@ export class WASMExpressionGen {
}

private wasmObjFieldGet(
value: DirectGetValue | ShapeGetValue | OffsetGetValue,
value: DirectGetValue | ShapeGetValue | OffsetGetValue | VTableGetValue,
) {
/* Workaround: ShapeGetValue's field index now based on its origin shape, not objectType */
const owner = value.owner;
Expand Down
51 changes: 51 additions & 0 deletions tests/samples/class_vtable_accessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

class A {
_ref: number;
get ref(): number {
return this._ref;
}
set ref(value: number) {
this._ref = value;
}

constructor(ref_value: number) {
this._ref = ref_value;
}
}

class B extends A {
printRef() {
console.log(this.ref); // vtable get
}
setRef() {
this.ref = 888; // vtable set
}
constructor(ref_value: number) {
super(ref_value);
}
}

class C extends B {
private _C_ref = 100;
set ref(value: number) {
this._ref = value;
}
get ref(): number {
return this._C_ref;
}

constructor(ref_value: number) {
super(ref_value);
}
}

export function vtableAccessor() {
const instance = new B(90);
instance.printRef();
instance.setRef();
instance.printRef();
}
10 changes: 10 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,16 @@
}
]
},
{
"module": "class_vtable_accessor",
"entries": [
{
"name": "vtableAccessor",
"args": [],
"result": "90\n888"
}
]
},
{
"module": "class_vtable_call",
"entries": [
Expand Down

0 comments on commit 90d2a18

Please sign in to comment.