Search in sources :

Example 26 with TruffleObject

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);
}
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 27 with TruffleObject

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

Example 28 with TruffleObject

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);
    }
}
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 29 with TruffleObject

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

Example 30 with TruffleObject

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

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)201 Test (org.junit.Test)135 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)34 InteropException (com.oracle.truffle.api.interop.InteropException)18 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)17 Specialization (com.oracle.truffle.api.dsl.Specialization)16 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)14 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)12 ArrayTruffleObject (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest.ArrayTruffleObject)10 Node (com.oracle.truffle.api.nodes.Node)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 CallTarget (com.oracle.truffle.api.CallTarget)7 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)7 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)7 LinkedHashMap (java.util.LinkedHashMap)7 Source (com.oracle.truffle.api.source.Source)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5