Search in sources :

Example 16 with Property

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

the class LocationTest method testChangeFlagsChangeLocation.

@Test
public void testChangeFlagsChangeLocation() {
    DynamicObject object = rootShape.newInstance();
    object.define("foo", 42);
    Location location = object.getShape().getProperty("foo").getLocation();
    object.define("foo", "str", 111);
    Assert.assertEquals("str", object.get("foo"));
    Property newProperty = object.getShape().getProperty("foo");
    Assert.assertEquals(111, newProperty.getFlags());
    Location newLocation = newProperty.getLocation();
    Assert.assertNotSame(location, newLocation);
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Property(com.oracle.truffle.api.object.Property) Location(com.oracle.truffle.api.object.Location) ObjectLocation(com.oracle.truffle.api.object.ObjectLocation) Test(org.junit.Test)

Example 17 with Property

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

the class LocationTest method testChangeFlagsReuseLocation.

@Test
public void testChangeFlagsReuseLocation() {
    DynamicObject object = rootShape.newInstance();
    object.define("foo", 42);
    Location location = object.getShape().getProperty("foo").getLocation();
    object.define("foo", 43, 111);
    Assert.assertEquals(43, object.get("foo"));
    Property newProperty = object.getShape().getProperty("foo");
    Assert.assertEquals(111, newProperty.getFlags());
    Location newLocation = newProperty.getLocation();
    Assert.assertSame(location, newLocation);
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Property(com.oracle.truffle.api.object.Property) Location(com.oracle.truffle.api.object.Location) ObjectLocation(com.oracle.truffle.api.object.ObjectLocation) Test(org.junit.Test)

Example 18 with Property

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

the class ConstantLocationTest method testAddConstantLocation.

@Test
public void testAddConstantLocation() {
    Property property = shapeWithConstant.getProperty("constant");
    DynamicObject object = rootShape.newInstance();
    property.setSafe(object, value, rootShape, shapeWithConstant);
    Assert.assertSame(shapeWithConstant, object.getShape());
    Assert.assertSame(value, object.get("constant"));
    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, shapeWithConstant);
        Assert.fail();
    } catch (IncompatibleLocationException e) {
    // Expected
    }
    Assert.assertSame(rootShape, object2.getShape());
    Assert.assertEquals(false, object2.containsKey("constant"));
}
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)

Example 19 with Property

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

the class DeclaredLocationTest method testDeclaredLocation.

@Test
public void testDeclaredLocation() {
    DynamicObject object = shapeWithDeclared.newInstance();
    Assert.assertSame(value, object.get("declared"));
    object.set("declared", value);
    Assert.assertSame(shapeWithDeclared, object.getShape());
    Property property = object.getShape().getProperty("declared");
    Assert.assertEquals(true, property.getLocation().canStore(value));
    Assert.assertEquals(true, property.getLocation().canSet(value));
    try {
        property.set(object, value, shapeWithDeclared);
    } 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, shapeWithDeclared);
        Assert.fail();
    } catch (FinalLocationException | IncompatibleLocationException e) {
        Assert.assertTrue(e instanceof FinalLocationException);
    }
    Assert.assertSame(value, object.get("declared"));
}
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 20 with Property

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

the class LayoutStrategy method objectDefineProperty.

/**
 * @since 0.17 or earlier
 */
protected void objectDefineProperty(DynamicObjectImpl object, Object key, Object value, int flags, LocationFactory locationFactory, ShapeImpl currentShape) {
    ShapeImpl oldShape = currentShape;
    Property oldProperty = oldShape.getProperty(key);
    ShapeImpl newShape = defineProperty(oldShape, key, value, flags, locationFactory, oldProperty);
    if (oldShape == newShape) {
        assert oldProperty.equals(newShape.getProperty(key));
        oldProperty.setSafe(object, value, oldShape);
    } else {
        Property newProperty = newShape.getProperty(key);
        newProperty.setSafe(object, value, oldShape, newShape);
    }
}
Also used : Property(com.oracle.truffle.api.object.Property)

Aggregations

Property (com.oracle.truffle.api.object.Property)31 DynamicObject (com.oracle.truffle.api.object.DynamicObject)8 Test (org.junit.Test)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 IncompatibleLocationException (com.oracle.truffle.api.object.IncompatibleLocationException)4 Location (com.oracle.truffle.api.object.Location)4 ArrayDeque (java.util.ArrayDeque)3 FinalLocationException (com.oracle.truffle.api.object.FinalLocationException)2 ObjectLocation (com.oracle.truffle.api.object.ObjectLocation)2 DeclaredLocation (com.oracle.truffle.object.Locations.DeclaredLocation)2 Layout (com.oracle.truffle.api.object.Layout)1 Shape (com.oracle.truffle.api.object.Shape)1 ValueLocation (com.oracle.truffle.object.Locations.ValueLocation)1 PropertyMap (com.oracle.truffle.object.PropertyMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1