Revert "[Wasm] Support lazy associated object initialisation"

This reverts commit 552ee1ee38.
This commit is contained in:
Anastasiia Spaseeva
2024-01-25 14:11:56 +01:00
parent 91002eacda
commit 945f602b91
12 changed files with 58 additions and 140 deletions
@@ -77,7 +77,8 @@ class WasmSymbols(
getProperty(FqName.fromSegments(listOf("kotlin", "wasm", "internal", "isNotFirstWasmExportCall")))
)
internal val tryGetAssociatedObject = getInternalFunction("tryGetAssociatedObject")
internal val initAssociatedObjects = getInternalFunction("initAssociatedObjects")
internal val addAssociatedObject = getInternalFunction("addAssociatedObject")
override val throwNullPointerException = getInternalFunction("THROW_NPE")
override val throwISE = getInternalFunction("THROW_ISE")
@@ -7,12 +7,11 @@ package org.jetbrains.kotlin.backend.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irIfThen
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.WasmSymbols
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.utils.associatedObject
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.IrClass
@@ -46,7 +45,7 @@ class AssociatedObjectsLowering(val context: WasmBackendContext) : FileLoweringP
}
private val visitor = object : IrElementVisitorVoid {
val tryGetAssociatedObject = (context.wasmSymbols.tryGetAssociatedObject.owner.body as IrBlockBody).statements
val initFunctionStatements = (context.wasmSymbols.initAssociatedObjects.owner.body as IrBlockBody).statements
override fun visitElement(element: IrElement) {
if (element is IrClass) {
@@ -63,44 +62,49 @@ class AssociatedObjectsLowering(val context: WasmBackendContext) : FileLoweringP
if (klassAnnotation.valueArgumentsCount != 1) continue
val associatedObject = klassAnnotation.associatedObject() ?: continue
val builder = cachedBuilder ?: context.createIrBuilder(context.wasmSymbols.tryGetAssociatedObject)
val builder = cachedBuilder ?: context.createIrBuilder(context.wasmSymbols.initAssociatedObjects)
cachedBuilder = builder
val selector = builder.createAssociatedObjectSelector(
val addCall = builder.createAssociatedObjectAdd(
wasmSymbols = context.wasmSymbols,
irBuiltIns = context.irBuiltIns,
targetClass = declaration.symbol,
keyAnnotation = annotationClass.symbol,
associatedObject = associatedObject.symbol
)
tryGetAssociatedObject.add(0, selector)
initFunctionStatements.add(addCall)
}
}
}
}
private fun IrBuilderWithScope.createAssociatedObjectSelector(
private fun IrBuilderWithScope.createAssociatedObjectAdd(
wasmSymbols: WasmSymbols,
irBuiltIns: IrBuiltIns,
targetClass: IrClassSymbol,
keyAnnotation: IrClassSymbol,
associatedObject: IrClassSymbol
): IrStatement {
val classIdParam = irGet(wasmSymbols.tryGetAssociatedObject.owner.valueParameters[0])
val keyIdParam = irGet(wasmSymbols.tryGetAssociatedObject.owner.valueParameters[1])
val classId = irCall(wasmSymbols.wasmTypeId, irBuiltIns.intType).also {
it.putTypeArgument(0, targetClass.defaultType)
}
val keyId = irCall(wasmSymbols.wasmTypeId, irBuiltIns.intType).also {
it.putTypeArgument(0, keyAnnotation.defaultType)
}
return irIfThen(
condition = irEquals(classIdParam, classId),
thenPart = irIfThen(
condition = irEquals(keyIdParam, keyId),
thenPart = irReturn(irGetObjectValue(irBuiltIns.anyType, associatedObject))
): IrCall = buildStatement(UNDEFINED_OFFSET, UNDEFINED_OFFSET) {
irCall(wasmSymbols.addAssociatedObject, irBuiltIns.unitType).also { addCall ->
addCall.putValueArgument(
0,
irGet(wasmSymbols.initAssociatedObjects.owner.valueParameters[0])
)
)
addCall.putValueArgument(
1,
irCall(wasmSymbols.wasmTypeId, irBuiltIns.intType).also {
it.putTypeArgument(0, targetClass.defaultType)
}
)
addCall.putValueArgument(
2,
irCall(wasmSymbols.wasmTypeId, irBuiltIns.intType).also {
it.putTypeArgument(0, keyAnnotation.defaultType)
}
)
addCall.putValueArgument(
3,
irGetObjectValue(irBuiltIns.anyType, associatedObject)
)
}
}