Search in sources :

Example 1 with ProxyArray

use of org.graalvm.polyglot.proxy.ProxyArray in project graal by oracle.

the class ProxySPITest method testArrayProxy.

@Test
public void testArrayProxy() throws Throwable {
    Context context = Context.create();
    final int size = 42;
    ProxyArray proxyOuter = new ProxyArray() {

        int[] array = new int[size];

        {
            Arrays.fill(array, 42);
        }

        public Object get(long index) {
            return array[(int) index];
        }

        public void set(long index, Value value) {
            array[(int) index] = value.asInt();
        }

        public long getSize() {
            return size;
        }
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertEquals(size, Message.GET_SIZE, proxyInner);
        for (int i = 0; i < size; i++) {
            assertEquals(42, Message.READ, proxyInner, i);
        }
        for (int i = 0; i < size; i++) {
            assertEquals(41, Message.WRITE, proxyInner, i, 41);
        }
        for (int i = 0; i < size; i++) {
            assertEquals(41, Message.READ, proxyInner, i);
        }
        assertUnknownIdentifier(Message.READ, proxyInner, 42);
        assertUnknownIdentifier(Message.READ, proxyInner, -1);
        assertUnknownIdentifier(Message.READ, proxyInner, Integer.MAX_VALUE);
        assertUnknownIdentifier(Message.READ, proxyInner, Integer.MIN_VALUE);
        assertEquals(true, Message.HAS_SIZE, proxyInner);
        assertEquals(EXISTING_KEY, Message.KEY_INFO, proxyInner, 41);
        assertEquals(NO_KEY, Message.KEY_INFO, proxyInner, 42);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertEquals(false, Message.HAS_KEYS, proxyInner);
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner, "");
        assertUnsupported(Message.WRITE, proxyInner, "");
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.UNBOX, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.createNew(0), proxyInner);
        assertEquals(false, Message.IS_BOXED, proxyInner);
        assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 2 with ProxyArray

use of org.graalvm.polyglot.proxy.ProxyArray in project graal by oracle.

the class ValueAPITest method testObjectCoercion.

