From 1749bafc30cd54f5591b971a26dd87f37647570a Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Thu, 18 May 2023 19:37:02 +0200 Subject: [PATCH] [Wasm] Rename dateref to structref --- .../kotlin/backend/wasm/WasmSymbols.kt | 4 +-- .../backend/wasm/ir2wasm/BodyGenerator.kt | 4 +-- .../wasm/ir2wasm/DeclarationGenerator.kt | 2 +- .../backend/wasm/ir2wasm/TypeTransformer.kt | 2 +- .../wasm/lower/JsInteropFunctionsLowering.kt | 18 ++++++------- .../boxWasmJsInterop/jsToKotlinAdapters.kt | 26 +++++++++---------- .../kotlin/wasm/internal/WasmInstructions.kt | 4 --- .../internal/reftypes/WasmReferenceTypes.kt | 2 +- .../src/org/jetbrains/kotlin/wasm/ir/Types.kt | 4 +-- 9 files changed, 31 insertions(+), 35 deletions(-) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt index 9609cfe8c7e..bcf0b6a4dd2 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt @@ -289,8 +289,8 @@ class WasmSymbols( } } - private val wasmDataRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.dataref")) - val wasmDataRefType by lazy { wasmDataRefClass.defaultType } + private val wasmStructRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.structref")) + val wasmStructRefType by lazy { wasmStructRefClass.defaultType } val wasmAnyRefClass = getIrClass(FqName("kotlin.wasm.internal.reftypes.anyref")) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt index d7844985bd2..ecde31b17ef 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt @@ -314,7 +314,7 @@ class BodyGenerator( if (klassSymbol.owner.hasInterfaceSuperClass()) { body.buildGetGlobal(context.referenceGlobalClassITable(klassSymbol), location) } else { - body.buildRefNull(WasmHeapType.Simple.Data, location) + body.buildRefNull(WasmHeapType.Simple.Struct, location) } body.buildConstI32Symbol(context.referenceTypeId(klassSymbol), location) @@ -543,7 +543,7 @@ class BodyGenerator( val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER) body.buildSetLocal(parameterLocal, location) 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.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt index f810365e739..ca28d412190 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt @@ -364,7 +364,7 @@ class DeclarationGenerator( createClassITable(metadata) val vtableRefGcType = WasmRefType(WasmHeapType.Type(context.referenceVTableGcType(symbol))) - val classITableRefGcType = WasmRefNullType(WasmHeapType.Simple.Data) + val classITableRefGcType = WasmRefNullType(WasmHeapType.Simple.Struct) val fields = mutableListOf() fields.add(WasmStructFieldDeclaration("vtable", vtableRefGcType, false)) fields.add(WasmStructFieldDeclaration("itable", classITableRefGcType, false)) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt index 69dcf0009b4..bc48119b5ca 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/TypeTransformer.kt @@ -101,7 +101,7 @@ class WasmTypeTransformer( when (val name = klass.name.identifier) { "anyref" -> WasmAnyRef "eqref" -> WasmEqRef - "dataref" -> WasmRefNullType(WasmHeapType.Simple.Data) + "structref" -> WasmRefNullType(WasmHeapType.Simple.Struct) "i31ref" -> WasmI31Ref "funcref" -> WasmRefNullType(WasmHeapType.Simple.Func) else -> error("Unknown reference type $name") diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt index 96f84a66f2e..c0fa36c020c 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/JsInteropFunctionsLowering.kt @@ -282,7 +282,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT // Thus, we export helper "caller" method that JavaScript will use to call kotlin closures: // // @JsExport - // fun __callFunction_(f: dataref, p1: JsType1, p2: JsType2, ...): JsTypeRes { + // fun __callFunction_(f: structref, p1: JsType1, p2: JsType2, ...): JsTypeRes { // return adapt( // cast(f).invoke( // adapt(p1), @@ -302,7 +302,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT // @JsFun("""(f) => { // (p1, p2, ...) => .__callFunction_(f, p1, p2, ...) // }""") - // external fun __convertKotlinClosureToJsClosure_(f: dataref): ExternalRef + // external fun __convertKotlinClosureToJsClosure_(f: structref): ExternalRef // val kotlinToJsClosureConvertor = context.kotlinClosureToJsConverters.getOrPut(functionTypeInfo.signatureString) { createKotlinToJsClosureConvertor(functionTypeInfo) @@ -436,7 +436,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT result.parent = currentParent result.addValueParameter { name = Name.identifier("f") - type = context.wasmSymbols.wasmDataRefType + type = context.wasmSymbols.wasmStructRefType } var count = 0 info.adaptedParameterTypes.forEach { type -> @@ -476,7 +476,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT result.parent = currentParent result.addValueParameter { name = Name.identifier("f") - type = context.wasmSymbols.wasmDataRefType + type = context.wasmSymbols.wasmStructRefType } val builder = context.createIrBuilder(result.symbol) // 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( override val fromType: IrType ) : InteropTypeAdapter { - override val toType: IrType = context.wasmSymbols.wasmDataRefType + override val toType: IrType = context.wasmSymbols.wasmStructRefType override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression { 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( override val toType: IrType ) : InteropTypeAdapter { - override val fromType: IrType = context.wasmSymbols.wasmDataRefType + override val fromType: IrType = context.wasmSymbols.wasmStructRefType override fun adapt(expression: IrExpression, builder: IrBuilderWithScope): IrExpression { val call = builder.irCall(context.wasmSymbols.refCastNull) 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 */ /** diff --git a/compiler/testData/codegen/boxWasmJsInterop/jsToKotlinAdapters.kt b/compiler/testData/codegen/boxWasmJsInterop/jsToKotlinAdapters.kt index 6339a749938..30816ce4f8f 100644 --- a/compiler/testData/codegen/boxWasmJsInterop/jsToKotlinAdapters.kt +++ b/compiler/testData/codegen/boxWasmJsInterop/jsToKotlinAdapters.kt @@ -43,23 +43,23 @@ fun testExterRef() { check(null2ExternRef() == null) } -class DataRefImpl -typealias DataRef = JsReference +class StructRefImpl +typealias StructRef = JsReference -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() { - val dataRef = DataRefImpl().toJsReference() - check(notNullDataRef(dataRef) == dataRef) - checkNPE { notNull2DataRef(dataRef) } - check (nullDataRef(dataRef) == dataRef) - check (null2DataRef(dataRef) == null) +fun testStructRef() { + val structRef = StructRefImpl().toJsReference() + check(notNullStructRef(structRef) == structRef) + checkNPE { notNull2StructRef(structRef) } + check (nullStructRef(structRef) == structRef) + check (null2StructRef(structRef) == null) } fun notNullInt(): Int = js("123") @@ -125,7 +125,7 @@ fun testFloat() { fun box(): String { testString() testExterRef() - testDataRef() + testStructRef() testInt() testBoolean() testShort() diff --git a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt index 7f097b84ee9..ab1fb81f02a 100644 --- a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt +++ b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/WasmInstructions.kt @@ -9,10 +9,6 @@ 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 @WasmOp(WasmOp.UNREACHABLE) diff --git a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/reftypes/WasmReferenceTypes.kt b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/reftypes/WasmReferenceTypes.kt index bbf9d671d51..6c75bb4de90 100644 --- a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/reftypes/WasmReferenceTypes.kt +++ b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/reftypes/WasmReferenceTypes.kt @@ -21,6 +21,6 @@ import kotlin.wasm.internal.* internal interface anyref internal interface eqref : anyref -internal interface dataref : eqref +internal interface structref : eqref internal interface i31ref : eqref internal interface funcref : anyref \ No newline at end of file diff --git a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/Types.kt b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/Types.kt index bdeb999a94b..210560adec2 100644 --- a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/Types.kt +++ b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/Types.kt @@ -35,7 +35,7 @@ data class WasmRefType(val heapType: WasmHeapType) : WasmType("ref", -0x15) object WasmI31Ref : WasmType("i31ref", -0x16) @Suppress("unused") -object WasmDataRef : WasmType("dataref", -0x19) +object WasmStructRef : WasmType("structref", -0x19) sealed class WasmHeapType { data class Type(val type: WasmSymbolReadOnly) : WasmHeapType() { @@ -49,7 +49,7 @@ sealed class WasmHeapType { object Extern : Simple("extern", -0x11) object Any : Simple("any", -0x12) object Eq : Simple("eq", -0x13) - object Data : Simple("data", -0x19) + object Struct : Simple("struct", -0x19) object NullNone : Simple("nullref", -0x1b) object NullNoExtern : Simple("nullexternref", -0x17)