Java Code Examples for org.graalvm.polyglot.Value#getArraySize()

The following examples show how to use org.graalvm.polyglot.Value#getArraySize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCanReadSomeDeviceProperties() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        for (int i = 0; i < devices.getArraySize(); ++i) {
            Value device = devices.getArrayElement(i);
            Value prop = device.getMember("properties");
            // Sanity tests on some of the properties
            // device name is a non-zero string
            assertTrue(prop.getMember("deviceName").asString().length() > 0);

            // compute capability is at least compute Kepler (3.0)
            assertTrue(prop.getMember("computeCapabilityMajor").asInt() >= 3);

            // there is at least one multiprocessors
            assertTrue(prop.getMember("multiProcessorCount").asInt() > 0);

            // there is some device memory
            assertTrue(prop.getMember("totalDeviceMemory").asLong() > 0L);
        }
    }
}
 
Example 2
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCanSelectDevice() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        if (devices.getArraySize() > 1) {
            Value firstDevice = devices.getArrayElement(0);
            Value secondDevice = devices.getArrayElement(1);
            secondDevice.invokeMember("setCurrent");
            assertFalse(firstDevice.invokeMember("isCurrent").asBoolean());
            assertTrue(secondDevice.invokeMember("isCurrent").asBoolean());

            firstDevice.invokeMember("setCurrent");
            assertTrue(firstDevice.invokeMember("isCurrent").asBoolean());
            assertFalse(secondDevice.invokeMember("isCurrent").asBoolean());
        } else {
            // only one device available
            Value device = devices.getArrayElement(0);
            device.invokeMember("setCurrent");
            assertTrue(device.invokeMember("isCurrent").asBoolean());
        }
    }
}
 
Example 3
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGetDevicesMatchesAllGetDevice() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        Value getDevice = ctx.eval("grcuda", "getdevice");
        for (int i = 0; i < devices.getArraySize(); ++i) {
            Value deviceFromArray = devices.getArrayElement(i);
            Value deviceFromFunction = getDevice.execute(i);
            assertEquals(i, deviceFromArray.getMember("id").asInt());
            assertEquals(i, deviceFromFunction.getMember("id").asInt());
        }
    }
}
 
Example 4
Source File: GraalScriptRunner.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively convert a JavaScript array to a list
 * @param arr the array to convert
 * @return the list
 */
private static List<Object> convertArray(Value arr) {
    List<Object> l = new ArrayList<>();
    for (int i = 0; i < arr.getArraySize(); ++i) {
        Value v = arr.getArrayElement(i);
        Object o = convert(v);
        l.add(o);
    }
    return l;
}
 
Example 5
Source File: PolyglotValuesConverter.java    From crate with Apache License 2.0 5 votes vote down vote up
static Object toCrateObject(Value value, DataType<?> type) {
    if (value == null) {
        return null;
    }
    switch (type.id()) {
        case ArrayType.ID:
            ArrayList<Object> items = new ArrayList<>((int) value.getArraySize());
            for (int idx = 0; idx < value.getArraySize(); idx++) {
                var item = toCrateObject(value.getArrayElement(idx), ((ArrayType) type).innerType());
                items.add(idx, item);
            }
            return type.value(items);
        case ObjectType.ID:
            return type.value(value.as(MAP_TYPE_LITERAL));
        case GeoPointType.ID:
            if (value.hasArrayElements()) {
                return type.value(toCrateObject(value, DataTypes.DOUBLE_ARRAY));
            } else {
                return type.value(value.asString());
            }
        case GeoShapeType.ID:
            if (value.isString()) {
                return type.value(value.asString());
            } else {
                return type.value(value.as(MAP_TYPE_LITERAL));
            }
        default:
            final Object polyglotValue;
            if (value.isNumber()) {
                polyglotValue = value.as(NUMBER_TYPE_LITERAL);
            } else if (value.isString()) {
                polyglotValue = value.asString();
            } else if (value.isBoolean()) {
                polyglotValue = value.asBoolean();
            } else {
                polyglotValue = value.asString();
            }
            return type.value(polyglotValue);
    }
}