@@ -42,7 +42,7 @@ trait ExprBuilder {
42
42
}
43
43
}
44
44
45
- /** A sequence of statements the concludes with a unconditional transition to `nextState` */
45
+ /** A sequence of statements that concludes with a unconditional transition to `nextState` */
46
46
final class SimpleAsyncState (val stats : List [Tree ], val state : Int , nextState : Int , symLookup : SymLookup )
47
47
extends AsyncState {
48
48
@@ -65,7 +65,7 @@ trait ExprBuilder {
65
65
}
66
66
67
67
/** A sequence of statements that concludes with an `await` call. The `onComplete`
68
- * handler will unconditionally transition to `nestState`.``
68
+ * handler will unconditionally transition to `nextState`.
69
69
*/
70
70
final class AsyncStateWithAwait (val stats : List [Tree ], val state : Int , nextState : Int ,
71
71
val awaitable : Awaitable , symLookup : SymLookup )
@@ -110,7 +110,7 @@ trait ExprBuilder {
110
110
}
111
111
112
112
/*
113
- * Builder for a single state of an async method .
113
+ * Builder for a single state of an async expression .
114
114
*/
115
115
final class AsyncStateBuilder (state : Int , private val symLookup : SymLookup ) {
116
116
/* Statements preceding an await call. */
@@ -123,9 +123,10 @@ trait ExprBuilder {
123
123
def addStat () = stats += stat
124
124
stat match {
125
125
case Apply (fun, Nil ) =>
126
+ // labelDefStates belongs to the current ExprBuilder
126
127
labelDefStates get fun.symbol match {
127
- case Some (nextState) => nextJumpState = Some (nextState)
128
- case None => addStat()
128
+ case opt @ Some (nextState) => nextJumpState = opt // re-use object
129
+ case None => addStat()
129
130
}
130
131
case _ => addStat()
131
132
}
@@ -258,7 +259,7 @@ trait ExprBuilder {
258
259
currState = afterMatchState
259
260
stateBuilder = new AsyncStateBuilder (currState, symLookup)
260
261
261
- case ld@ LabelDef (name, params, rhs) if rhs exists isAwait =>
262
+ case ld @ LabelDef (name, params, rhs) if rhs exists isAwait =>
262
263
val startLabelState = nextState()
263
264
val afterLabelState = nextState()
264
265
asyncStates += stateBuilder.resultWithLabel(startLabelState, symLookup)
@@ -268,7 +269,8 @@ trait ExprBuilder {
268
269
269
270
currState = afterLabelState
270
271
stateBuilder = new AsyncStateBuilder (currState, symLookup)
271
- case _ =>
272
+
273
+ case _ =>
272
274
checkForUnsupportedAwait(stat)
273
275
stateBuilder += stat
274
276
}
@@ -293,6 +295,13 @@ trait ExprBuilder {
293
295
gen.mkAttributedRef(stateMachineMember(name))
294
296
}
295
297
298
+ /**
299
+ * Uses `AsyncBlockBuilder` to create an instance of `AsyncBlock`.
300
+ *
301
+ * @param block a `Block` tree in ANF
302
+ * @param symLookup helper for looking up members of the state machine class
303
+ * @return an `AsyncBlock`
304
+ */
296
305
def buildAsyncBlock (block : Block , symLookup : SymLookup ): AsyncBlock = {
297
306
val Block (stats, expr) = block
298
307
val startState = stateAssigner.nextState()
@@ -323,19 +332,23 @@ trait ExprBuilder {
323
332
val initStates = asyncStates.init
324
333
325
334
/**
326
- * def resume(): Unit = {
327
- * try {
328
- * state match {
329
- * case 0 => {
330
- * f11 = exprReturningFuture
331
- * f11.onComplete(onCompleteHandler)(context)
335
+ * Builds the definition of the `resume` method.
336
+ *
337
+ * The resulting tree has the following shape:
338
+ *
339
+ * def resume(): Unit = {
340
+ * try {
341
+ * state match {
342
+ * case 0 => {
343
+ * f11 = exprReturningFuture
344
+ * f11.onComplete(onCompleteHandler)(context)
345
+ * }
346
+ * ...
347
+ * }
348
+ * } catch {
349
+ * case NonFatal(t) => result.failure(t)
332
350
* }
333
- * ...
334
351
* }
335
- * } catch {
336
- * case NonFatal(t) => result.failure(t)
337
- * }
338
- * }
339
352
*/
340
353
def resumeFunTree [T : WeakTypeTag ]: DefDef =
341
354
DefDef (Modifiers (), name.resume, Nil , List (Nil ), Ident (definitions.UnitClass ),
@@ -344,22 +357,23 @@ trait ExprBuilder {
344
357
List (
345
358
CaseDef (
346
359
Bind (name.t, Ident (nme.WILDCARD )),
347
- Apply (Ident (defn.NonFatalClass ), List (Ident (name.t))),
348
- Block (List ({
360
+ Apply (Ident (defn.NonFatalClass ), List (Ident (name.t))), {
349
361
val t = Expr [Throwable ](Ident (name.t))
350
362
futureSystemOps.completeProm[T ](
351
363
Expr [futureSystem.Prom [T ]](symLookup.memberRef(name.result)), reify(scala.util.Failure (t.splice))).tree
352
- }), literalUnit)) ), EmptyTree ))
364
+ })), EmptyTree ))
353
365
354
366
/**
355
- * assumes tr: Try[Any] is in scope .
367
+ * Builds a `match` expression used as an onComplete handler .
356
368
*
357
- * state match {
358
- * case 0 =>
359
- * x11 = tr.get.asInstanceOf[Double]
360
- * state = 1
361
- * resume()
362
- * }
369
+ * Assumes `tr: Try[Any]` is in scope. The resulting tree has the following shape:
370
+ *
371
+ * state match {
372
+ * case 0 =>
373
+ * x11 = tr.get.asInstanceOf[Double]
374
+ * state = 1
375
+ * resume()
376
+ * }
363
377
*/
364
378
def onCompleteHandler [T : WeakTypeTag ]: Tree =
365
379
Match (symLookup.memberRef(name.state), initStates.flatMap(_.mkOnCompleteHandler[T ]).toList)
@@ -373,7 +387,8 @@ trait ExprBuilder {
373
387
374
388
case class Awaitable (expr : Tree , resultName : Symbol , resultType : Type , resultValDef : ValDef )
375
389
376
- private def mkResumeApply (symLookup : SymLookup ) = Apply (symLookup.memberRef(name.resume), Nil )
390
+ private def mkResumeApply (symLookup : SymLookup ) =
391
+ Apply (symLookup.memberRef(name.resume), Nil )
377
392
378
393
private def mkStateTree (nextState : Int , symLookup : SymLookup ): Tree =
379
394
Assign (symLookup.memberRef(name.state), Literal (Constant (nextState)))
0 commit comments