@SuppressWarnings("unchecked")
@Test
public void testObjectCoercion() {
    objectCoercionTest(null, Object.class, null);
    objectCoercionTest(null, String.class, null);
    objectCoercionTest(null, Boolean.class, null);
    objectCoercionTest(null, Boolean.class, null);
    objectCoercionTest(true, Boolean.class, null);
    objectCoercionTest("foo", String.class, null);
    objectCoercionTest('c', Character.class, null);
    objectCoercionTest((byte) 42, Byte.class, null);
    objectCoercionTest((short) 42, Short.class, null);
    objectCoercionTest(42, Integer.class, null);
    objectCoercionTest((long) 42, Long.class, null);
    objectCoercionTest(42.5d, Double.class, null);
    objectCoercionTest(42.5f, Float.class, null);
    objectCoercionTest(new JavaClass(), JavaClass.class, null);
    objectCoercionTest(new JavaClass(), JavaSuperClass.class, null);
    objectCoercionTest(new JavaClass(), JavaInterface.class, null);
    objectCoercionTest(new JavaClass(), JavaFunctionalInterface.class, null);
    objectCoercionTest(new JavaClass(), Object.class, null);
    Map<String, Object> map = new HashMap<>();
    map.put("foobar", "baz");
    map.put("foobar2", "baz2");
    objectCoercionTest(ProxyObject.fromMap(map), Map.class, (v) -> assertEquals("baz", v.get("foobar")));
    objectCoercionTest(ProxyObject.fromMap(map), Map.class, (v) -> {
        assertNull(v.remove("notAMember"));
        assertEquals("baz", v.remove("foobar"));
        assertFalse(v.remove("foobar2", "baz"));
        assertFalse(v.remove("foobar", "baz2"));
        assertTrue(v.remove("foobar2", "baz2"));
        assertTrue(map.isEmpty());
        map.put("foobar", "baz");
        map.put("foobar2", "baz2");
    });
    ProxyArray array = ProxyArray.fromArray(42, 42, 42);
    objectCoercionTest(array, List.class, (v) -> assertEquals(42, v.get(2)));
    List<Object> arrayList = new ArrayList<>();
    ProxyArray list = ProxyArray.fromList(arrayList);
    objectCoercionTest(list, List.class, (v) -> {
        arrayList.addAll(Arrays.asList(41, 42));
        assertEquals(42, v.remove(1));
        assertTrue(v.remove((Object) 41));
        assertFalse(v.remove((Object) 14));
    });
    ArrayElements arrayElements = new ArrayElements();
    arrayElements.array.add(42);
    objectCoercionTest(arrayElements, List.class, (v) -> {
        assertEquals(42, v.get(0));
        assertEquals(1, v.size());
        assertFalse(v instanceof Function);
        Value value = context.asValue(v);
        assertFalse(value.canExecute());
        assertFalse(value.canInstantiate());
        assertFalse(value.hasMembers());
        assertTrue(value.hasArrayElements());
    });
    MembersAndArray mapAndArray = new MembersAndArray();
    mapAndArray.map.put("foo", "bar");
    mapAndArray.array.add(42);
    objectCoercionTest(mapAndArray, Map.class, (v) -> {
        assertNull(v.get(0L));
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertFalse(v instanceof Function);
        Value value = context.asValue(v);
        assertFalse(value.canExecute());
        assertFalse(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertTrue(value.hasArrayElements());
    });
    MembersAndInstantiable membersAndInstantiable = new MembersAndInstantiable();
    membersAndInstantiable.map.put("foo", "bar");
    membersAndInstantiable.instantiableResult = "foobarbaz";
    objectCoercionTest(membersAndInstantiable, Map.class, (v) -> {
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertTrue(v instanceof Function);
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertFalse(value.canExecute());
        assertTrue(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertFalse(value.hasArrayElements());
    });
    MembersAndExecutable membersAndExecutable = new MembersAndExecutable();
    membersAndExecutable.map.put("foo", "bar");
    membersAndExecutable.executableResult = "foobarbaz";
    objectCoercionTest(membersAndExecutable, Map.class, (v) -> {
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertTrue(v instanceof Function);
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertTrue(value.canExecute());
        assertFalse(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertFalse(value.hasArrayElements());
    });
    MembersAndArrayAndExecutable mapAndArrayAndExecutable = new MembersAndArrayAndExecutable();
    mapAndArrayAndExecutable.map.put("foo", "bar");
    mapAndArrayAndExecutable.array.add(42);
    mapAndArrayAndExecutable.executableResult = "foobarbaz";
    objectCoercionTest(mapAndArrayAndExecutable, Map.class, (v) -> {
        assertNull(v.get(0L));
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertTrue(v instanceof Function);
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertTrue(value.canExecute());
        assertFalse(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertTrue(value.hasArrayElements());
    });
    MembersAndArrayAndInstantiable mapAndArrayAndInstantiable = new MembersAndArrayAndInstantiable();
    mapAndArrayAndInstantiable.map.put("foo", "bar");
    mapAndArrayAndInstantiable.array.add(42);
    mapAndArrayAndInstantiable.instantiableResult = "foobarbaz";
    objectCoercionTest(mapAndArrayAndInstantiable, Map.class, (v) -> {
        assertNull(v.get(0L));
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertTrue(v instanceof Function);
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertFalse(value.canExecute());
        assertTrue(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertTrue(value.hasArrayElements());
    });
    MembersAndArrayAndExecutableAndInstantiable mapAndArrayAndExecutableAndInstantiable = new MembersAndArrayAndExecutableAndInstantiable();
    mapAndArrayAndExecutableAndInstantiable.map.put("foo", "bar");
    mapAndArrayAndExecutableAndInstantiable.array.add(42);
    mapAndArrayAndExecutableAndInstantiable.executableResult = "foobarbaz";
    objectCoercionTest(mapAndArrayAndExecutableAndInstantiable, Map.class, (v) -> {
        assertNull(v.get(0L));
        assertEquals("bar", v.get("foo"));
        assertEquals(1, v.size());
        assertTrue(v instanceof Function);
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertTrue(value.canExecute());
        assertTrue(value.canInstantiate());
        assertTrue(value.hasMembers());
        assertTrue(value.hasArrayElements());
    });
    Executable exectable = new Executable();
    exectable.executableResult = "foobarbaz";
    objectCoercionTest(exectable, Function.class, (v) -> {
        assertEquals("foobarbaz", ((Function<Object, Object>) v).apply(new Object[0]));
        Value value = context.asValue(v);
        assertTrue(value.canExecute());
        assertFalse(value.canInstantiate());
        assertFalse(value.hasMembers());
        assertFalse(value.hasArrayElements());
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Function(java.util.function.Function) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Test(org.junit.Test)

Example 3 with ProxyArray

use of org.graalvm.polyglot.proxy.ProxyArray in project graal by oracle.

the class ProxySPITest method testArrayElementRemove.

@Test
public void testArrayElementRemove() throws Throwable {
    Context context = Context.create();
    final int size = 42;
    ArrayList<Object> list = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        list.add(i);
    }
    ProxyArray proxyOuter = ProxyArray.fromList(list);
    eval(context, proxyOuter, (proxyInner) -> {
        assertEquals(size, Message.GET_SIZE, proxyInner);
        assertEquals(true, Message.REMOVE, proxyInner, 10);
        assertEquals(size - 1, Message.GET_SIZE, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) ArrayList(java.util.ArrayList) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 4 with ProxyArray

use of org.graalvm.polyglot.proxy.ProxyArray in project sulong by graalvm.

the class LLVMInteropTest method testScaleVector.

@Test
public void testScaleVector() {
    try (Runner runner = new Runner("scaleVector")) {
        runner.load();
        Value fn = runner.findGlobalSymbol("scale_vector");
        ProxyArray proxy = ProxyArray.fromArray(1.0, 2.0, 3.0, 4.0, 5.0);
        fn.execute(proxy, proxy.getSize(), 0.1);
        for (int i = 0; i < proxy.getSize(); i++) {
            double expected = 0.1 * (i + 1);
            Value actual = (Value) proxy.get(i);
            Assert.assertEquals("index " + i, expected, actual.asDouble(), 0.0001);
        }
    }
}
Also used : ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Value(org.graalvm.polyglot.Value) BoxedTestValue(com.oracle.truffle.llvm.test.interop.values.BoxedTestValue) Test(org.junit.Test)

Aggregations

ProxyArray (org.graalvm.polyglot.proxy.ProxyArray)4 Test (org.junit.Test)4 Value (org.graalvm.polyglot.Value)3 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)2 ArrayList (java.util.ArrayList)2 Context (org.graalvm.polyglot.Context)2 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)1 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)1 ProxyNativeObject (org.graalvm.polyglot.proxy.ProxyNativeObject)1