From fc80104d5510b25db22d7acd7dffd3db9ef94c86 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Mon, 19 Dec 2022 22:49:44 +0100 Subject: [PATCH] [Wasm] Fix external functions import clashing --- .../wasm/ir2wasm/DeclarationGenerator.kt | 5 ++++- .../codegen/boxWasmJsInterop/nameClash.kt | 21 +++++++++++++++++++ ...irJsCodegenWasmJsInteropTestGenerated.java | 6 ++++++ ...IrCodegenWasmJsInteropJsTestGenerated.java | 6 ++++++ ...CodegenWasmJsInteropWasmTestGenerated.java | 5 +++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/boxWasmJsInterop/nameClash.kt diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt index 09c8e3eea16..f890b2281cf 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt @@ -54,7 +54,10 @@ class DeclarationGenerator( } private fun jsCodeName(declaration: IrFunction): String { - return declaration.fqNameWhenAvailable!!.asString() + "_" + (declaration as IrSimpleFunction).wasmSignature(irBuiltIns).hashCode() + require(declaration is IrSimpleFunction) + val fqName = declaration.fqNameWhenAvailable!!.asString() + val hashCode = declaration.wasmSignature(irBuiltIns).hashCode() + declaration.file.path.hashCode() + return "${fqName}_$hashCode" } override fun visitFunction(declaration: IrFunction) { diff --git a/compiler/testData/codegen/boxWasmJsInterop/nameClash.kt b/compiler/testData/codegen/boxWasmJsInterop/nameClash.kt new file mode 100644 index 00000000000..fba82b665d3 --- /dev/null +++ b/compiler/testData/codegen/boxWasmJsInterop/nameClash.kt @@ -0,0 +1,21 @@ +// IGNORE_BACKEND: JS_IR, JS +// FILE: file1.kt +@JsFun("() => 42") +private external fun clashName(): Int + +public fun getClashName1(): Int = clashName() + +// FILE: file2.kt +@JsFun("() => 24") +private external fun clashName(): Int + +public fun getClashName2(): Int = clashName() + +fun box(): String { + + if (getClashName1() != 42) return "Error1" + if (getClashName2() != 24) return "Error2" + + + return "OK" +} \ No newline at end of file diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenWasmJsInteropTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenWasmJsInteropTestGenerated.java index 0436d651a55..281baf74741 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenWasmJsInteropTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenWasmJsInteropTestGenerated.java @@ -73,6 +73,12 @@ public class FirJsCodegenWasmJsInteropTestGenerated extends AbstractFirJsCodegen runTest("compiler/testData/codegen/boxWasmJsInterop/longStrings.kt"); } + @Test + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/boxWasmJsInterop/nameClash.kt"); + } + @Test @TestMetadata("types.kt") public void testTypes() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrCodegenWasmJsInteropJsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrCodegenWasmJsInteropJsTestGenerated.java index 779755af838..bb2791365be 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrCodegenWasmJsInteropJsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrCodegenWasmJsInteropJsTestGenerated.java @@ -73,6 +73,12 @@ public class IrCodegenWasmJsInteropJsTestGenerated extends AbstractIrCodegenWasm runTest("compiler/testData/codegen/boxWasmJsInterop/longStrings.kt"); } + @Test + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/boxWasmJsInterop/nameClash.kt"); + } + @Test @TestMetadata("types.kt") public void testTypes() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenWasmJsInteropWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenWasmJsInteropWasmTestGenerated.java index a1ca0827041..a488ac365e8 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenWasmJsInteropWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenWasmJsInteropWasmTestGenerated.java @@ -85,6 +85,11 @@ public class IrCodegenWasmJsInteropWasmTestGenerated extends AbstractIrCodegenWa runTest("compiler/testData/codegen/boxWasmJsInterop/longStrings.kt"); } + @TestMetadata("nameClash.kt") + public void testNameClash() throws Exception { + runTest("compiler/testData/codegen/boxWasmJsInterop/nameClash.kt"); + } + @TestMetadata("nullableExternRefs.kt") public void testNullableExternRefs() throws Exception { runTest("compiler/testData/codegen/boxWasmJsInterop/nullableExternRefs.kt");