Search in sources :

Example 46 with DynamicObject

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

the class LayoutParser method parseGetAndSet.

private void parseGetAndSet(ExecutableElement methodElement) {
    if (methodElement.getParameters().size() != 2) {
        processor.reportError(methodElement, "@Layout get and set methods must have two parameters");
    }
    final VariableElement objectParameter = methodElement.getParameters().get(0);
    final VariableElement newValueParameter = methodElement.getParameters().get(1);
    if (!isSameType(objectParameter.asType(), DynamicObject.class)) {
        processor.reportError(methodElement, "@Layout get and set method should have a first parameter of type DynamicObject");
    }
    if (!objectParameter.getSimpleName().toString().equals("object")) {
        processor.reportError(methodElement, "@Layout get and set method should have a first parameter named object");
    }
    if (!newValueParameter.getSimpleName().toString().equals("value")) {
        processor.reportError(methodElement, "@Layout get and set method should have a second parameter named value");
    }
    String propertyName = NameUtils.titleToCamel(methodElement.getSimpleName().toString().substring("getAndSet".length()));
    final PropertyBuilder property = getProperty(propertyName);
    property.setHasGetAndSet(true);
    setPropertyType(methodElement, property, methodElement.getParameters().get(1).asType());
    setPropertyType(methodElement, property, methodElement.getReturnType());
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) VariableElement(javax.lang.model.element.VariableElement) PropertyBuilder(com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)

Example 47 with DynamicObject

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

the class LayoutParser method parseCompareAndSet.

private void parseCompareAndSet(ExecutableElement methodElement) {
    if (methodElement.getParameters().size() != 3) {
        processor.reportError(methodElement, "@Layout compare and set methods must have three parameters");
    }
    final VariableElement objectParameter = methodElement.getParameters().get(0);
    final VariableElement currentValueParameter = methodElement.getParameters().get(1);
    final VariableElement newValueParameter = methodElement.getParameters().get(2);
    if (!isSameType(objectParameter.asType(), DynamicObject.class)) {
        processor.reportError(methodElement, "@Layout compare and set method should have a first parameter of type DynamicObject");
    }
    if (!objectParameter.getSimpleName().toString().equals("object")) {
        processor.reportError(methodElement, "@Layout compare and set method should have a first parameter named object");
    }
    if (!currentValueParameter.getSimpleName().toString().equals("expectedValue")) {
        processor.reportError(methodElement, "@Layout compare and set method should have a second parameter named expectedValue");
    }
    if (!newValueParameter.getSimpleName().toString().equals("value")) {
        processor.reportError(methodElement, "@Layout compare and set method should have a third parameter named value");
    }
    String propertyName = NameUtils.titleToCamel(methodElement.getSimpleName().toString().substring("compareAndSet".length()));
    final PropertyBuilder property = getProperty(propertyName);
    property.setHasCompareAndSet(true);
    setPropertyType(methodElement, property, methodElement.getParameters().get(1).asType());
    setPropertyType(methodElement, property, methodElement.getParameters().get(2).asType());
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) VariableElement(javax.lang.model.element.VariableElement) PropertyBuilder(com.oracle.truffle.object.dsl.processor.model.PropertyBuilder)

Example 48 with DynamicObject

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

the class ImplicitCastTest method testOtherInt.

@Test
public void testOtherInt() {
    Shape rootShape = layout.createShape(new ObjectType());
    DynamicObject object = rootShape.newInstance();
    object.define("a", otherVal);
    Location location1 = object.getShape().getProperty("a").getLocation();
    Assert.assertEquals(otherPrimClass, getLocationType(location1));
    object.define("a", intVal);
    Location location2 = object.getShape().getProperty("a").getLocation();
    Assert.assertEquals(otherPrimClass, getLocationType(location2));
    Assert.assertEquals(otherVal.getClass(), object.get("a").getClass());
    DOTestAsserts.assertSameLocation(location1, location2);
}
Also used : ObjectType(com.oracle.truffle.api.object.ObjectType) Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Location(com.oracle.truffle.api.object.Location) Test(org.junit.Test)

Example 49 with DynamicObject

use of com.oracle.truffle.api.object.DynamicObject 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);
}
Also used : ObjectType(com.oracle.truffle.api.object.ObjectType) Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Layout(com.oracle.truffle.api.object.Layout) DefaultLayoutFactory(com.oracle.truffle.object.basic.DefaultLayoutFactory) Location(com.oracle.truffle.api.object.Location) Test(org.junit.Test)

Example 50 with DynamicObject

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

the class ImplicitCastTest method testIntObject.

@Test
public void testIntObject() {
    Shape rootShape = layout.createShape(new ObjectType());
    DynamicObject object = rootShape.newInstance();
    object.define("a", intVal);
    object.define("a", "");
    Location location = object.getShape().getProperty("a").getLocation();
    Assert.assertEquals(Object.class, getLocationType(location));
    Assert.assertEquals(String.class, object.get("a").getClass());
}
Also used : ObjectType(com.oracle.truffle.api.object.ObjectType) Shape(com.oracle.truffle.api.object.Shape) DynamicObject(com.oracle.truffle.api.object.DynamicObject) Location(com.oracle.truffle.api.object.Location) 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