Search in sources :

Example 51 with Value

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

the class ValueHostInteropTest method testNewObject.

@Test
public void testNewObject() {
    Value objectClass = context.asValue(Object.class);
    Value object = objectClass.newInstance();
    assertTrue(object.isHostObject());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 52 with Value

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

the class ValueHostInteropTest method accessAllProperties.

@Test
public void accessAllProperties() {
    Value pojo = context.asValue(new PrivatePOJO());
    Map<?, ?> map = pojo.as(Map.class);
    int cnt = 0;
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        Object key = entry.getKey();
        Object value = entry.getValue();
        assertNotNull(key);
        assertNotNull(value);
        cnt++;
    }
    assertEquals("No properties", 0, cnt);
    assertEquals("Empty: " + map, 0, map.size());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) Test(org.junit.Test)

Example 53 with Value

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

the class ValueHostInteropTest method testException2.

@Test
public void testException2() {
    Value hashMapClass = context.asValue(HashMap.class);
    try {
        hashMapClass.newInstance(-1);
        fail("expected an exception but none was thrown");
    } catch (PolyglotException ex) {
        assertTrue("expected HostException but was: " + ex.getClass(), ex.isHostException());
        assertThat(ex.asHostException(), CoreMatchers.instanceOf(IllegalArgumentException.class));
    }
    try {
        hashMapClass.newInstance("");
        fail("expected an exception but none was thrown");
    } catch (IllegalArgumentException ex) {
    }
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 54 with Value

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

the class ValueHostInteropTest method testRecursiveListMarshalling.

@Test
public void testRecursiveListMarshalling() {
    List<GregorianCalendar> testList = Arrays.asList(new GregorianCalendar());
    Value testListValue = context.asValue(testList);
    assertTrue(testListValue.isHostObject());
    Value calendarValue = testListValue.getArrayElement(0);
    assertTrue(calendarValue.isHostObject());
    assertValue(context, testListValue);
    assertValue(context, calendarValue);
}
Also used : GregorianCalendar(java.util.GregorianCalendar) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 55 with Value

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

the class ValueHostInteropTest method accessAllPublicPropertiesDirectly.

@Test
public void accessAllPublicPropertiesDirectly() {
    final PublicPOJO orig = new PublicPOJO();
    final Value pojo = context.asValue(orig);
    Object[] propertyNames = pojo.getMemberKeys().toArray();
    assertEquals("One instance field and one method", 2, propertyNames.length);
    assertEquals("One field x", "x", propertyNames[0]);
    assertEquals("One method to access x", "readX", propertyNames[1]);
    Value readX = pojo.getMember("readX");
    assertTrue(readX.canExecute());
    orig.writeX(10);
    assertEquals(10, readX.execute().asInt());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) 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