diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ScriptRemoveReceiverLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ScriptRemoveReceiverLowering.kt index 87cadb725da..0bdad880334 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ScriptRemoveReceiverLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ScriptRemoveReceiverLowering.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -7,7 +7,6 @@ package org.jetbrains.kotlin.ir.backend.js.lower import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* @@ -15,10 +14,9 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionReferenceImpl import org.jetbrains.kotlin.ir.expressions.impl.IrPropertyReferenceImpl import org.jetbrains.kotlin.ir.symbols.IrClassSymbol -import org.jetbrains.kotlin.ir.types.IrSimpleType -import org.jetbrains.kotlin.ir.types.IrTypeProjection +import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol +import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl -import org.jetbrains.kotlin.ir.types.makeNullable import org.jetbrains.kotlin.ir.util.transformFlat import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid @@ -37,7 +35,6 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe private fun IrExpression.nullConst() = IrConstImpl.constNull(startOffset, endOffset, type.makeNullable()) - @OptIn(ObsoleteDescriptorBasedAPI::class) fun lower(script: IrScript): List { val transformer: IrElementTransformerVoid = object : IrElementTransformerVoid() { override fun visitCall(expression: IrCall): IrExpression { @@ -59,6 +56,8 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe return super.visitFieldAccess(expression) } + private fun isScript(it: IrTypeArgument) = it.typeOrNull?.classifierOrNull is IrScriptSymbol + override fun visitFunctionReference(expression: IrFunctionReference): IrExpression { expression.transformChildrenVoid(this) @@ -66,14 +65,15 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe expression.dispatchReceiver = null val result = with(super.visitFunctionReference(expression) as IrFunctionReference) { - val arguments = (type as IrSimpleType).arguments.filter { - !(it is IrTypeProjection && it.type is IrSimpleType && (it.type as IrSimpleType).classifier.descriptor is ScriptDescriptor) - } + // TODO do we really need to fix type or removing dispatchReceiver is enough? + val arguments = (type as IrSimpleType).arguments.filterNot(::isScript) + val newN = arguments.size - 1 + IrFunctionReferenceImpl( startOffset, endOffset, IrSimpleTypeImpl( - context.ir.symbols.functionN(arguments.size), + context.ir.symbols.functionN(newN), (type as IrSimpleType).hasQuestionMark, arguments, type.annotations @@ -102,14 +102,15 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe expression.dispatchReceiver = null val result = with(super.visitPropertyReference(expression) as IrPropertyReference) { - val arguments = (type as IrSimpleType).arguments.filter { - !(it is IrTypeProjection && it.type is IrSimpleType && (it.type as IrSimpleType).classifier.descriptor is ScriptDescriptor) - } + // TODO do we really need to fix type or removing dispatchReceiver is enough? + val arguments = (type as IrSimpleType).arguments.filterNot(::isScript) + val newN = arguments.size - 1 + IrPropertyReferenceImpl( startOffset, endOffset, IrSimpleTypeImpl( - (if (setter == null) getPropertyN(arguments.size) else getMutablePropertyN(arguments.size)), + (if (setter == null) getPropertyN(newN) else getMutablePropertyN(newN)), (type as IrSimpleType).hasQuestionMark, arguments, type.annotations diff --git a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/ReplTest.kt b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/ReplTest.kt index bd72817ec26..6dcca37eef7 100644 --- a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/ReplTest.kt +++ b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/ReplTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -214,6 +214,42 @@ abstract class AbstractReplTestRunner : TestCase() { Assert.assertEquals("10#$@123456_81goo", compileAndEval(lines)) } + @Test + fun testFunctionReference() { + val lines = listOf( + """ + fun foo(k: String) = "O" + k + val f = ::foo + f("K") + """ + ) + + Assert.assertEquals("OK", compileAndEval(lines)) + } + + @Test + fun testPropertyReference() { + val lines = listOf( + """ + var r = "" + val o = "O" + val ro = ::o + r += ro.get() + r += ro() + + var k = "k" + var rk = ::k + r += rk.get() + rk.set("y") + r += rk() + + r + """ + ) + + Assert.assertEquals("OOky", compileAndEval(lines)) + } + private fun compileAndEval(lines: List): Any? { var result: Any? = null getTester().use { tester -> diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsCoreScriptingCompiler.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsCoreScriptingCompiler.kt index 584d28f457c..6407fca3ef7 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsCoreScriptingCompiler.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsCoreScriptingCompiler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.generateJsCode import org.jetbrains.kotlin.ir.backend.js.utils.NameTables +import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList @@ -63,6 +64,10 @@ class JsCoreScriptingCompiler( val providers = generateTypicalIrProviderList(module, psi2irContext.irBuiltIns, psi2irContext.symbolTable) val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files, providers, emptyList(), null) // TODO: deserializer + psi2irContext.irBuiltIns.let { irBuiltIns -> + irBuiltIns.functionFactory = IrFunctionFactory(irBuiltIns, symbolTable) + } + val context = JsIrBackendContext( irModuleFragment.descriptor, psi2irContext.irBuiltIns,