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"));
}
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);
}
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());
}
}
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);
}
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);
}
Aggregations