Search in sources :

Example 1 with ReflectionLibrary

use of com.oracle.truffle.api.library.ReflectionLibrary in project graal by oracle.

the class ReflectionTest method testReflectionOnObject.

@Test
public void testReflectionOnObject() throws Exception {
    ReflectiveObject object = new ReflectiveObject();
    ReflectionLibrary reflection = createLibrary(ReflectionLibrary.class, object);
    Message primitive = Message.resolve(ReflectionTestLibrary.class, "primitive");
    // TODO more tests necessary
    Assert.assertEquals(11, (int) reflection.send(object, primitive, 11));
    try {
        reflection.send(object, primitive, new Object());
        Assert.fail();
    } catch (ClassCastException e) {
    }
    try {
        reflection.send(object, primitive, 11, new Object());
        Assert.fail();
    } catch (IllegalArgumentException e) {
    }
}
Also used : Message(com.oracle.truffle.api.library.Message) ExportMessage(com.oracle.truffle.api.library.ExportMessage) ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) Test(org.junit.Test) AbstractParametrizedLibraryTest(com.oracle.truffle.api.test.AbstractParametrizedLibraryTest)

Example 2 with ReflectionLibrary

use of com.oracle.truffle.api.library.ReflectionLibrary in project graal by oracle.

the class ReflectiveCallExample method runExample.

@Test
public void runExample() throws Exception {
    ReflectionLibrary reflection = ReflectionLibrary.getFactory().getUncached();
    Object value = new UnknownObject();
    // reflective lookup of the message.
    // might be a good idea to cache in a singleton.
    Message targetMessage = Message.resolve(ReflectiveCallTestLibrary.class, "message");
    assertEquals("result", reflection.send(value, targetMessage));
}
Also used : ExportMessage(com.oracle.truffle.api.library.ExportMessage) Message(com.oracle.truffle.api.library.Message) ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) Test(org.junit.Test)

Example 3 with ReflectionLibrary

use of com.oracle.truffle.api.library.ReflectionLibrary in project graal by oracle.

the class HostEntryPoint method remoteMessage.

public Object remoteMessage(long contextId, long receiverId, Message message, Object[] args) {
    Context c = unmarshall(Context.class, contextId);
    c.enter();
    try {
        Object receiver = unmarshall(Object.class, receiverId);
        Object[] localValues = unmarshallAtGuest(contextId, args);
        ReflectionLibrary lib = ReflectionLibrary.getFactory().getUncached(receiver);
        Object result;
        try {
            result = lib.send(receiver, message, localValues);
        } catch (Exception e) {
            // probably needs to support TruffleException too, but this is just a sketch
            if (e instanceof AbstractTruffleException) {
                // also send over stack traces and messages
                return new GuestExceptionPointer(guestToHost(e), e.getMessage());
            } else {
                throw new RuntimeException("Internal error thrown by remote message.", e);
            }
        }
        return marshallAtGuest(result);
    } finally {
        c.leave();
    }
}
Also used : Context(org.graalvm.polyglot.Context) ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException)

Example 4 with ReflectionLibrary

use of com.oracle.truffle.api.library.ReflectionLibrary in project graal by oracle.

the class GuestEntryPoint method remoteMessage.

public Object remoteMessage(long contextId, long hostObjectId, Message message, Object[] values) {
    Object receiver = unmarshall(Object.class, hostObjectId);
    Object[] localValues = unmarshallAtHost(contextId, values);
    ReflectionLibrary lib = ReflectionLibrary.getFactory().getUncached(receiver);
    Object result;
    try {
        result = lib.send(receiver, message, localValues);
    } catch (Exception e) {
        throw new UnsupportedOperationException("exceptions not implemented", e);
    }
    if (result instanceof HostGuestValue) {
        throw new UnsupportedOperationException();
    } else if (result instanceof TruffleObject) {
        return new GuestHostValue(null, contextId, hostObjectId);
    } else {
        // send primitives directly
        return result;
    }
}
Also used : ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 5 with ReflectionLibrary

use of com.oracle.truffle.api.library.ReflectionLibrary in project graal by oracle.

the class LibraryAssertionsTest method testAssertion.

@Test
public void testAssertion() throws Exception {
    fooCalls = 0;
    boolean assertsOn = assertionsEnabled();
    int expectedCalls = 0;
    TestLibrary1 lib = createCached(TestLibrary1.class, "");
    if (assertsOn) {
        assertTrue(lib instanceof TestLibrary1Assertions);
        expectedCalls++;
    } else {
        assertFalse(lib instanceof TestLibrary1Assertions);
    }
    assertEquals(42, lib.foo("", 42));
    assertEquals(expectedCalls, fooCalls);
    lib = getUncached(TestLibrary1.class, "");
    if (assertsOn) {
        assertTrue(lib instanceof TestLibrary1Assertions);
        expectedCalls++;
    } else {
        assertFalse(lib instanceof TestLibrary1Assertions);
    }
    assertEquals(42, lib.foo("", 42));
    assertEquals(expectedCalls, fooCalls);
    if (assertsOn) {
        expectedCalls++;
    }
    ReflectionLibrary reflection = createCached(ReflectionLibrary.class, "");
    assertEquals(42, reflection.send("", Message.resolve(TestLibrary1.class, "foo"), 42));
    assertEquals(expectedCalls, fooCalls);
}
Also used : ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) AbstractLibraryTest(com.oracle.truffle.api.test.AbstractLibraryTest) Test(org.junit.Test)

Aggregations

ReflectionLibrary (com.oracle.truffle.api.library.ReflectionLibrary)6 Test (org.junit.Test)4 ExportMessage (com.oracle.truffle.api.library.ExportMessage)3 Message (com.oracle.truffle.api.library.Message)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 AbstractParametrizedLibraryTest (com.oracle.truffle.api.test.AbstractParametrizedLibraryTest)2 AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)1 AbstractLibraryTest (com.oracle.truffle.api.test.AbstractLibraryTest)1 Context (org.graalvm.polyglot.Context)1