# HG changeset patch # User Andreas Woess # Date 1433156156 -7200 # Node ID 5f3dda39d20576c61daaf811a64660c95d4f8c05 # Parent f4cd6b1c2efc0d49116d26ccd997cc6e93d4c341 Truffle: add DynamicObject#containsKey, make flags parameter optional, minor simplifications diff -r f4cd6b1c2efc -r 5f3dda39d205 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java --- 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 diff -r f4cd6b1c2efc -r 5f3dda39d205 graal/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/LayoutFactory.java --- 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(); diff -r f4cd6b1c2efc -r 5f3dda39d205 graal/com.oracle.truffle.object.basic/src/com/oracle/truffle/object/basic/DefaultLayoutFactory.java --- 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); } diff -r f4cd6b1c2efc -r 5f3dda39d205 graal/com.oracle.truffle.object/src/com/oracle/truffle/object/DynamicObjectImpl.java --- 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); diff -r f4cd6b1c2efc -r 5f3dda39d205 graal/com.oracle.truffle.object/src/com/oracle/truffle/object/PropertyImpl.java --- 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")