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.CharBuffer;
  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  * Uint16 array for TypedArray extension
  48  */
  49 @ScriptClass("Uint16Array")
  50 public final class NativeUint16Array 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 = 2;
  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 NativeUint16Array(buffer, byteOffset, length);
  65         }
  66 
  67         @Override
  68         public Uint16ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
  69             return new Uint16ArrayData(nb.asCharBuffer(), start, end);
  70         }
  71 
  72         @Override
  73         public String getClassName() {
  74             return "Uint16Array";
  75         }
  76     };
  77 
  78     private static final class Uint16ArrayData extends TypedArrayData<CharBuffer> {
  79 
  80         private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint16ArrayData.class, "getElem", int.class, int.class).methodHandle();
  81         private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint16ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle();
  82 
  83         private Uint16ArrayData(final CharBuffer nb, final int start, final int end) {
  84             super(((CharBuffer)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);
 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                 if (index < nb.limit()) {
 108                     nb.put(index, (char) elem);
 109                 }
 110             } catch (final IndexOutOfBoundsException e) {
 111                 throw new ClassCastException();
 112             }
 113         }
 114 
 115         @Override
 116         public boolean isUnsigned() {
 117             return true;
 118         }
 119 
 120         @Override
 121         public Class<?> getElementType() {
 122             return int.class;
 123         }
 124 
 125         @Override
 126         public Class<?> getBoxedElementType() {
 127             return Integer.class;
 128         }
 129 
 130         @Override
 131         public int getInt(final int index) {
 132             return getElem(index);
 133         }
 134 
 135         @Override
 136         public int getIntOptimistic(final int index, final int programPoint) {
 137             return getElem(index);
 138         }
 139 
 140         @Override
 141         public long getLong(final int index) {
 142             return getInt(index);
 143         }
 144 
 145         @Override
 146         public long getLongOptimistic(final int index, final int programPoint) {
 147             return getElem(index);
 148         }
 149 
 150         @Override
 151         public double getDouble(final int index) {
 152             return getInt(index);
 153         }
 154 
 155         @Override
 156         public double getDoubleOptimistic(final int index, final int programPoint) {
 157             return getElem(index);
 158         }
 159 
 160         @Override
 161         public Object getObject(final int index) {
 162             return getInt(index);
 163         }
 164 
 165         @Override
 166         public ArrayData set(final int index, final Object value, final boolean strict) {
 167             return set(index, JSType.toInt32(value), strict);
 168         }
 169 
 170         @Override
 171         public ArrayData set(final int index, final int value, final boolean strict) {
 172             setElem(index, value);
 173             return this;
 174         }
 175 
 176         @Override
 177         public ArrayData set(final int index, final long value, final boolean strict) {
 178             return set(index, (int)value, strict);
 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 NativeUint16Array constructor(final boolean newObj, final Object self, final Object... args) {
 198         return (NativeUint16Array)constructorImpl(newObj, args, FACTORY);
 199     }
 200 
 201     NativeUint16Array(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 NativeUint16Array subarray(final Object self, final Object begin, final Object end) {
 244         return (NativeUint16Array)ArrayBufferView.subarrayImpl(self, begin, end);
 245     }
 246 
 247     @Override
 248     protected ScriptObject getPrototype(final Global global) {
 249         return global.getUint16ArrayPrototype();
 250     }
 251 }