src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java

Wed, 15 Oct 2014 15:57:46 +0200

author
attila
date
Wed, 15 Oct 2014 15:57:46 +0200
changeset 1056
db675278b4d3
parent 1028
d79265f2fa92
child 1060
6de46794603c
permissions
-rw-r--r--

8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
Reviewed-by: hannesw, lagergren, sundar

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.runtime.arrays;
jlaskey@3 27
attila@963 28 import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
attila@963 29 import static jdk.nashorn.internal.lookup.Lookup.MH;
jlaskey@3 30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
attila@1056 31
attila@963 32 import java.lang.invoke.MethodHandle;
attila@963 33 import java.lang.invoke.MethodHandles;
jlaskey@3 34 import java.util.Arrays;
jlaskey@3 35
jlaskey@3 36 /**
jlaskey@3 37 * Implementation of {@link ArrayData} as soon as a double has been
jlaskey@3 38 * written to the array
jlaskey@3 39 */
lagergren@1028 40 final class NumberArrayData extends ContinuousArrayData implements NumericElements {
jlaskey@3 41 /**
jlaskey@3 42 * The wrapped array
jlaskey@3 43 */
jlaskey@3 44 private double[] array;
jlaskey@3 45
jlaskey@3 46 /**
jlaskey@3 47 * Constructor
jlaskey@3 48 * @param array an int array
jlaskey@3 49 * @param length a length, not necessarily array.length
jlaskey@3 50 */
jlaskey@3 51 NumberArrayData(final double array[], final int length) {
jlaskey@3 52 super(length);
hannesw@482 53 assert array.length >= length;
jlaskey@3 54 this.array = array;
hannesw@482 55 }
hannesw@482 56
hannesw@482 57 @Override
lagergren@1028 58 public Class<?> getElementType() {
lagergren@1028 59 return double.class;
lagergren@1028 60 }
lagergren@1028 61
lagergren@1028 62 @Override
hannesw@482 63 public ArrayData copy() {
lagergren@1028 64 return new NumberArrayData(array.clone(), (int)length);
jlaskey@3 65 }
jlaskey@3 66
jlaskey@3 67 @Override
jlaskey@3 68 public Object[] asObjectArray() {
lagergren@1028 69 return toObjectArray(array, (int)length);
jlaskey@3 70 }
jlaskey@3 71
jlaskey@3 72 private static Object[] toObjectArray(final double[] array, final int length) {
hannesw@442 73 assert length <= array.length : "length exceeds internal array size";
hannesw@442 74 final Object[] oarray = new Object[array.length];
hannesw@442 75
jlaskey@3 76 for (int index = 0; index < length; index++) {
jlaskey@3 77 oarray[index] = Double.valueOf(array[index]);
jlaskey@3 78 }
jlaskey@3 79 return oarray;
jlaskey@3 80 }
jlaskey@3 81
jlaskey@3 82 @Override
jlaskey@3 83 public Object asArrayOfType(final Class<?> componentType) {
jlaskey@3 84 if(componentType == double.class) {
lagergren@1028 85 return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length);
jlaskey@3 86 }
jlaskey@3 87 return super.asArrayOfType(componentType);
jlaskey@3 88 }
jlaskey@3 89
jlaskey@3 90 @Override
jlaskey@3 91 public ArrayData convert(final Class<?> type) {
jlaskey@3 92 if (type != Double.class && type != Integer.class && type != Long.class) {
lagergren@1028 93 final int len = (int)length;
lagergren@1028 94 return new ObjectArrayData(NumberArrayData.toObjectArray(array, len), len);
jlaskey@3 95 }
jlaskey@3 96 return this;
jlaskey@3 97 }
jlaskey@3 98
jlaskey@3 99 @Override
jlaskey@3 100 public void shiftLeft(final int by) {
jlaskey@3 101 System.arraycopy(array, by, array, 0, array.length - by);
jlaskey@3 102 }
jlaskey@3 103
jlaskey@3 104 @Override
jlaskey@3 105 public ArrayData shiftRight(final int by) {
lagergren@1028 106 final ArrayData newData = ensure(by + length - 1);
jlaskey@3 107 if (newData != this) {
jlaskey@3 108 newData.shiftRight(by);
jlaskey@3 109 return newData;
jlaskey@3 110 }
jlaskey@3 111 System.arraycopy(array, 0, array, by, array.length - by);
jlaskey@3 112 return this;
jlaskey@3 113 }
jlaskey@3 114
jlaskey@3 115 @Override
jlaskey@3 116 public ArrayData ensure(final long safeIndex) {
attila@963 117 if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
jlaskey@3 118 return new SparseArrayData(this, safeIndex + 1);
jlaskey@3 119 }
attila@963 120 final int alen = array.length;
attila@963 121 if (safeIndex >= alen) {
attila@963 122 final int newLength = ArrayData.nextSize((int)safeIndex);
attila@963 123 array = Arrays.copyOf(array, newLength); //todo fill with nan or never accessed?
attila@963 124 }
attila@963 125 setLength(safeIndex + 1);
attila@963 126 return this;
jlaskey@3 127
jlaskey@3 128 }
jlaskey@3 129
jlaskey@3 130 @Override
jlaskey@3 131 public ArrayData shrink(final long newLength) {
jlaskey@3 132 Arrays.fill(array, (int) newLength, array.length, 0.0);
jlaskey@3 133 return this;
jlaskey@3 134 }
jlaskey@3 135
jlaskey@3 136 @Override
jlaskey@3 137 public ArrayData set(final int index, final Object value, final boolean strict) {
hannesw@442 138 if (value instanceof Double || value instanceof Integer || value instanceof Long) {
hannesw@442 139 return set(index, ((Number)value).doubleValue(), strict);
hannesw@442 140 } else if (value == UNDEFINED) {
hannesw@442 141 return new UndefinedArrayFilter(this).set(index, value, strict);
jlaskey@3 142 }
hannesw@442 143
jlaskey@3 144 final ArrayData newData = convert(value == null ? Object.class : value.getClass());
jlaskey@3 145 return newData.set(index, value, strict);
jlaskey@3 146 }
jlaskey@3 147
jlaskey@3 148 @Override
jlaskey@3 149 public ArrayData set(final int index, final int value, final boolean strict) {
jlaskey@3 150 array[index] = value;
lagergren@1028 151 setLength(Math.max(index + 1, length));
jlaskey@3 152 return this;
jlaskey@3 153 }
jlaskey@3 154
jlaskey@3 155 @Override
jlaskey@3 156 public ArrayData set(final int index, final long value, final boolean strict) {
jlaskey@3 157 array[index] = value;
lagergren@1028 158 setLength(Math.max(index + 1, length));
jlaskey@3 159 return this;
jlaskey@3 160 }
jlaskey@3 161
jlaskey@3 162 @Override
jlaskey@3 163 public ArrayData set(final int index, final double value, final boolean strict) {
jlaskey@3 164 array[index] = value;
lagergren@1028 165 setLength(Math.max(index + 1, length));
jlaskey@3 166 return this;
jlaskey@3 167 }
jlaskey@3 168
attila@963 169 private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "getElem", double.class, int.class).methodHandle();
attila@963 170 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "setElem", void.class, int.class, double.class).methodHandle();
attila@963 171
attila@963 172 @SuppressWarnings("unused")
attila@963 173 private double getElem(final int index) {
attila@963 174 if (has(index)) {
attila@963 175 return array[index];
attila@963 176 }
attila@963 177 throw new ClassCastException();
attila@963 178 }
attila@963 179
attila@963 180 @SuppressWarnings("unused")
attila@963 181 private void setElem(final int index, final double elem) {
attila@963 182 if (hasRoomFor(index)) {
attila@963 183 array[index] = elem;
attila@963 184 return;
attila@963 185 }
attila@963 186 throw new ClassCastException();
attila@963 187 }
attila@963 188
attila@963 189 @Override
attila@963 190 public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
attila@963 191 if (returnType == int.class || returnType == long.class) {
attila@963 192 return null;
attila@963 193 }
attila@963 194 return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
attila@963 195 }
attila@963 196
attila@963 197 @Override
attila@963 198 public MethodHandle getElementSetter(final Class<?> elementType) {
attila@963 199 return elementType.isPrimitive() ? getContinuousElementSetter(MH.asType(SET_ELEM, SET_ELEM.type().changeParameterType(2, elementType)), elementType) : null;
attila@963 200 }
attila@963 201
attila@963 202 @Override
jlaskey@3 203 public int getInt(final int index) {
jlaskey@3 204 return (int)array[index];
jlaskey@3 205 }
jlaskey@3 206
jlaskey@3 207 @Override
jlaskey@3 208 public long getLong(final int index) {
jlaskey@3 209 return (long)array[index];
jlaskey@3 210 }
jlaskey@3 211
jlaskey@3 212 @Override
jlaskey@3 213 public double getDouble(final int index) {
jlaskey@3 214 return array[index];
jlaskey@3 215 }
jlaskey@3 216
jlaskey@3 217 @Override
attila@963 218 public double getDoubleOptimistic(final int index, final int programPoint) {
attila@963 219 return array[index];
attila@963 220 }
attila@963 221
attila@963 222 @Override
jlaskey@3 223 public Object getObject(final int index) {
jlaskey@3 224 return array[index];
jlaskey@3 225 }
jlaskey@3 226
jlaskey@3 227 @Override
jlaskey@3 228 public boolean has(final int index) {
lagergren@1028 229 return 0 <= index && index < length;
jlaskey@3 230 }
jlaskey@3 231
jlaskey@3 232 @Override
jlaskey@3 233 public ArrayData delete(final int index) {
jlaskey@3 234 return new DeletedRangeArrayFilter(this, index, index);
jlaskey@3 235 }
jlaskey@3 236
jlaskey@3 237 @Override
jlaskey@3 238 public ArrayData delete(final long fromIndex, final long toIndex) {
jlaskey@3 239 return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
jlaskey@3 240 }
jlaskey@3 241
jlaskey@3 242 @Override
jlaskey@3 243 public Object pop() {
lagergren@1028 244 if (length == 0) {
jlaskey@3 245 return UNDEFINED;
jlaskey@3 246 }
jlaskey@3 247
lagergren@1028 248 final int newLength = (int)length - 1;
jlaskey@3 249 final double elem = array[newLength];
jlaskey@3 250 array[newLength] = 0;
jlaskey@3 251 setLength(newLength);
jlaskey@3 252 return elem;
jlaskey@3 253 }
jlaskey@3 254
jlaskey@3 255 @Override
jlaskey@3 256 public ArrayData slice(final long from, final long to) {
lagergren@1028 257 final long start = from < 0 ? from + length : from;
jlaskey@3 258 final long newLength = to - start;
jlaskey@3 259 return new NumberArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
jlaskey@3 260 }
hannesw@633 261
hannesw@633 262 @Override
attila@963 263 public final ArrayData push(final boolean strict, final double item) {
lagergren@1028 264 final long len = length;
lagergren@1028 265 final ArrayData newData = ensure(len);
attila@963 266 if (newData == this) {
lagergren@1028 267 array[(int)len] = item;
attila@963 268 return this;
attila@963 269 }
lagergren@1028 270 return newData.set((int)len, item, strict);
attila@963 271 }
attila@963 272
attila@963 273 @Override
hannesw@633 274 public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
lagergren@1028 275 final long oldLength = length;
hannesw@633 276 final long newLength = oldLength - removed + added;
hannesw@633 277 if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
hannesw@633 278 throw new UnsupportedOperationException();
hannesw@633 279 }
attila@963 280 final ArrayData returnValue = removed == 0 ?
hannesw@633 281 EMPTY_ARRAY : new NumberArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
hannesw@633 282
hannesw@633 283 if (newLength != oldLength) {
hannesw@633 284 final double[] newArray;
hannesw@633 285
hannesw@633 286 if (newLength > array.length) {
hannesw@633 287 newArray = new double[ArrayData.nextSize((int)newLength)];
hannesw@633 288 System.arraycopy(array, 0, newArray, 0, start);
hannesw@633 289 } else {
hannesw@633 290 newArray = array;
hannesw@633 291 }
hannesw@633 292
hannesw@633 293 System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed));
hannesw@633 294 array = newArray;
hannesw@633 295 setLength(newLength);
hannesw@633 296 }
hannesw@633 297
hannesw@633 298 return returnValue;
hannesw@633 299 }
lagergren@1028 300
lagergren@1028 301 @Override
lagergren@1028 302 public long fastPush(final int arg) {
lagergren@1028 303 return fastPush((double)arg);
lagergren@1028 304 }
lagergren@1028 305
lagergren@1028 306 @Override
lagergren@1028 307 public long fastPush(final long arg) {
lagergren@1028 308 return fastPush((double)arg);
lagergren@1028 309 }
lagergren@1028 310
lagergren@1028 311 @Override
lagergren@1028 312 public long fastPush(final double arg) {
lagergren@1028 313 final int len = (int)length;
lagergren@1028 314 if (len == array.length) {
lagergren@1028 315 //note that fastpush never creates spares arrays, there is nothing to gain by that - it will just use even more memory
lagergren@1028 316 array = Arrays.copyOf(array, nextSize(len));
lagergren@1028 317 }
lagergren@1028 318 array[len] = arg;
lagergren@1028 319 return ++length;
lagergren@1028 320 }
lagergren@1028 321
lagergren@1028 322 @Override
lagergren@1028 323 public double fastPopDouble() {
lagergren@1028 324 if (length == 0) {
lagergren@1028 325 throw new ClassCastException();
lagergren@1028 326 }
lagergren@1028 327 final int newLength = (int)--length;
lagergren@1028 328 final double elem = array[newLength];
lagergren@1028 329 array[newLength] = 0;
lagergren@1028 330 return elem;
lagergren@1028 331 }
lagergren@1028 332
lagergren@1028 333 @Override
lagergren@1028 334 public Object fastPopObject() {
lagergren@1028 335 return fastPopDouble();
lagergren@1028 336 }
jlaskey@3 337 }

mercurial