Skip to content

Commit 3b804bc

Browse files
committed
[JS] Introduce the JsNoLifting annotation class
It will be used in the next commit. (cherry picked from commit 1519a80)
1 parent e690a3a commit 3b804bc

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractFunctionReferenceLowering.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ abstract class AbstractFunctionReferenceLowering<C : CommonBackendContext>(val c
274274
isSuspend = superFunction.isSuspend
275275
}.apply {
276276
attributeOwnerId = functionReference.attributeOwnerId
277+
annotations = invokeFunction.annotations
277278

278279
parameters += createDispatchReceiverParameterWithClassParent()
279280
require(superFunction.typeParameters.isEmpty()) { "Fun interface abstract function can't have type parameters" }

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/InteropCallableReferenceLowering.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.ir.types.typeWith
3131
import org.jetbrains.kotlin.ir.util.*
3232
import org.jetbrains.kotlin.ir.visitors.*
3333
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
34+
import org.jetbrains.kotlin.name.JsStandardClassIds
3435
import org.jetbrains.kotlin.name.Name
3536
import org.jetbrains.kotlin.utils.addToStdlib.assignFrom
3637
import org.jetbrains.kotlin.utils.addToStdlib.cast
@@ -316,7 +317,7 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
316317
val lambdaInfo = LambdaInfo(lambdaClass)
317318

318319
return if (lambdaClass.origin == CallableReferenceLowering.LAMBDA_IMPL && !lambdaInfo.isSuspendLambda) {
319-
if (lambdaClass.fields.none()) {
320+
if (lambdaClass.fields.none() && !lambdaInfo.invokeFun.hasAnnotation(JsStandardClassIds.Annotations.JsNoLifting)) {
320321
// Optimization:
321322
// If the lambda has no context, we lift it, i.e. instead of generating an anonymous function,
322323
// we generate a named free function. The usage of the lambda is then replaced with a reference to the free function.

core/compiler.common.js/src/org/jetbrains/kotlin/name/JsStandardClassIds.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ object JsStandardClassIds {
8585

8686
@JvmField
8787
val nativeAnnotations = setOf(JsNative, JsNativeInvoke, JsNativeGetter, JsNativeSetter)
88+
89+
@JvmField
90+
val JsNoLifting = "JsNoLifting".jsId()
8891
}
8992

9093
object Callables {

libraries/stdlib/js/src/kotlin/annotationsJs.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,10 @@ public annotation class JsExternalArgument
311311
@MustBeDocumented
312312
@SinceKotlin("2.0")
313313
public actual annotation class JsStatic()
314+
315+
/**
316+
* Prevents the annotated contextless lambda from being transformed into a global function.
317+
*/
318+
@Retention(AnnotationRetention.BINARY)
319+
@Target(FUNCTION)
320+
internal annotation class JsNoLifting

0 commit comments

Comments
 (0)