Search in sources :

Example 11 with Shape

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

the class LocationTest method testDeleteDeclaredProperty.

@Test
public void testDeleteDeclaredProperty() {
    DynamicObject object = rootShape.newInstance();
    object.define("a", new Object(), 0, new LocationFactory() {

        public Location createLocation(Shape shape, Object value) {
            return shape.allocator().declaredLocation(value);
        }
    });
    Assert.assertTrue(object.containsKey("a"));
    object.define("a", 42);
    Assert.assertEquals(1, object.getShape().getPropertyCount());
    object.delete("a");
    Assert.assertFalse(object.containsKey("a"));
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) DynamicObject(com.oracle.truffle.api.object.DynamicObject) LocationFactory(com.oracle.truffle.api.object.LocationFactory) Location(com.oracle.truffle.api.object.Location) ObjectLocation(com.oracle.truffle.api.object.ObjectLocation) Test(org.junit.Test)

Example 12 with Shape

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

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

the class DynamicObjectFactoryTest method testFactory.

@Test
public void testFactory() {
    Shape shape = rootShape.defineProperty("x", 1, 0).defineProperty("y", 2, 0);
    DynamicObjectFactory factory = shape.createFactory();
    try {
        factory.newInstance();
        Assert.fail();
    } catch (AssertionError e) {
        Assert.assertEquals("0 arguments given but the factory takes 2: x, y", e.getMessage());
    }
    try {
        factory.newInstance("only one argument");
        Assert.fail();
    } catch (AssertionError e) {
        Assert.assertEquals("1 arguments given but the factory takes 2: x, y", e.getMessage());
    }
    DynamicObject object = factory.newInstance(3, 4);
    Assert.assertEquals(3, object.get("x"));
    Assert.assertEquals(4, object.get("y"));
    try {
        factory.newInstance(1, 2, 3);
        Assert.fail();
    } catch (AssertionError e) {
        Assert.assertEquals("3 arguments given but the factory takes 2: x, y", e.getMessage());
    }
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObjectFactory(com.oracle.truffle.api.object.DynamicObjectFactory) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Test(org.junit.Test)

Example 14 with Shape

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

the class ShapeProfiler method dump.

public void dump(PrintWriter out) {
    ShapeStats globalStats = new ShapeStats("Cumulative results for all shapes");
    for (DynamicObject obj : queue) {
        Shape shape = obj.getShape();
        globalStats.profile(shape);
    }
    globalStats.dump(out);
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject)

Example 15 with Shape

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

the class ShapeProfiler method dump.

public void dump(PrintWriter out, int topResults) {
    if (topResults > 0) {
        IdentityHashMap<Shape, ShapeStats> shapeMap = new IdentityHashMap<>();
        for (DynamicObject obj : queue) {
            Shape shape = obj.getShape();
            ShapeStats stats = shapeMap.get(shape);
            if (stats == null) {
                shapeMap.put(shape, stats = new ShapeStats(createLabel(shape)));
            }
            stats.profile(shape);
        }
        List<ShapeStats> allStats = new ArrayList<>(shapeMap.values());
        Collections.sort(allStats, new Comparator<ShapeStats>() {

            public int compare(ShapeStats a, ShapeStats b) {
                return Long.compare(b.objects, a.objects);
            }
        });
        int top = Math.min(topResults, allStats.size());
        ShapeStats avgStats = new ShapeStats("Cumulative results for top " + top + " shapes");
        for (int i = 0; i < top; i++) {
            ShapeStats stats = allStats.get(i);
            stats.setLabel("Shape " + (i + 1) + ": " + stats.getLabel());
            stats.dump(out);
            avgStats.add(stats);
        }
        avgStats.dump(out);
    }
    // Dump also cumulative results.
    dump(out);
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList)

Aggregations

Shape (com.oracle.truffle.api.object.Shape)18 DynamicObject (com.oracle.truffle.api.object.DynamicObject)16 Test (org.junit.Test)15 Location (com.oracle.truffle.api.object.Location)7 ObjectType (com.oracle.truffle.api.object.ObjectType)7 Layout (com.oracle.truffle.api.object.Layout)5 DefaultLayoutFactory (com.oracle.truffle.object.basic.DefaultLayoutFactory)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 DynamicObjectFactory (com.oracle.truffle.api.object.DynamicObjectFactory)1 LocationFactory (com.oracle.truffle.api.object.LocationFactory)1 ObjectLocation (com.oracle.truffle.api.object.ObjectLocation)1 Property (com.oracle.truffle.api.object.Property)1 ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1