-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a wrapper around keys in WeakMap to ensure correct ruby reference…
… comparison & hashing semantics
- Loading branch information
Nicolas Laurent
committed
Mar 12, 2021
1 parent
df35b7c
commit e839f3f
Showing
3 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/org/truffleruby/core/hash/CompareByRubyIdentityWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 2.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.core.hash; | ||
|
||
import org.truffleruby.core.basicobject.BasicObjectNodes.ReferenceEqualNode; | ||
import org.truffleruby.core.hash.HashingNodes.ToHashByIdentity; | ||
|
||
/** Wraps a value so that it will compared and hashed according to Ruby identity semantics. These semantics differ from | ||
* Java semantics notably for primitives (e.g. all the NaN are different according to Ruby, but Java compares them | ||
* equal). */ | ||
public final class CompareByRubyIdentityWrapper { | ||
|
||
public final Object value; | ||
|
||
public CompareByRubyIdentityWrapper(Object value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return ToHashByIdentity.getUncached().execute(value); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof CompareByRubyIdentityWrapper && | ||
ReferenceEqualNode | ||
.getUncached() | ||
.executeReferenceEqual(value, ((CompareByRubyIdentityWrapper) obj).value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters