Skip to content

Commit c65c276

Browse files
committed
Sorting properties corresponding to named capture groups by the group index.
1 parent 254200a commit c65c276

File tree

1 file changed

+21
-1
lines changed
  • graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins

1 file changed

+21
-1
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/JSRegExp.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242

4343
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetRegexResult;
4444

45+
import java.util.ArrayList;
46+
import java.util.Collections;
47+
import java.util.Comparator;
4548
import java.util.EnumSet;
49+
import java.util.List;
4650

4751
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4852
import com.oracle.truffle.api.object.DynamicObject;
@@ -64,6 +68,7 @@
6468
import com.oracle.truffle.js.runtime.objects.PropertyProxy;
6569
import com.oracle.truffle.js.runtime.objects.Undefined;
6670
import com.oracle.truffle.js.runtime.truffleinterop.JSInteropUtil;
71+
import com.oracle.truffle.js.runtime.util.Pair;
6772
import com.oracle.truffle.js.runtime.util.TRegexUtil;
6873
import com.oracle.truffle.js.runtime.util.TRegexUtil.InteropReadStringMemberNode;
6974
import com.oracle.truffle.js.runtime.util.TRegexUtil.TRegexMaterializeResultNode;
@@ -275,15 +280,30 @@ private static JSObjectFactory computeGroupsFactory(JSContext ctx, Object compil
275280
}
276281
}
277282

283+
private static final Comparator<Pair<Integer, String>> NAMED_GROUPS_COMPARATOR = new Comparator<Pair<Integer, String>>() {
284+
@Override
285+
public int compare(Pair<Integer, String> group1, Pair<Integer, String> group2) {
286+
return group1.getFirst() - group2.getFirst();
287+
}
288+
};
289+
278290
@TruffleBoundary
279291
public static JSObjectFactory buildGroupsFactory(JSContext ctx, Object namedCaptureGroups) {
280292
Shape groupsShape = ctx.getEmptyShapeNullPrototype();
281293
groupsShape = groupsShape.addProperty(GROUPS_RESULT_PROPERTY);
282294
groupsShape = groupsShape.addProperty(GROUPS_ORIGINAL_INPUT_PROPERTY);
283295
groupsShape = groupsShape.addProperty(GROUPS_IS_INDICES_PROPERTY);
284-
for (Object key : JSInteropUtil.keys(namedCaptureGroups)) {
296+
List<Object> keys = JSInteropUtil.keys(namedCaptureGroups);
297+
List<Pair<Integer, String>> pairs = new ArrayList<>(keys.size());
298+
for (Object key : keys) {
285299
String groupName = (String) key;
286300
int groupIndex = TRegexUtil.InteropReadIntMemberNode.getUncached().execute(namedCaptureGroups, groupName);
301+
pairs.add(new Pair<>(groupIndex, groupName));
302+
}
303+
Collections.sort(pairs, NAMED_GROUPS_COMPARATOR);
304+
for (Pair<Integer, String> pair : pairs) {
305+
int groupIndex = pair.getFirst();
306+
String groupName = pair.getSecond();
287307
Property groupProperty = JSObjectUtil.makeProxyProperty(groupName, new LazyNamedCaptureGroupProperty(groupName, groupIndex), JSAttributes.getDefault());
288308
groupsShape = groupsShape.addProperty(groupProperty);
289309
}

0 commit comments

Comments
 (0)