Skip to content

Commit

Permalink
Documenting the example of Text and extra Integer equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Nov 26, 2024
1 parent 57da6eb commit 3d048be
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,37 @@ public void testEqualityIntegerAndMultiValue() {
}

@Test
public void testEqualityIntegerAndTextMultiValue() {
public void testEqualityTextAndExtraIntegerMultiValue() {
executeInContext(
context,
() -> {
var builtins = ContextUtils.leakContext(context).getBuiltins();
var intType = builtins.number().getInteger();
var textText = builtins.text();
var fourExtraText =
EnsoMultiValue.create(
new Type[] {textText, intType}, 1, new Object[] {Text.create("Hi"), 4L});

var textType = builtins.text();
var bothTypes = new Type[] {textType, intType};

var text = Text.create("Hi");
var ahoj = Text.create("Ahoj");
var integer = 4L;
//
// following variable represents result of
// x = _ : (Text & Integer) : Text
// e.g. multi value with Text and Integer, casted to Text only
//
var multiV = EnsoMultiValue.create(bothTypes, 1, text, integer);

assertTrue("'Hi' == multiV", equalityCheck(text, multiV));
assertFalse("'Ahoj' != multiV", equalityCheck(ahoj, multiV));
assertTrue(
"Only Text is 'dispatch type'. Not integer. Shall we be equal to 4?",
equalityCheck(4L, fourExtraText));
assertFalse("5 != t4", equalityCheck(5L, fourExtraText));
equalityCheck(integer, multiV));
assertFalse("5 != t4", equalityCheck(5L, multiV));
assertTrue(
"Only Text is 'dispatch type'. Not integer. Shall we be equal to 4?",
equalityCheck(fourExtraText, 4L));
assertFalse("4 != t5", equalityCheck(fourExtraText, 5L));
equalityCheck(multiV, integer));
assertFalse("4 != t5", equalityCheck(multiV, 5L));
assertTrue("multiV == 'Hi'", equalityCheck(multiV, text));
assertFalse("multiV != 'Ahoj'", equalityCheck(multiV, ahoj));

return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ EqualsAndInfo equalsTextBigInt(Text self, EnsoBigInteger other) {
* lexicographical order, handling Unicode normalization. See {@code Text_Utils.compare_to}.
*/
@Specialization(
guards = {"selfInterop.isString(selfString)", "isNotMulti(selfString)"},
guards = {
"selfInterop.isString(selfString)",
"isNotMulti(selfString)",
"isNotMulti(otherString)"
},
limit = "3")
EqualsAndInfo equalsStrings(
Object selfString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.NeverDefault;
Expand Down Expand Up @@ -57,7 +56,21 @@ private EnsoMultiValue(Type[] types, int dispatchTypes, Object[] values) {
: "Avoid double wrapping " + Arrays.toString(values);
}

public static EnsoObject create(Type[] types, int dispatchTypes, Object[] values) {
/**
* Creates new instance of EnsoMultiValue from provided information.
*
* @param types all the types this value can be {@link CastToNode cast to}
* @param dispatchTypes the (subset of) types that the value is cast to currently - bigger than
* {@code 0} and at most {@code type.length}
* @param values value of each of the provided {@code types}
* @return non-{@code null} multi value instance
*/
@NeverDefault
public static EnsoMultiValue create(
@NeverDefault Type[] types, @NeverDefault int dispatchTypes, @NeverDefault Object... values) {
assert dispatchTypes > 0;
assert dispatchTypes <= types.length;
assert types.length == values.length;
return new EnsoMultiValue(types, dispatchTypes, values);
}

Expand Down Expand Up @@ -451,13 +464,9 @@ public static CastToNode getUncached() {
}

@Specialization
Object castsToAType(
Type type,
EnsoMultiValue mv,
boolean reorderOnly,
@Cached(value = "type", allowUncached = true, neverDefault = true) Type cachedType) {
Object castsToAType(Type type, EnsoMultiValue mv, boolean reorderOnly) {
for (var i = 0; i < mv.types.length; i++) {
if (mv.types[i] == cachedType) {
if (mv.types[i] == type) {
if (reorderOnly) {
var copyTypes = mv.types.clone();
var copyValues = mv.values.clone();
Expand Down

0 comments on commit 3d048be

Please sign in to comment.