[dart2wasm] Fix JSCM String.fromCharCodes
Fixes test corelib/string_fromcharcodes_test in JSCM.
Change-Id: I9c2a1b7f67819c193a98b00932328571303589fb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392901
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
diff --git a/sdk/lib/_internal/wasm_js_compatibility/lib/string_patch.dart b/sdk/lib/_internal/wasm_js_compatibility/lib/string_patch.dart
index 213f32d..1478e12 100644
--- a/sdk/lib/_internal/wasm_js_compatibility/lib/string_patch.dart
+++ b/sdk/lib/_internal/wasm_js_compatibility/lib/string_patch.dart
@@ -15,12 +15,12 @@
@patch
factory String.fromCharCodes(Iterable<int> charCodes,
[int start = 0, int? end]) {
- final length = charCodes.length;
-
- RangeError.checkValueInInterval(start, 0, length);
-
+ RangeError.checkNotNegative(start, "start");
if (end != null) {
- RangeError.checkValueInInterval(end, start, length);
+ if (end < start) {
+ throw RangeError.range(end, start, null, "end");
+ }
+ if (end == start) return "";
}
// Skip until `start`.
@@ -32,14 +32,15 @@
// The part of the iterable converted to string is collected in a JS typed
// array, to be able to effciently get subarrays, to pass to
// `String.fromCharCode.apply`.
- final charCodesLength = (end ?? length) - start;
+ final charCodesLength = (end ?? charCodes.length) - start;
+ if (charCodesLength <= 0) return "";
final typedArrayLength = charCodesLength * 2;
final list = JSUint32ArrayImpl(typedArrayLength);
int index = 0; // index in `list`.
end ??= start + charCodesLength;
for (int i = start; i < end; i++) {
if (!it.moveNext()) {
- throw RangeError.range(end, start, i);
+ break;
}
final charCode = it.current;
if (charCode >= 0 && charCode <= 0xffff) {