[Wasm] Rename dateref to structref

This commit is contained in:
Igor Yakovlev
2023-05-18 19:37:02 +02:00
committed by Space Team
parent 1e0be4e0af
commit 1749bafc30
9 changed files with 31 additions and 35 deletions
@@ -289,8 +289,8 @@ class WasmSymbols(
} }
} }
private val wasmDataRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.dataref")) private val wasmStructRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.structref"))
val wasmDataRefType by lazy { wasmDataRefClass.defaultType } val wasmStructRefType by lazy { wasmStructRefClass.defaultType }
val wasmAnyRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.anyref")) val wasmAnyRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.anyref"))
@@ -314,7 +314,7 @@ class BodyGenerator(
if (klassSymbol.owner.hasInterfaceSuperClass()) { if (klassSymbol.owner.hasInterfaceSuperClass()) {
body.buildGetGlobal(context.referenceGlobalClassITable(klassSymbol), location) body.buildGetGlobal(context.referenceGlobalClassITable(klassSymbol), location)
} else { } else {
body.buildRefNull(WasmHeapType.Simple.Data, location) body.buildRefNull(WasmHeapType.Simple.Struct, location)
} }
body.buildConstI32Symbol(context.referenceTypeId(klassSymbol), location) body.buildConstI32Symbol(context.referenceTypeId(klassSymbol), location)
@@ -543,7 +543,7 @@ class BodyGenerator(
val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER) val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER)
body.buildSetLocal(parameterLocal, location) body.buildSetLocal(parameterLocal, location)
body.buildBlock("isInterface", WasmI32) { outerLabel -> body.buildBlock("isInterface", WasmI32) { outerLabel ->
body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Struct)) { innerLabel ->
body.buildGetLocal(parameterLocal, location) body.buildGetLocal(parameterLocal, location)
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location) body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location)
@@ -364,7 +364,7 @@ class DeclarationGenerator(
createClassITable(metadata) createClassITable(metadata)
val vtableRefGcType = WasmRefType(WasmHeapType.Type(context.referenceVTableGcType(symbol))) val vtableRefGcType = WasmRefType(WasmHeapType.Type(context.referenceVTableGcType(symbol)))
val classITableRefGcType = WasmRefNullType(WasmHeapType.Simple.Data) val classITableRefGcType = WasmRefNullType(WasmHeapType.Simple.Struct)
val fields = mutableListOf<WasmStructFieldDeclaration>() val fields = mutableListOf<WasmStructFieldDeclaration>()
fields.add(WasmStructFieldDeclaration("vtable", vtableRefGcType, false)) fields.add(WasmStructFieldDeclaration("vtable", vtableRefGcType, false))
fields.add(WasmStructFieldDeclaration("itable", classITableRefGcType, false)) fields.add(WasmStructFieldDeclaration("itable", classITableRefGcType, false))
@@ -101,7 +101,7 @@ class WasmTypeTransformer(
when (val name = klass.name.identifier) { when (val name = klass.name.identifier) {
"anyref" -> WasmAnyRef "anyref" -> WasmAnyRef
"eqref" -> WasmEqRef "eqref" -> WasmEqRef
"dataref" -> WasmRefNullType(WasmHeapType.Simple.Data) "structref" -> WasmRefNullType(WasmHeapType.Simple.Struct)
"i31ref" -> WasmI31Ref "i31ref" -> WasmI31Ref
"funcref" -> WasmRefNullType(WasmHeapType.Simple.Func) "funcref" -> WasmRefNullType(WasmHeapType.Simple.Func)
else -> error("Unknown reference type $name") else -> error("Unknown reference type $name")
@@ -282,7 +282,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// Thus, we export helper "caller" method that JavaScript will use to call kotlin closures: // Thus, we export helper "caller" method that JavaScript will use to call kotlin closures:
// //
// @JsExport // @JsExport
// fun __callFunction_<signatureString>(f: dataref, p1: JsType1, p2: JsType2, ...): JsTypeRes { // fun __callFunction_<signatureString>(f: structref, p1: JsType1, p2: JsType2, ...): JsTypeRes {
// return adapt( // return adapt(
// cast<FunctionN>(f).invoke( // cast<FunctionN>(f).invoke(
// adapt(p1), // adapt(p1),
@@ -302,7 +302,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// @JsFun("""(f) => { // @JsFun("""(f) => {
// (p1, p2, ...) => <wasm-exports>.__callFunction_<signatureString>(f, p1, p2, ...) // (p1, p2, ...) => <wasm-exports>.__callFunction_<signatureString>(f, p1, p2, ...)
// }""") // }""")
// external fun __convertKotlinClosureToJsClosure_<signatureString>(f: dataref): ExternalRef // external fun __convertKotlinClosureToJsClosure_<signatureString>(f: structref): ExternalRef
// //
val kotlinToJsClosureConvertor = context.kotlinClosureToJsConverters.getOrPut(functionTypeInfo.signatureString) { val kotlinToJsClosureConvertor = context.kotlinClosureToJsConverters.getOrPut(functionTypeInfo.signatureString) {
createKotlinToJsClosureConvertor(functionTypeInfo) createKotlinToJsClosureConvertor(functionTypeInfo)
@@ -436,7 +436,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
result.parent = currentParent result.parent = currentParent
result.addValueParameter { result.addValueParameter {
name = Name.identifier("f") name = Name.identifier("f")
type = context.wasmSymbols.wasmDataRefType type = context.wasmSymbols.wasmStructRefType
} }
var count = 0 var count = 0
info.adaptedParameterTypes.forEach { type -> info.adaptedParameterTypes.forEach { type ->
@@ -476,7 +476,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
result.parent = currentParent result.parent = currentParent
result.addValueParameter { result.addValueParameter {
name = Name.identifier("f") name = Name.identifier("f")
type = context.wasmSymbols.wasmDataRefType type = context.wasmSymbols.wasmStructRefType
} }
val builder = context.createIrBuilder(result.symbol) val builder = context.createIrBuilder(result.symbol)
// TODO: Cache created JS closures // TODO: Cache created JS closures
@@ -722,24 +722,24 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
} }
/** /**
* Current V8 Wasm GC mandates dataref type instead of structs and arrays * Current V8 Wasm GC mandates structref type instead of structs and arrays
*/ */
inner class SendKotlinObjectToJsAdapter( inner class SendKotlinObjectToJsAdapter(
override val fromType: IrType override val fromType: IrType
) : InteropTypeAdapter { ) : InteropTypeAdapter {
override val toType: IrType = context.wasmSymbols.wasmDataRefType override val toType: IrType = context.wasmSymbols.wasmStructRefType
override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression { override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression {
return builder.irReinterpretCast(expression, toType) return builder.irReinterpretCast(expression, toType)
} }
} }
/** /**
* Current V8 Wasm GC mandates dataref type instead of structs and arrays * Current V8 Wasm GC mandates structref type instead of structs and arrays
*/ */
inner class ReceivingKotlinObjectFromJsAdapter( inner class ReceivingKotlinObjectFromJsAdapter(
override val toType: IrType override val toType: IrType
) : InteropTypeAdapter { ) : InteropTypeAdapter {
override val fromType: IrType = context.wasmSymbols.wasmDataRefType override val fromType: IrType = context.wasmSymbols.wasmStructRefType
override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression { override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression {
val call = builder.irCall(context.wasmSymbols.refCastNull) val call = builder.irCall(context.wasmSymbols.refCastNull)
call.putValueArgument(0, expression) call.putValueArgument(0, expression)
@@ -749,7 +749,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
} }
/** /**
* Current V8 Wasm GC mandates dataref type instead of structs and arrays * Current V8 Wasm GC mandates structref type instead of structs and arrays
*/ */
/** /**
@@ -43,23 +43,23 @@ fun testExterRef() {
check(null2ExternRef() == null) check(null2ExternRef() == null)
} }
class DataRefImpl class StructRefImpl
typealias DataRef = JsReference<DataRefImpl> typealias StructRef = JsReference<StructRefImpl>
fun notNullDataRef(x: DataRef): DataRef = js("x") fun notNullStructRef(x: StructRef): StructRef = js("x")
fun notNull2DataRef(x: DataRef): DataRef = js("null") fun notNull2StructRef(x: StructRef): StructRef = js("null")
fun nullDataRef(x: DataRef): DataRef? = js("x") fun nullStructRef(x: StructRef): StructRef? = js("x")
fun null2DataRef(x: DataRef): DataRef? = js("null") fun null2StructRef(x: StructRef): StructRef? = js("null")
fun testDataRef() { fun testStructRef() {
val dataRef = DataRefImpl().toJsReference() val structRef = StructRefImpl().toJsReference()
check(notNullDataRef(dataRef) == dataRef) check(notNullStructRef(structRef) == structRef)
checkNPE { notNull2DataRef(dataRef) } checkNPE { notNull2StructRef(structRef) }
check (nullDataRef(dataRef) == dataRef) check (nullStructRef(structRef) == structRef)
check (null2DataRef(dataRef) == null) check (null2StructRef(structRef) == null)
} }
fun notNullInt(): Int = js("123") fun notNullInt(): Int = js("123")
@@ -125,7 +125,7 @@ fun testFloat() {
fun box(): String { fun box(): String {
testString() testString()
testExterRef() testExterRef()
testDataRef() testStructRef()
testInt() testInt()
testBoolean() testBoolean()
testShort() testShort()
@@ -9,10 +9,6 @@
package kotlin.wasm.internal package kotlin.wasm.internal
import kotlin.wasm.internal.reftypes.anyref
import kotlin.wasm.internal.reftypes.dataref
import kotlin.wasm.internal.reftypes.funcref
import kotlin.wasm.internal.reftypes.i31ref
import kotlin.wasm.internal.ExternalInterfaceType import kotlin.wasm.internal.ExternalInterfaceType
@WasmOp(WasmOp.UNREACHABLE) @WasmOp(WasmOp.UNREACHABLE)
@@ -21,6 +21,6 @@ import kotlin.wasm.internal.*
internal interface anyref internal interface anyref
internal interface eqref : anyref internal interface eqref : anyref
internal interface dataref : eqref internal interface structref : eqref
internal interface i31ref : eqref internal interface i31ref : eqref
internal interface funcref : anyref internal interface funcref : anyref
@@ -35,7 +35,7 @@ data class WasmRefType(val heapType: WasmHeapType) : WasmType("ref", -0x15)
object WasmI31Ref : WasmType("i31ref", -0x16) object WasmI31Ref : WasmType("i31ref", -0x16)
@Suppress("unused") @Suppress("unused")
object WasmDataRef : WasmType("dataref", -0x19) object WasmStructRef : WasmType("structref", -0x19)
sealed class WasmHeapType { sealed class WasmHeapType {
data class Type(val type: WasmSymbolReadOnly<WasmTypeDeclaration>) : WasmHeapType() { data class Type(val type: WasmSymbolReadOnly<WasmTypeDeclaration>) : WasmHeapType() {
@@ -49,7 +49,7 @@ sealed class WasmHeapType {
object Extern : Simple("extern", -0x11) object Extern : Simple("extern", -0x11)
object Any : Simple("any", -0x12) object Any : Simple("any", -0x12)
object Eq : Simple("eq", -0x13) object Eq : Simple("eq", -0x13)
object Data : Simple("data", -0x19) object Struct : Simple("struct", -0x19)
object NullNone : Simple("nullref", -0x1b) object NullNone : Simple("nullref", -0x1b)
object NullNoExtern : Simple("nullexternref", -0x17) object NullNoExtern : Simple("nullexternref", -0x17)