Compare commits

...

4 Commits

Author SHA1 Message Date
Anton Bannykh
bedcc739ad Properties work 2016-11-24 20:27:25 +03:00
Anton Bannykh
c5d32e84b9 Should work for functions 2016-11-24 14:50:03 +03:00
Anton Bannykh
9f4c77e56e wip 2016-11-23 20:02:33 +03:00
Anton Bannykh
cfdcf46af1 Enabled tests 2016-11-23 14:42:04 +03:00
17 changed files with 360 additions and 115 deletions

View File

@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
enum class E {
A, B;

View File

@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
inline fun foo(x: () -> String) = x()
fun String.id() = this

View File

@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
inline fun go(f: () -> String) = f()
fun String.id(): String = this

View File

@@ -1,8 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// See KT-12995
fun box(): String {
var state = 0
val name = (state++)::toString.name

View File

@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun <T> get(t: T): () -> String {
return t::toString
}

View File

@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
fun box(): String {
val f = "KOTLIN"::get
return "${f(1)}${f(0)}"

View File

@@ -1,8 +1,33 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
class A(val res: Int) {
fun foo() = res
}
fun A.bar(x: Int) = foo() * x;
object Foo {
val qqq = 6
}
fun box(): String {
val f = "kotlin"::length
val result = f.get()
return if (result == 6) "OK" else "Fail: $result"
// var a = "kotlin".length
// print(a)
// val g = A::foo
// print(g(A(78)))
//
// val f = (if (1 < 2) A(6) else { print(1); A(2)})::foo
// if (f() != 6) return "fail"
//
// val z = A(3)::bar
// if (z(11) != 33) return "fail"
//
// val q = 100
// fun A.zzz(x: Int) = (x + q) * foo()
// val w = A(5)::zzz
//
// if (w(11) != 555) return "fail"
val ddd = Foo::qqq
if (ddd() != 6) return "fail"
return "OK"
}

View File

@@ -1,6 +1,4 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: A.java

View File

@@ -1,3 +1,5 @@
// Enable when callble references for intrinsics are suported.
// IGNORE_BACKEND: JS
// FILE: 1.kt
package test

View File

@@ -1196,13 +1196,17 @@ fun main(args: Array<String>) {
model("codegen/boxInline/nonLocalReturns/", targetBackend = TargetBackend.JS)
}
testClass<AbstractPropertyAccessorsInlineTests>() {
testClass<AbstractPropertyAccessorsInlineTests> {
model("codegen/boxInline/property/", targetBackend = TargetBackend.JS)
}
testClass<AbstractNoInlineTests>() {
testClass<AbstractNoInlineTests> {
model("codegen/boxInline/noInline/", targetBackend = TargetBackend.JS)
}
testClass<AbstractCallableReferenceInlineTests> {
model("codegen/boxInline/callableReference/", targetBackend = TargetBackend.JS)
}
}
}

View File

@@ -0,0 +1,161 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.test.semantics;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/boxInline/callableReference")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class CallableReferenceInlineTestsGenerated extends AbstractCallableReferenceInlineTests {
public void testAllFilesPresentInCallableReference() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/callableReference"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("classLevel.kt")
public void testClassLevel() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/classLevel.kt");
doTest(fileName);
}
@TestMetadata("classLevel2.kt")
public void testClassLevel2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/classLevel2.kt");
doTest(fileName);
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/constructor.kt");
doTest(fileName);
}
@TestMetadata("intrinsic.kt")
public void testIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/intrinsic.kt");
doTest(fileName);
}
@TestMetadata("propertyIntrinsic.kt")
public void testPropertyIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyIntrinsic.kt");
doTest(fileName);
}
@TestMetadata("propertyReference.kt")
public void testPropertyReference() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/propertyReference.kt");
doTest(fileName);
}
@TestMetadata("topLevel.kt")
public void testTopLevel() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/topLevel.kt");
doTest(fileName);
}
@TestMetadata("topLevelExtension.kt")
public void testTopLevelExtension() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/topLevelExtension.kt");
doTest(fileName);
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/topLevelProperty.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/codegen/boxInline/callableReference/bound")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Bound extends AbstractCallableReferenceInlineTests {
public void testAllFilesPresentInBound() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/callableReference/bound"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("classProperty.kt")
public void testClassProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/classProperty.kt");
doTest(fileName);
}
@TestMetadata("expression.kt")
public void testExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/expression.kt");
doTest(fileName);
}
@TestMetadata("extensionReceiver.kt")
public void testExtensionReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/extensionReceiver.kt");
doTest(fileName);
}
@TestMetadata("filter.kt")
public void testFilter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/filter.kt");
doTest(fileName);
}
@TestMetadata("intrinsic.kt")
public void testIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/intrinsic.kt");
doTest(fileName);
}
@TestMetadata("map.kt")
public void testMap() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/map.kt");
doTest(fileName);
}
@TestMetadata("objectProperty.kt")
public void testObjectProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/objectProperty.kt");
doTest(fileName);
}
@TestMetadata("propertyImportedFromObject.kt")
public void testPropertyImportedFromObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/propertyImportedFromObject.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/simple.kt");
doTest(fileName);
}
@TestMetadata("topLevelExtensionProperty.kt")
public void testTopLevelExtensionProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/callableReference/bound/topLevelExtensionProperty.kt");
doTest(fileName);
}
}
}

