From 33fb3fb6e4624350460b118f31052759c85b1205 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 1 Jun 2015 14:52:29 +0300 Subject: [PATCH] JS backend: support secondary constructors in native classes --- .../js/test/semantics/NativeInteropTest.java | 4 ++++ .../callTranslator/FunctionCallCases.kt | 4 ++-- .../js/translate/context/StaticContext.java | 6 +++++ .../native/cases/secondaryConstructor.kt | 24 +++++++++++++++++++ .../native/native/secondaryConstructor.js | 3 +++ 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 js/js.translator/testData/native/cases/secondaryConstructor.kt create mode 100644 js/js.translator/testData/native/native/secondaryConstructor.js diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/NativeInteropTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/NativeInteropTest.java index 3c954ffc30d..e37b3a675ce 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/NativeInteropTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/NativeInteropTest.java @@ -151,4 +151,8 @@ public final class NativeInteropTest extends SingleFileTranslationTest { public void testTypeof() throws Exception { checkFooBoxIsOk(); } + + public void testSecondaryConstructor() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt index 7fcc89ea963..130df76297a 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/callTranslator/FunctionCallCases.kt @@ -17,7 +17,6 @@ package org.jetbrains.kotlin.js.translate.callTranslator import com.google.dart.compiler.backend.js.ast.* -import com.intellij.util.SmartList import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.Visibilities @@ -211,7 +210,8 @@ object ConstructorCallCase : FunctionCallCase { val functionRef = if (isNative()) fqName else context.aliasOrValue(callableDescriptor) { fqName } - if((callableDescriptor as ConstructorDescriptor).isPrimary()) { + val constructorDescriptor = callableDescriptor as ConstructorDescriptor + if(constructorDescriptor.isPrimary() || AnnotationsUtils.isNativeObject(constructorDescriptor)) { return JsNew(functionRef, argumentsInfo.translateArguments) } else { 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 dbc06e97bb3..6fdf64c54a3 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 @@ -327,6 +327,12 @@ public final class StaticContext { return null; } + if (descriptor instanceof ConstructorDescriptor) { + DeclarationDescriptor classDescriptor = descriptor.getContainingDeclaration(); + assert classDescriptor != null; + descriptor = classDescriptor; + } + String name = getNameForAnnotatedObjectWithOverrides(descriptor); if (name != null) return getEnclosingScope(descriptor).declareName(name); return null; diff --git a/js/js.translator/testData/native/cases/secondaryConstructor.kt b/js/js.translator/testData/native/cases/secondaryConstructor.kt new file mode 100644 index 00000000000..49d81a6e2c0 --- /dev/null +++ b/js/js.translator/testData/native/cases/secondaryConstructor.kt @@ -0,0 +1,24 @@ +package foo + +native +class A { + constructor() + constructor(s: String) + constructor(i: Int) + + val value: Any? +} + +fun test(a: A, expectedValue: Any?, expectedTypeOfValue: String) { + assertTrue(a is A) + assertEquals(expectedValue, a.value) + assertEquals(expectedTypeOfValue, typeof(a.value)) +} + +fun box(): String { + test(A(), undefined, "undefined") + test(A("foo"), "foo", "string") + test(A(124), 124, "number") + + return "OK" +} diff --git a/js/js.translator/testData/native/native/secondaryConstructor.js b/js/js.translator/testData/native/native/secondaryConstructor.js new file mode 100644 index 00000000000..2a6e1873686 --- /dev/null +++ b/js/js.translator/testData/native/native/secondaryConstructor.js @@ -0,0 +1,3 @@ +function A(value) { + this.value = value +}