[Wasm] export __callFunction_* only when a respective closure/lambda converter is used

This commit is contained in:
Zalim Bashorov
2023-04-06 15:03:04 +02:00
committed by Space Team
parent 699ad87be2
commit c114afb55d
7 changed files with 19 additions and 8 deletions
@@ -53,6 +53,9 @@ private fun buildRoots(modules: List<IrModuleFragment>, context: WasmBackendCont
add(context.irBuiltIns.throwableClass.owner)
add(context.mainCallsWrapperFunction)
add(context.fieldInitFunction)
// Remove all functions used to call a kotlin closure from JS side, reachable ones will be added back later.
removeAll(context.closureCallExports.values)
}
private inline fun List<IrModuleFragment>.onAllFiles(body: IrFile.() -> Unit) {
@@ -23,6 +23,10 @@ internal class WasmUsefulDeclarationProcessor(
private val unitGetInstance: IrSimpleFunction = context.findUnitGetInstanceFunction()
// The mapping from function for wrapping a kotlin closure/lambda with JS closure to function used to call a kotlin closure from JS side.
private val kotlinClosureToJsClosureConvertFunToKotlinClosureCallFun =
context.kotlinClosureToJsConverters.entries.associate { (k, v) -> v to context.closureCallExports[k] }
override val bodyVisitor: BodyVisitorBase = object : BodyVisitorBase() {
override fun visitConst(expression: IrConst<*>, data: IrDeclaration) = when (expression.kind) {
is IrConstKind.Null -> expression.type.enqueueType(data, "expression type")
@@ -160,6 +164,8 @@ internal class WasmUsefulDeclarationProcessor(
irFunction.getEffectiveValueParameters().forEach { it.enqueueValueParameterType(irFunction) }
irFunction.returnType.enqueueType(irFunction, "function return type")
kotlinClosureToJsClosureConvertFunToKotlinClosureCallFun[irFunction]?.enqueue(irFunction, "kotlin closure to JS closure conversion", false)
}
override fun processSimpleFunction(irFunction: IrSimpleFunction) {
@@ -458,6 +458,8 @@ class JsInteropFunctionsLowering(val context: WasmBackendContext) : DeclarationT
}
+irReturn(info.resultAdapter.adaptIfNeeded(callInvoke, builder))
}
// TODO find out a better way to export the such declarations only when it's required. Also, fix building roots for DCE, then.
result.annotations += builder.irCallConstructor(context.wasmSymbols.jsExportConstructor, typeArguments = emptyList())
additionalDeclarations += result
return result
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 34_869
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_359
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 29_800
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_104
// FILE: test.kt
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 65_687
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_458
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 60_691
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_130
fun box(): String {
println("Hello, World!")
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 38_128
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_798
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 31_232
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_659
// FILE: test.kt
+2 -2
View File
@@ -1,6 +1,6 @@
// TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 34_917
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_231
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 29_848
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 5_976
fun box() = "OK"