add hashCode to primitives

This commit is contained in:
TopchetoEU 2024-12-09 23:38:39 +02:00
parent 2e8e123ec4
commit b0d8a072aa
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 14 additions and 1 deletions

View File

@ -20,6 +20,9 @@ public final class BoolValue extends PrimitiveValue {
return env.get(BOOL_PROTO); return env.get(BOOL_PROTO);
} }
@Override public int hashCode() {
return Boolean.hashCode(value);
}
@Override public boolean equals(Object other) { @Override public boolean equals(Object other) {
if (other == this) return true; if (other == this) return true;
else if (other instanceof BoolValue bool) return value == bool.value; else if (other instanceof BoolValue bool) return value == bool.value;

View File

@ -36,6 +36,9 @@ public final class StringValue extends PrimitiveValue {
} }
@Override public String toString(Environment ext) { return value; } @Override public String toString(Environment ext) { return value; }
@Override public int hashCode() {
return value.hashCode();
}
@Override public boolean equals(Object other) { @Override public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;
else if (other instanceof StringValue val) return value.length() == val.value.length() && value.equals(val.value); else if (other instanceof StringValue val) return value.length() == val.value.length() && value.equals(val.value);

View File

@ -23,7 +23,8 @@ public final class VoidValue extends PrimitiveValue {
@Override public ObjectValue getPrototype(Environment env) { return null; } @Override public ObjectValue getPrototype(Environment env) { return null; }
@Override public Member getOwnMember(Environment env, KeyCache key) { @Override public Member getOwnMember(Environment env, KeyCache key) {
throw EngineException.ofError(String.format("Cannot read properties of %s (reading '%s')", name, key.toString(env))); if (key.isSymbol()) throw EngineException.ofError(String.format("Cannot read properties of %s (reading '%s')", name, key.toSymbol().toString()));
else throw EngineException.ofError(String.format("Cannot read properties of %s (reading '%s')", name, key.toString(env)));
} }
@Override public List<String> toReadableLines(Environment env, HashSet<ObjectValue> passed) { @Override public List<String> toReadableLines(Environment env, HashSet<ObjectValue> passed) {

View File

@ -24,6 +24,9 @@ public final class DoubleValue extends NumberValue {
@Override public String toString() { return JSON.stringify(JSONElement.number(value)); } @Override public String toString() { return JSON.stringify(JSONElement.number(value)); }
@Override public int hashCode() {
return Double.hashCode(value);
}
@Override public boolean equals(Object other) { @Override public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;
else if (other instanceof NumberValue val) return value == val.getDouble(); else if (other instanceof NumberValue val) return value == val.getDouble();

View File

@ -26,6 +26,9 @@ public final class IntValue extends NumberValue {
return value; return value;
} }
@Override public int hashCode() {
return Long.hashCode(value);
}
@Override public String toString() { return value + ""; } @Override public String toString() { return value + ""; }
@Override public boolean equals(Object other) { @Override public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;