Search in sources :

Example 71 with DynamicObject

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

the class SharedShapeTest method testCannotDeleteFromSharedShape.

@Test
public void testCannotDeleteFromSharedShape() {
    DynamicObject object = sharedShape.newInstance();
    object.define("a", 1);
    try {
        object.delete("a");
        Assert.fail();
    } catch (UnsupportedOperationException e) {
        Assert.assertEquals(1, object.get("a"));
    }
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Test(org.junit.Test)

Example 72 with DynamicObject

use of com.oracle.truffle.api.object.DynamicObject 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 73 with DynamicObject

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

the class DeclaredLocationTest method testMigrateDeclaredLocation.

@Test
public void testMigrateDeclaredLocation() {
    DynamicObject object = shapeWithDeclared.newInstance();
    Assert.assertSame(shapeWithDeclared, object.getShape());
    Assert.assertSame(value, object.get("declared"));
    Object newValue = new Object();
    object.set("declared", newValue);
    Assert.assertNotSame(shapeWithDeclared, object.getShape());
    Assert.assertSame(newValue, object.get("declared"));
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Test(org.junit.Test)

Example 74 with DynamicObject

use of com.oracle.truffle.api.object.DynamicObject 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 75 with DynamicObject

use of com.oracle.truffle.api.object.DynamicObject 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());
    }
}
Also used : Shape(com.oracle.truffle.api.object.Shape) DynamicObjectFactory(com.oracle.truffle.api.object.DynamicObjectFactory) DynamicObject(com.oracle.truffle.api.object.DynamicObject) 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