|
42 | 42 |
|
43 | 43 | import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetRegexResult; |
44 | 44 |
|
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.Comparator; |
45 | 48 | import java.util.EnumSet; |
| 49 | +import java.util.List; |
46 | 50 |
|
47 | 51 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
48 | 52 | import com.oracle.truffle.api.object.DynamicObject; |
|
64 | 68 | import com.oracle.truffle.js.runtime.objects.PropertyProxy; |
65 | 69 | import com.oracle.truffle.js.runtime.objects.Undefined; |
66 | 70 | import com.oracle.truffle.js.runtime.truffleinterop.JSInteropUtil; |
| 71 | +import com.oracle.truffle.js.runtime.util.Pair; |
67 | 72 | import com.oracle.truffle.js.runtime.util.TRegexUtil; |
68 | 73 | import com.oracle.truffle.js.runtime.util.TRegexUtil.InteropReadStringMemberNode; |
69 | 74 | import com.oracle.truffle.js.runtime.util.TRegexUtil.TRegexMaterializeResultNode; |
@@ -275,15 +280,30 @@ private static JSObjectFactory computeGroupsFactory(JSContext ctx, Object compil |
275 | 280 | } |
276 | 281 | } |
277 | 282 |
|
| 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 | + |
278 | 290 | @TruffleBoundary |
279 | 291 | public static JSObjectFactory buildGroupsFactory(JSContext ctx, Object namedCaptureGroups) { |
280 | 292 | Shape groupsShape = ctx.getEmptyShapeNullPrototype(); |
281 | 293 | groupsShape = groupsShape.addProperty(GROUPS_RESULT_PROPERTY); |
282 | 294 | groupsShape = groupsShape.addProperty(GROUPS_ORIGINAL_INPUT_PROPERTY); |
283 | 295 | 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) { |
285 | 299 | String groupName = (String) key; |
286 | 300 | 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(); |
287 | 307 | Property groupProperty = JSObjectUtil.makeProxyProperty(groupName, new LazyNamedCaptureGroupProperty(groupName, groupIndex), JSAttributes.getDefault()); |
288 | 308 | groupsShape = groupsShape.addProperty(groupProperty); |
289 | 309 | } |
|
0 commit comments