Skip to content

Commit

Permalink
Make sure callbacks are initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jul 8, 2024
1 parent d4d5f96 commit 18dde9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
28 changes: 14 additions & 14 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ordering.enso
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,6 @@ type Comparable

hash self -> Integer = self.comparator.hash self.value

## PRIVATE
Called as a callback from internal engine code for an atom with a custom
comparator. It is assumed that the given atom has a custom comparator, that is
a comparator different than `Default_Comparator`.
hash_callback : Atom -> Integer
hash_callback atom = (Comparable.from atom).hash atom

## PRIVATE
A callback allowing to compare two atoms with a custom comparator.
compare_callback : Atom -> Atom -> Integer | Nothing
compare_callback atom that =
ordering = Ordering.compare atom that
if ordering.is_error then Nothing else ordering.to_sign

## PRIVATE
Default implementation of a _comparator_.
@Builtin_Type
Expand All @@ -167,6 +153,20 @@ type Default_Comparator
hash : Number -> Integer
hash x = Comparable.hash_builtin x

## PRIVATE
Called as a callback from internal engine code for an atom with a custom
comparator. It is assumed that the given atom has a custom comparator, that is
a comparator different than `Default_Comparator`.
hash_callback : Atom -> Integer
hash_callback atom = (Comparable.from atom).hash atom

## PRIVATE
A callback allowing to compare two atoms with a custom comparator.
compare_callback : Atom -> Atom -> Integer | Nothing
compare_callback atom that =
ordering = Ordering.compare atom that
if ordering.is_error then Nothing else ordering.to_sign

## PRIVATE
Comparable.from (that:Any) = Comparable.new that Default_Comparator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.time.ZonedDateTime;
import java.util.Comparator;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.graalvm.polyglot.Context;
Expand All @@ -22,9 +23,10 @@ private static void initCallbacks() {
Context.getCurrent()
.getBindings("enso")
.invokeMember("get_module", "Standard.Base.Data.Ordering");
var type = module.invokeMember("get_type", "Comparable");
var type = module.invokeMember("get_type", "Default_Comparator");

var hash_callback = module.invokeMember("get_method", type, "hash_callback");
Objects.requireNonNull(hash_callback, "hash_callback");
ensoHashCodeCallback =
v -> {
var result = hash_callback.execute(null, v);
Expand All @@ -37,6 +39,7 @@ private static void initCallbacks() {
};

var compare_callback = module.invokeMember("get_method", type, "compare_callback");
Objects.requireNonNull(compare_callback, "compare_callback");
ensoCompareCallback =
(v, u) -> {
var result = compare_callback.execute(null, v, u);
Expand Down

0 comments on commit 18dde9b

Please sign in to comment.