Inserting pre and post-call logic around a handle?
John Rose
john.r.rose at oracle.com
Thu May 19 13:53:57 PDT 2011
On May 19, 2011, at 12:53 PM, Charles Oliver Nutter wrote:
> I have cases where I want to do some pre and post-call logic around a
> target handle. Basically, I need to twiddle some global state before
> and after to set up one of our artificial call frames for the target
> method to use. How could I do this in handles?
>
> The logic basically looks like this:
>
> preLogic(some of incoming args);
> try {
> return callSomeDirectTargetHandle();
> } finally {
> postLogic(some args);
> }
>
> The structure I figured would work best is something like this:
>
> foldArguments(preLogic)
>> catchException(Throwable)
> =>body:direct MH to method
> =>handler:foldArguments(postLogic)
>
> fold seems to be the only way to insert some arbitrary logic, but it
> has the side effect of trying to insert its result back into the
> argument list. My pre/post logic has a void return, so I'd need to
> wrap that logic with an additional call, I think.
If you do a fold with a void return, nothing gets inserted into the argument list.
The doc for this needs to be fixed, but the implementation should work now.
Your sketch seems good, except that you cannot handle both the return value and the thrown exception in one place. (API limitation.) So if your post logic wants to see the return value, you have more work to do.
Suggestion: Make the direct MH be a free variable, by doing the fancy transform on MethodHandles.exactInvoker(dmh.type()). That is, instead of mh = T(dmh); mh.invoke(a*); do inv = T(exactInvoker(dmh.type())); if (?) inv.invokeExact(dmh, a*) else { mh = inv.bindTo(dmh); mh.invoke(a*); }.
-- John
More information about the mlvm-dev
mailing list