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