[dart2wasm] Fix for RegExp error handling.

Change-Id: I9bac9edc4ff35da069767c1576cded9e0b10813e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275784
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
diff --git a/sdk/lib/_internal/wasm/lib/regexp_helper.dart b/sdk/lib/_internal/wasm/lib/regexp_helper.dart
index d87b498..03d2fa8 100644
--- a/sdk/lib/_internal/wasm/lib/regexp_helper.dart
+++ b/sdk/lib/_internal/wasm/lib/regexp_helper.dart
@@ -175,7 +175,12 @@
 
   int get end => (start + (_match[0].toString()).length);
 
-  String? group(int index) => _match[index]?.toString();
+  String? group(int index) {
+    if (index < 0 || index >= _match.length) {
+      throw RangeError("Index $index is out of range ${_match.length}");
+    }
+    return _match[index]?.toString();
+  }
 
   String? operator [](int index) => group(index);