[JS IR BE] Put closure function declaration in container (factory function)

This commit is contained in:
Roman Artemev
2018-10-24 17:31:08 +03:00
committed by romanart
parent 9515de0b7e
commit 43d14ed954
3 changed files with 17 additions and 4 deletions
@@ -103,6 +103,8 @@ class JsIrBackendContext(
// 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 GET_COROUTINE_CONTEXT_NAME = "getCoroutineContext"
val callableClosureOrigin = object : IrDeclarationOriginImpl("CALLABLE_CLOSURE_DECLARATION") {}
}
private val internalPackage = module.getPackage(JS_PACKAGE_FQNAME)
@@ -218,7 +218,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
putValueArgument(1, callableNameConst)
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 additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
val statements = mutableListOf<IrStatement>()
val statements = mutableListOf<IrStatement>(getterDeclaration)
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
@@ -270,6 +270,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
}
if (setterFunction != null) {
statements += setterFunction
val setterFunctionType = context.builtIns.getFunction(setterFunction.valueParameters.size + 1)
val irSetReference = JsIrBuilder.buildFunctionReference(
setterFunctionType.toIrType(symbolTable = context.symbolTable),
@@ -324,7 +325,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
val getterFunction = buildClosureFunction(context.irBuiltIns.throwIseFun, refGetFunction, propertyReference)
val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) {
val statements = mutableListOf<IrStatement>()
val statements = mutableListOf<IrStatement>(getterFunction)
val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1)
val type = getterFunctionType.toIrType(symbolTable = context.symbolTable)
@@ -520,7 +521,9 @@ class CallableReferenceLowering(val context: JsIrBackendContext) {
reference: IrCallableReference
): IrFunction {
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
val closureParamDeclarations = generateSignatureForClosure(declaration, refGetFunction, refClosureDeclaration, reference)
@@ -5,8 +5,10 @@
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.utils.JsGenerationContext
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.isAny
@@ -16,6 +18,12 @@ import org.jetbrains.kotlin.js.backend.ast.*
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
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 {
return JsBlock(body.statements.map { it.accept(this, context) })
}