[K/JS] Prepare JS Plain Objects plugin to publication

This commit is contained in:
Artem Kobzar
2024-01-17 10:52:58 +00:00
committed by Space Team
parent 561be747c1
commit dfe2d8651e
53 changed files with 479 additions and 393 deletions
@@ -0,0 +1,25 @@
description = "Kotlin JavaScript Plain Objects Compiler Plugin (Backend)"
plugins {
kotlin("jvm")
id("jps-compatible")
}
dependencies {
compileOnly(project(":compiler:backend"))
compileOnly(project(":compiler:ir.backend.common"))
compileOnly(project(":compiler:ir.tree"))
implementation(project(":plugins:js-plain-objects:compiler-plugin:js-plain-objects.common"))
compileOnly(intellijCore())
}
sourceSets {
"main" { projectDefault() }
"test" { none() }
}
runtimeJar()
sourcesJar()
javadocJar()
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlinx.jspo.compiler.backend
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlinx.jspo.compiler.resolve.JsPlainObjectsPluginKey
import org.jetbrains.kotlinx.jspo.compiler.resolve.StandardIds
private class MoveExternalInlineFunctionsWithBodiesOutsideLowering(private val context: IrPluginContext) : DeclarationTransformer {
private val jsFunction = context.referenceFunctions(StandardIds.JS_FUNCTION_ID).single()
private val EXPECTED_ORIGIN = IrDeclarationOrigin.GeneratedByPlugin(JsPlainObjectsPluginKey)
@OptIn(UnsafeDuringIrConstructionAPI::class)
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
val file = declaration.file
val parent = declaration.parentClassOrNull
if (parent == null || declaration !is IrSimpleFunction || declaration.origin != EXPECTED_ORIGIN) return null
file.declarations.add(declaration)
declaration.body = when (declaration.name) {
StandardNames.DATA_CLASS_COPY, OperatorNameConventions.INVOKE -> declaration.generateBodyForFactoryAndCopyFunction()
else -> error("Unexpected function with name `${declaration.name.identifier}`")
}
declaration.parent = file
declaration.isExternal = false
return emptyList()
}
private fun IrSimpleFunction.generateBodyForFactoryAndCopyFunction(): IrBlockBody {
val declaration = this
return context.irFactory.createBlockBody(startOffset, declaration.endOffset).apply {
statements += IrReturnImpl(
declaration.startOffset,
declaration.endOffset,
declaration.returnType,
declaration.symbol,
IrCallImpl(
declaration.startOffset,
declaration.endOffset,
declaration.returnType,
jsFunction,
0,
1,
).apply {
val jsObject = "{ ${declaration.valueParameters.joinToString(", ") { "${it.name.identifier}:${it.name.identifier}" }} }"
putValueArgument(0, jsObject.toIrConst(context.irBuiltIns.stringType))
}
)
}
}
}
open class JsPlainObjectsLoweringExtension : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
MoveExternalInlineFunctionsWithBodiesOutsideLowering(pluginContext).lower(moduleFragment)
}
}