[JS scripting] Remove usages of descriptor based APIs and proper support for callable references
This commit is contained in:
+15
-14
@@ -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.
|
* 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.CommonBackendContext
|
||||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||||
import org.jetbrains.kotlin.descriptors.ScriptDescriptor
|
|
||||||
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
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.IrFunctionReferenceImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrPropertyReferenceImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrPropertyReferenceImpl
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
|
||||||
import org.jetbrains.kotlin.ir.types.IrTypeProjection
|
import org.jetbrains.kotlin.ir.types.*
|
||||||
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
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.util.transformFlat
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
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())
|
private fun IrExpression.nullConst() = IrConstImpl.constNull(startOffset, endOffset, type.makeNullable())
|
||||||
|
|
||||||
@OptIn(ObsoleteDescriptorBasedAPI::class)
|
|
||||||
fun lower(script: IrScript): List<IrScript> {
|
fun lower(script: IrScript): List<IrScript> {
|
||||||
val transformer: IrElementTransformerVoid = object : IrElementTransformerVoid() {
|
val transformer: IrElementTransformerVoid = object : IrElementTransformerVoid() {
|
||||||
override fun visitCall(expression: IrCall): IrExpression {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
@@ -59,6 +56,8 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe
|
|||||||
return super.visitFieldAccess(expression)
|
return super.visitFieldAccess(expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isScript(it: IrTypeArgument) = it.typeOrNull?.classifierOrNull is IrScriptSymbol
|
||||||
|
|
||||||
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
|
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
|
||||||
expression.transformChildrenVoid(this)
|
expression.transformChildrenVoid(this)
|
||||||
|
|
||||||
@@ -66,14 +65,15 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe
|
|||||||
expression.dispatchReceiver = null
|
expression.dispatchReceiver = null
|
||||||
|
|
||||||
val result = with(super.visitFunctionReference(expression) as IrFunctionReference) {
|
val result = with(super.visitFunctionReference(expression) as IrFunctionReference) {
|
||||||
val arguments = (type as IrSimpleType).arguments.filter {
|
// TODO do we really need to fix type or removing dispatchReceiver is enough?
|
||||||
!(it is IrTypeProjection && it.type is IrSimpleType && (it.type as IrSimpleType).classifier.descriptor is ScriptDescriptor)
|
val arguments = (type as IrSimpleType).arguments.filterNot(::isScript)
|
||||||
}
|
val newN = arguments.size - 1
|
||||||
|
|
||||||
IrFunctionReferenceImpl(
|
IrFunctionReferenceImpl(
|
||||||
startOffset,
|
startOffset,
|
||||||
endOffset,
|
endOffset,
|
||||||
IrSimpleTypeImpl(
|
IrSimpleTypeImpl(
|
||||||
context.ir.symbols.functionN(arguments.size),
|
context.ir.symbols.functionN(newN),
|
||||||
(type as IrSimpleType).hasQuestionMark,
|
(type as IrSimpleType).hasQuestionMark,
|
||||||
arguments,
|
arguments,
|
||||||
type.annotations
|
type.annotations
|
||||||
@@ -102,14 +102,15 @@ class ScriptRemoveReceiverLowering(val context: CommonBackendContext) : FileLowe
|
|||||||
expression.dispatchReceiver = null
|
expression.dispatchReceiver = null
|
||||||
|
|
||||||
val result = with(super.visitPropertyReference(expression) as IrPropertyReference) {
|
val result = with(super.visitPropertyReference(expression) as IrPropertyReference) {
|
||||||
val arguments = (type as IrSimpleType).arguments.filter {
|
// TODO do we really need to fix type or removing dispatchReceiver is enough?
|
||||||
!(it is IrTypeProjection && it.type is IrSimpleType && (it.type as IrSimpleType).classifier.descriptor is ScriptDescriptor)
|
val arguments = (type as IrSimpleType).arguments.filterNot(::isScript)
|
||||||
}
|
val newN = arguments.size - 1
|
||||||
|
|
||||||
IrPropertyReferenceImpl(
|
IrPropertyReferenceImpl(
|
||||||
startOffset,
|
startOffset,
|
||||||
endOffset,
|
endOffset,
|
||||||
IrSimpleTypeImpl(
|
IrSimpleTypeImpl(
|
||||||
(if (setter == null) getPropertyN(arguments.size) else getMutablePropertyN(arguments.size)),
|
(if (setter == null) getPropertyN(newN) else getMutablePropertyN(newN)),
|
||||||
(type as IrSimpleType).hasQuestionMark,
|
(type as IrSimpleType).hasQuestionMark,
|
||||||
arguments,
|
arguments,
|
||||||
type.annotations
|
type.annotations
|
||||||
|
|||||||
+37
-1
@@ -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.
|
* 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))
|
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<String>): Any? {
|
private fun compileAndEval(lines: List<String>): Any? {
|
||||||
var result: Any? = null
|
var result: Any? = null
|
||||||
getTester().use { tester ->
|
getTester().use { tester ->
|
||||||
|
|||||||
+6
-1
@@ -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.
|
* 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.JsIrBackendContext
|
||||||
import org.jetbrains.kotlin.ir.backend.js.generateJsCode
|
import org.jetbrains.kotlin.ir.backend.js.generateJsCode
|
||||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
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.ExternalDependenciesGenerator
|
||||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||||
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
|
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
|
||||||
@@ -63,6 +64,10 @@ class JsCoreScriptingCompiler(
|
|||||||
val providers = generateTypicalIrProviderList(module, psi2irContext.irBuiltIns, psi2irContext.symbolTable)
|
val providers = generateTypicalIrProviderList(module, psi2irContext.irBuiltIns, psi2irContext.symbolTable)
|
||||||
val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files, providers, emptyList(), null) // TODO: deserializer
|
val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files, providers, emptyList(), null) // TODO: deserializer
|
||||||
|
|
||||||
|
psi2irContext.irBuiltIns.let { irBuiltIns ->
|
||||||
|
irBuiltIns.functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
|
||||||
|
}
|
||||||
|
|
||||||
val context = JsIrBackendContext(
|
val context = JsIrBackendContext(
|
||||||
irModuleFragment.descriptor,
|
irModuleFragment.descriptor,
|
||||||
psi2irContext.irBuiltIns,
|
psi2irContext.irBuiltIns,
|
||||||
|
|||||||
Reference in New Issue
Block a user