[Wasm] WasmExport implementation

This commit is contained in:
Igor Yakovlev
2023-07-05 12:55:10 +02:00
committed by Space Team
parent 58639951f6
commit b5eafb9eb7
15 changed files with 149 additions and 10 deletions
@@ -6,6 +6,8 @@
package org.jetbrains.kotlin.backend.wasm.dce
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.ir2wasm.isExported
import org.jetbrains.kotlin.backend.wasm.utils.getWasmExportNameIfWasmExport
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.dce.DceDumpNameCache
import org.jetbrains.kotlin.ir.backend.js.utils.*
@@ -51,7 +53,7 @@ private fun buildRoots(modules: List<IrModuleFragment>, context: WasmBackendCont
modules.onAllFiles {
declarations.forEach { declaration ->
if (declaration.isJsExport()) {
if (declaration is IrFunction && declaration.isExported()) {
declaration.acceptVoid(declarationsCollector)
}
}
@@ -207,5 +207,5 @@ internal class WasmUsefulDeclarationProcessor(
}
}
override fun isExported(declaration: IrDeclaration): Boolean = declaration.isJsExport()
override fun isExported(declaration: IrDeclaration): Boolean = (declaration is IrFunction && declaration.isExported())
}
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.parentOrNull
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
@@ -182,11 +183,16 @@ class DeclarationGenerator(
if (initPriority != null)
context.registerInitFunction(function, initPriority)
if (declaration.isExported()) {
val nameIfExported = when {
declaration.isJsExport() -> declaration.getJsNameOrKotlinName().identifier
else -> declaration.getWasmExportNameIfWasmExport()
}
if (nameIfExported != null) {
context.addExport(
WasmExport.Function(
field = function,
name = declaration.getJsNameOrKotlinName().identifier
name = nameIfExported
)
)
}
@@ -500,8 +506,7 @@ fun IrFunction.getEffectiveValueParameters(): List<IrValueParameter> {
}
fun IrFunction.isExported(): Boolean =
isJsExport()
isJsExport() || getWasmExportNameIfWasmExport() != null
fun generateConstExpression(
expression: IrConst<*>,
@@ -12,13 +12,13 @@ import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.ir2wasm.isBuiltInWasmRefType
import org.jetbrains.kotlin.backend.wasm.ir2wasm.isExported
import org.jetbrains.kotlin.backend.wasm.ir2wasm.isExternalType
import org.jetbrains.kotlin.backend.wasm.ir2wasm.toJsStringLiteral
import org.jetbrains.kotlin.backend.wasm.utils.getJsFunAnnotation
import org.jetbrains.kotlin.backend.wasm.utils.getWasmImportDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.backend.js.utils.getJsNameOrKotlinName
import org.jetbrains.kotlin.ir.backend.js.utils.isJsExport
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
@@ -49,7 +49,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
if (declaration.isFakeOverride) return null
if (declaration !is IrSimpleFunction) return null
val isExported = declaration.isExported()
val isExported = declaration.isJsExport()
val isExternal = declaration.isExternal || declaration.getJsFunAnnotation() != null
if (declaration.isPropertyAccessor) return null
if (declaration.parent !is IrPackageFragment) return null
@@ -61,3 +61,10 @@ fun IrAnnotationContainer.getJsFunAnnotation(): String? =
fun IrAnnotationContainer.getJsPrimitiveType(): String? =
getAnnotation(FqName("kotlin.wasm.internal.JsPrimitive"))?.getSingleConstStringArgument()
@Suppress("UNCHECKED_CAST")
fun IrFunction.getWasmExportNameIfWasmExport(): String? {
val annotation = getAnnotation(FqName("kotlin.wasm.WasmExport")) ?: return null
if (annotation.valueArgumentsCount == 0) return name.identifier
return (annotation.getValueArgument(0) as? IrConst<String>)?.value ?: name.identifier
}
@@ -0,0 +1,19 @@
// TARGET_BACKEND: WASM
import kotlin.wasm.WasmExport
@WasmExport
fun exportDefaultName(): String = "some string"
fun checkDefaultName(): Int = js("typeof wasmExports.exportDefaultName() === 'object'")
@WasmExport("exportOverriddenName")
fun exportWithName(): String = "some string"
fun checkOverriddenName(): Int = js("typeof wasmExports.exportOverriddenName() === 'object'")
fun box(): String {
if (checkDefaultName() != 1) return "checkDefaultName fail"
if (checkOverriddenName() != 1) return "checkOverriddenName fail"
return "OK"
}
@@ -0,0 +1,23 @@
import kotlin.wasm.WasmExport
<!WASM_EXPORT_ON_EXTERNAL_DECLARATION!>@WasmExport("a")
external fun foo0(): Unit<!>
<!WASM_EXPORT_ON_EXTERNAL_DECLARATION!>@WasmExport("a")
fun foo1(): Int = js("42")<!>
class C() {
<!NESTED_WASM_EXPORT!>@WasmExport("a")
fun foo2(): Int = 42<!>
}
@WasmExport("a")
fun foo3(): Int = 42
@WasmExport()
fun foo4(): Int = 42
<!JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION!>@OptIn(kotlin.js.ExperimentalJsExport::class)
@JsExport()
@WasmExport()
fun foo6(): Int = 42<!>
@@ -11,11 +11,23 @@ package kotlin.wasm
*
* Can only be used on top-level external functions.
*
* In JavaScript environment,
* function will be imported from ES module without type adapters.
* The annotated function will be imported into Wasm module without type adapters.
*/
@Target(AnnotationTarget.FUNCTION)
public annotation class WasmImport(
val module: String,
val name: String = ""
)
/**
* Exports a function with the given optional [name].
* The declaration name will be used if the [name] argument is not provided.
*
* Can only be used on top-level non-external functions.
*
* The annotated function will be exported from Wasm module without type adapters.
*/
@Target(AnnotationTarget.FUNCTION)
public annotation class WasmExport(
val name: String = ""
)
@@ -32,6 +32,7 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
WasmImportAnnotationChecker,
WasmJsFunAnnotationChecker,
WasmJsInteropTypesChecker,
WasmInteropTypesChecker,
),
additionalCallCheckers = listOf(
JsModuleCallChecker,
@@ -25,6 +25,10 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
Renderers.RENDER_TYPE
)
put(ErrorsWasm.NESTED_WASM_EXPORT, "Only top-level functions can be exported with @WasmExport")
put(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION, "Functions annotated with @WasmExport must not be external")
put(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION, "Cannot use @WasmExport and @JsExport for same function")
put(ErrorsWasm.NESTED_WASM_IMPORT, "Only top-level functions can be imported with @WasmImport")
put(ErrorsWasm.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION, "Functions annotated with @WasmImport must be external")
put(ErrorsWasm.WASM_IMPORT_PARAMETER_DEFAULT_VALUE, "Default parameter values are not supported with @WasmImport")
@@ -19,6 +19,10 @@ public interface ErrorsWasm {
DiagnosticFactory2<PsiElement, String, KotlinType>
WRONG_JS_INTEROP_TYPE = DiagnosticFactory2.create(ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory0<PsiElement> NESTED_WASM_EXPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_EXPORT_ON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
@@ -0,0 +1,44 @@
/*
* 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.kotlin.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.wasm.util.hasValidJsCodeBody
// TODO: Implement in K2: KT-56849
object WasmInteropTypesChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is FunctionDescriptor) return
if (!descriptor.annotations.hasAnnotation(FqName("kotlin.wasm.WasmExport"))) return
val trace = context.trace
val bindingContext = trace.bindingContext
if (descriptor.annotations.hasAnnotation(FqName("kotlin.js.JsExport"))) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION.on(reportOn))
}
if (descriptor.isEffectivelyExternal() || descriptor.hasValidJsCodeBody(bindingContext)) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(reportOn))
}
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.NESTED_WASM_EXPORT.on(reportOn))
}
}
}
@@ -151,6 +151,12 @@ public class FirWasmCodegenWasmJsInteropTestGenerated extends AbstractFirWasmCod
runTest("compiler/testData/codegen/boxWasmJsInterop/vararg.kt");
}
@Test
@TestMetadata("wasmExport.kt")
public void testWasmExport() throws Exception {
runTest("compiler/testData/codegen/boxWasmJsInterop/wasmExport.kt");
}
@Test
@TestMetadata("wasmImport.kt")
public void testWasmImport() throws Exception {
@@ -151,6 +151,12 @@ public class K1WasmCodegenWasmJsInteropTestGenerated extends AbstractK1WasmCodeg
runTest("compiler/testData/codegen/boxWasmJsInterop/vararg.kt");
}
@Test
@TestMetadata("wasmExport.kt")
public void testWasmExport() throws Exception {
runTest("compiler/testData/codegen/boxWasmJsInterop/wasmExport.kt");
}
@Test
@TestMetadata("wasmImport.kt")
public void testWasmImport() throws Exception {
@@ -85,6 +85,12 @@ public class DiagnosticsWasmTestGenerated extends AbstractDiagnosticsWasmTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/wasmTests/wasmInterop"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("wasmExport.kt")
public void testWasmExport() throws Exception {
runTest("compiler/testData/diagnostics/wasmTests/wasmInterop/wasmExport.kt");
}
@Test
@TestMetadata("wasmImport.kt")
public void testWasmImport() throws Exception {