Skip to content

Commit

Permalink
Merge pull request #43458 from ballerina-platform/revert-43290-issue_…
Browse files Browse the repository at this point in the history
…43029

Revert "Add checks and logic to support `sort` operation for tuples at runtime"
  • Loading branch information
gimantha authored Oct 7, 2024
2 parents ac0b842 + b668071 commit 8197500
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 650 deletions.
2 changes: 1 addition & 1 deletion bvm/ballerina-runtime/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
io.ballerina.lang.function, io.ballerina.lang.regexp, io.ballerina.lang.value, io.ballerina.lang.internal, io.ballerina.lang.array;
exports io.ballerina.runtime.internal.configurable to io.ballerina.lang.internal;
exports io.ballerina.runtime.internal.types to io.ballerina.lang.typedesc, io.ballerina.testerina.runtime,
org.ballerinalang.debugadapter.runtime, io.ballerina.lang.function, io.ballerina.lang.regexp, io.ballerina.testerina.core, io.ballerina.lang.array;
org.ballerinalang.debugadapter.runtime, io.ballerina.lang.function, io.ballerina.lang.regexp, io.ballerina.testerina.core;
exports io.ballerina.runtime.observability.metrics.noop;
exports io.ballerina.runtime.observability.tracer.noop;
exports io.ballerina.runtime.internal.regexp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,22 @@

package org.ballerinalang.langlib.array;

import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BFunctionPointer;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.internal.ValueComparisonUtils;
import io.ballerina.runtime.internal.scheduling.Scheduler;
import io.ballerina.runtime.internal.types.BTupleType;

import java.util.HashSet;
import java.util.Set;

import static io.ballerina.runtime.api.constants.RuntimeConstants.ARRAY_LANG_LIB;
import static io.ballerina.runtime.internal.errors.ErrorReasons.INVALID_TYPE_TO_SORT;
import static io.ballerina.runtime.internal.errors.ErrorReasons.getModulePrefixedReason;
import static org.ballerinalang.langlib.array.utils.SortUtils.isOrderedType;
import static org.ballerinalang.langlib.array.utils.ArrayUtils.checkIsArrayOnlyOperation;

/**
* Native implementation of lang.array:sort((any|error)[], direction, function).
Expand All @@ -52,16 +46,12 @@ private Sort() {
}

public static BArray sort(BArray arr, Object direction, Object func) {
BArray sortedArray;
Type arrType = arr.getType();
checkIsArrayOnlyOperation(TypeUtils.getImpliedType(arr.getType()), "sort()");
BFunctionPointer<Object, Object> function = (BFunctionPointer<Object, Object>) func;
// Check if the array type is an Ordered type, otherwise a key function is mandatory
if (!isOrderedType(arrType) && function == null) {
throw ErrorCreator.createError(getModulePrefixedReason(ARRAY_LANG_LIB, INVALID_TYPE_TO_SORT),
StringUtils.fromString("valid key function required"));
}

Object[][] sortArr = new Object[arr.size()][2];
Object[][] sortArrClone = new Object[arr.size()][2];

if (function != null) {
for (int i = 0; i < arr.size(); i++) {
sortArr[i][0] = function.call(new Object[]{Scheduler.getStrand(), arr.get(i), true});
Expand All @@ -72,21 +62,15 @@ public static BArray sort(BArray arr, Object direction, Object func) {
sortArr[i][0] = sortArr[i][1] = arr.get(i);
}
}

mergesort(sortArr, sortArrClone, 0, sortArr.length - 1, direction.toString());
if (arrType.getTag() == TypeTags.TUPLE_TAG) {
BTupleType tupleType = (BTupleType) arrType;
Set<Type> typeList = new HashSet<>(tupleType.getTupleTypes());
if (tupleType.getRestType() != null) {
typeList.add(tupleType.getRestType());
}
sortedArray = ValueCreator.createArrayValue(TypeCreator.createArrayType(
TypeCreator.createUnionType(typeList.stream().toList())));
} else {
sortedArray = ValueCreator.createArrayValue(TypeCreator.createArrayType(arr.getElementType()));
}

BArray sortedArray = ValueCreator.createArrayValue(TypeCreator.createArrayType(arr.getElementType()));

for (int k = 0; k < sortArr.length; k++) {
sortedArray.add(k, sortArr[k][1]);
}

return sortedArray;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,6 @@ public Object[] testFunctions() {
"testSort8",
"testSort9",
"testSort10",
"testSort11",
"testSortNegative",
"testReadOnlyArrayFilter",
"testTupleFilter",
"testTupleReverse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void testRemove() {
BRunUtil.invoke(compileResult, "testRemove");
}

@Test
@Test(expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp = ".*error: \\{ballerina/lang.array\\}OperationNotSupported.*")
public void testSort() {
BRunUtil.invoke(compileResult, "testSort");
}
Expand Down
Loading

0 comments on commit 8197500

Please sign in to comment.