Skip to content

Commit e619b03

Browse files
committed
Upgrade asm to 5.1
The constructor of scala.tools.asm.Handle now takes an additional boolean parameter to denote whether the owner is an interface.
1 parent 5ddb0bb commit e619b03

File tree

9 files changed

+47
-32
lines changed

9 files changed

+47
-32
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,11 +1334,13 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
13341334
val isStaticMethod = lambdaTarget.hasFlag(Flags.STATIC)
13351335
def asmType(sym: Symbol) = classBTypeFromSymbol(sym).toASMType
13361336

1337+
val isInterface = lambdaTarget.owner.isTrait
13371338
val implMethodHandle =
1338-
new asm.Handle(if (lambdaTarget.hasFlag(Flags.STATIC)) asm.Opcodes.H_INVOKESTATIC else if (lambdaTarget.owner.isTrait) asm.Opcodes.H_INVOKEINTERFACE else asm.Opcodes.H_INVOKEVIRTUAL,
1339+
new asm.Handle(if (lambdaTarget.hasFlag(Flags.STATIC)) asm.Opcodes.H_INVOKESTATIC else if (isInterface) asm.Opcodes.H_INVOKEINTERFACE else asm.Opcodes.H_INVOKEVIRTUAL,
13391340
classBTypeFromSymbol(lambdaTarget.owner).internalName,
13401341
lambdaTarget.name.toString,
1341-
methodBTypeFromSymbol(lambdaTarget).descriptor)
1342+
methodBTypeFromSymbol(lambdaTarget).descriptor,
1343+
/* itf = */ isInterface)
13421344
val receiver = if (isStaticMethod) Nil else lambdaTarget.owner :: Nil
13431345
val (capturedParams, lambdaParams) = lambdaTarget.paramss.head.splitAt(lambdaTarget.paramss.head.length - arity)
13441346
// Requires https://github.com/scala/scala-java8-compat on the runtime classpath
@@ -1351,7 +1353,7 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
13511353
val flags = java.lang.invoke.LambdaMetafactory.FLAG_SERIALIZABLE | java.lang.invoke.LambdaMetafactory.FLAG_MARKERS
13521354

13531355
val ScalaSerializable = classBTypeFromSymbol(definitions.SerializableClass).toASMType
1354-
bc.jmethod.visitInvokeDynamicInsn(samName, invokedType, lambdaMetaFactoryBootstrapHandle,
1356+
bc.jmethod.visitInvokeDynamicInsn(samName, invokedType, lambdaMetaFactoryAltMetafactoryHandle,
13551357
/* samMethodType = */ samMethodType,
13561358
/* implMethod = */ implMethodHandle,
13571359
/* instantiatedMethodType = */ constrainedType,

src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes {
157157
def staticHandleFromSymbol(sym: Symbol): asm.Handle = {
158158
val owner = if (sym.owner.isModuleClass) sym.owner.linkedClassOfClass else sym.owner
159159
val descriptor = methodBTypeFromMethodType(sym.info, isConstructor = false).descriptor
160-
new asm.Handle(asm.Opcodes.H_INVOKESTATIC, classBTypeFromSymbol(owner).internalName, sym.name.encoded, descriptor)
160+
val ownerBType = classBTypeFromSymbol(owner)
161+
new asm.Handle(asm.Opcodes.H_INVOKESTATIC, ownerBType.internalName, sym.name.encoded, descriptor, /* itf = */ ownerBType.isInterface.get)
161162
}
162163

163164
/**

src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,22 @@ class CoreBTypes[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes: BTFS) {
248248
})
249249
}
250250

251-
lazy val lambdaMetaFactoryBootstrapHandle =
251+
lazy val lambdaMetaFactoryMetafactoryHandle =
252+
new asm.Handle(asm.Opcodes.H_INVOKESTATIC,
253+
coreBTypes.jliLambdaMetafactoryRef.internalName, sn.Metafactory.toString,
254+
MethodBType(
255+
List(
256+
coreBTypes.jliMethodHandlesLookupRef,
257+
coreBTypes.StringRef,
258+
coreBTypes.jliMethodTypeRef,
259+
coreBTypes.jliMethodTypeRef,
260+
coreBTypes.jliMethodHandleRef,
261+
coreBTypes.jliMethodTypeRef),
262+
coreBTypes.jliCallSiteRef
263+
).descriptor,
264+
/* itf = */ coreBTypes.jliLambdaMetafactoryRef.isInterface.get)
265+
266+
lazy val lambdaMetaFactoryAltMetafactoryHandle =
252267
new asm.Handle(asm.Opcodes.H_INVOKESTATIC,
253268
coreBTypes.jliLambdaMetafactoryRef.internalName, sn.AltMetafactory.toString,
254269
MethodBType(
@@ -258,7 +273,8 @@ class CoreBTypes[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes: BTFS) {
258273
coreBTypes.jliMethodTypeRef,
259274
ArrayBType(ObjectRef)),
260275
coreBTypes.jliCallSiteRef
261-
).descriptor)
276+
).descriptor,
277+
/* itf = */ coreBTypes.jliLambdaMetafactoryRef.isInterface.get)
262278

