Search in sources :

Example 1 with Property

use of com.oracle.truffle.api.object.Property in project graal by oracle.

the class PropertyMapTest method testPropertyMap.

@Test
public void testPropertyMap() {
    PropertyMap map = PropertyMap.empty();
    Map<Object, Property> referenceMap = new LinkedHashMap<>();
    Random rnd = new Random();
    final int size = 1000;
    int[] randomSequence = rnd.ints().limit(size).toArray();
    int[] shuffledSequence = randomSequence.clone();
    shuffle(shuffledSequence, rnd);
    Layout layout = Layout.newLayout().build();
    // fill the map
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
        assertEqualsOrdered(referenceMap, map);
    }
    // put the same values again, should not modify the map
    PropertyMap initial = map;
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        assertSame(initial, map);
    }
    assertEqualsOrdered(referenceMap, map);
    // update existing values
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (double) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    for (int i = size - 1; i >= 0; i--) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (double) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    // update existing values, in random order
    for (int i = 0; i < size; i++) {
        int id = shuffledSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (long) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    // remove keys
    for (int i = size - 10; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
    for (int i = 10; i >= 0; i--) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
    for (int i = 0; i < size; i++) {
        int id = shuffledSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
}
Also used : PropertyMap(com.oracle.truffle.object.PropertyMap) Random(java.util.Random) Layout(com.oracle.truffle.api.object.Layout) Property(com.oracle.truffle.api.object.Property) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with Property

use of com.oracle.truffle.api.object.Property in project graal by oracle.

the class LayoutStrategy method generalizeProperty.

/**
 * @since 0.17 or earlier
 */
protected ShapeImpl generalizeProperty(Property oldProperty, Object value, ShapeImpl currentShape, ShapeImpl nextShape) {
    Location oldLocation = oldProperty.getLocation();
    Location newLocation = currentShape.allocator().locationForValueUpcast(value, oldLocation);
    Property newProperty = oldProperty.relocate(newLocation);
    ShapeImpl newShape = nextShape.replaceProperty(oldProperty, newProperty);
    return newShape;
}
Also used : Property(com.oracle.truffle.api.object.Property) Location(com.oracle.truffle.api.object.Location) DeclaredLocation(com.oracle.truffle.object.Locations.DeclaredLocation)

Example 3 with Property

use of com.oracle.truffle.api.object.Property in project graal by oracle.

the class LayoutStrategy method propertySetWithShapeFallback.

/**
 * @since 0.17 or earlier
 */
protected void propertySetWithShapeFallback(Property property, DynamicObject store, Object value, ShapeImpl currentShape, ShapeImpl nextShape) {
    ShapeImpl oldShape = currentShape;
    ShapeImpl newNextShape = generalizeProperty(property, value, oldShape, nextShape);
    Property newProperty = newNextShape.getProperty(property.getKey());
    newProperty.setSafe(store, value, oldShape, newNextShape);
}
Also used : Property(com.oracle.truffle.api.object.Property)

Example 4 with Property

use of com.oracle.truffle.api.object.Property in project graal by oracle.

the class LayoutStrategy method defineProperty.

/**
 * @since 0.17 or earlier
 */
protected ShapeImpl defineProperty(ShapeImpl oldShape, Object key, Object value, int flags, LocationFactory locationFactory, Property existing) {
    if (existing == null) {
        Property property = Property.create(key, locationFactory.createLocation(oldShape, value), flags);
        ShapeImpl newShape = oldShape.addProperty(property);
        return newShape;
    } else {
        if (existing.getFlags() == flags) {
            if (existing.getLocation().canSet(value)) {
                return oldShape;
            } else {
                return definePropertyGeneralize(oldShape, existing, value, locationFactory);
            }
        } else {
            return definePropertyChangeFlags(oldShape, existing, value, flags);
        }
    }
}
Also used : Property(com.oracle.truffle.api.object.Property)

Example 5 with Property

use of com.oracle.truffle.api.object.Property in project graal by oracle.

the class LayoutStrategy method definePropertyChangeFlags.

/**
 * @since 0.17 or earlier
 */
protected ShapeImpl definePropertyChangeFlags(ShapeImpl oldShape, Property oldProperty, Object value, int flags) {
    Location oldLocation = oldProperty.getLocation();
    Location newLocation;
    if (oldLocation.canSet(value)) {
        newLocation = oldLocation;
    } else {
        newLocation = oldShape.allocator().locationForValueUpcast(value, oldLocation);
    }
    Property newProperty = Property.create(oldProperty.getKey(), newLocation, flags);
    ShapeImpl newShape = oldShape.replaceProperty(oldProperty, newProperty);
    return newShape;
}
Also used : Property(com.oracle.truffle.api.object.Property) Location(com.oracle.truffle.api.object.Location) DeclaredLocation(com.oracle.truffle.object.Locations.DeclaredLocation)

Aggregations

Property (com.oracle.truffle.api.object.Property)31 DynamicObject (com.oracle.truffle.api.object.DynamicObject)8 Test (org.junit.Test)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 IncompatibleLocationException (com.oracle.truffle.api.object.IncompatibleLocationException)4 Location (com.oracle.truffle.api.object.Location)4 ArrayDeque (java.util.ArrayDeque)3 FinalLocationException (com.oracle.truffle.api.object.FinalLocationException)2 ObjectLocation (com.oracle.truffle.api.object.ObjectLocation)2 DeclaredLocation (com.oracle.truffle.object.Locations.DeclaredLocation)2 Layout (com.oracle.truffle.api.object.Layout)1 Shape (com.oracle.truffle.api.object.Shape)1 ValueLocation (com.oracle.truffle.object.Locations.ValueLocation)1 PropertyMap (com.oracle.truffle.object.PropertyMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1