[JS IR BE] Add JsExport annotations
This commit is contained in:
@@ -48,6 +48,7 @@ class JsIrBackendContext(
|
||||
override val irBuiltIns: IrBuiltIns,
|
||||
val symbolTable: SymbolTable,
|
||||
irModuleFragment: IrModuleFragment,
|
||||
val additionalExportedDeclarations: Set<FqName>,
|
||||
override val configuration: CompilerConfiguration
|
||||
) : CommonBackendContext {
|
||||
|
||||
@@ -275,6 +276,8 @@ class JsIrBackendContext(
|
||||
val throwableConstructors by lazy { throwableClass.owner.declarations.filterIsInstance<IrConstructor>().map { it.symbol } }
|
||||
val defaultThrowableCtor by lazy { throwableConstructors.single { it.owner.valueParameters.size == 0 } }
|
||||
|
||||
|
||||
|
||||
private fun referenceOperators(): Map<Name, MutableMap<IrClassifierSymbol, IrSimpleFunctionSymbol>> {
|
||||
val primitiveIrSymbols = irBuiltIns.primitiveIrTypes.map { it.classifierOrFail as IrClassSymbol }
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
|
||||
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
|
||||
import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
@@ -34,7 +35,8 @@ fun compile(
|
||||
phaseConfig: PhaseConfig,
|
||||
allDependencies: List<KotlinLibrary>,
|
||||
friendDependencies: List<KotlinLibrary>,
|
||||
mainArguments: List<String>?
|
||||
mainArguments: List<String>?,
|
||||
exportedDeclarations: Set<FqName> = emptySet()
|
||||
): String {
|
||||
val (moduleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) =
|
||||
loadIr(project, files, configuration, allDependencies, friendDependencies)
|
||||
@@ -43,7 +45,7 @@ fun compile(
|
||||
|
||||
val mainFunction = JsMainFunctionDetector.getMainFunctionOrNull(moduleFragment)
|
||||
|
||||
val context = JsIrBackendContext(moduleDescriptor, irBuiltIns, symbolTable, moduleFragment, configuration)
|
||||
val context = JsIrBackendContext(moduleDescriptor, irBuiltIns, symbolTable, moduleFragment, exportedDeclarations, configuration)
|
||||
|
||||
// Load declarations referenced during `context` initialization
|
||||
dependencyModules.forEach {
|
||||
|
||||
+28
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
|
||||
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
|
||||
import org.jetbrains.kotlin.ir.util.isObject
|
||||
import org.jetbrains.kotlin.js.backend.ast.*
|
||||
@@ -94,6 +95,9 @@ class IrModuleToJsTransformer(
|
||||
return null
|
||||
}
|
||||
|
||||
if (!declaration.isExported())
|
||||
return null
|
||||
|
||||
if (declaration.isEffectivelyExternal())
|
||||
return null
|
||||
|
||||
@@ -278,4 +282,28 @@ class IrModuleToJsTransformer(
|
||||
declarationHandler
|
||||
)
|
||||
}
|
||||
|
||||
fun IrDeclarationWithName.isExported(): Boolean {
|
||||
if (fqNameWhenAvailable in backendContext.additionalExportedDeclarations)
|
||||
return true
|
||||
|
||||
// Hack to support properties
|
||||
val correspondingProperty = when {
|
||||
this is IrField -> correspondingPropertySymbol
|
||||
this is IrSimpleFunction -> correspondingPropertySymbol
|
||||
else -> null
|
||||
}
|
||||
correspondingProperty?.let {
|
||||
return it.owner.isExported()
|
||||
}
|
||||
|
||||
if (isJsExport())
|
||||
return true
|
||||
|
||||
return when (val parent = parent) {
|
||||
is IrDeclarationWithName -> parent.isExported()
|
||||
is IrAnnotationContainer -> parent.isJsExport()
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -5,8 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.backend.js.utils
|
||||
|
||||
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithName
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConst
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
|
||||
@@ -20,6 +19,7 @@ object JsAnnotations {
|
||||
val jsNonModuleFqn = FqName("kotlin.js.JsNonModule")
|
||||
val jsNameFqn = FqName("kotlin.js.JsName")
|
||||
val jsQualifierFqn = FqName("kotlin.js.JsQualifier")
|
||||
val jsExportFqn = FqName("kotlin.js.JsExport")
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@@ -38,6 +38,8 @@ fun IrAnnotationContainer.getJsQualifier(): String? =
|
||||
fun IrAnnotationContainer.getJsName(): String? =
|
||||
getAnnotation(JsAnnotations.jsNameFqn)?.getSingleConstStringArgument()
|
||||
|
||||
fun IrAnnotationContainer.isJsExport(): Boolean =
|
||||
hasAnnotation(JsAnnotations.jsExportFqn)
|
||||
|
||||
fun IrDeclarationWithName.getJsNameOrKotlinName(): Name =
|
||||
when (val jsName = getJsName()) {
|
||||
|
||||
Reference in New Issue
Block a user