Search in sources :

Example 56 with DynamicObject

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));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Location(com.oracle.truffle.api.object.Location) Test(org.junit.Test)

Example 57 with DynamicObject

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"));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Location(com.oracle.truffle.api.object.Location) Test(org.junit.Test)

Example 58 with DynamicObject

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"));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Test(org.junit.Test)

Example 59 with DynamicObject

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"));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) IncompatibleLocationException(com.oracle.truffle.api.object.IncompatibleLocationException) DynamicObject(com.oracle.truffle.api.object.DynamicObject) FinalLocationException(com.oracle.truffle.api.object.FinalLocationException) Property(com.oracle.truffle.api.object.Property) Test(org.junit.Test)

Example 60 with DynamicObject

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"));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) IncompatibleLocationException(com.oracle.truffle.api.object.IncompatibleLocationException) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Property(com.oracle.truffle.api.object.Property) Test(org.junit.Test)

Aggregations

DynamicObject (com.oracle.truffle.api.object.DynamicObject)100 Test (org.junit.Test)91 Location (com.oracle.truffle.api.object.Location)17 Shape (com.oracle.truffle.api.object.Shape)16 DynamicObjectFactory (com.oracle.truffle.api.object.DynamicObjectFactory)8 ObjectLocation (com.oracle.truffle.api.object.ObjectLocation)7 Property (com.oracle.truffle.api.object.Property)7 ObjectType (com.oracle.truffle.api.object.ObjectType)6 IncompatibleLocationException (com.oracle.truffle.api.object.IncompatibleLocationException)4 Layout (com.oracle.truffle.api.object.Layout)4 FinalLocationException (com.oracle.truffle.api.object.FinalLocationException)2 PropertyBuilder (com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)2 VariableElement (javax.lang.model.element.VariableElement)2 LocationFactory (com.oracle.truffle.api.object.LocationFactory)1 ShapeImpl (com.oracle.truffle.object.ShapeImpl)1 DefaultLayoutFactory (com.oracle.truffle.object.basic.DefaultLayoutFactory)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1