View File

@@ -1877,73 +1877,37 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
@TestMetadata("enumEntryMember.kt")
public void testEnumEntryMember() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/enumEntryMember.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("kCallableNameIntrinsic.kt")
public void testKCallableNameIntrinsic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/kCallableNameIntrinsic.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("kt12738.kt")
public void testKt12738() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/kt12738.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("simpleFunction.kt")
public void testSimpleFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/simpleFunction.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("simpleProperty.kt")
public void testSimpleProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/simpleProperty.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("syntheticExtensionOnLHS.kt")
public void testSyntheticExtensionOnLHS() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/syntheticExtensionOnLHS.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("compiler/testData/codegen/box/callableReference/bound/inline")
@@ -1957,25 +1921,13 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/inline/simple.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
@TestMetadata("simpleVal.kt")
public void testSimpleVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/callableReference/bound/inline/simpleVal.kt");
try {
doTest(fileName);
}
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
doTest(fileName);
}
}
}

View File

@@ -33,6 +33,8 @@ abstract class AbstractPropertyAccessorsInlineTests : BorrowedInlineTest("proper
abstract class AbstractNoInlineTests : BorrowedInlineTest("noInline/")
abstract class AbstractCallableReferenceInlineTests : BorrowedInlineTest("callableReference/")
abstract class AbstractBoxJsTest() : BasicBoxTest(
BasicBoxTest.TEST_DATA_DIR_PATH + "box/",
BasicBoxTest.TEST_DATA_DIR_PATH + "out/box/"

View File

@@ -77,12 +77,17 @@ public final class Namer {
public static final String OUTER_FIELD_NAME = "$outer";
private static final String CALLABLE_REF_FOR_MEMBER_FUNCTION_NAME = "getCallableRefForMemberFunction";
private static final String BOUND_CALLABLE_REF_FOR_MEMBER_FUNCTION_NAME = "getBoundCallableRefForMemberFunction";
private static final String CALLABLE_REF_FOR_EXTENSION_FUNCTION_NAME = "getCallableRefForExtensionFunction";
private static final String BOUND_CALLABLE_REF_FOR_EXTENSION_FUNCTION_NAME = "getBoundCallableRefForExtensionFunction";
private static final String CALLABLE_REF_FOR_LOCAL_EXTENSION_FUNCTION_NAME = "getCallableRefForLocalExtensionFunction";
private static final String BOUND_CALLABLE_REF_FOR_LOCAL_EXTENSION_FUNCTION_NAME = "getBoundCallableRefForLocalExtensionFunction";
private static final String CALLABLE_REF_FOR_CONSTRUCTOR_NAME = "getCallableRefForConstructor";
private static final String CALLABLE_REF_FOR_TOP_LEVEL_PROPERTY = "getCallableRefForTopLevelProperty";
private static final String CALLABLE_REF_FOR_MEMBER_PROPERTY = "getCallableRefForMemberProperty";
private static final String BOUND_CALLABLE_REF_FOR_MEMBER_PROPERTY = "getBoundCallableRefForMemberProperty";
private static final String CALLABLE_REF_FOR_EXTENSION_PROPERTY = "getCallableRefForExtensionProperty";
private static final String BOUND_CALLABLE_REF_FOR_EXTENSION_PROPERTY = "getBoundCallableRefForExtensionProperty";
private static final String DELEGATE = "$delegate";
@@ -191,18 +196,28 @@ public final class Namer {
@NotNull
private final JsName callableRefForMemberFunctionName;
@NotNull
private final JsName boundCallableRefForMemberFunctionName;
@NotNull
private final JsName callableRefForExtensionFunctionName;
@NotNull
private final JsName boundCallableRefForExtensionFunctionName;
@NotNull
private final JsName callableRefForLocalExtensionFunctionName;
@NotNull
private final JsName boundCallableRefForLocalExtensionFunctionName;
@NotNull
private final JsName callableRefForConstructorName;
@NotNull
private final JsName callableRefForTopLevelProperty;
@NotNull
private final JsName callableRefForMemberProperty;
@NotNull
private final JsName boundCallableRefForMemberProperty;
@NotNull
private final JsName callableRefForExtensionProperty;
@NotNull
private final JsName boundCallableRefForExtensionProperty;
@NotNull
private final JsExpression callGetProperty;
@NotNull
private final JsExpression callSetProperty;
@@ -217,12 +232,17 @@ public final class Namer {
callSetProperty = kotlin("callSetter");
callableRefForMemberFunctionName = kotlinScope.declareName(CALLABLE_REF_FOR_MEMBER_FUNCTION_NAME);
boundCallableRefForMemberFunctionName = kotlinScope.declareName(BOUND_CALLABLE_REF_FOR_MEMBER_FUNCTION_NAME);
callableRefForExtensionFunctionName = kotlinScope.declareName(CALLABLE_REF_FOR_EXTENSION_FUNCTION_NAME);
boundCallableRefForExtensionFunctionName = kotlinScope.declareName(BOUND_CALLABLE_REF_FOR_EXTENSION_FUNCTION_NAME);
callableRefForLocalExtensionFunctionName = kotlinScope.declareName(CALLABLE_REF_FOR_LOCAL_EXTENSION_FUNCTION_NAME);
boundCallableRefForLocalExtensionFunctionName = kotlinScope.declareName(BOUND_CALLABLE_REF_FOR_LOCAL_EXTENSION_FUNCTION_NAME);
callableRefForConstructorName = kotlinScope.declareName(CALLABLE_REF_FOR_CONSTRUCTOR_NAME);
callableRefForTopLevelProperty = kotlinScope.declareName(CALLABLE_REF_FOR_TOP_LEVEL_PROPERTY);
callableRefForMemberProperty = kotlinScope.declareName(CALLABLE_REF_FOR_MEMBER_PROPERTY);
boundCallableRefForMemberProperty = kotlinScope.declareName(BOUND_CALLABLE_REF_FOR_MEMBER_PROPERTY);
callableRefForExtensionProperty = kotlinScope.declareName(CALLABLE_REF_FOR_EXTENSION_PROPERTY);
boundCallableRefForExtensionProperty = kotlinScope.declareName(BOUND_CALLABLE_REF_FOR_EXTENSION_PROPERTY);
isTypeName = kotlinScope.declareName("isType");
}
@@ -243,16 +263,31 @@ public final class Namer {
return kotlin(callableRefForMemberFunctionName);
}
@NotNull
public JsExpression boundCallableRefForMemberFunctionReference() {
return kotlin(boundCallableRefForMemberFunctionName);
}
@NotNull
public JsExpression callableRefForExtensionFunctionReference() {
return kotlin(callableRefForExtensionFunctionName);
}
@NotNull
public JsExpression boundCallableRefForExtensionFunctionReference() {
return kotlin(boundCallableRefForExtensionFunctionName);
}
@NotNull
public JsExpression callableRefForLocalExtensionFunctionReference() {
return kotlin(callableRefForLocalExtensionFunctionName);
}
@NotNull
public JsExpression boundCallableRefForLocalExtensionFunctionReference() {
return kotlin(boundCallableRefForLocalExtensionFunctionName);
}
@NotNull
public JsExpression callableRefForConstructorReference() {
return kotlin(callableRefForConstructorName);
@@ -268,11 +303,21 @@ public final class Namer {
return kotlin(callableRefForMemberProperty);
}
@NotNull
public JsExpression boundCallableRefForMemberPropertyReference() {
return kotlin(boundCallableRefForMemberProperty);
}
@NotNull
public JsExpression callableRefForExtensionPropertyReference() {
return kotlin(callableRefForExtensionProperty);
}
@NotNull
public JsExpression boundCallableRefForExtensionPropertyReference() {
return kotlin(boundCallableRefForExtensionProperty);
}
@NotNull
public static JsExpression throwNPEFunctionRef() {
return new JsNameRef(THROW_NPE_FUN_NAME, kotlinObject());

View File

@@ -21,20 +21,39 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.js.resolve.diagnostics.ErrorsJs
import org.jetbrains.kotlin.js.translate.context.TranslationContext
import org.jetbrains.kotlin.js.translate.general.Translation
import org.jetbrains.kotlin.js.translate.utils.*
import org.jetbrains.kotlin.psi.KtCallableReferenceExpression
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.PropertyImportedFromObject
import java.util.*
object CallableReferenceTranslator {
fun translate(expression: KtCallableReferenceExpression, context: TranslationContext): JsExpression {
val descriptor = BindingUtils.getDescriptorForReferenceExpression(context.bindingContext(), expression.callableReference)
val receiver = expression.receiverExpression?.let { r ->
if (context.bindingContext().getType(r) == null) {
null
} else {
val block = JsBlock()
val e = Translation.translateAsExpression(r, context, block)
if (!block.isEmpty) {
context.addStatementsToCurrentBlockFrom(block)
}
e
}
} ?: (descriptor as? PropertyImportedFromObject)?.let {
ReferenceTranslator.translateAsValueReference(it.containingObject, context)
}
return when (descriptor) {
is PropertyDescriptor ->
translateForProperty(descriptor, context, expression)
translateForProperty(descriptor, context, expression, receiver)
is FunctionDescriptor ->
translateForFunction(descriptor, context, expression)
translateForFunction(descriptor, context, expression, receiver)
else ->
throw IllegalArgumentException("Expected property or function: $descriptor, expression=${expression.text}")
}
@@ -45,7 +64,10 @@ object CallableReferenceTranslator {
return JsLiteral.NULL
}
private fun translateForFunction(descriptor: FunctionDescriptor, context: TranslationContext, expression: KtCallableReferenceExpression): JsExpression {
private fun translateForFunction(descriptor: FunctionDescriptor,
context: TranslationContext,
expression: KtCallableReferenceExpression,
receiver: JsExpression?): JsExpression {
return when {
// TODO Support for callable reference to builtin functions and members
KotlinBuiltIns.isBuiltIn(descriptor) ->
@@ -53,23 +75,26 @@ object CallableReferenceTranslator {
isConstructor(descriptor) ->
translateForConstructor(descriptor, context)
isExtension(descriptor) ->
translateForExtensionFunction(descriptor, context)
translateForExtensionFunction(descriptor, context, receiver)
isMember(descriptor) ->
translateForMemberFunction(descriptor, context)
translateForMemberFunction(descriptor, context, receiver)
else ->
ReferenceTranslator.translateAsValueReference(descriptor, context)
}
}
private fun translateForProperty(descriptor: PropertyDescriptor, context: TranslationContext, expression: KtCallableReferenceExpression): JsExpression {
private fun translateForProperty(descriptor: PropertyDescriptor,
context: TranslationContext,
expression: KtCallableReferenceExpression,
receiver: JsExpression?): JsExpression {
return when {
// TODO Support for callable reference to builtin properties
KotlinBuiltIns.isBuiltIn(descriptor) ->
reportNotSupported(context, expression)
isExtension(descriptor) ->
translateForExtensionProperty(descriptor, context)
translateForExtensionProperty(descriptor, context, receiver)
isMember(descriptor) ->
translateForMemberProperty(descriptor, context)
translateForMemberProperty(descriptor, context, receiver)
else ->
translateForTopLevelProperty(descriptor, context)
}
@@ -137,27 +162,39 @@ object CallableReferenceTranslator {
return function
}
private fun translateForMemberProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
private fun translateForMemberProperty(descriptor: PropertyDescriptor, context: TranslationContext, receiver: JsExpression?): JsExpression {
val jsPropertyName = context.getNameForDescriptor(descriptor)
val jsPropertyNameAsString = context.program().getStringLiteral(jsPropertyName.toString())
return JsInvocation(context.namer().callableRefForMemberPropertyReference(), jsPropertyNameAsString, isVar(descriptor))
if (receiver == null) {
return JsInvocation(context.namer().callableRefForMemberPropertyReference(), jsPropertyNameAsString, isVar(descriptor))
} else {
return JsInvocation(context.namer().boundCallableRefForMemberPropertyReference(), receiver, jsPropertyNameAsString, isVar(descriptor))
}
}
private fun translateForExtensionProperty(descriptor: PropertyDescriptor, context: TranslationContext): JsExpression {
private fun translateForExtensionProperty(descriptor: PropertyDescriptor, context: TranslationContext, receiver: JsExpression?): JsExpression {
val jsGetterNameRef = ReferenceTranslator.translateAsValueReference(descriptor.getter!!, context)
val propertyName = descriptor.name
val jsPropertyNameAsString = context.program().getStringLiteral(propertyName.asString())
val argumentList = ArrayList<JsExpression>(3)
val argumentList = ArrayList<JsExpression>(4)
if (receiver != null) {
argumentList.add(receiver)
}
argumentList.add(jsPropertyNameAsString)
argumentList.add(jsGetterNameRef)
if (descriptor.isVar) {
val jsSetterNameRef = ReferenceTranslator.translateAsValueReference(descriptor.setter!!, context)
argumentList.add(jsSetterNameRef)
}
return if (AnnotationsUtils.isNativeObject(descriptor))
translateForMemberProperty(descriptor, context)
else
return if (AnnotationsUtils.isNativeObject(descriptor)) {
translateForMemberProperty(descriptor, context, receiver)
}
else if (receiver == null) {
JsInvocation(context.namer().callableRefForExtensionPropertyReference(), argumentList)
}
else {
JsInvocation(context.namer().boundCallableRefForExtensionPropertyReference(), argumentList)
}
}
private fun translateForConstructor(descriptor: FunctionDescriptor, context: TranslationContext): JsExpression {
@@ -165,39 +202,54 @@ object CallableReferenceTranslator {
return JsInvocation(context.namer().callableRefForConstructorReference(), jsFunctionRef)
}
private fun translateForExtensionFunction(descriptor: FunctionDescriptor, context: TranslationContext): JsExpression {
private fun translateForExtensionFunction(descriptor: FunctionDescriptor,
context: TranslationContext,
receiver: JsExpression?): JsExpression {
val receiverParameterDescriptor = descriptor.extensionReceiverParameter ?:
error("receiverParameter for extension should not be null")
val jsFunctionRef = ReferenceTranslator.translateAsValueReference(descriptor, context)
if (descriptor.visibility == Visibilities.LOCAL) {
return JsInvocation(context.namer().callableRefForLocalExtensionFunctionReference(), jsFunctionRef)
if (receiver == null) {
return JsInvocation(context.namer().callableRefForLocalExtensionFunctionReference(), jsFunctionRef)
} else {
return JsInvocation(context.namer().boundCallableRefForLocalExtensionFunctionReference(), jsFunctionRef, receiver)
}
}
else if (AnnotationsUtils.isNativeObject(descriptor)) {
val jetType = receiverParameterDescriptor.type
val receiverClassDescriptor = DescriptorUtils.getClassDescriptorForType(jetType)
return translateAsMemberFunctionReference(descriptor, receiverClassDescriptor, context)
return translateAsMemberFunctionReference(descriptor, receiverClassDescriptor, context, receiver)
}
else {
return JsInvocation(context.namer().callableRefForExtensionFunctionReference(), jsFunctionRef)
if (receiver == null) {
return JsInvocation(context.namer().callableRefForExtensionFunctionReference(), jsFunctionRef)
} else {
return JsInvocation(context.namer().boundCallableRefForExtensionFunctionReference(), jsFunctionRef, receiver)
}
}
}
private fun translateForMemberFunction(descriptor: FunctionDescriptor, context: TranslationContext): JsExpression {
private fun translateForMemberFunction(descriptor: FunctionDescriptor, context: TranslationContext, receiver: JsExpression?): JsExpression {
val classDescriptor = JsDescriptorUtils.getContainingDeclaration(descriptor) as? ClassDescriptor ?:
throw IllegalArgumentException("Expected ClassDescriptor: $descriptor")
return translateAsMemberFunctionReference(descriptor, classDescriptor, context)
return translateAsMemberFunctionReference(descriptor, classDescriptor, context, receiver)
}
private fun translateAsMemberFunctionReference(
descriptor: CallableDescriptor,
classDescriptor: ClassDescriptor,
context: TranslationContext
context: TranslationContext,
receiver: JsExpression?
): JsExpression {
val jsClassNameRef = ReferenceTranslator.translateAsTypeReference(classDescriptor, context)
val funName = context.getNameForDescriptor(descriptor)
val funNameAsString = context.program().getStringLiteral(funName.toString())
return JsInvocation(context.namer().callableRefForMemberFunctionReference(), jsClassNameRef, funNameAsString)
if (receiver == null) {
val jsClassNameRef = ReferenceTranslator.translateAsTypeReference(classDescriptor, context)
return JsInvocation(context.namer().callableRefForMemberFunctionReference(), jsClassNameRef, funNameAsString)
} else {
return JsInvocation(context.namer().boundCallableRefForMemberFunctionReference(), funNameAsString, receiver)
}
}
}

View File

@@ -138,12 +138,18 @@
};
};
Kotlin.getBoundCallableRefForMemberFunction = function (memberName, instance) {
return instance[memberName].bind(instance);
};
// TODO Store callable references for extension functions in class
// extFun expected receiver as the first argument
Kotlin.getCallableRefForExtensionFunction = function (extFun) {
return function () {
return extFun.apply(null, arguments);
};
return extFun.bind(null);
};
Kotlin.getBoundCallableRefForExtensionFunction = function (extFun, instance) {
return extFun.bind(null, instance);
};
Kotlin.getCallableRefForLocalExtensionFunction = function (extFun) {
@@ -154,6 +160,10 @@
};
};
Kotlin.getBoundCallableRefForLocalExtensionFunction = function (extFun, instance) {
return extFun.bind(instance);
};
Kotlin.getCallableRefForConstructor = function (klass) {
return function () {
var obj = Object.create(klass.prototype);
@@ -173,11 +183,25 @@
return getPropertyRefClass(getFun, "get_za3rmp$", setFun, "set_wn2jw4$", propertyRefClassMetadataCache.oneArg);
};
Kotlin.getBoundCallableRefForMemberProperty = function(receiver, name, isVar) {
var getFun = Function("receiver", "return function " + name + "() { return receiver['" + name + "']; }")(receiver);
var setFun = isVar ? function(value) { receiver[name] = value; } : null;
return getPropertyRefClass(getFun, "get_za3rmp$", setFun, "set_wn2jw4$", propertyRefClassMetadataCache.oneArg);
};
Kotlin.getCallableRefForExtensionProperty = function(name, getFun, setFun) {
var getFunWrapper = Function("getFun", "return function " + name + "(receiver, extensionReceiver) { return getFun(receiver, extensionReceiver) }")(getFun);
return getPropertyRefClass(getFunWrapper, "get_za3rmp$", setFun, "set_wn2jw4$", propertyRefClassMetadataCache.oneArg);
};
Kotlin.getBoundCallableRefForExtensionProperty = function(receiver, name, getFun, setFun) {
var getFunWrapper = Function("receiver", "getFun", "return function " + name + "(extensionReceiver) { return getFun(receiver, extensionReceiver) }")(receiver, getFun);
if (setFun) {
setFun = setFun.bind(null, receiver);
}
return getPropertyRefClass(getFunWrapper, "get_za3rmp$", setFun, "set_wn2jw4$", propertyRefClassMetadataCache.oneArg);
};
function getPropertyRefClass(getFun, getName, setFun, setName, cache) {
var obj = getFun;
var isMutable = typeof setFun === "function";

View File

@@ -2,10 +2,10 @@
<html>
<head>
<script type="application/javascript" src="../../../dist/js/kotlin.js"></script>
<script type="application/javascript" src="out/box/native/eval_v5.js"></script>
<script type="application/javascript" src="out/codegen/boxInline/callableReference/bound/topLevelExtensionProperty_v5.js"></script>
<script type="application/javascript">
console.log(JS_TESTS.foo.box());
console.log(JS_TESTS.box());
</script>
</head>
<body>