Rikito Taniguchi
01/02/2024, 11:18 AMclass Derived: Base
, it's vtable's type is sth like (field (ref $Derived.vtable___type_30))
. On the other hand, the itable's type is always (field (ref null struct))
and an interface dispatch always requires ref.cast
at the call site, as mentioned in the presentation.
My question is: Why don't we define a class with a concrete itable type, and remove the ref.cast
at call site?
Can we replace val classITableRefGcType = WasmRefNullType(WasmHeapType.Simple.Struct)
with val classITableRefGcType = WasmRefNullType(WasmHeapType.Type(context.referenceClassITableGcType(symbol)))
in https://github.com/JetBrains/kotlin/blob/c4fc0b919dfe9b746f9ecfcc08668fa05b839064/[…]g/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt ?Svyatoslav Kuzmich [JB]
01/02/2024, 12:28 PMkotlin.Any
, requiring at least one cast at the call site.
In cases where we know concrete static class type, we bypass the itable and generate either direct (final) or vtable (open) call.Svyatoslav Kuzmich [JB]
01/02/2024, 12:30 PMRikito Taniguchi
01/04/2024, 7:24 AMinterface Animal {
public void sound();
}
class Cat implements Animal {
public void sound() {}
}
Cat
's type and itable are like
(type $<http://com.google.j2cl.samples.wasm.Cat|com.google.j2cl.samples.wasm.Cat> (sub $java.lang.Object (struct
(field $vtable (ref $com.google.j2cl.samples.wasm.Cat.vtable))
(field $itable (ref $com.google.j2cl.samples.wasm.Cat.itable))
(field $$systemIdentityHashCode@java.lang.Object (mut i32))
))
)
(type $com.google.j2cl.samples.wasm.Cat.itable (sub $itable (struct
(field $slot0 (ref $com.google.j2cl.samples.wasm.Animal.vtable))
(field $slot1 (ref null struct))
(field $slot2 (ref null struct))
(field $slot3 (ref null struct))
(field $slot4 (ref null struct))
(field $slot5 (ref null struct))
(field $slot6 (ref null struct))
)))
Rikito Taniguchi
01/04/2024, 7:25 AMjava.lang.Object
(like kotlin.Any
for Kotlin), at least one cast is required. (not sure which virtual methods are available from it's vtable at interface call site.)Rikito Taniguchi
01/04/2024, 7:25 AMprivate static void doSound(Animal animal) {
animal.sound();
}
it's gonna be compiled to like
(func $m_doSound__com_google_j2cl_samples_wasm_Animal__void@com.google.j2cl.samples.wasm.HelloWorld
(param $animal (ref null $java.lang.Object))
(call_ref $function.m_sound__void
(ref.as_non_null (local.get $animal))
(struct.get
$com.google.j2cl.samples.wasm.Animal.vtable
$m_sound__void
(ref.cast
(ref $com.google.j2cl.samples.wasm.Animal.vtable)
(struct.get
$itable
$slot0
(struct.get
$java.lang.Object
$itable
(local.get $animal)
)
)
)
)
)
)