Search in sources :

Example 1 with TestCallback

use of com.oracle.truffle.nfi.test.interop.TestCallback in project graal by oracle.

the class NullNFITest method testNullCallbackArg.

@Test
public void testNullCallbackArg(@Inject(TestNullCallbackArgNode.class) CallTarget callTarget) {
    TruffleObject nullCallback = new TestCallback(1, (args) -> {
        Assert.assertThat("callback argument", args[0], is(instanceOf(TruffleObject.class)));
        Assert.assertTrue("isNull", isNull((TruffleObject) args[0]));
        return null;
    });
    callTarget.call(nullCallback);
}
Also used : TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 2 with TestCallback

use of com.oracle.truffle.nfi.test.interop.TestCallback in project graal by oracle.

the class NullNFITest method testNullCallbackRet.

@Test
public void testNullCallbackRet(@Inject(TestNullCallbackRetNode.class) CallTarget callTarget) {
    String expected = getExpected();
    TruffleObject nullCallback = new TestCallback(0, (args) -> {
        return new NullObject();
    });
    Object ret = callTarget.call(nullCallback);
    Assert.assertThat("return value", ret, is(instanceOf(TruffleObject.class)));
    TruffleObject obj = (TruffleObject) ret;
    Assert.assertTrue("isBoxed", isBoxed(obj));
    Assert.assertEquals("return value", expected, unbox(obj));
}
Also used : NullObject(com.oracle.truffle.nfi.test.interop.NullObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) NullObject(com.oracle.truffle.nfi.test.interop.NullObject) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 3 with TestCallback

use of com.oracle.truffle.nfi.test.interop.TestCallback 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);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 4 with TestCallback

use of com.oracle.truffle.nfi.test.interop.TestCallback 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);
    }
}
Also used : InteropException(com.oracle.truffle.api.interop.InteropException) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) BeforeClass(org.junit.BeforeClass)

Example 5 with TestCallback

use of com.oracle.truffle.nfi.test.interop.TestCallback 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);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)9 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)9 Test (org.junit.Test)7 InteropException (com.oracle.truffle.api.interop.InteropException)2 NullObject (com.oracle.truffle.nfi.test.interop.NullObject)2 BeforeClass (org.junit.BeforeClass)1