-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work in progress to have single ObjectComparator calling back to new …
…APIs.
- Loading branch information
1 parent
314b152
commit 410cbdc
Showing
20 changed files
with
221 additions
and
296 deletions.
There are no files selected for viewing
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
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
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
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
76 changes: 76 additions & 0 deletions
76
std-bits/base/src/main/java/org/enso/base/polyglot/EnsoObjectWrapper.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,76 @@ | ||
package org.enso.base.polyglot; | ||
|
||
import org.enso.base.ObjectComparator; | ||
import org.graalvm.polyglot.Context; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
public class EnsoObjectWrapper implements Comparable<EnsoObjectWrapper> { | ||
private static Function<Object, Integer> EnsoHashCodeCallback = null; | ||
private static BiFunction<Object, Object, Boolean> EnsoAreEqualCallback = null; | ||
|
||
private static void initCallbacks() { | ||
if (EnsoHashCodeCallback == null) { | ||
var module = Context.getCurrent().getBindings("enso").invokeMember("get_module", "Standard.Base.Data.Ordering"); | ||
var type = module.invokeMember("get_type", "Comparable"); | ||
|
||
var hash_callback = module.invokeMember("get_method", type, "hash_callback"); | ||
EnsoHashCodeCallback = v -> { | ||
var result = hash_callback.execute(null, v); | ||
if (result.isNull()) { | ||
throw new IllegalStateException("Unable to object hash in EnsoObjectWrapper for " + v.toString()); | ||
} else { | ||
return result.asInt(); | ||
} | ||
}; | ||
|
||
var are_equal = module.invokeMember("get_method", type, "compare_callback"); | ||
EnsoAreEqualCallback = (v, u) -> { | ||
var result = are_equal.execute(null, v, u); | ||
return !result.isNull() && result.asInt() == 0; | ||
}; | ||
} | ||
} | ||
|
||
private static int getEnsoHashCode(Object value) { | ||
initCallbacks(); | ||
return EnsoHashCodeCallback.apply(value); | ||
} | ||
|
||
private static boolean areEqual(Object value, Object other) { | ||
initCallbacks(); | ||
return EnsoAreEqualCallback.apply(value, other); | ||
} | ||
|
||
private final Object value; | ||
private final int ensoHashCode; | ||
|
||
public EnsoObjectWrapper(Object value) { | ||
this.value = value; | ||
this.ensoHashCode = getEnsoHashCode(value); | ||
} | ||
|
||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return ensoHashCode; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof EnsoObjectWrapper that) { | ||
return areEqual(this.value, that.value); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int compareTo(EnsoObjectWrapper o) { | ||
return ObjectComparator.ensoCompare(this.value, o.value); | ||
} | ||
} |
Oops, something went wrong.