Guest User

Untitled

a guest
May 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. /*
  2. * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
  3. * that can be found in the license/LICENSE.txt file.
  4. */
  5.  
  6. package kotlin.coroutines.jvm.internal
  7.  
  8. import java.lang.IllegalStateException
  9. import java.util.*
  10. import kotlin.coroutines.Continuation
  11. import kotlin.coroutines.CoroutineContext
  12. import kotlin.coroutines.processBareContinuationResume
  13. import kotlin.jvm.internal.Lambda
  14.  
  15. /**
  16. * @suppress
  17. */
  18. @SinceKotlin("1.3")
  19. public abstract class CoroutineImpl(
  20. arity: Int,
  21. @JvmField
  22. protected var completion: Continuation<Any?>?
  23. ) : Lambda(arity), Continuation<Any?> {
  24.  
  25. // label == -1 when coroutine cannot be started (it is just a factory object) or has already finished execution
  26. // label == 0 in initial part of the coroutine
  27. @JvmField
  28. protected var label: Int = if (completion != null) 0 else -1
  29.  
  30. private var objects: Array<Any?>
  31. private var objectsTop: IntArray
  32. private var ints: IntArray
  33. private var intsTop: IntArray
  34.  
  35. init {
  36. val comp = completion
  37. if (comp is CoroutineImpl) {
  38. objects = comp.objects
  39. objectsTop = comp.objectsTop
  40. ints = comp.ints
  41. intsTop = comp.intsTop
  42. } else {
  43. objects = arrayOfNulls(5)
  44. objectsTop = intArrayOf(0)
  45. ints = intArrayOf(0, 0, 0, 0, 0)
  46. intsTop = intArrayOf(0)
  47. }
  48. }
  49.  
  50. fun pushObjects(o: Any?) {
  51. val top = objectsTop[0]
  52. reallocObjectsIfNeeded(top + 1)
  53. objects[top] = o
  54. objectsTop[0] = top + 1
  55. }
  56.  
  57. fun pushObjects(a: Any?, b: Any?) {
  58. val top = objectsTop[0]
  59. reallocObjectsIfNeeded(top + 2)
  60. objects[top] = a
  61. objects[top + 1] = b
  62. objectsTop[0] = top + 2
  63. }
  64.  
  65. fun pushObjects(a: Any?, b: Any?, c: Any?) {
  66. val top = objectsTop[0]
  67. reallocObjectsIfNeeded(top + 3)
  68. objects[top] = a
  69. objects[top + 1] = b
  70. objects[top + 2] = c
  71. objectsTop[0] = top + 3
  72. }
  73.  
  74. private fun reallocObjectsIfNeeded(i: Int) {
  75. if (i >= objects.size) {
  76. objects = Arrays.copyOf(objects, i * 2)
  77. }
  78. }
  79.  
  80. // TODO: add multiple methods: escape analysis will take care of allocations
  81. fun popObject(): Any? {
  82. return objects[--objectsTop[0]]
  83. }
  84.  
  85. fun dropObjects(i: Int) {
  86. objectsTop[0] -= i
  87. }
  88.  
  89. fun pushInts(i: Int) {
  90. val top = intsTop[0]
  91. reallocIntsIfNeeded(top + 1)
  92. ints[top] = i
  93. intsTop[0] = top + 1
  94. }
  95.  
  96. fun pushInts(a: Int, b: Int) {
  97. val top = intsTop[0]
  98. reallocIntsIfNeeded(top + 2)
  99. ints[top] = a
  100. ints[top + 1] = b
  101. intsTop[0] = top + 2
  102. }
  103.  
  104. fun pushInts(a: Int, b: Int, c: Int) {
  105. val top = intsTop[0]
  106. reallocIntsIfNeeded(top + 3)
  107. ints[top] = a
  108. ints[top + 1] = b
  109. ints[top + 2] = c
  110. intsTop[0] = top + 3
  111. }
  112.  
  113. private fun reallocIntsIfNeeded(i: Int) {
  114. if (i >= ints.size) {
  115. ints = Arrays.copyOf(ints, i * 2)
  116. }
  117. }
  118.  
  119. fun popInt(): Int {
  120. return ints[--intsTop[0]]
  121. }
  122.  
  123. fun dropInts(i: Int) {
  124. intsTop[0] -= i
  125. }
  126.  
  127. private val _context: CoroutineContext? = completion?.context
  128.  
  129. override val context: CoroutineContext
  130. get() = _context!!
  131.  
  132. private var _facade: Continuation<Any?>? = null
  133.  
  134. val facade: Continuation<Any?> get() {
  135. if (_facade == null) _facade = interceptContinuationIfNeeded(_context!!, this)
  136. return _facade!!
  137. }
  138.  
  139. override fun resume(value: Any?) {
  140. processBareContinuationResume(completion!!) {
  141. doResume(value, null)
  142. }
  143. }
  144.  
  145. override fun resumeWithException(exception: Throwable) {
  146. processBareContinuationResume(completion!!) {
  147. doResume(null, exception)
  148. }
  149. }
  150.  
  151. protected abstract fun doResume(data: Any?, exception: Throwable?): Any?
  152.  
  153. open fun create(completion: Continuation<*>): Continuation<Unit> {
  154. throw IllegalStateException("create(Continuation) has not been overridden")
  155. }
  156.  
  157. open fun create(value: Any?, completion: Continuation<*>): Continuation<Unit> {
  158. throw IllegalStateException("create(Any?;Continuation) has not been overridden")
  159. }
  160. }
Add Comment
Please, Sign In to add comment