From 18dde9bf0a2d1ce27c045f7b6119595311790d5f Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 8 Jul 2024 19:18:32 +0200 Subject: [PATCH] Make sure callbacks are initialized --- .../Base/0.0.0-dev/src/Data/Ordering.enso | 28 +++++++++---------- .../java/org/enso/base/ObjectComparator.java | 5 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ordering.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ordering.enso index efdb09ad7002..4c0d77aad817 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ordering.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ordering.enso @@ -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 @@ -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 diff --git a/std-bits/base/src/main/java/org/enso/base/ObjectComparator.java b/std-bits/base/src/main/java/org/enso/base/ObjectComparator.java index 661160893c99..a0e1df9009eb 100644 --- a/std-bits/base/src/main/java/org/enso/base/ObjectComparator.java +++ b/std-bits/base/src/main/java/org/enso/base/ObjectComparator.java @@ -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; @@ -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); @@ -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);