[ 
https://issues.apache.org/jira/browse/GROOVY-12679?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137475#comment-18137475
 ] 

ASF GitHub Bot commented on GROOVY-12679:
-----------------------------------------

codeconsole opened a new pull request, #3200:
URL: https://github.com/apache/groovy/pull/3200

   The closures returned by `AsyncClosureUtils.wrapAsync` and 
`wrapAsyncGenerator` copied only the owner and this object of the closure they 
wrap. A delegate or resolve strategy set on them was never seen by the wrapped 
closure, which resolves the names in the body, so `with { }`, builders and 
other DSL use resolved names against the owner:
   
   ```groovy
   class Ctx { String greet() { 'hi' } }
   Closure wrapped = AsyncClosureUtils.wrapAsync { -> greet() }
   wrapped.delegate = new Ctx()
   wrapped.resolveStrategy = Closure.DELEGATE_FIRST
   await(wrapped())   // MissingMethodException: greet for class: Script1
   ```
   
   They also reported the parameters of their own `doCall(Object...)`, so `[a: 
1].collect(AsyncClosureUtils.wrapAsync { k, v -> ... })` passed the map entry 
as one argument and failed. Seen in 6.0.0 too. (The `async { }` keyword does 
not use these wrappers.)
   
   The wrappers are now nested classes that read and set the delegate and 
resolve strategy on the wrapped closure, as `ComposedClosure` does. `clone`, 
`dehydrate` and `rehydrate` copy the wrapped closure too, so `with` and 
rehydration act on a copy rather than the original. They report the wrapped 
closure's parameters; the generator wrapper drops the leading 
`GeneratorBridge`, which it supplies itself. Because the getters now forward 
through the wrapped closure, it is declared in `additionalReferences()`, and a 
deserialized wrapper is checked for reference cycles as Memoize and 
TrampolineClosure are. Two commits: delegation, then parameters.
   
   ## Tests
   
   These tests in `AsyncAwaitTest` fail without the change: 
`testWrapAsyncSetDelegateAndResolveStrategy`, 
`testWrapAsyncReportsDelegateAndThisObject`, `testWrapAsyncWith`, 
`testWrapAsyncDehydrateAndRehydrate`, `testWrapAsyncReportsParameters`, 
`testWrapAsyncGeneratorSetDelegateAndResolveStrategy`, 
`testWrapAsyncGeneratorWith` and 
`testWrapAsyncGeneratorReportsParametersAfterBridge`. 
`ClosureSerializationCycleTest.testAsyncClosureCyclesRejected` and 
`testLegitimateAsyncClosuresRoundTrip` cover serialization. 
`groovy.AsyncAwaitTest`, `groovy.lang.*Closure*`, 
`org.apache.groovy.runtime.async.*` and `groovy.concurrent.*` pass. The 
Checkstyle and CodeNarc gates pass.
   




> Closures from AsyncClosureUtils.wrapAsync/wrapAsyncGenerator ignore the 
> delegate and resolve strategy and misreport their parameters
> ------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12679
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12679
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 7.0.0-alpha-1, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> import org.apache.groovy.runtime.async.AsyncClosureUtils
> class Ctx { String greet() { 'hi' } }
> Closure wrapped = AsyncClosureUtils.wrapAsync { -> greet() }
> wrapped.delegate = new Ctx()
> wrapped.resolveStrategy = Closure.DELEGATE_FIRST
> assert await(wrapped()) == 'hi'
> assert await(new Ctx().with(AsyncClosureUtils.wrapAsync { greet() })) == 'hi'
> def w = AsyncClosureUtils.wrapAsync { String k, Integer v -> k * v }
> assert w.maximumNumberOfParameters == 2
> assert [a: 1, b: 2].collect(w).collect { await(it) } == ['a', 'bb']
> {code}
> *Actual:* MissingMethodException: No signature of method: greet for class: 
> Script1. After setting a delegate on the wrapper, getDelegate() still returns 
> the script and getResolveStrategy() returns OWNER_FIRST. If the wrapped 
> closure is configured instead, the wrapper reports its own owner and 
> OWNER_FIRST. The wrapper reports one Object[] parameter, so Map.collect 
> passes the entry to a two-parameter closure and fails with 
> MissingMethodException: doCall ... (Entry). wrapAsyncGenerator behaves the 
> same.
> *Expected:* the same behaviour as calling the wrapped closure directly: names 
> resolve against the delegate, with(), dehydrate() and rehydrate() work, and 
> the wrapped closure's parameters are reported.
> Cause: the wrappers are anonymous Closure subclasses that copy only the owner 
> and this object of the wrapped closure. They keep their own delegate and 
> resolve strategy, which the wrapped closure never sees, and report the 
> parameters of their own doCall(Object...).
> Verified on 6.0.0 and master (the API is new in 6.0.0).



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to