use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.
the class NumericNFITest method testPingPong.
@Test
public void testPingPong(@Inject(TestPingPongNode.class) CallTarget callTarget) {
TruffleObject wrap = new TestCallback(1, (args) -> {
Assert.assertThat("argument", args[0], is(instanceOf(TruffleObject.class)));
TruffleObject fn = (TruffleObject) args[0];
TruffleObject wrapped = new TestCallback(1, (innerArgs) -> {
checkExpectedArg(6, innerArgs[0]);
try {
return ForeignAccess.sendExecute(Message.createExecute(1).createNode(), fn, unboxNumber(innerArgs[0]) * 3);
} catch (InteropException ex) {
throw new AssertionError(ex);
}
});
return wrapped;
});
Object ret = callTarget.call(wrap, 5);
checkExpectedRet(38, ret);
}
use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.
the class NumericNFITest method checkExpected.
private void checkExpected(String thing, long expected, Object arg) {
Object value = arg;
switch(type) {
case UINT8:
case SINT8:
Assert.assertThat(thing + " type", value, is(instanceOf(Byte.class)));
break;
case UINT16:
case SINT16:
Assert.assertThat(thing + " type", value, is(instanceOf(Short.class)));
break;
case UINT32:
case SINT32:
Assert.assertThat(thing + " type", value, is(instanceOf(Integer.class)));
break;
case UINT64:
case SINT64:
Assert.assertThat(thing + " type", value, is(instanceOf(Long.class)));
break;
case FLOAT:
Assert.assertThat(thing + " type", value, is(instanceOf(Float.class)));
break;
case DOUBLE:
Assert.assertThat(thing + " type", value, is(instanceOf(Double.class)));
break;
case POINTER:
Assert.assertThat(thing + " type", value, is(instanceOf(TruffleObject.class)));
TruffleObject obj = (TruffleObject) value;
Assert.assertTrue(thing + " is boxed", isBoxed(obj));
value = unbox(obj);
Assert.assertThat("unboxed " + thing, value, is(instanceOf(Long.class)));
break;
default:
Assert.fail();
}
Assert.assertEquals(expected, ((Number) value).longValue());
}
use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.
the class ObjectNFITest method initEnv.
@BeforeClass
public static void initEnv() {
TruffleObject createNewObject = new TestCallback(0, (args) -> new TestObject());
TruffleObject readIntField = new TestCallback(2, (args) -> {
Assert.assertThat("args[0]", args[0], is(instanceOf(TestObject.class)));
Assert.assertThat("args[1]", args[1], is(instanceOf(String.class)));
return ((TestObject) args[0]).readField((String) args[1]);
});
TruffleObject writeIntField = new TestCallback(3, (args) -> {
Assert.assertThat("args[0]", args[0], is(instanceOf(TestObject.class)));
Assert.assertThat("args[1]", args[1], is(instanceOf(String.class)));
Assert.assertThat("args[2]", args[2], is(instanceOf(Integer.class)));
((TestObject) args[0]).writeField((String) args[1], (Integer) args[2]);
return null;
});
TruffleObject initializeAPI = lookupAndBind("initialize_api", "( env, ():object, (object,string):sint32, (object,string,sint32):void ) : pointer");
try {
nativeAPI = (TruffleObject) ForeignAccess.sendExecute(Message.createExecute(3).createNode(), initializeAPI, createNewObject, readIntField, writeIntField);
} catch (InteropException ex) {
throw new AssertionError(ex);
}
}
use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.
the class StringNFITest method testStringCallback.
private static void testStringCallback(CallTarget target, Object callbackRet) {
TruffleObject strArgCallback = new TestCallback(1, (args) -> {
Assert.assertEquals("string argument", "Hello, Truffle!", args[0]);
return 42;
});
TruffleObject strRetCallback = new TestCallback(0, (args) -> {
return callbackRet;
});
Object ret = target.call(strArgCallback, strRetCallback);
Assert.assertThat("return value", ret, is(instanceOf(Integer.class)));
Assert.assertEquals("return value", 42, (int) (Integer) ret);
}
use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.
the class StringNFITest method testNativeStringCallback.
@Test
public void testNativeStringCallback(@Inject(NativeStringCallbackNode.class) CallTarget target) {
Object ret = target.call();
Assert.assertThat("return value", ret, is(instanceOf(TruffleObject.class)));
TruffleObject obj = (TruffleObject) ret;
Assert.assertTrue("isBoxed", isBoxed(obj));
Assert.assertEquals("return value", "same", unbox(obj));
}
Aggregations