Hide all System.arrayCopy checking related helper code
... from compilers via isTypeChecked flag.
PiperOrigin-RevId: 392506658
Change-Id: I99f44ca204c4460593e4e6939d5b8edea72d226d
diff --git a/user/super/com/google/gwt/emul/java/lang/System.java b/user/super/com/google/gwt/emul/java/lang/System.java
index f1aaa83..92f8677 100644
--- a/user/super/com/google/gwt/emul/java/lang/System.java
+++ b/user/super/com/google/gwt/emul/java/lang/System.java
@@ -20,11 +20,9 @@
import static javaemul.internal.InternalPreconditions.isTypeChecked;
import java.io.PrintStream;
-
import javaemul.internal.ArrayHelper;
import javaemul.internal.HashCodes;
import javaemul.internal.JsUtils;
-
import jsinterop.annotations.JsMethod;
/**
@@ -52,6 +50,13 @@
checkNotNull(src, "src");
checkNotNull(dest, "dest");
+ // Fast path for no type checking. Also hides rest of the checking specific code from compilers.
+ if (!isTypeChecked()) {
+ checkArrayCopyIndicies(src, srcOfs, dest, destOfs, len);
+ ArrayHelper.copy(src, srcOfs, dest, destOfs, len);
+ return;
+ }
+
Class<?> srcType = src.getClass();
Class<?> destType = dest.getClass();
checkArrayType(srcType.isArray(), "srcType is not an array");
@@ -61,18 +66,14 @@
Class<?> destComp = destType.getComponentType();
checkArrayType(arrayTypeMatch(srcComp, destComp), "Array types don't match");
- int srclen = ArrayHelper.getLength(src);
- int destlen = ArrayHelper.getLength(dest);
- if (srcOfs < 0 || destOfs < 0 || len < 0 || srcOfs + len > srclen || destOfs + len > destlen) {
- throw new IndexOutOfBoundsException();
- }
+ checkArrayCopyIndicies(src, srcOfs, dest, destOfs, len);
/*
* If the arrays are not references or if they are exactly the same type, we
* can copy them in native code for speed. Otherwise, we have to copy them
* in Java so we get appropriate errors.
*/
- if (isTypeChecked() && !srcComp.isPrimitive() && !srcType.equals(destType)) {
+ if (!srcComp.isPrimitive() && !srcType.equals(destType)) {
// copy in Java to make sure we get ArrayStoreExceptions if the values
// aren't compatible
Object[] srcArray = (Object[]) src;
@@ -81,19 +82,28 @@
// TODO(jat): how does backward copies handle failures in the middle?
// copy backwards to avoid destructive copies
srcOfs += len;
- for (int destEnd = destOfs + len; destEnd-- > destOfs;) {
+ for (int destEnd = destOfs + len; destEnd-- > destOfs; ) {
destArray[destEnd] = srcArray[--srcOfs];
}
} else {
- for (int destEnd = destOfs + len; destOfs < destEnd;) {
+ for (int destEnd = destOfs + len; destOfs < destEnd; ) {
destArray[destOfs++] = srcArray[srcOfs++];
}
}
- } else if (len > 0) {
+ } else {
ArrayHelper.copy(src, srcOfs, dest, destOfs, len);
}
}
+ private static void checkArrayCopyIndicies(
+ Object src, int srcOfs, Object dest, int destOfs, int len) {
+ int srclen = ArrayHelper.getLength(src);
+ int destlen = ArrayHelper.getLength(dest);
+ if (srcOfs < 0 || destOfs < 0 || len < 0 || srcOfs + len > srclen || destOfs + len > destlen) {
+ throw new IndexOutOfBoundsException();
+ }
+ }
+
public static long currentTimeMillis() {
return (long) JsUtils.getTime();
}
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/ArrayHelper.java b/user/super/com/google/gwt/emul/javaemul/internal/ArrayHelper.java
index 5d9086f..049caa3 100644
--- a/user/super/com/google/gwt/emul/javaemul/internal/ArrayHelper.java
+++ b/user/super/com/google/gwt/emul/javaemul/internal/ArrayHelper.java
@@ -99,6 +99,11 @@
private static void copy(
Object src, int srcOfs, Object dest, int destOfs, int len, boolean overwrite) {
+
+ if (len == 0) {
+ return;
+ }
+
/*
* Array.prototype.splice is not used directly to overcome the limits imposed to the number of
* function parameters by browsers.