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) {
}
}
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));
}
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();
}
}
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;
}
}
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);
}
Aggregations