From bd90b4e0510e3206d5d28bc7a494353de8413d99 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 14 Nov 2016 19:40:04 +0300 Subject: [PATCH] KJS: don't overwrite prototype of native classes when inheriting from them --- .../js/test/semantics/BoxJsTestGenerated.java | 6 +++ .../js/translate/context/StaticContext.java | 3 +- .../box/inheritance/nativeNativeKotlin.kt | 45 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 js/js.translator/testData/box/inheritance/nativeNativeKotlin.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index 6b6d9ac42f8..5403f871b58 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -3455,6 +3455,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { doTest(fileName); } + @TestMetadata("nativeNativeKotlin.kt") + public void testNativeNativeKotlin() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inheritance/nativeNativeKotlin.kt"); + doTest(fileName); + } + @TestMetadata("overrideAnyMethods.kt") public void testOverrideAnyMethods() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inheritance/overrideAnyMethods.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index dbd3a49d2f9..f7202fdd286 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -684,8 +684,9 @@ public final class StaticContext { } private void addClassPrototypes(@NotNull ClassDescriptor cls, @NotNull Set visited) { - if (DescriptorUtilsKt.getModule(cls) != currentModule) return; if (!visited.add(cls)) return; + if (DescriptorUtilsKt.getModule(cls) != currentModule) return; + if (isNativeObject(cls) || isLibraryObject(cls)) return; ClassDescriptor superclass = DescriptorUtilsKt.getSuperClassNotAny(cls); if (superclass != null) { diff --git a/js/js.translator/testData/box/inheritance/nativeNativeKotlin.kt b/js/js.translator/testData/box/inheritance/nativeNativeKotlin.kt new file mode 100644 index 00000000000..4932c91b1e3 --- /dev/null +++ b/js/js.translator/testData/box/inheritance/nativeNativeKotlin.kt @@ -0,0 +1,45 @@ +// FILE: nativeNativeKotlin.kt + +package foo + +@native +open class A { + fun foo(): String +} + +@native +open class B : A() { + fun bar(): String +} + +class C : B() + +fun box(): String { + val c = C() + + assertEquals("A.foo", c.foo()) + assertEquals("B.bar", c.bar()) + + return "OK" +} + +// FILE: nativeNativeKotlin.js + +function A() { + +} + +A.prototype.foo = function () { + return "A.foo" +}; + +function B() { + +} + +B.prototype = Object.create(A.prototype); +B.prototype.constructor = B; + +B.prototype.bar = function () { + return "B.bar" +}; \ No newline at end of file