263279
lazy val lambdaDeserializeBootstrapHandle =
264280
new scala.tools.asm.Handle(scala.tools.asm.Opcodes.H_INVOKESTATIC,
@@ -270,7 +286,8 @@ class CoreBTypes[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes: BTFS) {
270286
coreBTypes.jliMethodTypeRef
271287
),
272288
coreBTypes.jliCallSiteRef
273-
).descriptor)
289+
).descriptor,
290+
/* itf = */ coreBTypes.srLambdaDeserialize.isInterface.get)
274291
}
275292

276293
/**
@@ -299,6 +316,7 @@ trait CoreBTypesProxyGlobalIndependent[BTS <: BTypes] {
299316
def juHashMapRef : ClassBType
300317
def juMapRef : ClassBType
301318
def jliCallSiteRef : ClassBType
319+
def jliLambdaMetafactoryRef : ClassBType
302320
def jliMethodTypeRef : ClassBType
303321
def jliSerializedLambdaRef : ClassBType
304322
def jliMethodHandleRef : ClassBType
@@ -322,8 +340,9 @@ trait CoreBTypesProxyGlobalIndependent[BTS <: BTypes] {
322340
def srRefConstructors : Map[InternalName, MethodNameAndType]
323341
def tupleClassConstructors : Map[InternalName, MethodNameAndType]
324342

325-
def lambdaMetaFactoryBootstrapHandle : asm.Handle
326-
def lambdaDeserializeBootstrapHandle : asm.Handle
343+
def lambdaMetaFactoryMetafactoryHandle : asm.Handle
344+
def lambdaMetaFactoryAltMetafactoryHandle : asm.Handle
345+
def lambdaDeserializeBootstrapHandle : asm.Handle
327346
}
328347

329348
/**
@@ -405,6 +424,7 @@ final class CoreBTypesProxy[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes:
405424

406425
def String_valueOf: Symbol = _coreBTypes.String_valueOf
407426

408-
def lambdaMetaFactoryBootstrapHandle = _coreBTypes.lambdaMetaFactoryBootstrapHandle
409-
def lambdaDeserializeBootstrapHandle = _coreBTypes.lambdaDeserializeBootstrapHandle
427+
def lambdaMetaFactoryMetafactoryHandle : asm.Handle = _coreBTypes.lambdaMetaFactoryMetafactoryHandle
428+
def lambdaMetaFactoryAltMetafactoryHandle : asm.Handle = _coreBTypes.lambdaMetaFactoryAltMetafactoryHandle
429+
def lambdaDeserializeBootstrapHandle : asm.Handle = _coreBTypes.lambdaDeserializeBootstrapHandle
410430
}

src/compiler/scala/tools/nsc/backend/jvm/opt/CallGraph.scala

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,8 @@ class CallGraph[BT <: BTypes](val btypes: BT) {
413413
final case class LambdaMetaFactoryCall(indy: InvokeDynamicInsnNode, samMethodType: Type, implMethod: Handle, instantiatedMethodType: Type)
414414

415415
object LambdaMetaFactoryCall {
416-
private val lambdaMetaFactoryInternalName: InternalName = "java/lang/invoke/LambdaMetafactory"
417-
418-
private val metafactoryHandle = {
419-
val metafactoryMethodName: String = "metafactory"
420-
val metafactoryDesc: String = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
421-
new Handle(Opcodes.H_INVOKESTATIC, lambdaMetaFactoryInternalName, metafactoryMethodName, metafactoryDesc)
422-
}
423-
424-
private val altMetafactoryHandle = {
425-
val altMetafactoryMethodName: String = "altMetafactory"
426-
val altMetafactoryDesc: String = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;"
427-
new Handle(Opcodes.H_INVOKESTATIC, lambdaMetaFactoryInternalName, altMetafactoryMethodName, altMetafactoryDesc)
428-
}
429-
430416
def unapply(insn: AbstractInsnNode): Option[(InvokeDynamicInsnNode, Type, Handle, Type)] = insn match {
431-
case indy: InvokeDynamicInsnNode if indy.bsm == metafactoryHandle || indy.bsm == altMetafactoryHandle =>
417+
case indy: InvokeDynamicInsnNode if indy.bsm == coreBTypes.lambdaMetaFactoryMetafactoryHandle || indy.bsm == coreBTypes.lambdaMetaFactoryAltMetafactoryHandle =>
432418
indy.bsmArgs match {
433419
case Array(samMethodType: Type, implMethod: Handle, instantiatedMethodType: Type, _@_*) =>
434420
// LambdaMetaFactory performs a number of automatic adaptations when invoking the lambda

src/partest-extras/scala/tools/partest/ASMConverters.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ object ASMConverters {
9494
case class FrameEntry (`type`: Int, local: List[Any], stack: List[Any]) extends Instruction { def opcode: Int = -1 }
9595
case class LineNumber (line: Int, start: Label) extends Instruction { def opcode: Int = -1 }
9696

97-
case class MethodHandle(tag: Int, owner: String, name: String, desc: String)
97+
case class MethodHandle(tag: Int, owner: String, name: String, desc: String, itf: Boolean)
9898

9999
case class ExceptionHandler(start: Label, end: Label, handler: Label, desc: Option[String])
100100
case class LocalVariable(name: String, desc: String, signature: Option[String], start: Label, end: Label, index: Int)
@@ -147,7 +147,7 @@ object ASMConverters {
147147
case _ => a // can be: Class, method Type, primitive constant
148148
})(collection.breakOut)
149149

150-
private def convertMethodHandle(h: asm.Handle): MethodHandle = MethodHandle(h.getTag, h.getOwner, h.getName, h.getDesc)
150+
private def convertMethodHandle(h: asm.Handle): MethodHandle = MethodHandle(h.getTag, h.getOwner, h.getName, h.getDesc, h.isInterface)
151151

152152
private def convertHandlers(method: t.MethodNode): List[ExceptionHandler] = {
153153
method.tryCatchBlocks.asScala.map(h => ExceptionHandler(applyLabel(h.start), applyLabel(h.end), applyLabel(h.handler), Option(h.`type`)))(collection.breakOut)
@@ -227,7 +227,7 @@ object ASMConverters {
227227
case x => x.asInstanceOf[Object]
228228
}
229229

230-
def unconvertMethodHandle(h: MethodHandle): asm.Handle = new asm.Handle(h.tag, h.owner, h.name, h.desc)
230+
def unconvertMethodHandle(h: MethodHandle): asm.Handle = new asm.Handle(h.tag, h.owner, h.name, h.desc, h.itf)
231231
def unconvertBsmArgs(a: List[Object]): Array[Object] = a.map({
232232
case h: MethodHandle => unconvertMethodHandle(h)
233233
case o => o

src/reflect/scala/reflect/internal/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ trait StdNames {
11711171
final val Invoke: TermName = newTermName("invoke")
11721172
final val InvokeExact: TermName = newTermName("invokeExact")
11731173

1174+
final val Metafactory: TermName = newTermName("metafactory")
11741175
final val AltMetafactory: TermName = newTermName("altMetafactory")
11751176
final val Bootstrap: TermName = newTermName("bootstrap")
11761177

test/files/run/classfile-format-51.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object Test extends DirectTest {
8080

8181
val test = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "test", s"()Ljava/lang/String;", null, null)
8282
test.visitCode()
83-
val bootstrapHandle = new Handle(H_INVOKESTATIC, invokerClassName, bootstrapMethodName, bootStrapMethodType)
83+
val bootstrapHandle = new Handle(H_INVOKESTATIC, invokerClassName, bootstrapMethodName, bootStrapMethodType, /* itf = */ false)
8484
test.visitInvokeDynamicInsn("invoke", targetMethodType, bootstrapHandle)
8585
test.visitInsn(ARETURN)
8686
test.visitMaxs(1, 1)

