@@ -42,25 +42,26 @@ object GoAsync
42
42
def goImpl [T : c.WeakTypeTag ](c: Context )(body: c.Expr [T ])(ec: c.Expr [ExecutionContext ]): c.Expr [Future [T ]] =
43
43
{
44
44
import c .universe ._
45
+ val ttype = c.weakTypeOf[T ]
45
46
val nbody = GoAsync .transformAsyncBody[T ](c)(body.tree)
46
47
val r = if (containsDefer(c)(body)) {
47
48
val defers = TermName (c.freshName)
48
49
val promise = TermName (c.freshName)
49
50
// asyn transform wantstyped tree on entry, so we must substitute 'defers' to untyped
50
51
// values after it, no before.
51
52
q """
52
- gopher.goasync.GoAsync.transformDeferMacro[ ${c.weakTypeOf[ T ] }](
53
+ gopher.goasync.GoAsync.transformDeferMacro[ ${ttype }](
53
54
{implicit val ${defers} = new Defers[ ${c.weakTypeOf[T ]}]()
54
55
val ${promise} = Promise[ ${c.weakTypeOf[T ]}]()
55
- scala.async.Async.async( ${nbody})( ${ec}).onComplete( x =>
56
+ scala.async.Async.async[ ${ttype} ] ( ${nbody})( ${ec}).onComplete( x =>
56
57
${promise}.complete( ${defers}.tryProcess(x))
57
58
)( ${ec})
58
59
${promise}.future
59
60
}
60
61
)
61
62
"""
62
63
} else {
63
- q " scala.async.Async.async( ${nbody})( ${ec}) "
64
+ q " scala.async.Async.async[ ${ttype} ] ( ${nbody})( ${ec}) "
64
65
}
65
66
c.Expr [Future [T ]](r)
66
67
}
@@ -125,8 +126,26 @@ object GoAsync
125
126
{
126
127
import c .universe ._
127
128
var found = false
129
+ var transformed = false
128
130
val transformer = new Transformer {
129
131
override def transform (tree: Tree ): Tree =
132
+ {
133
+
134
+ // if subtree was transformed, try to transform again origin tree,
135
+ // because await can be lifted-up from previous level.
136
+ def transformAgainIfNested (tree : Tree ): Tree =
137
+ {
138
+ val prevTransformed = transformed
139
+ transformed = false
140
+ val nested = super .transform(tree)
141
+ if (transformed) {
142
+ transform(nested)
143
+ }else {
144
+ transformed = prevTransformed
145
+ nested
146
+ }
147
+ }
148
+
130
149
tree match {
131
150
case q " ${f1}( ${a}=> ${b})(.. $a2) " =>
132
151
// TODO: cache in tree.
@@ -143,25 +162,33 @@ object GoAsync
143
162
} else false
144
163
} else true
145
164
if (isTwoParams) {
165
+ transformed = true
146
166
transformInlineHofCall1(c)(f1,a,b,a2)
147
167
} else {
148
168
super .transform(tree)
149
169
}
150
170
}else {
151
- super .transform(tree)
171
+ // TODO: think, may-be try to transform first b instead nested [?]
172
+ transformAgainIfNested(tree)
152
173
}
153
174
case q " ${f1}( ${a}=> ${b}) " =>
154
175
found = findAwait(c)(b)
155
176
if (found) {
177
+ transformed = true
156
178
transformInlineHofCall1(c)(f1,a,b,List ())
157
179
} else {
158
- super .transform(tree)
180
+ // TODO: think, may-be try to transform first b instead nested [?]
181
+ transformAgainIfNested(tree)
159
182
}
160
183
case _ =>
161
184
super .transform(tree)
162
185
}
186
+ }
187
+
163
188
}
164
- val r = transformer.transform(body)
189
+
190
+ var r = transformer.transform(body)
191
+
165
192
r
166
193
}
167
194
@@ -178,18 +205,13 @@ object GoAsync
178
205
val anb = atPos(body.pos){
179
206
val nnb = transformAsyncBody(c)(nb)
180
207
val ec = c.inferImplicitValue(c.weakTypeOf[ExecutionContext ])
181
- q " ( ${param})=>scala.async.Async.async[ ${ btype} ]( ${nnb})( $ec) "
208
+ q " ( ${param})=>scala.async.Async.async[ $btype]( ${nnb})( $ec) "
182
209
}
183
210
val ar = atPos(fun.pos) {
184
211
val uar = if (implicitParams.isEmpty) {
185
212
q " gopher.asyncApply1( ${fun})( ${anb}) "
186
213
} else {
187
- // we can't call macros here, becouse we don't know types of implicitParams
188
- // val a = param.tpe
189
- // val b = body.tpe
190
- // AsyncApply.impl1i(c)(fun)(anb,implicitParams)(a,b)
191
214
q " gopher.goasync.AsyncApply.apply1i( ${fun})( ${anb}, ${implicitParams}) "
192
- // q"gopher.asyncApply1i(${fun})(${anb})(..$implicitParams)"
193
215
}
194
216
// typecheck is necessory
195
217
// 1. to prevent runnint analysis of async over internal awaits in anb as on
@@ -229,6 +251,7 @@ object GoAsync
229
251
case q " ( ${a}=> ${b}) " =>
230
252
// don't touch nested functions
231
253
tree
254
+ // super.transform(tree)
232
255
case _ =>
233
256
super .transform(tree)
234
257
}
@@ -240,10 +263,6 @@ object GoAsync
240
263
found
241
264
}
242
265
243
- private def numberOfParamLists (c: Context )(obj: c.Tree ,m: c.Name ): Int =
244
- {
245
- ???
246
- }
247
266
248
267
}
249
268
0 commit comments