Skip to content

Commit

Permalink
Make KernelNodes.HashNode and HashingNodes.ToHashByHashCode consistent
Browse files Browse the repository at this point in the history
(cherry picked from commit 423aa6e)
  • Loading branch information
eregon committed Jul 2, 2021
1 parent e7b1cba commit c6acb0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/truffleruby/core/hash/HashingNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected int hashCompareByIdentity(Object key, boolean compareByIdentity,
}

// MRI: any_hash
/** Keep consistent with {@link org.truffleruby.core.kernel.KernelNodes.HashNode} */
@GenerateUncached
public abstract static class ToHashByHashCode extends RubyBaseNode {

Expand Down Expand Up @@ -116,7 +117,7 @@ protected int hashSymbol(RubySymbol value,
}

@Fallback
protected int hash(Object value,
protected int hashOther(Object value,
@Cached DispatchNode callHash,
@Cached HashCastResultNode cast) {
return cast.execute(callHash.call(value, "hash"));
Expand Down
38 changes: 27 additions & 11 deletions src/main/java/org/truffleruby/core/kernel/KernelNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.TimeUnit;

import com.oracle.truffle.api.dsl.CachedContext;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.utilities.AssumedValue;
import org.jcodings.specific.UTF8Encoding;
Expand Down Expand Up @@ -83,6 +84,7 @@
import org.truffleruby.core.support.TypeNodes.ObjectInstanceVariablesNode;
import org.truffleruby.core.support.TypeNodesFactory.ObjectInstanceVariablesNodeFactory;
import org.truffleruby.core.symbol.RubySymbol;
import org.truffleruby.core.symbol.SymbolNodes;
import org.truffleruby.core.symbol.SymbolTable;
import org.truffleruby.core.thread.GetCurrentRubyThreadNode;
import org.truffleruby.core.thread.RubyThread;
Expand Down Expand Up @@ -930,6 +932,7 @@ protected boolean isFrozen(Object self,

}

/** Keep consistent with {@link org.truffleruby.core.hash.HashingNodes.ToHashByHashCode} */
@CoreMethod(names = "hash")
public abstract static class HashNode extends CoreMethodArrayArgumentsNode {

Expand All @@ -940,37 +943,50 @@ public static HashNode create() {
public abstract Object execute(Object value);

@Specialization
protected long hash(int value) {
return HashOperations.hashLong(value, getContext(), this);
protected long hashBoolean(boolean value) {
return HashOperations.hashBoolean(value, getContext(), this);
}

@Specialization
protected long hash(long value) {
protected long hashInt(int value) {
return HashOperations.hashLong(value, getContext(), this);
}

@Specialization
protected long hash(double value) {
return HashOperations.hashDouble(value, getContext(), this);
protected long hashLong(long value) {
return HashOperations.hashLong(value, getContext(), this);
}

@Specialization
protected long hash(boolean value) {
return HashOperations.hashBoolean(value, getContext(), this);
protected long hashDouble(double value) {
return HashOperations.hashDouble(value, getContext(), this);
}

@Specialization
protected long hashBignum(RubyBignum value) {
return HashOperations.hashBignum(value, getContext(), this);
}

@Specialization(guards = "!isRubyBignum(self)")
protected int hash(ImmutableRubyObject self) {
return System.identityHashCode(self);
@Specialization
protected long hashString(RubyString value,
@Cached StringNodes.HashStringNode stringHashNode) {
return stringHashNode.execute(value);
}

@Specialization
protected long hashImmutableString(ImmutableRubyString value,
@Cached StringNodes.HashStringNode stringHashNode) {
return stringHashNode.execute(value);
}

@Specialization
protected int hash(RubyDynamicObject self) {
protected long hashSymbol(RubySymbol value,
@Cached SymbolNodes.HashSymbolNode symbolHashNode) {
return symbolHashNode.execute(value);
}

@Fallback
protected int hashOtherUsingIdentity(Object self) {
return System.identityHashCode(self);
}
}
Expand Down

0 comments on commit c6acb0f

Please sign in to comment.