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