Search in sources :

Example 31 with Value

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

the class ValueAPITest method testPrimitiveCoercionErrors.

@Test
public void testPrimitiveCoercionErrors() {
    Value bigNumber = context.asValue(Long.MAX_VALUE);
    assertFails(() -> bigNumber.asByte(), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'byte' using Value.asByte(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInByte().");
    assertFails(() -> bigNumber.as(byte.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'byte': Invalid or lossy primitive coercion.");
    assertFails(() -> bigNumber.as(Byte.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'java.lang.Byte': Invalid or lossy primitive coercion.");
    assertFails(() -> bigNumber.asShort(), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'short' using Value.asShort(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInShort().");
    assertFails(() -> bigNumber.as(short.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'short': Invalid or lossy primitive coercion.");
    assertFails(() -> bigNumber.as(Short.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'java.lang.Short': Invalid or lossy primitive coercion.");
    assertFails(() -> bigNumber.asInt(), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'int' using Value.asInt(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInInt().");
    assertFails(() -> bigNumber.as(int.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'int': Invalid or lossy primitive coercion.");
    assertFails(() -> bigNumber.as(Integer.class), ClassCastException.class, "Cannot convert '9223372036854775807'(language: Java, type: java.lang.Long) to Java type 'java.lang.Integer': Invalid or lossy primitive coercion.");
    Value nan = context.asValue(Double.NaN);
    assertFails(() -> nan.asLong(), ClassCastException.class, "Cannot convert 'NaN'(language: Java, type: java.lang.Double) to Java type 'long' using Value.asLong(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInLong().");
    assertFails(() -> nan.as(long.class), ClassCastException.class, "Cannot convert 'NaN'(language: Java, type: java.lang.Double) to Java type 'long': Invalid or lossy primitive coercion.");
    assertFails(() -> nan.as(Long.class), ClassCastException.class, "Cannot convert 'NaN'(language: Java, type: java.lang.Double) to Java type 'java.lang.Long': Invalid or lossy primitive coercion.");
    Value nofloat = context.asValue(Double.MAX_VALUE);
    assertFails(() -> nofloat.asFloat(), ClassCastException.class, "Cannot convert '1.7976931348623157E308'(language: Java, type: java.lang.Double) to Java type 'float' using Value.asFloat(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInFloat().");
    assertFails(() -> nofloat.as(float.class), ClassCastException.class, "Cannot convert '1.7976931348623157E308'(language: Java, type: java.lang.Double) to Java type 'float': Invalid or lossy primitive coercion.");
    assertFails(() -> nofloat.as(Float.class), ClassCastException.class, "Cannot convert '1.7976931348623157E308'(language: Java, type: java.lang.Double) to Java type 'java.lang.Float': Invalid or lossy primitive coercion.");
    Value nodouble = context.asValue(9007199254740992L);
    assertFails(() -> nodouble.asDouble(), ClassCastException.class, "Cannot convert '9007199254740992'(language: Java, type: java.lang.Long) to Java type 'double' using Value.asDouble(): Invalid or lossy primitive coercion. " + "You can ensure that the value can be converted using Value.fitsInDouble().");
    assertFails(() -> nodouble.as(double.class), ClassCastException.class, "Cannot convert '9007199254740992'(language: Java, type: java.lang.Long) to Java type 'double': Invalid or lossy primitive coercion.");
    assertFails(() -> nodouble.as(Double.class), ClassCastException.class, "Cannot convert '9007199254740992'(language: Java, type: java.lang.Long) to Java type 'java.lang.Double': Invalid or lossy primitive coercion.");
    Value noString = context.asValue(false);
    assertFails(() -> noString.asString(), ClassCastException.class, "Cannot convert 'false'(language: Java, type: java.lang.Boolean) to Java type 'java.lang.String' using Value.asString(): Invalid coercion. You can ensure that the value can be converted using Value.isString().");
    assertFails(() -> noString.as(char.class), ClassCastException.class, "Cannot convert 'false'(language: Java, type: java.lang.Boolean) to Java type 'char': Invalid or lossy primitive coercion.");
    assertFails(() -> noString.as(String.class), ClassCastException.class, "Cannot convert 'false'(language: Java, type: java.lang.Boolean) to Java type 'java.lang.String': Invalid or lossy primitive coercion.");
    Value noBoolean = context.asValue("foobar");
    assertFails(() -> noBoolean.asBoolean(), ClassCastException.class, "Cannot convert 'foobar'(language: Java, type: java.lang.String) to Java type 'boolean' using Value.asBoolean(): Invalid or lossy primitive coercion. You can ensure that the value can be converted using Value.isBoolean().");
    assertFails(() -> noBoolean.as(boolean.class), ClassCastException.class, "Cannot convert 'foobar'(language: Java, type: java.lang.String) to Java type 'boolean': Invalid or lossy primitive coercion.");
    assertFails(() -> noBoolean.as(Boolean.class), ClassCastException.class, "Cannot convert 'foobar'(language: Java, type: java.lang.String) to Java type 'java.lang.Boolean': Invalid or lossy primitive coercion.");
}
Also used : BigInteger(java.math.BigInteger) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 32 with Value

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

the class ValueAPITest method testInstantiate.

@Test
public void testInstantiate() {
    Value classValue = context.asValue(java.util.Date.class);
    Value dateInstance = classValue.newInstance();
    Date date = dateInstance.asHostObject();
    assertNotNull(date);
    long ms = 1_000_000_000L;
    dateInstance = classValue.newInstance(ms);
    date = dateInstance.asHostObject();
    assertEquals(ms, date.getTime());
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Date(java.util.Date) Test(org.junit.Test)

Example 33 with Value

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

the class ValueAPITest method testMemberErrors.

@Test
public void testMemberErrors() {
    Value noMembers = context.asValue("");
    assertFails(() -> noMembers.getMember(""), UnsupportedOperationException.class, "Unsupported operation Value.getMember(String) for ''(language: Java, type: java.lang.String). You can ensure that the operation is supported using Value.hasMembers().");
    assertFails(() -> noMembers.putMember("", null), UnsupportedOperationException.class, "Unsupported operation Value.putMember(String, Object) for ''(language: Java, type: java.lang.String). You can ensure that the operation is supported using Value.hasMembers().");
    assertFails(() -> noMembers.removeMember(""), UnsupportedOperationException.class, "Unsupported operation Value.removeMember(String, Object) for ''(language: Java, type: java.lang.String).");
    assertEquals(0, noMembers.getMemberKeys().size());
    assertFalse(noMembers.hasMembers());
    assertFalse(noMembers.hasMember(""));
    assertFalse(noMembers.getMemberKeys().contains(""));
    MemberErrorTest test = new MemberErrorTest();
    Value v = context.asValue(test);
    assertEquals(43, v.getMember("value").asInt());
    v.putMember("value", 42);
    assertEquals(42, v.getMember("value").asInt());
    assertFails(() -> v.putMember("value", ""), IllegalArgumentException.class, "Invalid member value ''(language: Java, type: java.lang.String) for object 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest) and member key 'value'.");
    assertFails(() -> v.putMember("finalValue", 42), IllegalArgumentException.class, "Invalid member key 'finalValue' for object 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest).");
    assertFails(() -> v.putMember("notAMember", ""), IllegalArgumentException.class, "Invalid member key 'notAMember' for object 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest).");
    assertNull(v.getMember("notAMember"));
    assertFalse(v.removeMember("notAMember"));
    @SuppressWarnings("unchecked") Map<Object, Object> map = v.as(Map.class);
    // maps behave slightly differently for not existing keys
    assertNull(map.get("notAMember"));
    map.put("value", 43);
    assertEquals(43, map.get("value"));
    assertFails(() -> map.put(new Object(), ""), IllegalArgumentException.class, "Illegal identifier type 'java.lang.Object' for Map<Object, Object> 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest).");
    Map<String, String> stringMap = v.as(STRING_MAP);
    assertFails(() -> stringMap.get("value"), ClassCastException.class, "Cannot convert '43'(language: Java, type: java.lang.Integer) to Java type 'java.lang.String': Invalid or lossy primitive coercion.");
    assertFails(() -> map.put("value", ""), ClassCastException.class, "Invalid value ''(language: Java, type: java.lang.String) for Map<Object, Object> 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest) and identifier 'value'.");
    assertFails(() -> map.put("finalValue", 42), IllegalArgumentException.class, "Invalid or unmodifiable value for identifier 'finalValue' for Map<Object, Object> 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest).");
    assertFails(() -> map.put("finalValue", "42"), ClassCastException.class, "Invalid value '42'(language: Java, type: java.lang.String) for Map<Object, Object> 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest) and identifier 'finalValue'.");
    assertFails(() -> map.put("notAMember", ""), IllegalArgumentException.class, "Invalid or unmodifiable value for identifier 'notAMember' for Map<Object, Object> 'MemberErrorTest'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$MemberErrorTest).");
    Map<Object, Object> rmap = new HashMap<>();
    rmap.put("value", 43);
    Value rv = context.asValue(rmap);
    assertFalse(rv.removeMember("notAMember"));
}
Also used : HashMap(java.util.HashMap) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 34 with Value

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

the class ValueAPITest method testArrayErrors.

@Test
@SuppressWarnings("unchecked")
public void testArrayErrors() {
    String[] array = new String[] { "asdf" };
    Value v = context.asValue(array);
    assertFails(() -> v.getArrayElement(1L), IndexOutOfBoundsException.class, "Invalid array index 1 for array '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> v.setArrayElement(1L, null), IndexOutOfBoundsException.class, "Invalid array index 1 for array '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> v.removeArrayElement(0), UnsupportedOperationException.class, "Unsupported operation Value.removeArrayElement(long, Object) for '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFalse(v.removeMember("a"));
    List<Object> list = v.as(List.class);
    assertFails(() -> list.get(1), IndexOutOfBoundsException.class, "Invalid index 1 for List<Object> '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> list.set(1, null), IndexOutOfBoundsException.class, "Invalid index 1 for List<Object> '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> list.remove(0), UnsupportedOperationException.class, "Unsupported operation remove for List<Object> '[asdf]'(language: Java, type: java.lang.String[]).");
    List<?> stringList = v.as(STRING_LIST);
    assertFails(() -> stringList.get(1), IndexOutOfBoundsException.class, "Invalid index 1 for List<java.lang.String> '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> stringList.set(1, null), IndexOutOfBoundsException.class, "Invalid index 1 for List<java.lang.String> '[asdf]'(language: Java, type: java.lang.String[]).");
    assertFails(() -> ((List<Object>) stringList).set(0, 42), ClassCastException.class, "Invalid value '42'(language: Java, type: java.lang.Integer) for List<java.lang.String> '[asdf]'(language: Java, type: java.lang.String[]) and index 0.");
    assertFails(() -> ((List<Object>) stringList).set(0, context.asValue(42)), ClassCastException.class, "Invalid value '42'(language: Java, type: java.lang.Integer) for List<java.lang.String> '[asdf]'(language: Java, type: java.lang.String[]) and index 0.");
    // just to make sure this works
    ((List<Object>) stringList).set(0, context.asValue("foo"));
    List<Integer> integerList = v.as(INTEGER_LIST);
    assertFails(() -> integerList.get(0), ClassCastException.class, "Cannot convert 'foo'(language: Java, type: java.lang.String) to Java type 'java.lang.Integer': Invalid or lossy primitive coercion.");
    Value notAnArray = context.asValue("");
    assertFails(() -> notAnArray.getArrayElement(0), UnsupportedOperationException.class, "Unsupported operation Value.getArrayElement(long) for ''(language: Java, type: java.lang.String). You can ensure that the operation is supported using Value.hasArrayElements().");
    assertFails(() -> notAnArray.setArrayElement(0, null), UnsupportedOperationException.class, "Unsupported operation Value.setArrayElement(long, Object) for ''(language: Java, type: java.lang.String). You can ensure that the operation is supported using Value.hasArrayElements().");
    assertFails(() -> notAnArray.getArraySize(), UnsupportedOperationException.class, "Unsupported operation Value.getArraySize() for ''(language: Java, type: java.lang.String). You can ensure that the operation is supported using Value.hasArrayElements().");
    Value rv = context.asValue(new ArrayList<>(Arrays.asList(new String[] { "a", "b", "c" })));
    assertFails(() -> rv.removeArrayElement(3), IndexOutOfBoundsException.class, "Invalid array index 3 for array '[a, b, c]'(language: Java, type: java.util.ArrayList).");
    assertFalse(v.removeMember("a"));
}
Also used : BigInteger(java.math.BigInteger) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 35 with Value

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

the class ValueAPITest method testExecutableErrors.

@Test
public void testExecutableErrors() {
    ExecutableInterface executable = new ExecutableInterface() {

        public String execute(String argument) {
            return argument;
        }

        @Override
        public String toString() {
            return "testExecutable";
        }
    };
    Value v = context.asValue(executable);
    assertTrue(v.canExecute());
    assertEquals("", v.execute("").as(Object.class));
    assertEquals("", v.execute("").asString());
    assertFails(() -> v.execute("", ""), IllegalArgumentException.class, "Invalid argument count when executing 'testExecutable'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) " + "with arguments [''(language: Java, type: java.lang.String), ''(language: Java, type: java.lang.String)]. Expected 1 argument(s) but got 2.");
    assertFails(() -> v.execute(), IllegalArgumentException.class, "Invalid argument count when executing 'testExecutable'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) with arguments []." + " Expected 1 argument(s) but got 0.");
    assertFails(() -> v.execute(42), IllegalArgumentException.class, "Invalid argument when executing 'testExecutable'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) " + "with arguments ['42'(language: Java, type: java.lang.Integer)].");
    assertFails(() -> context.asValue("").execute(), UnsupportedOperationException.class, "Unsupported operation Value.execute(Object...) for ''(language: Java, type: java.lang.String). You can ensure that the operation " + "is supported using Value.canExecute().");
    assertFails(() -> v.as(OtherInterface0.class).execute(), IllegalArgumentException.class, "Invalid argument count when executing 'testExecutable'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) " + "with arguments []. Expected 1 argument(s) but got 0.");
    assertEquals("", v.as(OtherInterface1.class).execute(""));
    assertFails(() -> v.as(OtherInterface1.class).execute(42), IllegalArgumentException.class, "Invalid argument when executing 'testExecutable'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) " + "with arguments ['42'(language: Java, type: java.lang.Integer)].");
    assertFails(() -> v.as(OtherInterface2.class).execute("", ""), IllegalArgumentException.class, "Invalid argument count when executing 'testExecutable'(language: Java, " + "type: com.oracle.truffle.api.test.polyglot.ValueAPITest$9) with arguments [''(language: Java, type: java.lang.String), " + "''(language: Java, type: java.lang.String)]. Expected 1 argument(s) but got 2.");
    assertSame(executable, v.as(ExecutableInterface.class));
    Value value = context.asValue(new AmbiguousType());
    assertFails(() -> value.getMember("f").execute(1, 2), IllegalArgumentException.class, "Invalid argument when executing 'com.oracle.truffle.api.test.polyglot.ValueAPITest$AmbiguousType.f'" + "(language: Java, type: Bound Method) with arguments ['1'(language: Java, type: java.lang.Integer), " + "'2'(language: Java, type: java.lang.Integer)].");
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) 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