Search in sources :

Example 1 with Layout

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

the class ObjectTypeTest method objectTypeRespondsToIsArray.

@Test
public void objectTypeRespondsToIsArray() {
    final Layout layout = Layout.newLayout().build();
    final Shape rootShape = layout.createShape(OBJECT_TYPE);
    final DynamicObject obj = rootShape.newInstance();
    final boolean is = JavaInteropTest.isArray(obj);
    assertFalse("It is not array", is);
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Layout(com.oracle.truffle.api.object.Layout) Test(org.junit.Test)

Example 2 with Layout

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

the class ImplicitCastTest method testLocationDecoratorEquals.

@Test
public void testLocationDecoratorEquals() {
    Layout defaultLayout = new DefaultLayoutFactory().createLayout(Layout.newLayout());
    Shape defaultRootShape = defaultLayout.createShape(new ObjectType());
    Shape implicitCastRootShape = layout.createShape(new ObjectType());
    DynamicObject object1 = implicitCastRootShape.newInstance();
    object1.define("a", otherVal);
    Location location1 = object1.getShape().getProperty("a").getLocation();
    // Location of "a" should not change if an Integer is set
    object1.set("a", intVal);
    Assert.assertEquals(location1, object1.getShape().getProperty("a").getLocation());
    DynamicObject object2 = defaultRootShape.newInstance();
    object2.define("a", otherVal);
    Location location2 = object2.getShape().getProperty("a").getLocation();
    // This test relies on the assumption that both locations are of the same class
    Assert.assertEquals(location1.getClass(), location2.getClass());
    Assert.assertNotEquals(location1, location2);
}
Also used : ObjectType(com.oracle.truffle.api.object.ObjectType) Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Layout(com.oracle.truffle.api.object.Layout) DefaultLayoutFactory(com.oracle.truffle.object.basic.DefaultLayoutFactory) Location(com.oracle.truffle.api.object.Location) Test(org.junit.Test)

Example 3 with Layout

use of com.oracle.truffle.api.object.Layout 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 4 with Layout

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

the class ShapeTest method testToString.

@Test
public void testToString() {
    Layout layout = new DefaultLayoutFactory().createLayout(Layout.newLayout().addAllowedImplicitCast(ImplicitCast.IntToLong));
    Shape rootShape = layout.createShape(new ObjectType());
    DOTestAsserts.assertShape("{}", rootShape);
    Shape aInt = rootShape.defineProperty("a", 1, 0);
    DOTestAsserts.assertShape("{\"a\":int@0" + "\n}", aInt);
    Shape aObj = aInt.defineProperty("a", new Object(), 0);
    DOTestAsserts.assertShape("{\"a\":Object@0" + "\n}", aObj);
    Shape aObjBInt = aObj.defineProperty("b", 2, 0);
    DOTestAsserts.assertShape("{" + "\"b\":int@1,\n" + "\"a\":Object@0" + "\n}", aObjBInt);
    Shape aIntBObj = aInt.defineProperty("b", new Object(), 0);
    DOTestAsserts.assertShape("{" + "\"b\":Object@0,\n" + "\"a\":int@0" + "\n}", aIntBObj);
    Shape bool = rootShape.addProperty(Property.create("bool", rootShape.allocator().locationForType(boolean.class), 0));
    DOTestAsserts.assertShape("{\"bool\":boolean@0\n}", bool);
    Shape str = rootShape.addProperty(Property.create("str", rootShape.allocator().locationForType(String.class), 0));
    DOTestAsserts.assertShape("{\"str\":Object@0\n}", str);
    Shape shapeWithExtArray = aIntBObj.defineProperty("c", true, 0).defineProperty("d", 3.14, 0).defineProperty("e", 1L << 44, 0);
    DOTestAsserts.assertShape("{" + "\"e\":long[0],\n" + "\"d\":double@2,\n" + "\"c\":boolean@1,\n" + "\"b\":Object@0,\n" + "\"a\":int@0" + "\n}", shapeWithExtArray);
}
Also used : ObjectType(com.oracle.truffle.api.object.ObjectType) Shape(com.oracle.truffle.api.object.Shape) Layout(com.oracle.truffle.api.object.Layout) DefaultLayoutFactory(com.oracle.truffle.object.basic.DefaultLayoutFactory) Test(org.junit.Test)

Example 5 with Layout

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

the class LayoutGenerator method generateAllocator.

private void generateAllocator(final PrintStream stream) {
    if (layout.getSuperLayout() == null) {
        stream.print("    protected static final Layout LAYOUT = Layout.newLayout()");
        for (Layout.ImplicitCast implicitCast : layout.getImplicitCasts()) {
            stream.print(".addAllowedImplicitCast(Layout.ImplicitCast.");
            stream.print(implicitCast.name());
            stream.print(")");
        }
        stream.println(".build();");
        stream.printf("    protected static final Shape.Allocator %S_ALLOCATOR = LAYOUT.createAllocator();%n", NameUtils.identifierToConstant(layout.getName()));
    } else {
        stream.printf("    protected static final Shape.Allocator %S_ALLOCATOR = LAYOUT.createAllocator();%n", NameUtils.identifierToConstant(layout.getName()));
        stream.println("    ");
        if (layout.getSuperLayout().hasInstanceProperties()) {
            stream.println("    static {");
            for (PropertyModel property : layout.getSuperLayout().getAllInstanceProperties()) {
                final List<String> modifiers = new ArrayList<>();
                if (!property.isNullable()) {
                    modifiers.add("LocationModifier.NonNull");
                }
                if (property.isFinal()) {
                    modifiers.add("LocationModifier.Final");
                }
                final String modifiersExpression;
                if (modifiers.isEmpty()) {
                    modifiersExpression = "";
                } else {
                    final StringBuilder modifiersExpressionBuilder = new StringBuilder();
                    modifiersExpressionBuilder.append(", EnumSet.of(");
                    for (String modifier : modifiers) {
                        if (!modifier.equals(modifiers.get(0))) {
                            modifiersExpressionBuilder.append(", ");
                        }
                        modifiersExpressionBuilder.append(modifier);
                    }
                    modifiersExpressionBuilder.append(")");
                    modifiersExpression = modifiersExpressionBuilder.toString();
                }
                final String locationType;
                if (property.isVolatile()) {
                    if (property.getType().getKind() == TypeKind.INT) {
                        locationType = "AtomicInteger";
                    } else if (property.getType().getKind() == TypeKind.BOOLEAN) {
                        locationType = "AtomicBoolean";
                    } else {
                        locationType = "AtomicReference";
                    }
                } else {
                    locationType = NameUtils.typeWithoutParameters(property.getType().toString());
                }
                stream.printf("         %s_ALLOCATOR.locationForType(%s.class%s);%n", NameUtils.identifierToConstant(layout.getName()), locationType, modifiersExpression);
            }
            stream.println("    }");
            stream.println("    ");
        }
    }
}
Also used : Layout(com.oracle.truffle.api.object.Layout) PropertyModel(com.oracle.truffle.object.dsl.processor.model.PropertyModel) ArrayList(java.util.ArrayList)

Aggregations

Layout (com.oracle.truffle.api.object.Layout)7 Test (org.junit.Test)6 Shape (com.oracle.truffle.api.object.Shape)5 DynamicObject (com.oracle.truffle.api.object.DynamicObject)4 ObjectType (com.oracle.truffle.api.object.ObjectType)2 DefaultLayoutFactory (com.oracle.truffle.object.basic.DefaultLayoutFactory)2 Location (com.oracle.truffle.api.object.Location)1 Property (com.oracle.truffle.api.object.Property)1 PropertyMap (com.oracle.truffle.object.PropertyMap)1 PropertyModel (com.oracle.truffle.object.dsl.processor.model.PropertyModel)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1