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