|
48 | 48 | import com.oracle.graal.python.builtins.objects.PNone;
|
49 | 49 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary.ForEachNode;
|
50 | 50 | import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary.HashingStorageIterable;
|
| 51 | +import com.oracle.graal.python.builtins.objects.dict.PDict; |
51 | 52 | import com.oracle.graal.python.builtins.objects.function.PArguments;
|
52 | 53 | import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
|
| 54 | +import com.oracle.graal.python.builtins.objects.object.PythonObject; |
53 | 55 | import com.oracle.graal.python.builtins.objects.str.PString;
|
54 | 56 | import com.oracle.graal.python.lib.PyObjectHashNode;
|
55 | 57 | import com.oracle.graal.python.lib.PyObjectRichCompareBool;
|
@@ -394,9 +396,30 @@ private static Object runNode(DynamicObjectStorage self, Object key, Object acc,
|
394 | 396 | }
|
395 | 397 |
|
396 | 398 | @ExportMessage
|
397 |
| - public HashingStorage clear(@Shared("dylib") @CachedLibrary(limit = "3") DynamicObjectLibrary dylib) { |
398 |
| - dylib.resetShape(store, PythonLanguage.get(dylib).getEmptyShape()); |
399 |
| - return this; |
| 399 | + @ImportStatic(PGuards.class) |
| 400 | + static class Clear { |
| 401 | + @Specialization(guards = "!isPythonObject(receiver.getStore())") |
| 402 | + static HashingStorage clearPlain(DynamicObjectStorage receiver, |
| 403 | + @Shared("dylib") @CachedLibrary(limit = "3") DynamicObjectLibrary dylib) { |
| 404 | + dylib.resetShape(receiver.getStore(), PythonLanguage.get(dylib).getEmptyShape()); |
| 405 | + return receiver; |
| 406 | + } |
| 407 | + |
| 408 | + @Specialization(guards = "isPythonObject(receiver.getStore())") |
| 409 | + static HashingStorage clearObjectBacked(DynamicObjectStorage receiver, |
| 410 | + @Shared("dylib") @CachedLibrary(limit = "3") DynamicObjectLibrary dylib) { |
| 411 | + /* |
| 412 | + * We cannot use resetShape as that would lose hidden keys, such as CLASS or OBJ_ID. |
| 413 | + * Construct a new storage instead and set it as the object's __dict__'s storage. |
| 414 | + */ |
| 415 | + DynamicObjectStorage newStorage = new DynamicObjectStorage(new Store(PythonLanguage.get(dylib).getEmptyShape())); |
| 416 | + PythonObject owner = (PythonObject) receiver.getStore(); |
| 417 | + PDict dict = (PDict) dylib.getOrDefault(owner, PythonObject.DICT, null); |
| 418 | + if (dict != null && dict.getDictStorage() == receiver) { |
| 419 | + dict.setDictStorage(newStorage); |
| 420 | + } |
| 421 | + return newStorage; |
| 422 | + } |
400 | 423 | }
|
401 | 424 |
|
402 | 425 | @ExportMessage
|
|
0 commit comments