@@ -15,7 +15,7 @@ import scala.tools.asm
15
15
import GenBCode ._
16
16
import BackendReporting ._
17
17
import scala .tools .asm .Opcodes
18
- import scala .tools .asm .tree .MethodInsnNode
18
+ import scala .tools .asm .tree .{ MethodInsnNode , MethodNode }
19
19
import scala .tools .nsc .backend .jvm .BCodeHelpers .{InvokeStyle , TestOp }
20
20
21
21
/*
@@ -630,7 +630,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
630
630
case Apply (fun, args) if app.hasAttachment[delambdafy.LambdaMetaFactoryCapable ] =>
631
631
val attachment = app.attachments.get[delambdafy.LambdaMetaFactoryCapable ].get
632
632
genLoadArguments(args, paramTKs(app))
633
- genInvokeDynamicLambda(attachment.target, attachment.arity, attachment.functionalInterface, attachment.sam)
633
+ genInvokeDynamicLambda(attachment.target, attachment.arity, attachment.functionalInterface, attachment.sam, attachment.isSerializable, attachment.addScalaSerializableMarker )
634
634
generatedType = methodBTypeFromSymbol(fun.symbol).returnType
635
635
636
636
case Apply (fun, List (expr)) if currentRun.runDefinitions.isBox(fun.symbol) =>
@@ -1330,7 +1330,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
1330
1330
def genSynchronized (tree : Apply , expectedType : BType ): BType
1331
1331
def genLoadTry (tree : Try ): BType
1332
1332
1333
- def genInvokeDynamicLambda (lambdaTarget : Symbol , arity : Int , functionalInterface : Symbol , sam : Symbol ) {
1333
+ def genInvokeDynamicLambda (lambdaTarget : Symbol , arity : Int , functionalInterface : Symbol , sam : Symbol , isSerializable : Boolean , addScalaSerializableMarker : Boolean ) {
1334
1334
val isStaticMethod = lambdaTarget.hasFlag(Flags .STATIC )
1335
1335
def asmType (sym : Symbol ) = classBTypeFromSymbol(sym).toASMType
1336
1336
@@ -1343,26 +1343,24 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
1343
1343
/* itf = */ isInterface)
1344
1344
val receiver = if (isStaticMethod) Nil else lambdaTarget.owner :: Nil
1345
1345
val (capturedParams, lambdaParams) = lambdaTarget.paramss.head.splitAt(lambdaTarget.paramss.head.length - arity)
1346
- // Requires https://github.com/scala/scala-java8-compat on the runtime classpath
1347
1346
val invokedType = asm.Type .getMethodDescriptor(asmType(functionalInterface), (receiver ::: capturedParams).map(sym => typeToBType(sym.info).toASMType): _* )
1348
-
1349
1347
val constrainedType = new MethodBType (lambdaParams.map(p => typeToBType(p.tpe)), typeToBType(lambdaTarget.tpe.resultType)).toASMType
1350
- val samName = sam.name.toString
1351
1348
val samMethodType = methodBTypeFromSymbol(sam).toASMType
1352
-
1353
- val flags = java.lang.invoke.LambdaMetafactory .FLAG_SERIALIZABLE | java.lang.invoke.LambdaMetafactory .FLAG_MARKERS
1354
-
1355
- val ScalaSerializable = classBTypeFromSymbol(definitions.SerializableClass ).toASMType
1356
- bc.jmethod.visitInvokeDynamicInsn(samName, invokedType, lambdaMetaFactoryAltMetafactoryHandle,
1357
- /* samMethodType = */ samMethodType,
1358
- /* implMethod = */ implMethodHandle,
1359
- /* instantiatedMethodType = */ constrainedType,
1360
- /* flags = */ flags.asInstanceOf [AnyRef ],
1361
- /* markerInterfaceCount = */ 1 .asInstanceOf [AnyRef ],
1362
- /* markerInterfaces[0] = */ ScalaSerializable ,
1363
- /* bridgeCount = */ 0 .asInstanceOf [AnyRef ]
1364
- )
1365
- indyLambdaHosts += cnode.name
1349
+ val markers = if (addScalaSerializableMarker) classBTypeFromSymbol(definitions.SerializableClass ).toASMType :: Nil else Nil
1350
+ visitInvokeDynamicInsnLMF(bc.jmethod, sam.name.toString, invokedType, samMethodType, implMethodHandle, constrainedType, isSerializable, markers)
1351
+ if (isSerializable)
1352
+ indyLambdaHosts += cnode.name
1366
1353
}
1367
1354
}
1355
+
1356
+ private def visitInvokeDynamicInsnLMF (jmethod : MethodNode , samName : String , invokedType : String , samMethodType : asm.Type ,
1357
+ implMethodHandle : asm.Handle , instantiatedMethodType : asm.Type ,
1358
+ serializable : Boolean , markerInterfaces : Seq [asm.Type ]) = {
1359
+ import java .lang .invoke .LambdaMetafactory .{FLAG_MARKERS , FLAG_SERIALIZABLE }
1360
+ def flagIf (b : Boolean , flag : Int ): Int = if (b) flag else 0
1361
+ val flags = FLAG_MARKERS | flagIf(serializable, FLAG_SERIALIZABLE )
1362
+ val bsmArgs = Seq (samMethodType, implMethodHandle, instantiatedMethodType, Int .box(flags), Int .box(markerInterfaces.length)) ++ markerInterfaces
1363
+ jmethod.visitInvokeDynamicInsn(samName, invokedType, lambdaMetaFactoryAltMetafactoryHandle, bsmArgs : _* )
1364
+ }
1365
+
1368
1366
}
0 commit comments