test/files/run/noInlineUnknownIndy/Test.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ object Test extends DirectTest {
1515
}
1616

1717
def show(): Unit = {
18-
val unknownBootstrapMethod = new Handle(Opcodes.H_INVOKESTATIC, "not/java/lang/SomeLambdaMetafactory", "notAMetaFactoryMethod", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;")
18+
val unknownBootstrapMethod = new Handle(
19+
Opcodes.H_INVOKESTATIC,
20+
"not/java/lang/SomeLambdaMetafactory",
21+
"notAMetaFactoryMethod",
22+
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;",
23+
/* itf = */ false)
1924
modifyClassFile(new File(testOutput.toFile, "A_1.class"))((cn: ClassNode) => {
2025
val testMethod = cn.methods.iterator.asScala.find(_.name == "test").head
2126
val indy = testMethod.instructions.iterator.asScala.collect({ case i: InvokeDynamicInsnNode => i }).next()

versions.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ scala-parser-combinators.version.number=1.0.4
2727
scala-swing.version.number=2.0.0-M2
2828
scala-swing.version.osgi=2.0.0.M2
2929
jline.version=2.14.1
30-
scala-asm.version=5.0.4-scala-3
30+
scala-asm.version=5.1.0-scala-1
3131

3232
# external modules, used internally (not shipped)
3333
partest.version.number=1.0.17

0 commit comments

Comments
 (0)