Search in sources :

Example 26 with Value

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

the class ConstraintInteropTypesTest method forbidNonPrimitiveObjectReturn.

@Test(expected = PolyglotException.class)
public void forbidNonPrimitiveObjectReturn() {
    BrokenTruffleObject obj = new BrokenTruffleObject(this);
    polyglot.putMember("value", obj);
    Value result = polyglot.getMember("value").execute();
    fail("No result, an exception should be thrown: " + result);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 27 with Value

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

the class ConstraintInteropTypesTest method forbidStringBuilderReturn.

@Test(expected = PolyglotException.class)
public void forbidStringBuilderReturn() {
    BrokenTruffleObject obj = new BrokenTruffleObject(new StringBuilder("I am string builder!"));
    polyglot.putMember("value", obj);
    Value result = polyglot.getMember("value").execute();
    fail("No result, an exception should be thrown: " + result);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 28 with Value

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

the class ValueAPITest method testTypeCoercionError.

@Test
public void testTypeCoercionError() {
    Value pipe = context.asValue("not a pipe");
    assertFails(() -> pipe.as(List.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.List': Unsupported target type.");
    assertFails(() -> pipe.as(Map.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.Map': Unsupported target type.");
    assertFails(() -> pipe.as(Function.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.function.Function': Unsupported target type.");
    assertFails(() -> pipe.as(JavaInterface.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'com.oracle.truffle.api.test.polyglot.ValueAPITest$JavaInterface': Unsupported target type.");
    Value other = context.asValue(new EmptyProxy());
    assertFails(() -> other.as(List.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.List': Value must have array elements.");
    assertFails(() -> other.as(Map.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.Map': Value must have members or array elements.");
    assertFails(() -> other.as(Function.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.function.Function': Value must be executable or instantiable.");
    assertFails(() -> other.as(JavaInterface.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'com.oracle.truffle.api.test.polyglot.ValueAPITest$JavaInterface': Value must have members.");
}
Also used : Function(java.util.function.Function) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with Value

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

the class ValueAPITest method objectCoercionTest.

@SuppressWarnings({ "unchecked" })
private <T> void objectCoercionTest(Object value, Class<T> expectedType, Consumer<T> validator) {
    Value coerce = context.asValue(new CoerceObject()).getMember("coerce");
    T result = (T) context.asValue(value).as(Object.class);
    if (result != null) {
        assertTrue("expected " + expectedType + " but was " + result.getClass(), expectedType.isInstance(result));
    } else if (value != null) {
        fail("expected " + expectedType + " but was null");
    }
    if (validator == null) {
        assertEquals(value, result);
    } else {
        validator.accept(result);
        coerce.execute(value, validator);
    }
    assertValue(context, context.asValue(value));
    assertValue(context, context.asValue(result));
}
Also used : HOST_OBJECT(com.oracle.truffle.api.test.polyglot.ValueAssert.Trait.HOST_OBJECT) PROXY_OBJECT(com.oracle.truffle.api.test.polyglot.ValueAssert.Trait.PROXY_OBJECT) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject)

Example 30 with Value

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

the class ValueAPITest method testNullCoercionErrors.

@Test
public void testNullCoercionErrors() {
    Value nullValue = context.asValue(null);
    assertFails(() -> nullValue.asInt(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'int' using Value.asInt(). " + "You can ensure that the operation is supported using Value.fitsInInt().");
    assertFails(() -> nullValue.as(int.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'int'.");
    assertFails(() -> nullValue.asByte(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'byte' using Value.asByte(). " + "You can ensure that the operation is supported using Value.fitsInByte().");
    assertFails(() -> nullValue.as(byte.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'byte'.");
    assertFails(() -> nullValue.asShort(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'short' using Value.asShort(). " + "You can ensure that the operation is supported using Value.fitsInShort().");
    assertFails(() -> nullValue.as(short.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'short'.");
    assertFails(() -> nullValue.asLong(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'long' using Value.asLong(). " + "You can ensure that the operation is supported using Value.fitsInLong().");
    assertFails(() -> nullValue.as(long.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'long'.");
    assertFails(() -> nullValue.asFloat(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'float' using Value.asFloat(). " + "You can ensure that the operation is supported using Value.fitsInFloat().");
    assertFails(() -> nullValue.as(float.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'float'.");
    assertFails(() -> nullValue.asDouble(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'double' using Value.asDouble(). " + "You can ensure that the operation is supported using Value.fitsInDouble().");
    assertFails(() -> nullValue.as(double.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'double'.");
    assertFails(() -> nullValue.as(char.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'char'.");
    assertFails(() -> nullValue.asBoolean(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'boolean' using Value.asBoolean(). " + "You can ensure that the operation is supported using Value.isBoolean().");
    assertFails(() -> nullValue.as(boolean.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'boolean'.");
}
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