changeset 21652:5f3dda39d205

Truffle: add DynamicObject#containsKey, make flags parameter optional, minor simplifications
author Andreas Woess <andreas.woess@oracle.com>
date Mon, 01 Jun 2015 12:55:56 +0200
parents f4cd6b1c2efc
children ec47283499ef cd6b1b2189a0
files graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java graal/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/DefaultLayoutFactory.java graal/com.oracle.truffle.object/src/com/oracle/truffle/object/DynamicObjectImpl.java graal/com.oracle.truffle.object/src/com/oracle/truffle/object/PropertyImpl.java
diffstat 5 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java	Mon Jun 01 13:49:38 2015 +0200
+++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java	Mon Jun 01 12:55:56 2015 +0200
@@ -52,6 +52,23 @@
     public abstract boolean set(Object key, Object value);
 
     /**
+     * Returns {@code true} if this object contains a property with the given key.
+     */
+    public final boolean containsKey(Object key) {
+        return getShape().getProperty(key) != null;
+    }
+
+    /**
+     * Define new property or redefine existing property.
+     *
+     * @param key property identifier
+     * @param value value to be set
+     */
+    public final void define(Object key, Object value) {
+        define(key, value, 0);
+    }
+
+    /**
      * Define new property or redefine existing property.
      *
      * @param key property identifier
--- a/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java	Mon Jun 01 13:49:38 2015 +0200
+++ b/graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java	Mon Jun 01 12:55:56 2015 +0200
@@ -27,6 +27,8 @@
 public interface LayoutFactory {
     Layout createLayout(LayoutBuilder layoutBuilder);
 
+    Property createProperty(Object id, Location location);
+
     Property createProperty(Object id, Location location, int flags);
 
     int getPriority();
--- a/graal/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/DefaultLayoutFactory.java	Mon Jun 01 13:49:38 2015 +0200
+++ b/graal/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/DefaultLayoutFactory.java	Mon Jun 01 12:55:56 2015 +0200
@@ -30,6 +30,10 @@
         return BasicLayout.createLayoutImpl(layoutBuilder.getAllowedImplicitCasts(), new DefaultStrategy());
     }
 
+    public Property createProperty(Object id, Location location) {
+        return createProperty(id, location, 0);
+    }
+
     public Property createProperty(Object id, Location location, int flags) {
         return new PropertyImpl(id, location, flags);
     }
--- a/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/DynamicObjectImpl.java	Mon Jun 01 13:49:38 2015 +0200
+++ b/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/DynamicObjectImpl.java	Mon Jun 01 12:55:56 2015 +0200
@@ -209,8 +209,8 @@
         Shape oldShape = getShape();
         Property existing = oldShape.getProperty(id);
         if (existing != null) {
-            Integer newFlags = updateFunction.apply(existing.getFlags());
-            if (newFlags != null && existing.getFlags() != newFlags.intValue()) {
+            int newFlags = updateFunction.apply(existing.getFlags());
+            if (existing.getFlags() != newFlags) {
                 Property newProperty = existing.copyWithFlags(newFlags);
                 Shape newShape = oldShape.replaceProperty(existing, newProperty);
                 this.setShape(newShape);
--- a/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/PropertyImpl.java	Mon Jun 01 13:49:38 2015 +0200
+++ b/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/PropertyImpl.java	Mon Jun 01 12:55:56 2015 +0200
@@ -42,6 +42,8 @@
      * Generic, usual-case constructor for properties storing at least a name.
      *
      * @param key the name of the property
+     * @param location the storage location used to access the property
+     * @param flags property flags (optional)
      */
     protected PropertyImpl(Object key, Location location, int flags, boolean shadow, boolean relocatable) {
         this.key = Objects.requireNonNull(key);
@@ -67,8 +69,8 @@
 
     @Override
     public Property relocate(Location newLocation) {
-        if ((getLocation() == null || !getLocation().equals(newLocation)) && relocatable) {
-            return construct(getKey(), newLocation, getFlags());
+        if (!getLocation().equals(newLocation) && relocatable) {
+            return construct(key, newLocation, flags);
         }
         return this;
     }
@@ -163,8 +165,7 @@
         }
 
         PropertyImpl other = (PropertyImpl) obj;
-        return key.equals(other.key) && ((location == null && other.location == null) || (location != null && location.equals(other.location))) && flags == other.flags && shadow == other.shadow &&
-                        relocatable == other.relocatable;
+        return key.equals(other.key) && location.equals(other.location) && flags == other.flags && shadow == other.shadow && relocatable == other.relocatable;
     }
 
     @Override
@@ -189,7 +190,7 @@
         int result = 1;
         result = prime * result + getClass().hashCode();
         result = prime * result + key.hashCode();
-        result = prime * result + (location != null ? location.hashCode() : 0);
+        result = prime * result + location.hashCode();
         result = prime * result + flags;
         return result;
     }
@@ -240,7 +241,7 @@
 
     private Property relocateShadow(Location newLocation) {
         assert !isShadow() && getLocation() instanceof DeclaredLocation && relocatable;
-        return new PropertyImpl(getKey(), newLocation, flags, true, relocatable);
+        return new PropertyImpl(key, newLocation, flags, true, relocatable);
     }
 
     @SuppressWarnings("hiding")