[Wasm] Restrict WasmExport functions for primitive typed signature

This commit is contained in:
Igor Yakovlev
2023-07-14 18:57:00 +02:00
committed by Space Team
parent f47b2f404f
commit 78fa93d75c
8 changed files with 93 additions and 49 deletions
@@ -30,9 +30,9 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
JsExportDeclarationChecker,
WasmExternalDeclarationChecker,
WasmImportAnnotationChecker,
WasmExportAnnotationChecker,
WasmJsFunAnnotationChecker,
WasmJsInteropTypesChecker,
WasmInteropTypesChecker,
),
additionalCallCheckers = listOf(
JsModuleCallChecker,
@@ -31,10 +31,10 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
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")
put(ErrorsWasm.WASM_IMPORT_VARARG_PARAMETER, "Vararg parameters are not supported with @WasmImport")
put(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE, "Unsupported @WasmImport parameter type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_RETURN_TYPE, "Unsupported @WasmImport return type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE, "Default parameter values are not supported with @WasmImport and @WasmExport")
put(ErrorsWasm.WASM_IMPORT_EXPORT_VARARG_PARAMETER, "Vararg parameters are not supported with @WasmImport and @WasmExport")
put(ErrorsWasm.WASM_IMPORT_EXPORT_UNSUPPORTED_PARAMETER_TYPE, "Unsupported @WasmImport and @WasmExport parameter type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE, "Unsupported @WasmImport and @WasmExport return type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WRONG_JS_FUN_TARGET, "Only top-level external functions can be implemented using @JsFun")
@@ -25,10 +25,10 @@ public interface ErrorsWasm {
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);
DiagnosticFactory0<PsiElement> WASM_IMPORT_VARARG_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_VARARG_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> WRONG_JS_FUN_TARGET = DiagnosticFactory0.create(ERROR);
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.components.isVararg
@@ -44,26 +45,12 @@ object WasmImportAnnotationChecker : DeclarationChecker {
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
trace.report(ErrorsWasm.NESTED_WASM_IMPORT.on(wasmImportPsi))
}
if (descriptor is FunctionDescriptor) {
if (!descriptor.isEffectivelyExternal()) {
trace.report(ErrorsWasm.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION.on(wasmImportPsi))
}
for (parameter: ValueParameterDescriptor in descriptor.valueParameters) {
val valueParameterDeclaration by lazy { DescriptorToSourceUtils.descriptorToDeclaration(parameter)!! }
if (parameter.declaresDefaultValue()) {
trace.report(ErrorsWasm.WASM_IMPORT_PARAMETER_DEFAULT_VALUE.on(valueParameterDeclaration))
}
if (parameter.isVararg) {
trace.report(ErrorsWasm.WASM_IMPORT_VARARG_PARAMETER.on(valueParameterDeclaration))
}
if (!isParameterTypeSupported(parameter.type)) {
trace.report(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE.on(valueParameterDeclaration, parameter.type))
}
}
val returnType = descriptor.returnType
if (returnType != null && !isReturnTypeSupported(returnType)) {
trace.report(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_RETURN_TYPE.on(declaration, returnType))
}
checkSignatureIsPrimitive(descriptor, trace, declaration)
}
}
@@ -72,5 +59,23 @@ object WasmImportAnnotationChecker : DeclarationChecker {
private fun isReturnTypeSupported(type: KotlinType): Boolean =
isParameterTypeSupported(type) || type.isUnit()
}
fun checkSignatureIsPrimitive(descriptor: FunctionDescriptor, trace: BindingTrace, declaration: KtDeclaration) {
for (parameter: ValueParameterDescriptor in descriptor.valueParameters) {
val valueParameterDeclaration by lazy { DescriptorToSourceUtils.descriptorToDeclaration(parameter)!! }
if (parameter.declaresDefaultValue()) {
trace.report(ErrorsWasm.WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE.on(valueParameterDeclaration))
}
if (parameter.isVararg) {
trace.report(ErrorsWasm.WASM_IMPORT_EXPORT_VARARG_PARAMETER.on(valueParameterDeclaration))
}
if (!isParameterTypeSupported(parameter.type)) {
trace.report(ErrorsWasm.WASM_IMPORT_EXPORT_UNSUPPORTED_PARAMETER_TYPE.on(valueParameterDeclaration, parameter.type))
}
}
val returnType = descriptor.returnType
if (returnType != null && !isReturnTypeSupported(returnType)) {
trace.report(ErrorsWasm.WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE.on(declaration, returnType))
}
}
}
@@ -17,16 +17,18 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.wasm.util.hasValidJsCodeBody
// TODO: Implement in K2: KT-56849
object WasmInteropTypesChecker : DeclarationChecker {
object WasmExportAnnotationChecker : DeclarationChecker {
private val wasmExportFqName = FqName("kotlin.wasm.WasmExport")
private val jsExportFqName = FqName("kotlin.js.JsExport")
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is FunctionDescriptor) return
if (!descriptor.annotations.hasAnnotation(FqName("kotlin.wasm.WasmExport"))) return
if (!descriptor.annotations.hasAnnotation(wasmExportFqName)) return
val trace = context.trace
val bindingContext = trace.bindingContext
if (descriptor.annotations.hasAnnotation(FqName("kotlin.js.JsExport"))) {
if (descriptor.annotations.hasAnnotation(jsExportFqName)) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION.on(reportOn))
}
@@ -40,5 +42,7 @@ object WasmInteropTypesChecker : DeclarationChecker {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.NESTED_WASM_EXPORT.on(reportOn))
}
WasmImportAnnotationChecker.checkSignatureIsPrimitive(descriptor, trace, declaration)
}
}