NativeInt32Array.java revision 953:221a84ef44c0
199461Sobrien/* 2218822Sdim * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 399461Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 499461Sobrien * 599461Sobrien * This code is free software; you can redistribute it and/or modify it 699461Sobrien * under the terms of the GNU General Public License version 2 only, as 799461Sobrien * published by the Free Software Foundation. Oracle designates this 899461Sobrien * particular file as subject to the "Classpath" exception as provided 999461Sobrien * by Oracle in the LICENSE file that accompanied this code. 1099461Sobrien * 1199461Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT 1299461Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1399461Sobrien * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1499461Sobrien * version 2 for more details (a copy is included in the LICENSE file that 1599461Sobrien * accompanied this code). 1699461Sobrien * 1799461Sobrien * You should have received a copy of the GNU General Public License version 1899461Sobrien * 2 along with this work; if not, write to the Free Software Foundation, 19218822Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20218822Sdim * 2199461Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2299461Sobrien * or visit www.oracle.com if you need additional information or have any 2399461Sobrien * questions. 2499461Sobrien */ 2599461Sobrien 2699461Sobrienpackage jdk.nashorn.internal.objects; 27130561Sobrien 2899461Sobrienimport static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; 2999461Sobrien 30130561Sobrienimport java.lang.invoke.MethodHandle; 3199461Sobrienimport java.lang.invoke.MethodHandles; 3299461Sobrienimport java.nio.ByteBuffer; 33130561Sobrienimport java.nio.IntBuffer; 34130561Sobrienimport jdk.nashorn.internal.objects.annotations.Attribute; 35130561Sobrienimport jdk.nashorn.internal.objects.annotations.Constructor; 36130561Sobrienimport jdk.nashorn.internal.objects.annotations.Function; 37130561Sobrienimport jdk.nashorn.internal.objects.annotations.Property; 38218822Sdimimport jdk.nashorn.internal.objects.annotations.ScriptClass; 39218822Sdimimport jdk.nashorn.internal.objects.annotations.Where; 40218822Sdimimport jdk.nashorn.internal.runtime.JSType; 41130561Sobrienimport jdk.nashorn.internal.runtime.PropertyMap; 42130561Sobrienimport jdk.nashorn.internal.runtime.ScriptObject; 4399461Sobrienimport jdk.nashorn.internal.runtime.arrays.ArrayData; 4499461Sobrienimport jdk.nashorn.internal.runtime.arrays.TypedArrayData; 4599461Sobrien 4699461Sobrien/** 4799461Sobrien * Int32 array for the TypedArray extension 4899461Sobrien */ 4999461Sobrien@ScriptClass("Int32Array") 5099461Sobrienpublic final class NativeInt32Array extends ArrayBufferView { 5199461Sobrien /** 5299461Sobrien * The size in bytes of each element in the array. 5399461Sobrien */ 5499461Sobrien @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) 5599461Sobrien public static final int BYTES_PER_ELEMENT = 4; 5699461Sobrien 5799461Sobrien // initialized by nasgen 5899461Sobrien @SuppressWarnings("unused") 5999461Sobrien private static PropertyMap $nasgenmap$; 6099461Sobrien 6199461Sobrien private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { 6299461Sobrien @Override 6399461Sobrien public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 6499461Sobrien return new NativeInt32Array(buffer, byteOffset, length); 6599461Sobrien } 6699461Sobrien 6799461Sobrien @Override 68130561Sobrien public Int32ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) { 69130561Sobrien return new Int32ArrayData(nb.asIntBuffer(), start, length); 70130561Sobrien } 71130561Sobrien 72130561Sobrien @Override 7399461Sobrien public String getClassName() { 7499461Sobrien return "Int32Array"; 7599461Sobrien } 7699461Sobrien }; 7799461Sobrien 7899461Sobrien private static final class Int32ArrayData extends TypedArrayData<IntBuffer> { 7999461Sobrien 8099461Sobrien private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Int32ArrayData.class, "getElem", int.class, int.class).methodHandle(); 8199461Sobrien private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Int32ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 8299461Sobrien 8399461Sobrien private Int32ArrayData(final IntBuffer nb, final int start, final int end) { 8499461Sobrien super(((IntBuffer)nb.position(start).limit(end)).slice(), end - start); 8599461Sobrien } 8699461Sobrien 8799461Sobrien @Override 8899461Sobrien protected MethodHandle getGetElem() { 8999461Sobrien return GET_ELEM; 9099461Sobrien } 9199461Sobrien 9299461Sobrien @Override 9399461Sobrien protected MethodHandle getSetElem() { 9499461Sobrien return SET_ELEM; 9599461Sobrien } 9699461Sobrien 9799461Sobrien private int getElem(final int index) { 9899461Sobrien try { 9999461Sobrien return nb.get(index); 10099461Sobrien } catch (final IndexOutOfBoundsException e) { 10199461Sobrien throw new ClassCastException(); //force relink - this works for unoptimistic too 10299461Sobrien } 10399461Sobrien } 10499461Sobrien 10599461Sobrien private void setElem(final int index, final int elem) { 10699461Sobrien try { 10799461Sobrien nb.put(index, elem); 10899461Sobrien } catch (final IndexOutOfBoundsException e) { 10999461Sobrien if (index < 0) { 11099461Sobrien throw new ClassCastException(); 11199461Sobrien } 11299461Sobrien } 11399461Sobrien } 11499461Sobrien 11599461Sobrien @Override 11699461Sobrien public int getInt(final int index) { 11799461Sobrien return getElem(index); 11899461Sobrien } 11999461Sobrien 12099461Sobrien @Override 12199461Sobrien public long getLong(final int index) { 12299461Sobrien return getInt(index); 12399461Sobrien } 12499461Sobrien 12599461Sobrien @Override 12699461Sobrien public double getDouble(final int index) { 12799461Sobrien return getInt(index); 12899461Sobrien } 12999461Sobrien 13099461Sobrien @Override 13199461Sobrien public Object getObject(final int index) { 13299461Sobrien return getInt(index); 13399461Sobrien } 13499461Sobrien 13599461Sobrien @Override 13699461Sobrien public ArrayData set(final int index, final Object value, final boolean strict) { 13799461Sobrien return set(index, JSType.toInt32(value), strict); 13899461Sobrien } 13999461Sobrien 14099461Sobrien @Override 14199461Sobrien public ArrayData set(final int index, final int value, final boolean strict) { 14299461Sobrien setElem(index, value); 143218822Sdim return this; 144218822Sdim } 145218822Sdim 146218822Sdim @Override 147218822Sdim public ArrayData set(final int index, final long value, final boolean strict) { 148 return set(index, (int)value, strict); 149 } 150 151 @Override 152 public ArrayData set(final int index, final double value, final boolean strict) { 153 return set(index, (int)value, strict); 154 } 155 } 156 157 /** 158 * Constructor 159 * 160 * @param newObj is this typed array instantiated with the new operator 161 * @param self self reference 162 * @param args args 163 * 164 * @return new typed array 165 */ 166 @Constructor(arity = 1) 167 public static NativeInt32Array constructor(final boolean newObj, final Object self, final Object... args) { 168 return (NativeInt32Array)constructorImpl(newObj, args, FACTORY); 169 } 170 171 NativeInt32Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) { 172 super(buffer, byteOffset, length); 173 } 174 175 @Override 176 protected Factory factory() { 177 return FACTORY; 178 } 179 180 /** 181 * Set values 182 * @param self self reference 183 * @param array multiple values of array's type to set 184 * @param offset optional start index, interpreted 0 if undefined 185 * @return undefined 186 */ 187 @Function(attributes = Attribute.NOT_ENUMERABLE) 188 protected static Object set(final Object self, final Object array, final Object offset) { 189 return ArrayBufferView.setImpl(self, array, offset); 190 } 191 192 /** 193 * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, 194 * referencing the elements at begin, inclusive, up to end, exclusive. If either 195 * begin or end is negative, it refers to an index from the end of the array, 196 * as opposed to from the beginning. 197 * <p> 198 * If end is unspecified, the subarray contains all elements from begin to the end 199 * of the TypedArray. The range specified by the begin and end values is clamped to 200 * the valid index range for the current array. If the computed length of the new 201 * TypedArray would be negative, it is clamped to zero. 202 * <p> 203 * The returned TypedArray will be of the same type as the array on which this 204 * method is invoked. 205 * 206 * @param self self reference 207 * @param begin begin position 208 * @param end end position 209 * 210 * @return sub array 211 */ 212 @Function(attributes = Attribute.NOT_ENUMERABLE) 213 protected static NativeInt32Array subarray(final Object self, final Object begin, final Object end) { 214 return (NativeInt32Array)ArrayBufferView.subarrayImpl(self, begin, end); 215 } 216 217 @Override 218 protected ScriptObject getPrototype(final Global global) { 219 return global.getInt32ArrayPrototype(); 220 } 221} 222