Skip to content

Commit

Permalink
No need for Convert record. Boolean is enough.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Feb 19, 2024
1 parent 8b48232 commit 12fd925
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.enso.interpreter.test;

import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
import org.junit.AfterClass;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -89,7 +89,8 @@ public void testDifferentComparators() {
gen.intNumConversion = true;
gen.intComparator = true;
gen.numComparator = false;
gen.extraBlock = """
gen.extraBlock =
"""
type Second_Comparator
compare a:Num b:Num = Num_Comparator.compare a b
hash a:Num = Num_Comparator.hash a
Expand Down Expand Up @@ -165,7 +166,8 @@ boolean evaluate() {
r0 = 42 == num42
r0
""";
var res = TestBase.evalModule(context, block0 + block1 + block2 + block3 + mainBlock + extraBlock);
var res =
TestBase.evalModule(context, block0 + block1 + block2 + block3 + mainBlock + extraBlock);
return res.asBoolean();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ static Type findTypeUncached(Object obj) {
return findType(TypeOfNode.getUncached(), obj);
}

record Convert(boolean flip, Function f1, Function f2, Function f3) {}

private static boolean isDefinedIn(ModuleScope scope, Function fn) {
if (fn.getCallTarget().getRootNode() instanceof EnsoRootNode ensoRoot) {
return ensoRoot.getModuleScope() == scope;
Expand All @@ -146,23 +144,28 @@ private static Object convertor(EnsoContext ctx, Function convFn, Object value)
convFn, null, state, new Object[] {ctx.getBuiltins().comparable(), value});
}

Convert findConversions(Type selfType, Type thatType, Object self, Object that) {
/**
* @return {@code null} if no conversion found
*/
Boolean findConversions(Type selfType, Type thatType, Object self, Object that) {
if (selfType == null || thatType == null) {
return null;
}
var ctx = EnsoContext.get(this);

var c1 = findConversionImpl(ctx, false, selfType, thatType, self, that);
if (c1 != null) {
return c1;
if (findConversionImpl(ctx, selfType, thatType, self, that)) {
return false;
} else {
var c2 = findConversionImpl(ctx, true, thatType, selfType, that, self);
return c2;
if (findConversionImpl(ctx, thatType, selfType, that, self)) {
return true;
} else {
return null;
}
}
}

private static Convert findConversionImpl(
EnsoContext ctx, boolean flip, Type selfType, Type thatType, Object self, Object that) {
private static boolean findConversionImpl(
EnsoContext ctx, Type selfType, Type thatType, Object self, Object that) {
var selfScope = selfType.getDefinitionScope();
var comparableType = ctx.getBuiltins().comparable().getType();

Expand All @@ -176,14 +179,14 @@ private static Convert findConversionImpl(
&& isDefinedIn(selfScope, fromThatType)
&& convertor(ctx, fromSelfType, self) == convertor(ctx, fromThatType, that)
&& betweenBoth != null) {
return new Convert(flip, fromSelfType, fromThatType, betweenBoth);
return true;
} else {
return null;
return false;
}
}

@Specialization(
limit = "3",
limit = "10",
guards = {
"selfType != null",
"thatType != null",
Expand All @@ -199,13 +202,13 @@ final boolean doConversionCached(
Type selfType,
@Cached(value = "findType(typeOfNode, that)", uncached = "findTypeUncached(that)")
Type thatType,
@Cached("findConversions(selfType, thatType, self, that)") Convert convert,
@Cached("findConversions(selfType, thatType, self, that)") Boolean convert,
@Shared("convert") @Cached InteropConversionCallNode convertNode,
@Shared("invoke") @Cached(allowUncached = true) EqualsSimpleNode equalityNode) {
if (convert == null) {
return false;
}
if (convert.flip) {
if (convert) {
return doDispatch(frame, that, self, thatType, convertNode, equalityNode);
} else {
return doDispatch(frame, self, that, selfType, convertNode, equalityNode);
Expand All @@ -225,7 +228,7 @@ final boolean doConversionUncached(
var conv = findConversions(selfType, thatType, self, that);
if (conv != null) {
var result =
conv.flip
conv
? doDispatch(frame, that, self, thatType, convertNode, equalityNode)
: doDispatch(frame, self, that, selfType, convertNode, equalityNode);
return result;
Expand Down

0 comments on commit 12fd925

Please sign in to comment.