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