JS backend: support secondary constructors in native classes

This commit is contained in:
Zalim Bashorov
2015-06-01 14:52:29 +03:00
parent 08439c541e
commit 33fb3fb6e4
5 changed files with 39 additions and 2 deletions
@@ -151,4 +151,8 @@ public final class NativeInteropTest extends SingleFileTranslationTest {
public void testTypeof() throws Exception {
checkFooBoxIsOk();
}
public void testSecondaryConstructor() throws Exception {
checkFooBoxIsOk();
}
}
@@ -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 {
@@ -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;
@@ -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"
}
@@ -0,0 +1,3 @@
function A(value) {
this.value = value
}