[JS IR BE] Put closure function declaration in container (factory function)
This commit is contained in:
@@ -103,6 +103,8 @@ class JsIrBackendContext(
|
|||||||
// TODO: due to name clash those weird suffix is required, remove it once `NameGenerator` is implemented
|
// TODO: due to name clash those weird suffix is required, remove it once `NameGenerator` is implemented
|
||||||
private val COROUTINE_SUSPEND_OR_RETURN_JS_NAME = "suspendCoroutineUninterceptedOrReturnJS"
|
private val COROUTINE_SUSPEND_OR_RETURN_JS_NAME = "suspendCoroutineUninterceptedOrReturnJS"
|
||||||
private val GET_COROUTINE_CONTEXT_NAME = "getCoroutineContext"
|
private val GET_COROUTINE_CONTEXT_NAME = "getCoroutineContext"
|
||||||
|
|
||||||
|
val callableClosureOrigin = object : IrDeclarationOriginImpl("CALLABLE_CLOSURE_DECLARATION") {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val internalPackage = module.getPackage(JS_PACKAGE_FQNAME)
|
private val internalPackage = module.getPackage(JS_PACKAGE_FQNAME)
|
||||||
|
|||||||
+7
-4
@@ -218,7 +218,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
|
|||||||
putValueArgument(1, callableNameConst)
|
putValueArgument(1, callableNameConst)
|
||||||
putValueArgument(2, JsIrBuilder.buildString(context.irBuiltIns.stringType, getReferenceName(declaration)))
|
putValueArgument(2, JsIrBuilder.buildString(context.irBuiltIns.stringType, getReferenceName(declaration)))
|
||||||
}
|
}
|
||||||
Pair(listOf(irVar, irSetName), irVar.symbol)
|
Pair(listOf(refClosureFunction, irVar, irSetName), irVar.symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
|
|||||||
val setterFunction = propertyReference.setter?.let { buildClosureFunction(it.owner, refGetFunction, propertyReference) }
|
val setterFunction = propertyReference.setter?.let { buildClosureFunction(it.owner, refGetFunction, propertyReference) }
|
||||||
|
|
||||||
val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
|
val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
|
||||||
val statements = mutableListOf<IrStatement>()
|
val statements = mutableListOf<IrStatement>(getterDeclaration)
|
||||||
|
|
||||||
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
|
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
|
||||||
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
|
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
|
||||||
@@ -270,6 +270,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (setterFunction != null) {
|
if (setterFunction != null) {
|
||||||
|
statements += setterFunction
|
||||||
val setterFunctionType = context.builtIns.getFunction(setterFunction.valueParameters.size + 1)
|
val setterFunctionType = context.builtIns.getFunction(setterFunction.valueParameters.size + 1)
|
||||||
val irSetReference = JsIrBuilder.buildFunctionReference(
|
val irSetReference = JsIrBuilder.buildFunctionReference(
|
||||||
setterFunctionType.toIrType(symbolTable = context.symbolTable),
|
setterFunctionType.toIrType(symbolTable = context.symbolTable),
|
||||||
@@ -324,7 +325,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
|
|||||||
val getterFunction = buildClosureFunction(context.irBuiltIns.throwIseFun, refGetFunction, propertyReference)
|
val getterFunction = buildClosureFunction(context.irBuiltIns.throwIseFun, refGetFunction, propertyReference)
|
||||||
|
|
||||||
val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
|
val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
|
||||||
val statements = mutableListOf<IrStatement>()
|
val statements = mutableListOf<IrStatement>(getterFunction)
|
||||||
|
|
||||||
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
|
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
|
||||||
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
|
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
|
||||||
@@ -520,7 +521,9 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
|
|||||||
reference: IrCallableReference
|
reference: IrCallableReference
|
||||||
): IrFunction {
|
): IrFunction {
|
||||||
val closureName = createClosureInstanceName(declaration)
|
val closureName = createClosureInstanceName(declaration)
|
||||||
val refClosureDeclaration = JsIrBuilder.buildFunction(closureName, Visibilities.LOCAL).also { it.parent = refGetFunction }
|
val refClosureDeclaration =
|
||||||
|
JsIrBuilder.buildFunction(closureName, Visibilities.LOCAL, origin = JsIrBackendContext.callableClosureOrigin)
|
||||||
|
.also { it.parent = refGetFunction }
|
||||||
|
|
||||||
// the params which are passed to closure
|
// the params which are passed to closure
|
||||||
val closureParamDeclarations = generateSignatureForClosure(declaration, refGetFunction, refClosureDeclaration, reference)
|
val closureParamDeclarations = generateSignatureForClosure(declaration, refGetFunction, refClosureDeclaration, reference)
|
||||||
|
|||||||
+8
@@ -5,8 +5,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs
|
package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
||||||
import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.COROUTINE_SWITCH
|
import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.COROUTINE_SWITCH
|
||||||
import org.jetbrains.kotlin.ir.backend.js.utils.JsGenerationContext
|
import org.jetbrains.kotlin.ir.backend.js.utils.JsGenerationContext
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.ir.types.isAny
|
import org.jetbrains.kotlin.ir.types.isAny
|
||||||
@@ -16,6 +18,12 @@ import org.jetbrains.kotlin.js.backend.ast.*
|
|||||||
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
|
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
|
||||||
class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer<JsStatement, JsGenerationContext> {
|
class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer<JsStatement, JsGenerationContext> {
|
||||||
|
|
||||||
|
override fun visitFunction(declaration: IrFunction, data: JsGenerationContext) = JsEmpty.also {
|
||||||
|
assert(declaration.origin == JsIrBackendContext.callableClosureOrigin) {
|
||||||
|
"The only possible Function Declarartion is one composed in Callable Reference Lowering"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitBlockBody(body: IrBlockBody, context: JsGenerationContext): JsStatement {
|
override fun visitBlockBody(body: IrBlockBody, context: JsGenerationContext): JsStatement {
|
||||||
return JsBlock(body.statements.map { it.accept(this, context) })
|
return JsBlock(body.statements.map { it.accept(this, context) })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user