Search in sources :

Example 11 with MethodHandle

use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.

the class DefBootstrapTests method testNoNullGuardAddWhenCached.

public void testNoNullGuardAddWhenCached() throws Throwable {
    DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "add", MethodType.methodType(Object.class, int.class, Object.class), 0, DefBootstrap.BINARY_OPERATOR, 0);
    MethodHandle handle = site.dynamicInvoker();
    assertEquals(2, (Object) handle.invokeExact(1, (Object) 1));
    expectThrows(NullPointerException.class, () -> {
        assertNotNull((Object) handle.invokeExact(5, (Object) null));
    });
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 12 with MethodHandle

use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.

the class DefBootstrapTests method testNullGuardAddWhenCached.

public void testNullGuardAddWhenCached() throws Throwable {
    DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "add", MethodType.methodType(Object.class, Object.class, Object.class), 0, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL);
    MethodHandle handle = site.dynamicInvoker();
    assertEquals(2, (Object) handle.invokeExact((Object) 1, (Object) 1));
    assertEquals("nulltest", (Object) handle.invokeExact((Object) null, (Object) "test"));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 13 with MethodHandle

use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.

the class DefBootstrapTests method testMegamorphic.

/** test that we revert to the megamorphic classvalue cache and that it works as expected */
public void testMegamorphic() throws Throwable {
    DefBootstrap.PIC site = (DefBootstrap.PIC) DefBootstrap.bootstrap(MethodHandles.publicLookup(), "size", MethodType.methodType(int.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
    // mark megamorphic
    site.depth = DefBootstrap.PIC.MAX_DEPTH;
    MethodHandle handle = site.dynamicInvoker();
    assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("1", "2")));
    assertEquals(1, (int) handle.invokeExact((Object) Collections.singletonMap("a", "b")));
    assertEquals(3, (int) handle.invokeExact((Object) Arrays.asList("x", "y", "z")));
    assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("u", "v")));
    final HashMap<String, String> map = new HashMap<String, String>();
    map.put("x", "y");
    map.put("a", "b");
    assertEquals(2, (int) handle.invokeExact((Object) map));
    final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
        Integer.toString((int) handle.invokeExact(new Object()));
    });
    assertEquals("Unable to find dynamic method [size] with [0] arguments for class [java.lang.Object].", iae.getMessage());
    assertTrue("Does not fail inside ClassValue.computeValue()", Arrays.stream(iae.getStackTrace()).anyMatch(e -> {
        return e.getMethodName().equals("computeValue") && e.getClassName().startsWith("org.elasticsearch.painless.DefBootstrap$PIC$");
    }));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) MethodType(java.lang.invoke.MethodType) CallSite(java.lang.invoke.CallSite) MethodHandles(java.lang.invoke.MethodHandles) HashMap(java.util.HashMap) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) HashMap(java.util.HashMap) MethodHandle(java.lang.invoke.MethodHandle)

Example 14 with MethodHandle

use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.

the class DefBootstrapTests method testOneType.

/** calls toString() on integers, twice */
public void testOneType() throws Throwable {
    CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(), "toString", MethodType.methodType(String.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
    MethodHandle handle = site.dynamicInvoker();
    assertDepthEquals(site, 0);
    // invoke with integer, needs lookup
    assertEquals("5", (String) handle.invokeExact((Object) 5));
    assertDepthEquals(site, 1);
    // invoked with integer again: should be cached
    assertEquals("6", (String) handle.invokeExact((Object) 6));
    assertDepthEquals(site, 1);
}
Also used : CallSite(java.lang.invoke.CallSite) MethodHandle(java.lang.invoke.MethodHandle)

Example 15 with MethodHandle

use of java.lang.invoke.MethodHandle in project elasticsearch by elastic.

the class DefBootstrapTests method testTooManyTypes.

public void testTooManyTypes() throws Throwable {
    // if this changes, test must be rewritten
    assertEquals(5, DefBootstrap.PIC.MAX_DEPTH);
    CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(), "toString", MethodType.methodType(String.class, Object.class), 0, DefBootstrap.METHOD_CALL, "");
    MethodHandle handle = site.dynamicInvoker();
    assertDepthEquals(site, 0);
    assertEquals("5", (String) handle.invokeExact((Object) 5));
    assertDepthEquals(site, 1);
    assertEquals("1.5", (String) handle.invokeExact((Object) 1.5f));
    assertDepthEquals(site, 2);
    assertEquals("6", (String) handle.invokeExact((Object) 6L));
    assertDepthEquals(site, 3);
    assertEquals("3.2", (String) handle.invokeExact((Object) 3.2d));
    assertDepthEquals(site, 4);
    assertEquals("foo", (String) handle.invokeExact((Object) "foo"));
    assertDepthEquals(site, 5);
    assertEquals("c", (String) handle.invokeExact((Object) 'c'));
    assertDepthEquals(site, 5);
}
Also used : CallSite(java.lang.invoke.CallSite) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)300 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)43 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)12 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6