From a03e22e774d9f95842c2cee7658eeb6a6cc30a60 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 14 Nov 2016 22:08:06 +0300 Subject: [PATCH] KJS: fix refering to native nested class --- .../js/test/semantics/BoxJsTestGenerated.java | 6 ++++ .../js/translate/context/StaticContext.java | 14 ++++++---- .../translate/context/TranslationContext.java | 6 ++++ .../box/inheritance/formNestedNativeClass.kt | 28 +++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 js/js.translator/testData/box/inheritance/formNestedNativeClass.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 5403f871b58..e9a420a6356 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 @@ -3419,6 +3419,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { doTest(fileName); } + @TestMetadata("formNestedNativeClass.kt") + public void testFormNestedNativeClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inheritance/formNestedNativeClass.kt"); + doTest(fileName); + } + @TestMetadata("fromFakeClasses.kt") public void testFromFakeClasses() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/inheritance/fromFakeClasses.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 f7202fdd286..4ad2e7b1211 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 @@ -436,10 +436,6 @@ public final class StaticContext { if (descriptor instanceof LocalVariableDescriptor || descriptor instanceof ParameterDescriptor) { return getNameForDescriptor(descriptor); } - if (isNativeObject(descriptor)) { - String name = getNameForAnnotatedObject(descriptor); - if (name != null) return rootFunction.getScope().declareName(name); - } if (descriptor instanceof ConstructorDescriptor) { if (((ConstructorDescriptor) descriptor).isPrimary()) { return getInnerNameForDescriptor(((ConstructorDescriptor) descriptor).getConstructedClass()); @@ -694,7 +690,15 @@ public final class StaticContext { List statements = rootFunction.getBody().getStatements(); - JsExpression superPrototype = JsAstUtils.prototypeOf(new JsNameRef(getInnerNameForDescriptor(superclass))); + JsNameRef superclassRef; + if (isNativeObject(superclass) || isLibraryObject(superclass)) { + superclassRef = getQualifiedReference(superclass); + } + else { + superclassRef = getInnerNameForDescriptor(superclass).makeRef(); + } + + JsExpression superPrototype = JsAstUtils.prototypeOf(superclassRef); JsExpression superPrototypeInstance = new JsInvocation(new JsNameRef("create", "Object"), superPrototype); JsExpression classRef = new JsNameRef(getInnerNameForDescriptor(cls)); JsExpression prototype = JsAstUtils.prototypeOf(classRef); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java index c64df849dea..5cc6c051704 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java @@ -38,6 +38,8 @@ import java.util.*; import static org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsKt.isCoroutineLambda; import static org.jetbrains.kotlin.js.translate.context.UsageTrackerKt.getNameForCapturedDescriptor; +import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.isLibraryObject; +import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.isNativeObject; import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getDescriptorForElement; /** @@ -232,6 +234,10 @@ public class TranslationContext { @NotNull public JsNameRef getInnerReference(@NotNull DeclarationDescriptor descriptor) { + if (isNativeObject(descriptor) || isLibraryObject(descriptor)) { + return getQualifiedReference(descriptor); + } + return JsAstUtils.pureFqn(getInnerNameForDescriptor(descriptor), null); } diff --git a/js/js.translator/testData/box/inheritance/formNestedNativeClass.kt b/js/js.translator/testData/box/inheritance/formNestedNativeClass.kt new file mode 100644 index 00000000000..58dd00654f4 --- /dev/null +++ b/js/js.translator/testData/box/inheritance/formNestedNativeClass.kt @@ -0,0 +1,28 @@ +// FILE: foo.kt + +package foo + +@native +class A { + open class B { + fun foo(): String + } +} + +class C : A.B() + +fun box(): String { + return C().foo() +} + +// FILE: bar.js + +function A() { +} + +A.B = function() { +}; + +A.B.prototype.foo = function() { + return "OK" +}; \ No newline at end of file