Search in sources :

Example 61 with TruffleObject

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

the class JavaInteropTest method testHasKeysDefaults.

@Test
public void testHasKeysDefaults() {
    TruffleObject noKeys = new NoKeysObject();
    assertFalse(hasKeys(noKeys));
    TruffleObject keysObj = new DefaultHasKeysObject();
    assertTrue(hasKeys(keysObj));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test) ValueHostInteropTest(com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)

Example 62 with TruffleObject

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

the class JavaInteropTest method testNewObject.

@Test
public void testNewObject() throws InteropException {
    TruffleObject objectClass = JavaInterop.asTruffleObject(Object.class);
    Object object = ForeignAccess.sendNew(Message.createNew(0).createNode(), objectClass);
    assertThat(object, CoreMatchers.instanceOf(TruffleObject.class));
    assertTrue(JavaInterop.isJavaObject(Object.class, (TruffleObject) object));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test) ValueHostInteropTest(com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)

Example 63 with TruffleObject

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

the class AsCollectionsTest method testAsList.

@Test
public void testAsList() {
    List origList = Arrays.asList(new String[] { "a", "b", "c" });
    TruffleObject to = new ListBasedTO(origList);
    assertTrue(JavaInteropTest.isArray(to));
    List interopList = com.oracle.truffle.api.interop.java.JavaInterop.asJavaObject(List.class, to);
    assertEquals(origList.size(), interopList.size());
    assertEquals(origList.toString(), interopList.toString());
    // Test get out of bounds
    try {
        interopList.get(1000);
        fail();
    } catch (IndexOutOfBoundsException ioobex) {
    // O.K.
    }
    // Test set out of bounds
    try {
        interopList.set(1000, "1000");
        fail();
    } catch (IndexOutOfBoundsException ioobex) {
    // O.K.
    }
    Object old = interopList.set(1, "bbb");
    assertEquals("b", old);
    assertEquals("bbb", interopList.get(1));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 64 with TruffleObject

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

the class AsCollectionsTest method testInvokeListOfMapProxy.

@Test
public void testInvokeListOfMapProxy() throws InteropException {
    TruffleObject validator = com.oracle.truffle.api.interop.java.JavaInterop.asTruffleObject(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ProxyValidator.class }, (p, method, args) -> {
        List<Map> listOfMap = (List<Map>) args[0];
        assertEquals(1, listOfMap.size());
        Map map = listOfMap.get(0);
        assertEquals(1, map.size());
        assertEquals("bar", map.get("foo"));
        return true;
    }));
    MapBasedTO mapTO = new MapBasedTO(Collections.singletonMap("foo", "bar"));
    TruffleObject listOfMapTO = new ListBasedTO(Arrays.asList(mapTO));
    assertEquals(Boolean.TRUE, ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), validator, "test", listOfMapTO));
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) InteropException(com.oracle.truffle.api.interop.InteropException) ForeignAccess(com.oracle.truffle.api.interop.ForeignAccess) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) LinkedHashMap(java.util.LinkedHashMap) Resolve(com.oracle.truffle.api.interop.Resolve) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) Map(java.util.Map) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Node(com.oracle.truffle.api.nodes.Node) MessageResolution(com.oracle.truffle.api.interop.MessageResolution) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Message(com.oracle.truffle.api.interop.Message) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 65 with TruffleObject

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

the class AsCollectionsTest method testInvokeMap.

@Test
public void testInvokeMap() throws InteropException {
    TruffleObject validator = com.oracle.truffle.api.interop.java.JavaInterop.asTruffleObject(new Validator());
    MapBasedTO mapTO = new MapBasedTO(Collections.singletonMap("foo", "bar"));
    assertEquals(Boolean.TRUE, ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), validator, "validateMap", mapTO, mapTO));
}
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