[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
@@ -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))
}
}
}