Add version to DebugMetadata

#KT-26580 Fixed
This commit is contained in:
Ilmir Usmanov
2018-09-04 18:44:03 +03:00
parent e082fe19ea
commit 53a1526412
2 changed files with 27 additions and 10 deletions
@@ -5,25 +5,25 @@
package kotlin.coroutines.jvm.internal
import kotlin.coroutines.Continuation
@Target(AnnotationTarget.CLASS)
@SinceKotlin("1.3")
internal annotation class DebugMetadata(
@get:JvmName("v")
val version: Int = 1,
@get:JvmName("f")
val sourceFile: String,
val sourceFile: String = "",
@get:JvmName("l")
val lineNumbers: IntArray,
val lineNumbers: IntArray = [],
@get:JvmName("n")
val localNames: Array<String>,
val localNames: Array<String> = [],
@get:JvmName("s")
val spilled: Array<String>,
val spilled: Array<String> = [],
@get:JvmName("i")
val indexToLabel: IntArray,
val indexToLabel: IntArray = [],
@get:JvmName("m")
val methodName: String,
val methodName: String = "",
@get:JvmName("c")
val className: String
val className: String = ""
)
/**
@@ -38,6 +38,7 @@ internal annotation class DebugMetadata(
@JvmName("getStackTraceElement")
internal fun BaseContinuationImpl.getStackTraceElementImpl(): StackTraceElement? {
val debugMetadata = getDebugMetadataAnnotation() ?: return null
checkDebugMetadataVersion(COROUTINES_DEBUG_METADATA_VERSION, debugMetadata.version)
val label = getLabel()
val lineNumber = if (label < 0) -1 else debugMetadata.lineNumbers[label]
return StackTraceElement(debugMetadata.className, debugMetadata.methodName, debugMetadata.sourceFile, lineNumber)
@@ -55,6 +56,12 @@ private fun BaseContinuationImpl.getLabel(): Int =
-1
}
private fun checkDebugMetadataVersion(expected: Int, actual: Int) {
if (actual > expected) {
error("Debug metadata version mismatch. Expected: $expected, got $actual. Please update the Kotlin standard library.")
}
}
/**
* Returns an array of spilled variable names and continuation's field names where the variable has been spilled.
* The structure is the following:
@@ -70,6 +77,7 @@ private fun BaseContinuationImpl.getLabel(): Int =
@JvmName("getSpilledVariableFieldMapping")
internal fun BaseContinuationImpl.getSpilledVariableFieldMapping(): Array<String>? {
val debugMetadata = getDebugMetadataAnnotation() ?: return null
checkDebugMetadataVersion(COROUTINES_DEBUG_METADATA_VERSION, debugMetadata.version)
val res = arrayListOf<String>()
val label = getLabel()
for ((i, labelOfIndex) in debugMetadata.indexToLabel.withIndex()) {
@@ -79,4 +87,6 @@ internal fun BaseContinuationImpl.getSpilledVariableFieldMapping(): Array<String
}
}
return res.toTypedArray()
}
}
private const val COROUTINES_DEBUG_METADATA_VERSION = 1