Minor. Simplify coroutine transformation
This commit is contained in:
+18
-18
@@ -177,18 +177,20 @@ class CoroutineTransformerMethodVisitor(
|
|||||||
postponedActions.add {
|
postponedActions.add {
|
||||||
with(instructions) {
|
with(instructions) {
|
||||||
// store variable before suspension call
|
// store variable before suspension call
|
||||||
insertBefore(call, VarInsnNode(Opcodes.ALOAD, 0))
|
insertBefore(call, withInstructionAdapter {
|
||||||
insertBefore(call, VarInsnNode(type.getOpcode(Opcodes.ILOAD), index))
|
load(0, AsmTypes.OBJECT_TYPE)
|
||||||
insertBefore(call, coercionInsns(type, normalizedType))
|
load(index, type)
|
||||||
insertBefore(call, FieldInsnNode(Opcodes.PUTFIELD, classBuilder.thisName, fieldName, normalizedType.descriptor))
|
StackValue.coerce(type, normalizedType, this)
|
||||||
|
putfield(classBuilder.thisName, fieldName, normalizedType.descriptor)
|
||||||
|
})
|
||||||
|
|
||||||
// restore variable after suspension call
|
// restore variable after suspension call
|
||||||
val nextInsnAfterCall = call.tryCatchBlockEndLabelAfterSuspensionCall.next
|
insert(call.tryCatchBlockEndLabelAfterSuspensionCall, withInstructionAdapter {
|
||||||
insertBefore(nextInsnAfterCall, VarInsnNode(Opcodes.ALOAD, 0))
|
load(0, AsmTypes.OBJECT_TYPE)
|
||||||
insertBefore(nextInsnAfterCall,
|
getfield(classBuilder.thisName, fieldName, normalizedType.descriptor)
|
||||||
FieldInsnNode(Opcodes.GETFIELD, classBuilder.thisName, fieldName, normalizedType.descriptor))
|
StackValue.coerce(normalizedType, type, this)
|
||||||
insertBefore(nextInsnAfterCall, coercionInsns(normalizedType, type))
|
store(index, type)
|
||||||
insertBefore(nextInsnAfterCall, VarInsnNode(type.getOpcode(Opcodes.ISTORE), index))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -235,13 +237,13 @@ class CoroutineTransformerMethodVisitor(
|
|||||||
FieldInsnNode(
|
FieldInsnNode(
|
||||||
Opcodes.PUTFIELD, classBuilder.thisName, COROUTINE_LABEL_FIELD_NAME, Type.INT_TYPE.descriptor)))
|
Opcodes.PUTFIELD, classBuilder.thisName, COROUTINE_LABEL_FIELD_NAME, Type.INT_TYPE.descriptor)))
|
||||||
|
|
||||||
val nextInsnAfterCall = call.tryCatchBlockEndLabelAfterSuspensionCall.next
|
|
||||||
|
|
||||||
// Exit
|
insert(call.tryCatchBlockEndLabelAfterSuspensionCall, withInstructionAdapter {
|
||||||
insertBefore(nextInsnAfterCall, InsnNode(Opcodes.RETURN))
|
// Exit
|
||||||
|
areturn(Type.VOID_TYPE)
|
||||||
// Mark place for continuation
|
// Mark place for continuation
|
||||||
insertBefore(nextInsnAfterCall, continuationLabel)
|
visitLabel(continuationLabel.label)
|
||||||
|
})
|
||||||
|
|
||||||
// After suspension point there is always three nodes: L1, NOP, L2
|
// After suspension point there is always three nodes: L1, NOP, L2
|
||||||
// And if there are relevant exception handlers, they always start at L2
|
// And if there are relevant exception handlers, they always start at L2
|
||||||
@@ -319,8 +321,6 @@ class CoroutineTransformerMethodVisitor(
|
|||||||
|
|
||||||
private fun Type.fieldNameForVar(index: Int) = descriptor.first() + "$" + index
|
private fun Type.fieldNameForVar(index: Int) = descriptor.first() + "$" + index
|
||||||
|
|
||||||
private fun coercionInsns(from: Type, to: Type) = withInstructionAdapter { StackValue.coerce(from, to, this) }
|
|
||||||
|
|
||||||
private fun withInstructionAdapter(block: InstructionAdapter.() -> Unit): InsnList {
|
private fun withInstructionAdapter(block: InstructionAdapter.() -> Unit): InsnList {
|
||||||
val tmpMethodNode = MethodNode()
|
val tmpMethodNode = MethodNode()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user