NativeInt16Array.java revision 1266:26c3094182d6
1130146Sdas/*
2143708Sdas * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3130146Sdas * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130146Sdas *
5130146Sdas * This code is free software; you can redistribute it and/or modify it
6130146Sdas * under the terms of the GNU General Public License version 2 only, as
7130146Sdas * published by the Free Software Foundation.  Oracle designates this
8130146Sdas * particular file as subject to the "Classpath" exception as provided
9130146Sdas * by Oracle in the LICENSE file that accompanied this code.
10130146Sdas *
11130146Sdas * This code is distributed in the hope that it will be useful, but WITHOUT
12130146Sdas * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13130146Sdas * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14130146Sdas * version 2 for more details (a copy is included in the LICENSE file that
15130146Sdas * accompanied this code).
16130146Sdas *
17130146Sdas * You should have received a copy of the GNU General Public License version
18130146Sdas * 2 along with this work; if not, write to the Free Software Foundation,
19130146Sdas * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20130146Sdas *
21130146Sdas * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22130146Sdas * or visit www.oracle.com if you need additional information or have any
23130146Sdas * questions.
24130146Sdas */
25130146Sdas
26130146Sdaspackage jdk.nashorn.internal.objects;
27130146Sdas
28130146Sdasimport static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
29130146Sdas
30130146Sdasimport java.lang.invoke.MethodHandle;
31130146Sdasimport java.lang.invoke.MethodHandles;
32130146Sdasimport java.nio.ByteBuffer;
33130146Sdasimport java.nio.ShortBuffer;
34226218Sdasimport jdk.nashorn.internal.objects.annotations.Attribute;
35226218Sdasimport jdk.nashorn.internal.objects.annotations.Constructor;
36226218Sdasimport jdk.nashorn.internal.objects.annotations.Function;
37226218Sdasimport jdk.nashorn.internal.objects.annotations.Property;
38130146Sdasimport jdk.nashorn.internal.objects.annotations.ScriptClass;
39130146Sdasimport jdk.nashorn.internal.objects.annotations.Where;
40130146Sdasimport jdk.nashorn.internal.runtime.JSType;
41130146Sdasimport jdk.nashorn.internal.runtime.PropertyMap;
42130146Sdasimport jdk.nashorn.internal.runtime.ScriptObject;
43130146Sdasimport jdk.nashorn.internal.runtime.arrays.ArrayData;
44130146Sdasimport jdk.nashorn.internal.runtime.arrays.TypedArrayData;
45130146Sdas
46130146Sdas/**
47130146Sdas * Int16 array for the TypedArray extension
48130146Sdas */
49130146Sdas@ScriptClass("Int16Array")
50130146Sdaspublic final class NativeInt16Array extends ArrayBufferView {
51130146Sdas
52130146Sdas    // initialized by nasgen
53130146Sdas    @SuppressWarnings("unused")
54130146Sdas    private static PropertyMap $nasgenmap$;
55130146Sdas
56130146Sdas    /**
57130146Sdas     * The size in bytes of each element in the array.
58130146Sdas     */
59130146Sdas    @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR)
60130146Sdas    public static final int BYTES_PER_ELEMENT = 2;
61130146Sdas
62130146Sdas    private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
63130146Sdas        @Override
64130146Sdas        public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
65130146Sdas            return new NativeInt16Array(buffer, byteOffset, length);
66130146Sdas        }
67130146Sdas
68130146Sdas        @Override
69130146Sdas        public Int16ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
70130146Sdas            return new Int16ArrayData(nb.asShortBuffer(), start, end);
71130146Sdas        }
72130146Sdas
73130146Sdas        @Override
74130146Sdas        public String getClassName() {
75130146Sdas            return "Int16Array";
76130146Sdas        }
77130146Sdas    };
78130146Sdas
79130146Sdas    private static final class Int16ArrayData extends TypedArrayData<ShortBuffer> {
80130146Sdas
81130146Sdas        private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Int16ArrayData.class, "getElem", int.class, int.class).methodHandle();
82130146Sdas        private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Int16ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle();
83130146Sdas
84130146Sdas        private Int16ArrayData(final ShortBuffer nb, final int start, final int end) {
85130146Sdas            super(nb.position(start).limit(end).slice(), end - start);
86130146Sdas        }
87130146Sdas
88130146Sdas        @Override
89176530Sraj        protected MethodHandle getGetElem() {
90140219Sdas            return GET_ELEM;
91130146Sdas        }
92176530Sraj
93176530Sraj        @Override
94176530Sraj        protected MethodHandle getSetElem() {
95176530Sraj            return SET_ELEM;
96130146Sdas        }
97130146Sdas
98130146Sdas        @Override
99130146Sdas        public Class<?> getElementType() {
100296111Snwhitehorn            return int.class;
101296111Snwhitehorn        }
102130146Sdas
103296111Snwhitehorn        @Override
104296111Snwhitehorn        public Class<?> getBoxedElementType() {
105130146Sdas            return Integer.class;
106296111Snwhitehorn        }
107130146Sdas
108130146Sdas        private int getElem(final int index) {
109130146Sdas            try {
110226218Sdas                return nb.get(index);
111130146Sdas            } catch (final IndexOutOfBoundsException e) {
112130146Sdas                throw new ClassCastException(); //force relink - this works for unoptimistic too
113130146Sdas            }
114130146Sdas        }
115130146Sdas
116130146Sdas        private void setElem(final int index, final int elem) {
117130146Sdas            try {
118130146Sdas                if (index < nb.limit()) {
119130146Sdas                    nb.put(index, (short) elem);
120130146Sdas                }
121130146Sdas            } catch (final IndexOutOfBoundsException e) {
122130146Sdas                throw new ClassCastException();
123226218Sdas            }
124130146Sdas        }
125130146Sdas
126130146Sdas        @Override
127130146Sdas        public int getInt(final int index) {
128130146Sdas            return getElem(index);
129130146Sdas        }
130130146Sdas
131130146Sdas        @Override
132130146Sdas        public int getIntOptimistic(final int index, final int programPoint) {
133226218Sdas            return getElem(index);
134130146Sdas        }
135130146Sdas
136130146Sdas        @Override
137130146Sdas        public long getLong(final int index) {
138130146Sdas            return getInt(index);
139130146Sdas        }
140130146Sdas
141130146Sdas        @Override
142130146Sdas        public long getLongOptimistic(final int index, final int programPoint) {
143130146Sdas            return getElem(index);
144130146Sdas        }
145130146Sdas
146130146Sdas        @Override
147226218Sdas        public double getDouble(final int index) {
148130146Sdas            return getInt(index);
149130146Sdas        }
150130146Sdas
151130146Sdas        @Override
152130146Sdas        public double getDoubleOptimistic(final int index, final int programPoint) {
153130146Sdas            return getElem(index);
154130146Sdas        }
155130146Sdas
156130146Sdas        @Override
157130146Sdas        public Object getObject(final int index) {
158130146Sdas            return getInt(index);
159130146Sdas        }
160226218Sdas
161130146Sdas        @Override
162130146Sdas        public ArrayData set(final int index, final Object value, final boolean strict) {
163130146Sdas            return set(index, JSType.toInt32(value), strict);
164130146Sdas        }
165130146Sdas
166130146Sdas        @Override
167130146Sdas        public ArrayData set(final int index, final int value, final boolean strict) {
168130146Sdas            setElem(index, value);
169226218Sdas            return this;
170130146Sdas        }
171130146Sdas
172130146Sdas        @Override
173130146Sdas        public ArrayData set(final int index, final long value, final boolean strict) {
174130146Sdas            return set(index, (int)value, strict);
175130146Sdas        }
176130146Sdas
177130146Sdas        @Override
178226218Sdas        public ArrayData set(final int index, final double value, final boolean strict) {
179130146Sdas            return set(index, (int)value, strict);
180130146Sdas        }
181130146Sdas    }
182130146Sdas
183130146Sdas    /**
184130146Sdas     * Constructor
185130146Sdas     *
186130146Sdas     * @param newObj is this typed array instantiated with the new operator
187130146Sdas     * @param self   self reference
188130146Sdas     * @param args   args
189130146Sdas     *
190130146Sdas     * @return new typed array
191130146Sdas     */
192226218Sdas    @Constructor(arity = 1)
193130146Sdas    public static NativeInt16Array constructor(final boolean newObj, final Object self, final Object... args) {
194130146Sdas        return (NativeInt16Array)constructorImpl(newObj, args, FACTORY);
195130146Sdas    }
196130146Sdas
197130146Sdas    NativeInt16Array(final NativeArrayBuffer buffer, final int byteOffset, final int byteLength) {
198130146Sdas        super(buffer, byteOffset, byteLength);
199130146Sdas    }
200130146Sdas
201130146Sdas    @Override
202226218Sdas    protected Factory factory() {
203130146Sdas        return FACTORY;
204130146Sdas    }
205130146Sdas
206130146Sdas    /**
207130146Sdas     * Set values
208130146Sdas     * @param self   self reference
209130146Sdas     * @param array  multiple values of array's type to set
210130146Sdas     * @param offset optional start index, interpreted  0 if undefined
211130146Sdas     * @return undefined
212130146Sdas     */
213130146Sdas    @Function(attributes = Attribute.NOT_ENUMERABLE)
214226218Sdas    protected static Object set(final Object self, final Object array, final Object offset) {
215130146Sdas        return ArrayBufferView.setImpl(self, array, offset);
216130146Sdas    }
217130146Sdas
218130146Sdas    /**
219130146Sdas     * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray,
220130146Sdas     * referencing the elements at begin, inclusive, up to end, exclusive. If either
221130146Sdas     * begin or end is negative, it refers to an index from the end of the array,
222130146Sdas     * as opposed to from the beginning.
223130146Sdas     * <p>
224226218Sdas     * If end is unspecified, the subarray contains all elements from begin to the end
225130146Sdas     * of the TypedArray. The range specified by the begin and end values is clamped to
226130146Sdas     * the valid index range for the current array. If the computed length of the new
227130146Sdas     * TypedArray would be negative, it is clamped to zero.
228130146Sdas     * <p>
229130146Sdas     * The returned TypedArray will be of the same type as the array on which this
230130146Sdas     * method is invoked.
231130146Sdas     *
232130146Sdas     * @param self self reference
233130146Sdas     * @param begin begin position
234130146Sdas     * @param end end position
235130146Sdas     *
236130146Sdas     * @return sub array
237130146Sdas     */
238226218Sdas    @Function(attributes = Attribute.NOT_ENUMERABLE)
239226218Sdas    protected static NativeInt16Array subarray(final Object self, final Object begin, final Object end) {
240226218Sdas        return (NativeInt16Array)ArrayBufferView.subarrayImpl(self, begin, end);
241143708Sdas    }
242130146Sdas
243130146Sdas    @Override
244130146Sdas    protected ScriptObject getPrototype(final Global global) {
245130146Sdas        return global.getInt16ArrayPrototype();
246130146Sdas    }
247130146Sdas}
248143708Sdas