NativeUint8ClampedArray.java revision 1062:f9ed1ca59030
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; 29import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall; 30import static jdk.nashorn.internal.lookup.Lookup.MH; 31 32import java.lang.invoke.MethodHandle; 33import java.lang.invoke.MethodHandles; 34import java.nio.ByteBuffer; 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 * Uint8 clamped array for TypedArray extension 49 */ 50@ScriptClass("Uint8ClampedArray") 51public final class NativeUint8ClampedArray 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 = 1; 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 byteOffset, final int length) { 65 return new NativeUint8ClampedArray(buffer, byteOffset, length); 66 } 67 68 @Override 69 public Uint8ClampedArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { 70 return new Uint8ClampedArrayData(nb, start, end); 71 } 72 73 @Override 74 public String getClassName() { 75 return "Uint8ClampedArray"; 76 } 77 }; 78 79 private static final class Uint8ClampedArrayData extends TypedArrayData<ByteBuffer> { 80 81 private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "getElem", int.class, int.class).methodHandle(); 82 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 83 private static final MethodHandle RINT_D = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", double.class, double.class).methodHandle(); 84 private static final MethodHandle RINT_O = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", Object.class, Object.class).methodHandle(); 85 private static final MethodHandle CLAMP_LONG = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "clampLong", long.class, long.class).methodHandle(); 86 87 private Uint8ClampedArrayData(final ByteBuffer nb, final int start, final int end) { 88 super(((ByteBuffer)nb.position(start).limit(end)).slice(), end - start); 89 } 90 91 @Override 92 protected MethodHandle getGetElem() { 93 return GET_ELEM; 94 } 95 96 @Override 97 protected MethodHandle getSetElem() { 98 return SET_ELEM; 99 } 100 101 @Override 102 public Class<?> getElementType() { 103 return int.class; 104 } 105 106 private int getElem(final int index) { 107 try { 108 return nb.get(index) & 0xff; 109 } catch (final IndexOutOfBoundsException e) { 110 throw new ClassCastException(); //force relink - this works for unoptimistic too 111 } 112 } 113 114 @Override 115 public MethodHandle getElementSetter(final Class<?> elementType) { 116 final MethodHandle setter = super.getElementSetter(elementType); //getContinuousElementSetter(getClass(), setElem(), elementType); 117 if (setter != null) { 118 if (elementType == Object.class) { 119 return MH.filterArguments(setter, 2, RINT_O); 120 } else if (elementType == double.class) { 121 return MH.filterArguments(setter, 2, RINT_D); 122 } else if (elementType == long.class) { 123 return MH.filterArguments(setter, 2, CLAMP_LONG); 124 } 125 } 126 return setter; 127 } 128 129 private void setElem(final int index, final int elem) { 130 try { 131 final byte clamped; 132 if ((elem & 0xffff_ff00) == 0) { 133 clamped = (byte)elem; 134 } else { 135 clamped = elem < 0 ? 0 : (byte)0xff; 136 } 137 nb.put(index, clamped); 138 } catch (final IndexOutOfBoundsException e) { 139 //swallow valid array indexes. it's ok. 140 if (index < 0) { 141 throw new ClassCastException(); 142 } 143 } 144 } 145 146 @Override 147 public boolean isClamped() { 148 return true; 149 } 150 151 @Override 152 public boolean isUnsigned() { 153 return true; 154 } 155 156 @Override 157 public int getInt(final int index) { 158 return getElem(index); 159 } 160 161 @Override 162 public int getIntOptimistic(final int index, final int programPoint) { 163 return getElem(index); 164 } 165 166 @Override 167 public long getLong(final int index) { 168 return getInt(index); 169 } 170 171 @Override 172 public long getLongOptimistic(final int index, final int programPoint) { 173 return getElem(index); 174 } 175 176 @Override 177 public double getDouble(final int index) { 178 return getInt(index); 179 } 180 181 @Override 182 public double getDoubleOptimistic(final int index, final int programPoint) { 183 return getElem(index); 184 } 185 186 @Override 187 public Object getObject(final int index) { 188 return getInt(index); 189 } 190 191 @Override 192 public ArrayData set(final int index, final Object value, final boolean strict) { 193 return set(index, JSType.toNumber(value), strict); 194 } 195 196 @Override 197 public ArrayData set(final int index, final int value, final boolean strict) { 198 setElem(index, value); 199 return this; 200 } 201 202 @Override 203 public ArrayData set(final int index, final long value, final boolean strict) { 204 return set(index, (int)value, strict); 205 } 206 207 @Override 208 public ArrayData set(final int index, final double value, final boolean strict) { 209 return set(index, rint(value), strict); 210 } 211 212 private static double rint(final double rint) { 213 return (int)Math.rint(rint); 214 } 215 216 @SuppressWarnings("unused") 217 private static Object rint(final Object rint) { 218 return rint(JSType.toNumber(rint)); 219 } 220 221 @SuppressWarnings("unused") 222 private static long clampLong(final long l) { 223 if(l < 0L) { 224 return 0L; 225 } else if(l > 0xffL) { 226 return 0xffL; 227 } 228 return l; 229 } 230 } 231 232 /** 233 * Constructor 234 * 235 * @param newObj is this typed array instantiated with the new operator 236 * @param self self reference 237 * @param args args 238 * 239 * @return new typed array 240 */ 241 @Constructor(arity = 1) 242 public static NativeUint8ClampedArray constructor(final boolean newObj, final Object self, final Object... args) { 243 return (NativeUint8ClampedArray)constructorImpl(newObj, args, FACTORY); 244 } 245 246 NativeUint8ClampedArray(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 247 super(buffer, byteOffset, length); 248 } 249 250 @Override 251 protected Factory factory() { 252 return FACTORY; 253 } 254 255 /** 256 * Set values 257 * @param self self reference 258 * @param array multiple values of array's type to set 259 * @param offset optional start index, interpreted 0 if undefined 260 * @return undefined 261 */ 262 @Function(attributes = Attribute.NOT_ENUMERABLE) 263 protected static Object set(final Object self, final Object array, final Object offset) { 264 return ArrayBufferView.setImpl(self, array, offset); 265 } 266 267 /** 268 * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, 269 * referencing the elements at begin, inclusive, up to end, exclusive. If either 270 * begin or end is negative, it refers to an index from the end of the array, 271 * as opposed to from the beginning. 272 * <p> 273 * If end is unspecified, the subarray contains all elements from begin to the end 274 * of the TypedArray. The range specified by the begin and end values is clamped to 275 * the valid index range for the current array. If the computed length of the new 276 * TypedArray would be negative, it is clamped to zero. 277 * <p> 278 * The returned TypedArray will be of the same type as the array on which this 279 * method is invoked. 280 * 281 * @param self self reference 282 * @param begin begin position 283 * @param end end position 284 * 285 * @return sub array 286 */ 287 @Function(attributes = Attribute.NOT_ENUMERABLE) 288 protected static NativeUint8ClampedArray subarray(final Object self, final Object begin, final Object end) { 289 return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end); 290 } 291 292 @Override 293 protected ScriptObject getPrototype(final Global global) { 294 return global.getUint8ClampedArrayPrototype(); 295 } 296} 297