Skip to content

Commit

Permalink
Any.to behaves like type cast
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Dec 18, 2024
1 parent 24b40ce commit db5353b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.enso.interpreter.test;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import org.enso.interpreter.runtime.data.EnsoMultiValue;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.test.utils.ContextUtils;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AnyToTest {
private static Context ctx;

private static final ByteArrayOutputStream out = new ByteArrayOutputStream();

@BeforeClass
public static void initCtx() {
ctx = ContextUtils.createDefaultContext(out);
}

@AfterClass
public static void disposeCtx() {
ctx.close();
ctx = null;
}

@Before
public void resetOutput() {
out.reset();
}

private String getStdOut() {
return out.toString(StandardCharsets.UTF_8);
}

@Test
public void multiValueToInteger() throws Exception {
var ensoCtx = ContextUtils.leakContext(ctx);
var types =
new Type[] {ensoCtx.getBuiltins().number().getInteger(), ensoCtx.getBuiltins().text()};
var code =
"""
from Standard.Base import all
private eq a b = a == b
conv style v = case style of
0 -> v.to Integer
1 -> v:Integer
2 -> v.to Text
3 -> v:Text
99 -> eq
""";
var conv =
ContextUtils.evalModule(ctx, Source.newBuilder("enso", code, "conv.enso").build(), "conv");
var both = EnsoMultiValue.create(types, types.length, new Object[] {2L, Text.create("Two")});
var eq =
ContextUtils.executeInContext(
ctx,
() -> {
var bothValue = ctx.asValue(both);
var asIntegerTo = conv.execute(0, bothValue);
var asIntegerCast = conv.execute(1, bothValue);
var equals = conv.execute(99, null);
return equals.execute(asIntegerTo, asIntegerCast);
});
assertTrue("Any.to and : give the same result", eq.asBoolean());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ private Type extractType(Object self) {
return extractType(this, self);
}

static boolean hasType(TypeOfNode typeOfNode, Object value) {
static boolean hasTypeNoMulti(TypeOfNode typeOfNode, Object value) {
if (value instanceof EnsoMultiValue) {
return false;
}
return typeOfNode.hasType(value);
}

Expand All @@ -109,7 +112,11 @@ static boolean isDataflowError(Object value) {
}

@Specialization(
guards = {"hasType(dispatch, that)", "!isDataflowError(self)", "!isDataflowError(that)"})
guards = {
"hasTypeNoMulti(dispatch, that)",
"!isDataflowError(self)",
"!isDataflowError(that)"
})
Object doConvertFrom(
VirtualFrame frame,
State state,
Expand Down Expand Up @@ -265,7 +272,7 @@ Object doConvertText(

@Specialization(
guards = {
"!hasType(typeOfNode, that)",
"!hasTypeNoMulti(typeOfNode, that)",
"!interop.isTime(that)",
"interop.isDate(that)",
})
Expand All @@ -287,7 +294,7 @@ Object doConvertDate(

@Specialization(
guards = {
"!hasType(typeOfNode, that)",
"!hasTypeNoMulti(typeOfNode, that)",
"interop.isTime(that)",
"!interop.isDate(that)",
})
Expand All @@ -309,7 +316,7 @@ Object doConvertTime(

@Specialization(
guards = {
"!hasType(typeOfNode, that)",
"!hasTypeNoMulti(typeOfNode, that)",
"interop.isTime(that)",
"interop.isDate(that)",
})
Expand All @@ -331,7 +338,7 @@ Object doConvertDateTime(

@Specialization(
guards = {
"!hasType(typeOfNode, that)",
"!hasTypeNoMulti(typeOfNode, that)",
"interop.isDuration(that)",
})
Object doConvertDuration(
Expand All @@ -352,7 +359,7 @@ Object doConvertDuration(

@Specialization(
guards = {
"!hasType(typeOfNode, thatMap)",
"!hasTypeNoMulti(typeOfNode, thatMap)",
"interop.hasHashEntries(thatMap)",
})
Object doConvertMap(
Expand All @@ -374,7 +381,7 @@ Object doConvertMap(
return invokeFunctionNode.execute(function, frame, state, arguments);
}

@Specialization(guards = {"!hasType(methods, that)", "!interop.isString(that)"})
@Specialization(guards = {"!hasTypeNoMulti(methods, that)", "!interop.isString(that)"})
Object doFallback(
VirtualFrame frame,
State state,
Expand Down
2 changes: 1 addition & 1 deletion test/Base_Tests/src/Semantic/Conversion_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ add_specs suite_builder =
x==x . should_be_true
(x:Float)==42.3 . should_be_true
(x:Fool)==42.3 . should_be_false
x==42.3 . should_be_true
x==42.3 . should_be_false
42.3==(x.to Float) . should_be_true
42.3==(x.to Fool) . should_be_false
42.3==x . should_be_true
Expand Down

0 comments on commit db5353b

Please sign in to comment.