Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 ihromant.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util;

import org.teavm.backend.javascript.spi.Injector;
import org.teavm.backend.javascript.spi.InjectorContext;
import org.teavm.model.MethodReference;

public class JSArraysGenerator implements Injector {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that using generators here is an optimal approach. This may be trivial for sort and fill, but sort with comparison function and copyOfRange would be more problematic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please point to correct approach example then?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use JSO

@Override
public void generate(InjectorContext context, MethodReference methodRef) {
switch (methodRef.getName()) {
case "sortIntJS":
context.writeExpr(context.getArgument(0));
context.getWriter().append(".data.sort()");
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import org.teavm.backend.javascript.spi.InjectedBy;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.java.lang.TIllegalArgumentException;
import org.teavm.classlib.java.lang.TMath;
import org.teavm.classlib.java.lang.TObject;
Expand Down Expand Up @@ -488,7 +490,14 @@ public static void sort(int[] a, int fromIndex, int toIndex) {
}
}

@InjectedBy(JSArraysGenerator.class)
private static native void sortIntJS(int[] arr);

public static void sort(int[] a) {
if (PlatformDetector.isJavaScript()) {
sortIntJS(a);
return;
}
if (a.length == 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
public class ArraysTest {
@Test
public void arraySorted() {
int[] intArr = { 2, 5, 7, 3, 5, 6 };
Arrays.sort(intArr);
assertEquals(2, intArr[0]);
assertEquals(3, intArr[1]);
assertEquals(5, intArr[2]);
assertEquals(5, intArr[3]);
assertEquals(6, intArr[4]);
assertEquals(7, intArr[5]);
Integer[] array = { 2, 5, 7, 3, 5, 6 };
Arrays.sort(array);
assertEquals(Integer.valueOf(2), array[0]);
Expand Down
Loading