Search in sources :

Example 36 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueAPITest method testList.

@Test
public void testList() {
    List<String> list = new ArrayList<>();
    list.add("foo");
    list.add("bar");
    Value v = context.asValue(list);
    assertTrue(v.hasArrayElements());
    assertTrue(v.hasMembers());
    assertEquals("foo", v.getArrayElement(0).asString());
    assertEquals("bar", v.getArrayElement(1).asString());
    ValueAssert.assertFails(() -> v.getArrayElement(2), ArrayIndexOutOfBoundsException.class);
    // append to the list
    v.setArrayElement(2, "baz");
    assertEquals("foo", v.getArrayElement(0).asString());
    assertEquals("bar", v.getArrayElement(1).asString());
    assertEquals("baz", v.getArrayElement(2).asString());
    assertTrue(v.removeArrayElement(1));
    assertEquals("foo", v.getArrayElement(0).asString());
    assertEquals("baz", v.getArrayElement(1).asString());
    assertTrue(v.removeArrayElement(0));
    assertEquals("baz", v.getArrayElement(0).asString());
    assertTrue(v.removeArrayElement(0));
    assertTrue(v.getArraySize() == 0);
}
Also used : ArrayList(java.util.ArrayList) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 37 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueAssert method assertValueArrayElements.

@SuppressWarnings("unchecked")
private static void assertValueArrayElements(Context context, Value value) {
    assertTrue(value.hasArrayElements());
    List<Object> receivedObjects = new ArrayList<>();
    Map<Long, Object> receivedObjectsLongMap = new HashMap<>();
    Map<Integer, Object> receivedObjectsIntMap = new HashMap<>();
    for (long i = 0L; i < value.getArraySize(); i++) {
        Value arrayElement = value.getArrayElement(i);
        receivedObjects.add(arrayElement.as(Object.class));
        receivedObjectsLongMap.put(i, arrayElement.as(Object.class));
        receivedObjectsIntMap.put((int) i, arrayElement.as(Object.class));
        assertValue(context, arrayElement);
    }
    List<Object> objectList1 = value.as(OBJECT_LIST);
    List<Object> objectList2 = Arrays.asList(value.as(Object[].class));
    assertEquals(receivedObjects, objectList1);
    assertEquals(receivedObjects, objectList2);
    if (value.hasMembers()) {
        Map<Object, Object> objectMap1 = value.as(OBJECT_OBJECT_MAP);
        assertTrue(objectMap1.keySet().equals(value.getMemberKeys()));
    } else {
        assertFails(() -> value.as(OBJECT_OBJECT_MAP), ClassCastException.class);
    }
    Map<Long, Object> objectMap2 = value.as(LONG_OBJECT_MAP);
    Map<Integer, Object> objectMap3 = value.as(INTEGER_OBJECT_MAP);
    Map<Number, Object> objectMap4 = value.as(NUMBER_OBJECT_MAP);
    assertFails(() -> value.as(SHORT_OBJECT_MAP), ClassCastException.class);
    assertFails(() -> value.as(BYTE_OBJECT_MAP), ClassCastException.class);
    assertFails(() -> value.as(FLOAT_OBJECT_MAP), ClassCastException.class);
    assertFails(() -> value.as(DOUBLE_OBJECT_MAP), ClassCastException.class);
    assertEquals(receivedObjectsLongMap, objectMap2);
    assertEquals(receivedObjectsIntMap, objectMap3);
    assertEquals(receivedObjectsLongMap, objectMap4);
    if (value.isHostObject() && !value.isProxyObject()) {
        assertTrue(value.as(Object.class) instanceof List || value.as(Object.class).getClass().isArray());
    } else if (!value.hasMembers()) {
        List<Object> objectMap5 = (List<Object>) value.as(Object.class);
        assertEquals(receivedObjects, objectMap5);
    }
    // write them all
    for (long i = 0L; i < value.getArraySize(); i++) {
        value.setArrayElement(i, value.getArrayElement(i));
    }
    for (int i = 0; i < value.getArraySize(); i++) {
        objectList1.set(i, receivedObjects.get(i));
        objectList2.set(i, receivedObjects.get(i));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) ArrayList(java.util.ArrayList) List(java.util.List)

Example 38 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testProxyIdentityRestore.

@Test
public void testProxyIdentityRestore() {
    Proxy proxy = new Proxy() {
    };
    Value value = context.asValue(proxy);
    assertUnsupported(value, PROXY_OBJECT);
    assertSame(proxy, context.asValue(value.as(Object.class)).asProxyObject());
}
Also used : Proxy(org.graalvm.polyglot.proxy.Proxy) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 39 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testExceptionFrames2.

@Test
public void testExceptionFrames2() {
    Value value = context.asValue(new TestExceptionFrames2());
    try {
        value.getMember("foo").execute();
        Assert.fail();
    } catch (PolyglotException e) {
        assertTrue(e.isHostException());
        assertTrue(e.asHostException() instanceof RuntimeException);
        assertEquals("foo", e.getMessage());
        Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
        StackFrame frame;
        frame = frameIterator.next();
        assertTrue(frame.isHostFrame());
        assertEquals("foo", frame.toHostFrame().getMethodName());
        frame = frameIterator.next();
        assertTrue(frame.isHostFrame());
        assertEquals("execute", frame.toHostFrame().getMethodName());
        frame = frameIterator.next();
        assertTrue(frame.isHostFrame());
        assertEquals("testExceptionFrames2", frame.toHostFrame().getMethodName());
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 40 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ValueHostConversionTest method testNull.

@Test
public void testNull() {
    Value v = context.asValue(null);
    assertTrue(v.isNull());
    assertTrue(v.isHostObject());
    assertUnsupported(v, NULL, HOST_OBJECT);
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9