Search in sources :

Example 31 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.

the class TestMemberAccess method testIterate.

@Test
public void testIterate() throws InteropException {
    List<Object> l = new ArrayList<>();
    l.add("one");
    l.add("two");
    TruffleObject listObject = JavaInterop.asTruffleObject(l);
    TruffleObject itFunction = (TruffleObject) ForeignAccess.sendRead(readNode, listObject, "iterator");
    TruffleObject it = (TruffleObject) ForeignAccess.sendExecute(executeNode, itFunction);
    TruffleObject hasNextFunction = (TruffleObject) ForeignAccess.sendRead(readNode, it, "hasNext");
    List<Object> returned = new ArrayList<>();
    while ((boolean) ForeignAccess.sendExecute(executeNode, hasNextFunction)) {
        TruffleObject nextFunction = (TruffleObject) ForeignAccess.sendRead(readNode, it, "next");
        Object element = ForeignAccess.sendExecute(executeNode, nextFunction);
        returned.add(element);
    }
    assertEquals(l.size(), returned.size());
    assertEquals(l.get(0), returned.get(0));
    assertEquals(l.get(1), returned.get(1));
}
Also used : ArrayList(java.util.ArrayList) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 32 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.

the class TestMemberAccess method testOverloadedConstructor1.

@Test
public void testOverloadedConstructor1() throws InteropException {
    TruffleObject testClass = JavaInterop.asTruffleObject(TestConstructor.class);
    TruffleObject testObj;
    testObj = (TruffleObject) ForeignAccess.sendNew(newNode, testClass);
    assertEquals(void.class.getName(), ForeignAccess.sendRead(readNode, testObj, "ctor"));
    testObj = (TruffleObject) ForeignAccess.sendNew(newNode, testClass, 42);
    assertEquals(int.class.getName(), ForeignAccess.sendRead(readNode, testObj, "ctor"));
    testObj = (TruffleObject) ForeignAccess.sendNew(newNode, testClass, 4.2f);
    assertEquals(float.class.getName(), ForeignAccess.sendRead(readNode, testObj, "ctor"));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 33 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.

the class TestMemberAccess method testObjectReadIndex.

@Test
public void testObjectReadIndex() throws InteropException {
    TruffleObject arrayObject = JavaInterop.asTruffleObject(new TestClass());
    try {
        ForeignAccess.sendRead(readNode, arrayObject, 0);
        fail();
    } catch (UnsupportedMessageException e) {
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 34 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.

the class TestMemberAccess method testOverloadedConstructor2.

@Test
public void testOverloadedConstructor2() throws InteropException {
    TruffleObject testClass = JavaInterop.asTruffleObject(TestConstructor.class);
    TruffleObject testObj;
    testObj = (TruffleObject) ForeignAccess.sendNew(newNode, testClass, (short) 42);
    assertEquals(int.class.getName(), ForeignAccess.sendRead(readNode, testObj, "ctor"));
    testObj = (TruffleObject) ForeignAccess.sendNew(newNode, testClass, 4.2f);
// TODO prioritize conversion from double to float over double to int
// assertEquals(float.class.getName(), ForeignAccess.sendRead(readNode, testObj, "ctor"));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 35 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project graal by oracle.

the class TestMemberAccess method testNewArray.

@Test
public void testNewArray() throws InteropException {
    TruffleObject arrayClass = JavaInterop.asTruffleObject(Array.class);
    TruffleObject newInstanceMethod = (TruffleObject) ForeignAccess.send(readNode, arrayClass, "newInstance");
    TruffleObject stringArray = (TruffleObject) ForeignAccess.sendExecute(executeNode, newInstanceMethod, JavaInterop.asTruffleObject(String.class), 2);
    assertTrue(JavaInteropTest.isArray(stringArray));
}
Also used : 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