From 0e54e00e48cfdbd4ad9cf4e9759261a80d9f3c6c Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Fri, 28 Oct 2022 10:26:12 +0200 Subject: [PATCH] [WASM] Make object creation inside constructors --- .../backend/wasm/ir2wasm/BodyGenerator.kt | 33 +++++++++++++------ .../wasm/ir2wasm/DeclarationGenerator.kt | 4 +++ 2 files changed, 27 insertions(+), 10 deletions(-) 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 5402f6f117d..79e075760f2 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 @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.backend.wasm.utils.* import org.jetbrains.kotlin.backend.wasm.utils.isCanonical import org.jetbrains.kotlin.ir.IrBuiltIns import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.backend.js.lower.PrimaryConstructorLowering import org.jetbrains.kotlin.ir.backend.js.utils.erasedUpperBound import org.jetbrains.kotlin.ir.backend.js.utils.findUnitGetInstanceFunction import org.jetbrains.kotlin.ir.backend.js.utils.isDispatchReceiver @@ -262,9 +263,8 @@ class BodyGenerator( return } - generateAnyParameters(klassSymbol) - if (expression.symbol.owner.hasWasmPrimitiveConstructorAnnotation()) { + generateAnyParameters(klassSymbol) for (i in 0 until expression.valueArgumentsCount) { generateExpression(expression.getValueArgument(i)!!) } @@ -272,14 +272,7 @@ class BodyGenerator( return } - val irFields: List = klass.allFields(backendContext.irBuiltIns) - irFields.forEachIndexed { index, field -> - if (index > 1) { - generateDefaultInitializerForType(context.transformType(field.type), body) - } - } - - body.buildStructNew(wasmGcType) + body.buildRefNull(WasmHeapType.Simple.NullNone) // this = null generateCall(expression) } @@ -296,6 +289,26 @@ class BodyGenerator( body.buildConstI32(0) // Any::_hashCode } + fun generateObjectCreationPrefixIfNeeded(constructor: IrConstructor) { + val parentClass = constructor.parentAsClass + if (constructor.origin == PrimaryConstructorLowering.SYNTHETIC_PRIMARY_CONSTRUCTOR) return + if (parentClass.isAbstractOrSealed) return + val thisParameter = context.referenceLocal(parentClass.thisReceiver!!.symbol) + body.buildGetLocal(thisParameter) + body.buildInstr(WasmOp.REF_IS_NULL) + body.buildIf("this_init") + generateAnyParameters(parentClass.symbol) + val irFields: List = parentClass.allFields(backendContext.irBuiltIns) + irFields.forEachIndexed { index, field -> + if (index > 1) { + generateDefaultInitializerForType(context.transformType(field.type), body) + } + } + body.buildStructNew(context.referenceGcType(parentClass.symbol)) + body.buildSetLocal(thisParameter) + body.buildEnd() + } + override fun visitDelegatingConstructorCall(expression: IrDelegatingConstructorCall) { val klass = context.irFunction.parentAsClass 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 15e36294e5d..3a0fc44319b 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 @@ -133,6 +133,10 @@ class DeclarationGenerator( isGetUnitFunction = declaration == unitGetInstanceFunction ) + if (declaration is IrConstructor) { + bodyBuilder.generateObjectCreationPrefixIfNeeded(declaration) + } + require(declaration.body is IrBlockBody) { "Only IrBlockBody is supported" } declaration.body?.acceptVoid(bodyBuilder)