NativeUint32Array.java revision 1269:71a4e37769f8
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.objects; 27 28import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; 29 30import java.lang.invoke.MethodHandle; 31import java.lang.invoke.MethodHandles; 32import java.nio.ByteBuffer; 33import java.nio.ByteOrder; 34import java.nio.IntBuffer; 35import jdk.nashorn.internal.objects.annotations.Attribute; 36import jdk.nashorn.internal.objects.annotations.Constructor; 37import jdk.nashorn.internal.objects.annotations.Function; 38import jdk.nashorn.internal.objects.annotations.Property; 39import jdk.nashorn.internal.objects.annotations.ScriptClass; 40import jdk.nashorn.internal.objects.annotations.Where; 41import jdk.nashorn.internal.runtime.JSType; 42import jdk.nashorn.internal.runtime.PropertyMap; 43import jdk.nashorn.internal.runtime.ScriptObject; 44import jdk.nashorn.internal.runtime.arrays.ArrayData; 45import jdk.nashorn.internal.runtime.arrays.TypedArrayData; 46 47/** 48 * Uint32 array for TypedArray extension 49 */ 50@ScriptClass("Uint32Array") 51public final class NativeUint32Array extends ArrayBufferView { 52 /** 53 * The size in bytes of each element in the array. 54 */ 55 @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) 56 public static final int BYTES_PER_ELEMENT = 4; 57 58 // initialized by nasgen 59 @SuppressWarnings("unused") 60 private static PropertyMap $nasgenmap$; 61 62 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { 63 @Override 64 public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteBegin, final int length) { 65 return new NativeUint32Array(buffer, byteBegin, length); 66 } 67 68 @Override 69 public Uint32ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { 70 return new Uint32ArrayData(nb.order(ByteOrder.nativeOrder()).asIntBuffer(), start, end); 71 } 72 73 @Override 74 public String getClassName() { 75 return "Uint32Array"; 76 } 77 }; 78 79 private static final class Uint32ArrayData extends TypedArrayData<IntBuffer> { 80 81 private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "getElem", long.class, int.class).methodHandle(); 82 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 83 84 private Uint32ArrayData(final IntBuffer nb, final int start, final int end) { 85 super(((IntBuffer)nb.position(start).limit(end)).slice(), end - start); 86 } 87 88 @Override 89 protected MethodHandle getGetElem() { 90 return GET_ELEM; 91 } 92 93 @Override 94 protected MethodHandle getSetElem() { 95 return SET_ELEM; 96 } 97 98 @Override 99 public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { 100 if (returnType == int.class) { 101 return null; 102 } 103 return getContinuousElementGetter(getClass(), GET_ELEM, returnType, programPoint); 104 } 105 106 private long getElem(final int index) { 107 try { 108 return JSType.toUint32(nb.get(index)); 109 } catch (final IndexOutOfBoundsException e) { 110 throw new ClassCastException(); //force relink - this works for unoptimistic too 111 } 112 } 113 114 private void setElem(final int index, final int elem) { 115 try { 116 if (index < nb.limit()) { 117 nb.put(index, elem); 118 } 119 } catch (final IndexOutOfBoundsException e) { 120 throw new ClassCastException(); 121 } 122 } 123 124 @Override 125 public boolean isUnsigned() { 126 return true; 127 } 128 129 @Override 130 public Class<?> getElementType() { 131 return long.class; 132 } 133 134 @Override 135 public Class<?> getBoxedElementType() { 136 return Integer.class; 137 } 138 139 @Override 140 public int getInt(final int index) { 141 return (int)getLong(index); 142 } 143 144 @Override 145 public long getLong(final int index) { 146 return getElem(index); 147 } 148 149 @Override 150 public long getLongOptimistic(final int index, final int programPoint) { 151 return getElem(index); 152 } 153 154 @Override 155 public double getDouble(final int index) { 156 return getLong(index); 157 } 158 159 @Override 160 public double getDoubleOptimistic(final int index, final int programPoint) { 161 return getLong(index); 162 } 163 164 @Override 165 public Object getObject(final int index) { 166 return getLong(index); 167 } 168 169 @Override 170 public ArrayData set(final int index, final Object value, final boolean strict) { 171 return set(index, JSType.toInt32(value), strict); 172 } 173 174 @Override 175 public ArrayData set(final int index, final int value, final boolean strict) { 176 setElem(index, value); 177 return this; 178 } 179 180 @Override 181 public ArrayData set(final int index, final long value, final boolean strict) { 182 return set(index, (int)value, strict); 183 } 184 185 @Override 186 public ArrayData set(final int index, final double value, final boolean strict) { 187 return set(index, (int)value, strict); 188 } 189 } 190 191 /** 192 * Constructor 193 * 194 * @param newObj is this typed array instantiated with the new operator 195 * @param self self reference 196 * @param args args 197 * 198 * @return new typed array 199 */ 200 @Constructor(arity = 1) 201 public static NativeUint32Array constructor(final boolean newObj, final Object self, final Object... args) { 202 return (NativeUint32Array)constructorImpl(newObj, args, FACTORY); 203 } 204 205 NativeUint32Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 206 super(buffer, byteOffset, length); 207 } 208 209 @Override 210 protected Factory factory() { 211 return FACTORY; 212 } 213 214 /** 215 * Set values 216 * @param self self reference 217 * @param array multiple values of array's type to set 218 * @param offset optional start index, interpreted 0 if undefined 219 * @return undefined 220 */ 221 @Function(attributes = Attribute.NOT_ENUMERABLE) 222 protected static Object set(final Object self, final Object array, final Object offset) { 223 return ArrayBufferView.setImpl(self, array, offset); 224 } 225 226 /** 227 * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, 228 * referencing the elements at begin, inclusive, up to end, exclusive. If either 229 * begin or end is negative, it refers to an index from the end of the array, 230 * as opposed to from the beginning. 231 * <p> 232 * If end is unspecified, the subarray contains all elements from begin to the end 233 * of the TypedArray. The range specified by the begin and end values is clamped to 234 * the valid index range for the current array. If the computed length of the new 235 * TypedArray would be negative, it is clamped to zero. 236 * <p> 237 * The returned TypedArray will be of the same type as the array on which this 238 * method is invoked. 239 * 240 * @param self self reference 241 * @param begin begin position 242 * @param end end position 243 * 244 * @return sub array 245 */ 246 @Function(attributes = Attribute.NOT_ENUMERABLE) 247 protected static NativeUint32Array subarray(final Object self, final Object begin, final Object end) { 248 return (NativeUint32Array)ArrayBufferView.subarrayImpl(self, begin, end); 249 } 250 251 @Override 252 protected ScriptObject getPrototype(final Global global) { 253 return global.getUint32ArrayPrototype(); 254 } 255} 256