NativeUint8Array.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; 29 30import java.lang.invoke.MethodHandle; 31import java.lang.invoke.MethodHandles; 32import java.nio.ByteBuffer; 33import jdk.nashorn.internal.objects.annotations.Attribute; 34import jdk.nashorn.internal.objects.annotations.Constructor; 35import jdk.nashorn.internal.objects.annotations.Function; 36import jdk.nashorn.internal.objects.annotations.Property; 37import jdk.nashorn.internal.objects.annotations.ScriptClass; 38import jdk.nashorn.internal.objects.annotations.Where; 39import jdk.nashorn.internal.runtime.JSType; 40import jdk.nashorn.internal.runtime.PropertyMap; 41import jdk.nashorn.internal.runtime.ScriptObject; 42import jdk.nashorn.internal.runtime.arrays.ArrayData; 43import jdk.nashorn.internal.runtime.arrays.TypedArrayData; 44 45/** 46 * Uint8 array for TypedArray extension 47 */ 48@ScriptClass("Uint8Array") 49public final class NativeUint8Array extends ArrayBufferView { 50 51 /** 52 * The size in bytes of each element in the array. 53 */ 54 @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) 55 public static final int BYTES_PER_ELEMENT = 1; 56 57 // initialized by nasgen 58 @SuppressWarnings("unused") 59 private static PropertyMap $nasgenmap$; 60 61 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { 62 @Override 63 public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 64 return new NativeUint8Array(buffer, byteOffset, length); 65 } 66 67 @Override 68 public Uint8ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { 69 return new Uint8ArrayData(nb, start, end); 70 } 71 72 @Override 73 public String getClassName() { 74 return "Uint8Array"; 75 } 76 }; 77 78 private static final class Uint8ArrayData extends TypedArrayData<ByteBuffer> { 79 80 private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint8ArrayData.class, "getElem", int.class, int.class).methodHandle(); 81 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint8ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 82 83 private Uint8ArrayData(final ByteBuffer nb, final int start, final int end) { 84 super(((ByteBuffer)nb.position(start).limit(end)).slice(), end - start); 85 } 86 87 @Override 88 protected MethodHandle getGetElem() { 89 return GET_ELEM; 90 } 91 92 @Override 93 protected MethodHandle getSetElem() { 94 return SET_ELEM; 95 } 96 97 private int getElem(final int index) { 98 try { 99 return nb.get(index) & 0xff; 100 } catch (final IndexOutOfBoundsException e) { 101 throw new ClassCastException(); //force relink - this works for unoptimistic too 102 } 103 } 104 105 private void setElem(final int index, final int elem) { 106 try { 107 nb.put(index, (byte)elem); 108 } catch (final IndexOutOfBoundsException e) { 109 //swallow valid array indexes. it's ok. 110 if (index < 0) { 111 throw new ClassCastException(); 112 } 113 } 114 } 115 116 @Override 117 public boolean isUnsigned() { 118 return true; 119 } 120 121 @Override 122 public Class<?> getElementType() { 123 return int.class; 124 } 125 126 @Override 127 public int getInt(final int index) { 128 return getElem(index); 129 } 130 131 @Override 132 public int getIntOptimistic(final int index, final int programPoint) { 133 return getElem(index); 134 } 135 136 @Override 137 public long getLong(final int index) { 138 return getInt(index); 139 } 140 141 @Override 142 public long getLongOptimistic(final int index, final int programPoint) { 143 return getElem(index); 144 } 145 146 @Override 147 public double getDouble(final int index) { 148 return getInt(index); 149 } 150 151 @Override 152 public double getDoubleOptimistic(final int index, final int programPoint) { 153 return getElem(index); 154 } 155 156 @Override 157 public Object getObject(final int index) { 158 return getInt(index); 159 } 160 161 @Override 162 public ArrayData set(final int index, final Object value, final boolean strict) { 163 return set(index, JSType.toInt32(value), strict); 164 } 165 166 @Override 167 public ArrayData set(final int index, final int value, final boolean strict) { 168 setElem(index, value); 169 return this; 170 } 171 172 @Override 173 public ArrayData set(final int index, final long value, final boolean strict) { 174 return set(index, (int)value, strict); 175 } 176 177 @Override 178 public ArrayData set(final int index, final double value, final boolean strict) { 179 return set(index, (int)value, strict); 180 } 181 182 } 183 184 /** 185 * Constructor 186 * 187 * @param newObj is this typed array instantiated with the new operator 188 * @param self self reference 189 * @param args args 190 * 191 * @return new typed array 192 */ 193 @Constructor(arity = 1) 194 public static NativeUint8Array constructor(final boolean newObj, final Object self, final Object... args) { 195 return (NativeUint8Array)constructorImpl(newObj, args, FACTORY); 196 } 197 198 NativeUint8Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 199 super(buffer, byteOffset, length); 200 } 201 202 @Override 203 protected Factory factory() { 204 return FACTORY; 205 } 206 207 /** 208 * Set values 209 * @param self self reference 210 * @param array multiple values of array's type to set 211 * @param offset optional start index, interpreted 0 if undefined 212 * @return undefined 213 */ 214 @Function(attributes = Attribute.NOT_ENUMERABLE) 215 protected static Object set(final Object self, final Object array, final Object offset) { 216 return ArrayBufferView.setImpl(self, array, offset); 217 } 218 219 /** 220 * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, 221 * referencing the elements at begin, inclusive, up to end, exclusive. If either 222 * begin or end is negative, it refers to an index from the end of the array, 223 * as opposed to from the beginning. 224 * <p> 225 * If end is unspecified, the subarray contains all elements from begin to the end 226 * of the TypedArray. The range specified by the begin and end values is clamped to 227 * the valid index range for the current array. If the computed length of the new 228 * TypedArray would be negative, it is clamped to zero. 229 * <p> 230 * The returned TypedArray will be of the same type as the array on which this 231 * method is invoked. 232 * 233 * @param self self reference 234 * @param begin begin position 235 * @param end end position 236 * 237 * @return sub array 238 */ 239 @Function(attributes = Attribute.NOT_ENUMERABLE) 240 protected static NativeUint8Array subarray(final Object self, final Object begin, final Object end) { 241 return (NativeUint8Array)ArrayBufferView.subarrayImpl(self, begin, end); 242 } 243 244 @Override 245 protected ScriptObject getPrototype(final Global global) { 246 return global.getUint8ArrayPrototype(); 247 } 248} 249