Skip to content

Commit 4842fa3

Browse files
committed
Fix a couple of bugs with Downgrade
- Correctly check return type of methods - Handle doubles in string concat code
1 parent cdfde54 commit 4842fa3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

build-tools/src/main/kotlin/cc/tweaked/cobalt/build/CompatibilityChecker.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private object ExistenceCache {
3434

3535
private fun getClass(ty: Type): Class<*>? {
3636
return when (ty.sort) {
37-
Type.VOID -> Void::class.java
37+
Type.VOID -> Void.TYPE
3838
Type.BOOLEAN -> Boolean::class.java
3939
Type.CHAR -> Char::class.java
4040
Type.BYTE -> Byte::class.java
@@ -57,7 +57,7 @@ private object ExistenceCache {
5757

5858
private fun getMethod(className: String, methodName: String, descriptor: String): Executable? {
5959
val klass = getClassFromInternal(className) ?: return null
60-
getClass(Type.getReturnType(descriptor)) ?: return null
60+
val retTy = getClass(Type.getReturnType(descriptor)) ?: return null
6161

6262
val argTypes = Type.getArgumentTypes(descriptor)
6363
val args = arrayOfNulls<Class<*>>(argTypes.size)
@@ -68,8 +68,11 @@ private object ExistenceCache {
6868
return if (methodName == "<init>") {
6969
tryReflection { klass.getDeclaredConstructor(*args) }
7070
} else {
71-
tryReflection { klass.getMethod(methodName, *args) }
71+
val method = tryReflection { klass.getMethod(methodName, *args) }
7272
?: tryReflection { klass.getDeclaredMethod(methodName, *args) }
73+
?: return null
74+
if (method.returnType != retTy) return null
75+
return method
7376
}
7477
}
7578

build-tools/src/main/kotlin/cc/tweaked/cobalt/build/downgrade/Downgrade.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ private class DowngradeMethodVisitor(
225225

226226
val argTypes = Type.getArgumentTypes(descriptor)
227227
var stackIdx = 0
228+
var stackSlot = 0
228229
var constantIdx = 1
229230
val fragment = StringBuilder()
230231

@@ -246,7 +247,7 @@ private class DowngradeMethodVisitor(
246247
}
247248

248249
val ty = argTypes[stackIdx]
249-
mw.visitVarInsn(ty.getOpcode(ILOAD), stackIdx)
250+
mw.visitVarInsn(ty.getOpcode(ILOAD), stackSlot)
250251

251252
val tyDescriptor = when {
252253
!ty.isReference -> ty.descriptor
@@ -258,6 +259,7 @@ private class DowngradeMethodVisitor(
258259
STRING_BUILDER, "append", "($tyDescriptor)L$STRING_BUILDER;", false,
259260
)
260261
stackIdx++
262+
stackSlot += ty.size
261263
}
262264

263265
'\u0002' -> {

0 commit comments

Comments
 (0)