[Wasm] Reuse JS interop closure converters across modules

Otherwise, these function export names clash without proper linking
This commit is contained in:
Svyatoslav Kuzmich
2022-10-21 11:35:48 +02:00
committed by Space Team
parent f745d0db0e
commit ee2e966c5c
2 changed files with 10 additions and 12 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrTypeSystemContext import org.jetbrains.kotlin.ir.types.IrTypeSystemContext
import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable
@@ -57,6 +58,11 @@ class WasmBackendContext(
override val mapping = JsMapping() override val mapping = JsMapping()
val closureCallExports = mutableMapOf<IrSimpleType, IrSimpleFunction>()
val kotlinClosureToJsConverters = mutableMapOf<IrSimpleType, IrSimpleFunction>()
val jsClosureCallers = mutableMapOf<IrSimpleType, IrSimpleFunction>()
val jsToKotlinClosures = mutableMapOf<IrSimpleType, IrSimpleFunction>()
override val coroutineSymbols = override val coroutineSymbols =
JsCommonCoroutineSymbols(symbolTable, module,this) JsCommonCoroutineSymbols(symbolTable, module,this)
@@ -39,14 +39,6 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
val symbols = context.wasmSymbols val symbols = context.wasmSymbols
val adapters = symbols.jsInteropAdapters val adapters = symbols.jsInteropAdapters
// Used to for export lambdas
object KOTLIN_WASM_CLOSURE_FOR_JS_CLOSURE : IrStatementOriginImpl("KOTLIN_WASM_CLOSURE_FOR_JS_CLOSURE")
private val closureCallExports = mutableMapOf<IrSimpleType, IrSimpleFunction>()
private val kotlinClosureToJsConverters = mutableMapOf<IrSimpleType, IrSimpleFunction>()
private val jsClosureCallers = mutableMapOf<IrSimpleType, IrSimpleFunction>()
private val jsToKotlinClosures = mutableMapOf<IrSimpleType, IrSimpleFunction>()
val additionalDeclarations = mutableListOf<IrDeclaration>() val additionalDeclarations = mutableListOf<IrDeclaration>()
lateinit var currentParent: IrDeclarationParent lateinit var currentParent: IrDeclarationParent
@@ -265,7 +257,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// ) // )
// } // }
// //
closureCallExports.getOrPut(this) { context.closureCallExports.getOrPut(this) {
createKotlinClosureCaller(functionTypeInfo) createKotlinClosureCaller(functionTypeInfo)
} }
@@ -277,7 +269,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// }""") // }""")
// external fun __convertKotlinClosureToJsClosure_<signatureHash>(f: dataref): ExternalRef // external fun __convertKotlinClosureToJsClosure_<signatureHash>(f: dataref): ExternalRef
// //
val kotlinToJsClosureConvertor = kotlinClosureToJsConverters.getOrPut(this) { val kotlinToJsClosureConvertor = context.kotlinClosureToJsConverters.getOrPut(this) {
createKotlinToJsClosureConvertor(functionTypeInfo) createKotlinToJsClosureConvertor(functionTypeInfo)
} }
return FunctionBasedAdapter(kotlinToJsClosureConvertor) return FunctionBasedAdapter(kotlinToJsClosureConvertor)
@@ -380,7 +372,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// @JsFun("(f, p0, p1, ...) => f(p0, p1, ...)") // @JsFun("(f, p0, p1, ...) => f(p0, p1, ...)")
// external fun __callJsClosure_<signatureHash>(f: ExternalRef, p0: JsType1, p1: JsType2, ...): JsResType // external fun __callJsClosure_<signatureHash>(f: ExternalRef, p0: JsType1, p1: JsType2, ...): JsResType
// //
val jsClosureCaller = jsClosureCallers.getOrPut(this) { val jsClosureCaller = context.jsClosureCallers.getOrPut(this) {
createJsClosureCaller(functionTypeInfo) createJsClosureCaller(functionTypeInfo)
} }
@@ -392,7 +384,7 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
// adapt(__callJsClosure_<signatureHash>(f, adapt(p0), adapt(p1), ..)) // adapt(__callJsClosure_<signatureHash>(f, adapt(p0), adapt(p1), ..))
// } // }
// //
val jsToKotlinClosure = jsToKotlinClosures.getOrPut(this) { val jsToKotlinClosure = context.jsToKotlinClosures.getOrPut(this) {
createJsToKotlinClosureConverter(functionTypeInfo, jsClosureCaller) createJsToKotlinClosureConverter(functionTypeInfo, jsClosureCaller)
} }
return FunctionBasedAdapter(jsToKotlinClosure) return FunctionBasedAdapter(jsToKotlinClosure)