Skip to content

Commit e8446b7

Browse files
committed
Adapt putGeneric() to handle the IF_ABSENT flag
* Rename Flags.isSetExisting() to Flags.isPutIfPresent() for clarity.
1 parent 6ba3516 commit e8446b7

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObjectLibraryImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ protected boolean putImpl(DynamicObject object, Shape cachedShape, Object key, O
12331233
} else if (c instanceof PutCacheData putCache && putCache.putFlags == putFlags && putCache.propertyFlags == propertyFlags) {
12341234
Property newProperty = putCache.property;
12351235
if (newProperty == null) {
1236-
assert Flags.isSetExisting(putFlags);
1236+
assert Flags.isPutIfPresent(putFlags);
12371237
return false;
12381238
} else {
12391239
Location location = newProperty.getLocation();
@@ -1271,7 +1271,7 @@ protected boolean putIntImpl(DynamicObject object, Shape cachedShape, Object key
12711271
} else if (c instanceof PutCacheData putCache && putCache.putFlags == putFlags && putCache.propertyFlags == propertyFlags) {
12721272
Property newProperty = putCache.property;
12731273
if (newProperty == null) {
1274-
assert Flags.isSetExisting(putFlags);
1274+
assert Flags.isPutIfPresent(putFlags);
12751275
return false;
12761276
} else {
12771277
Location location = newProperty.getLocation();
@@ -1339,7 +1339,7 @@ protected boolean putLongImpl(DynamicObject object, Shape cachedShape, Object ke
13391339
} else if (c instanceof PutCacheData putCache && putCache.putFlags == putFlags && putCache.propertyFlags == propertyFlags) {
13401340
Property newProperty = putCache.property;
13411341
if (newProperty == null) {
1342-
assert Flags.isSetExisting(putFlags);
1342+
assert Flags.isPutIfPresent(putFlags);
13431343
return false;
13441344
} else {
13451345
Location location = newProperty.getLocation();
@@ -1388,7 +1388,7 @@ protected boolean putDoubleImpl(DynamicObject object, Shape cachedShape, Object
13881388
} else if (c instanceof PutCacheData putCache && putCache.putFlags == putFlags && putCache.propertyFlags == propertyFlags) {
13891389
Property newProperty = putCache.property;
13901390
if (newProperty == null) {
1391-
assert Flags.isSetExisting(putFlags);
1391+
assert Flags.isPutIfPresent(putFlags);
13921392
return false;
13931393
} else {
13941394
Location location = newProperty.getLocation();
@@ -1462,7 +1462,7 @@ protected KeyCacheNode insertIntoPutCache(DynamicObject object, Shape cachedShap
14621462

14631463
private Shape getNewShape(DynamicObject object, Object value, int newPropertyFlags, int putFlags, Property property, Shape oldShape) {
14641464
if (property == null) {
1465-
if (Flags.isSetExisting(putFlags)) {
1465+
if (Flags.isPutIfPresent(putFlags)) {
14661466
return oldShape;
14671467
} else {
14681468
return ObsolescenceStrategy.singleton().defineProperty(oldShape, cachedKey, value, newPropertyFlags, putFlags);

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Flags.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ static boolean isImplicitCastIntToDouble(int flags) {
7777
return getFlag(flags, IMPLICIT_CAST_INT_TO_DOUBLE);
7878
}
7979

80-
static boolean isSetExisting(int flags) {
80+
static boolean isPutIfPresent(int flags) {
8181
return getFlag(flags, IF_PRESENT);
8282
}
8383

84+
static boolean isPutIfAbsent(int flags) {
85+
return getFlag(flags, IF_ABSENT);
86+
}
87+
8488
static boolean isUpdateFlags(int flags) {
8589
return getFlag(flags, UPDATE_FLAGS);
8690
}

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/ObsolescenceStrategy.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,19 @@ public void visitObjectArray(int index, int count) {
175175

176176
@TruffleBoundary
177177
boolean putGeneric(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags, Shape s, Property existingProperty) {
178-
if (existingProperty == null && Flags.isSetExisting(putFlags)) {
179-
return false;
180-
}
181-
if (existingProperty != null && !Flags.isUpdateFlags(putFlags) && existingProperty.getLocation().canStore(value)) {
182-
existingProperty.getLocation().setSafe(object, value, false, false);
183-
return true;
178+
if (existingProperty == null) {
179+
if (Flags.isPutIfPresent(putFlags)) {
180+
return false;
181+
}
184182
} else {
185-
return putGenericSlowPath(object, key, value, newPropertyFlags, putFlags, s, existingProperty);
183+
if (Flags.isPutIfAbsent(putFlags)) {
184+
return true;
185+
} else if (!Flags.isUpdateFlags(putFlags) && existingProperty.getLocation().canStore(value)) {
186+
existingProperty.getLocation().setSafe(object, value, false, false);
187+
return true;
188+
}
186189
}
190+
return putGenericSlowPath(object, key, value, newPropertyFlags, putFlags, s, existingProperty);
187191
}
188192

189193
private boolean putGenericSlowPath(DynamicObject object, Object key, Object value, int newPropertyFlags, int putFlags,
@@ -197,12 +201,14 @@ private boolean putGenericSlowPath(DynamicObject object, Object key, Object valu
197201
oldShape = object.getShape();
198202
final Property existingProperty = reusePropertyLookup(key, initialShape, propertyOfInitialShape, oldShape);
199203
if (existingProperty == null) {
200-
if (Flags.isSetExisting(putFlags)) {
204+
if (Flags.isPutIfPresent(putFlags)) {
201205
return false;
202206
} else {
203207
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
204208
property = newShape.getProperty(key);
205209
}
210+
} else if (Flags.isPutIfAbsent(putFlags)) {
211+
return true;
206212
} else if (Flags.isUpdateFlags(putFlags) && newPropertyFlags != existingProperty.getFlags()) {
207213
newShape = defineProperty(oldShape, key, value, newPropertyFlags, existingProperty, putFlags);
208214
property = newShape.getProperty(key);
@@ -211,6 +217,7 @@ private boolean putGenericSlowPath(DynamicObject object, Object key, Object valu
211217
newShape = oldShape;
212218
property = existingProperty;
213219
} else {
220+
assert !Flags.isUpdateFlags(putFlags) || newPropertyFlags == existingProperty.getFlags();
214221
newShape = defineProperty(oldShape, key, value, existingProperty.getFlags(), existingProperty, putFlags);
215222
property = newShape.getProperty(key);
216223
}
@@ -224,6 +231,8 @@ private boolean putGenericSlowPath(DynamicObject object, Object key, Object valu
224231
location.setSafe(object, value, false, true);
225232
DynamicObjectSupport.setShapeWithStoreFence(object, newShape);
226233
updateShape(object);
234+
} else if (Flags.isPutIfAbsent(putFlags)) {
235+
return true;
227236
} else {
228237
location.setSafe(object, value, false, false);
229238
}

0 commit comments

Comments
 (0)