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

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

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

   The closures returned by `trampoline()`, `memoize*()`, `curry()`, `>>`/`<<` 
and `asWritable()` do not report or forward the delegation settings of the 
closure they wrap:
   
   ```groovy
   class D { def m() { 'delegate' } }
   class O {
       def m() { 'owner' }
       def run() {
           def c = { x -> m() }
           assert c.curry(1).thisObject.is(this)   // null
           def t = c.trampoline()
           t.resolveStrategy = Closure.DELEGATE_FIRST
           t.delegate = new D()
           assert t(1) == 'delegate'               // 'owner'
       }
   }
   new O().run()
   ```
   
   Seen in 4.0.33, 5.0.8 and 6.0.0 too.
   
   - `TrampolineClosure` passed the wrapped closure's delegate as its 
`thisObject`, so its `delegate` reported the owner. It now takes the wrapped 
closure's `thisObject` and forwards `getDelegate`, `setDelegate`, 
`getResolveStrategy` and `setResolveStrategy` to the wrapped closure, as 
`ComposedClosure` does. Its `clone()` copies the wrapped closure, so 
`with`/`tap`, which clone and then set the delegate, leave the original 
trampoline unchanged. Its parameter count and types now come from the wrapped 
closure's getters; before, a trampolined writable or composed closure reported 
a single `Object[]` parameter.
   - `Memoize.MemoizeFunction` gets the same forwarding, `thisObject` and 
`clone()`. The copy shares the cache.
   - `CurriedClosure`, `ComposedClosure` and `Closure.WritableClosure` forward 
`getThisObject` to the closure they call. `getOwner()` of these three still 
returns the wrapped closure, which they call through.
   
   One commit per wrapper.
   
   Merging: with #3186 (GROOVY-12665, wrapper rehydrate/dehydrate) this merges 
cleanly. With #3190 (GROOVY-12669, memoize rehydrate/dehydrate) one hunk in 
`MemoizeFunction` conflicts: keep #3190's `cacheFactory` field and constructor 
parameter, make `closure` non-final (needed by `clone()` here), and call 
`super(closure.getOwner(), closure.getThisObject())`.
   
   ## Tests
   
   These fail without the change:
   - `ClosureCurryTest.testThisObject`
   - `ClosureComposeTest.testThisObject`
   - `ClosureTest.testWritableClosureThisObject` (includes a `@CompileStatic` 
case)
   - `TrampolineTest`: `testDelegateAndThisObject`, 
`testSetDelegateAndResolveStrategy`, `testWith`, 
`testParametersOfWrappedClosure`
   - `AbstractMemoizeTestCase`: `testDelegateAndThisObject`, 
`testSetDelegateAndResolveStrategy`, `testWith`, run by `MemoizeTest`, 
`MemoizeAtMostTest`, `MemoizeAtLeastTest` and `MemoizeBetweenTest`
   
   `groovy.lang.*Closure*`, `groovy.*Closure*`, 
`org.codehaus.groovy.runtime.*Closure*`, 
`org.codehaus.groovy.runtime.trampoline.*`, 
`org.codehaus.groovy.runtime.memoize.*`, `MemoizedASTTransformationTest`, 
`gls.closures.*`, `groovy.transform.stc.*` and 
`org.codehaus.groovy.classgen.asm.sc.*` pass. The Checkstyle and CodeNarc gates 
pass.
   




> Closure wrappers (trampoline, memoize, curry, compose, asWritable) report the 
> wrong delegate or thisObject and ignore delegate and resolveStrategy changes
> ----------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12673
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12673
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.33, 5.0.8, 7.0.0-alpha-1, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> class D { def m() { 'delegate' } }
> class O {
>     def m() { 'owner' }
>     def run() {
>         def c = { x -> m() }
>         assert c.curry(1).thisObject.is(this)        // fails: null (also for 
> c >> {...}, and c.asWritable().getThisObject())
>         def t = c.trampoline()
>         assert t.thisObject.is(this)                 // fails: the delegate 
> of c
>         t.resolveStrategy = Closure.DELEGATE_FIRST
>         t.delegate = new D()
>         assert t(1) == 'delegate'                    // fails: 'owner'
>         def m = { x -> m() }.memoize()
>         m.resolveStrategy = Closure.DELEGATE_FIRST
>         m.delegate = new D()
>         assert m(1) == 'delegate'                    // fails: 'owner'
>     }
> }
> new O().run()
> {code}
> *Actual:* curried and composed closures report a null thisObject. A writable 
> closure's getThisObject() returns null, so the property read under 
> @CompileStatic gives null too. A trampolined closure reports the wrapped 
> closure's delegate as its thisObject and its owner as its delegate. A 
> memoized closure reports the owner as its delegate and null as its 
> thisObject. Setting delegate or resolveStrategy on a trampolined or memoized 
> closure has no effect on the call. {{c.asWritable().trampoline()}} also 
> reports one Object[] parameter instead of the parameters of c.
> *Expected:* a wrapper reports the delegate, resolve strategy and thisObject 
> of the closure it wraps, and setting delegate or resolveStrategy on it 
> applies to that closure, as ComposedClosure and CurriedClosure already do for 
> delegate and resolveStrategy.
> Cause: TrampolineClosure passes original.getDelegate() as thisObject, and 
> neither TrampolineClosure nor Memoize.MemoizeFunction forwards the delegate 
> and resolve strategy. CurriedClosure, ComposedClosure and WritableClosure do 
> not forward getThisObject. TrampolineClosure reads the wrapped closure's 
> parameter fields instead of its getters.
> Verified on 4.0.33, 5.0.8, 6.0.0 and master.



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

Reply via email to