use of com.oracle.truffle.api.object.DynamicObject in project graal by oracle.
the class SharedShapeTest method testDifferentLocationsImplicitCast.
@Test
public void testDifferentLocationsImplicitCast() {
DynamicObject object = sharedShape.newInstance();
object.define("a", 1);
Location location1 = object.getShape().getProperty("a").getLocation();
object.define("a", 2L);
Location location2 = object.getShape().getProperty("a").getLocation();
DOTestAsserts.assertNotSameLocation(location1, location2);
Assert.assertEquals(Object.class, getLocationType(location2));
// The old location can still be read
Assert.assertEquals(1, location1.get(object));
Assert.assertEquals(2L, location2.get(object));
}
use of com.oracle.truffle.api.object.DynamicObject in project graal by oracle.
the class SharedShapeTest method testCanReuseLocationsUntilShared.
@Test
public void testCanReuseLocationsUntilShared() {
DynamicObject object = rootShape.newInstance();
object.define("a", 1);
Location locationA1 = object.getShape().getProperty("a").getLocation();
object.define("a", 2L);
Location locationA2 = object.getShape().getProperty("a").getLocation();
DOTestAsserts.assertSameLocation(locationA1, locationA2);
Assert.assertEquals(long.class, getLocationType(locationA2));
DOTestAsserts.assertShape("{\"a\":long@0\n}", object.getShape());
DOTestAsserts.assertShapeFields(object, 1, 0);
// Share object
object.setShapeAndGrow(object.getShape(), object.getShape().makeSharedShape());
object.define("b", 3);
Location locationB1 = object.getShape().getProperty("b").getLocation();
object.define("b", 4L);
Location locationB2 = object.getShape().getProperty("b").getLocation();
object.define("c", 5);
DOTestAsserts.assertNotSameLocation(locationB1, locationB2);
Assert.assertEquals(Object.class, getLocationType(locationB2));
DOTestAsserts.assertShape("{" + "\"c\":int@2,\n" + "\"b\":Object@0,\n" + "\"a\":long@0\n" + "}", object.getShape());
DOTestAsserts.assertShapeFields(object, 3, 1);
Assert.assertEquals(2L, locationA2.get(object));
// The old location can still be read
Assert.assertEquals(3, locationB1.get(object));
Assert.assertEquals(4L, locationB2.get(object));
Assert.assertEquals(5, object.get("c"));
}
use of com.oracle.truffle.api.object.DynamicObject in project graal by oracle.
the class ConstantLocationTest method testMigrateConstantLocation.
@Test
public void testMigrateConstantLocation() {
DynamicObject object = shapeWithConstant.newInstance();
Assert.assertSame(shapeWithConstant, object.getShape());
Assert.assertSame(value, object.get("constant"));
Object newValue = new Object();
object.set("constant", newValue);
Assert.assertNotSame(shapeWithConstant, object.getShape());
Assert.assertSame(newValue, object.get("constant"));
}
use of com.oracle.truffle.api.object.DynamicObject in project graal by oracle.
the class ConstantLocationTest method testConstantLocation.
@Test
public void testConstantLocation() {
DynamicObject object = shapeWithConstant.newInstance();
Assert.assertSame(value, object.get("constant"));
object.set("constant", value);
Assert.assertSame(shapeWithConstant, object.getShape());
Property property = object.getShape().getProperty("constant");
Assert.assertEquals(true, property.getLocation().canStore(value));
Assert.assertEquals(true, property.getLocation().canSet(value));
try {
property.set(object, value, shapeWithConstant);
} catch (IncompatibleLocationException | FinalLocationException e) {
Assert.fail(e.getMessage());
}
Object newValue = new Object();
Assert.assertEquals(false, property.getLocation().canStore(newValue));
Assert.assertEquals(false, property.getLocation().canSet(newValue));
try {
property.set(object, newValue, shapeWithConstant);
Assert.fail();
} catch (FinalLocationException | IncompatibleLocationException e) {
Assert.assertTrue(e instanceof FinalLocationException);
}
Assert.assertSame(value, object.get("constant"));
}
use of com.oracle.truffle.api.object.DynamicObject in project graal by oracle.
the class DeclaredLocationTest method testAddDeclaredLocation.
@Test
public void testAddDeclaredLocation() {
Property property = shapeWithDeclared.getProperty("declared");
DynamicObject object = rootShape.newInstance();
property.setSafe(object, value, rootShape, shapeWithDeclared);
Assert.assertSame(shapeWithDeclared, object.getShape());
Assert.assertSame(value, object.get("declared"));
DynamicObject object2 = rootShape.newInstance();
Object newValue = new Object();
Assert.assertEquals(false, property.getLocation().canStore(newValue));
Assert.assertEquals(false, property.getLocation().canSet(newValue));
try {
property.set(object2, newValue, rootShape, shapeWithDeclared);
Assert.fail();
} catch (IncompatibleLocationException e) {
// Expected
}
Assert.assertSame(rootShape, object2.getShape());
Assert.assertEquals(false, object2.containsKey("declared"));
}
Aggregations