Capture scope variables into Lambdas

Juan Fumero juan.fumero at ed.ac.uk
Tue May 24 13:10:27 UTC 2016


Hi all, 
   I would like to do capture variables in lambda expression with
Graal.

I used to do it with Reflection as follow: 

<code>
@Test
 public void testScopeWithReflection() 
    throws IllegalArgumentException, IllegalAccessException {

    final int[] a = new int[]{100, 200, 300};

    Function<Integer, Double> lambdaFunction = (x -> x + 2.0 + a[0]);

    Field[] declaredFields =
lambdaFunction.getClass().getDeclaredFields();
    Object scoped = null;
    for (int i = 0; i < declaredFields.length; i++) {
        declaredFields[i].setAccessible(true);
        scoped = declaredFields[i].get(lambdaFunction);
    }
 
    assertTrue(scoped instanceof int[]);
    assertEquals(a, scoped);
}
</code>

So the output I get is the variables I introduce into the lambda. 
What I would like to do is the same but from the GraalIR instead. 
I do not have the "lambdaFunction.getClass()" in the part of the
compiler I am writing now, only the GraalIR. 

Is it possible to do it in the GraalIR level? Any idea? For each 
variable I introduce in the scope,  I can see a new ParameterNode in
the GraalIR. I can get the type, but I also want the value.

Here a unittest: https://bitbucket.org/snippets/juanfumero/b68KL 

Many thanks

-juan

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the graal-dev mailing list