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