NativeFloat64Array.java revision 1036:f0b5e3900a10
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;
29import java.lang.invoke.MethodHandle;
30import java.lang.invoke.MethodHandles;
31import java.nio.ByteBuffer;
32import java.nio.DoubleBuffer;
33import jdk.nashorn.internal.objects.annotations.Attribute;
34import jdk.nashorn.internal.objects.annotations.Constructor;
35import jdk.nashorn.internal.objects.annotations.Function;
36import jdk.nashorn.internal.objects.annotations.Property;
37import jdk.nashorn.internal.objects.annotations.ScriptClass;
38import jdk.nashorn.internal.objects.annotations.Where;
39import jdk.nashorn.internal.runtime.JSType;
40import jdk.nashorn.internal.runtime.PropertyMap;
41import jdk.nashorn.internal.runtime.ScriptObject;
42import jdk.nashorn.internal.runtime.arrays.ArrayData;
43import jdk.nashorn.internal.runtime.arrays.TypedArrayData;
44
45/**
46 * Float64 array for the TypedArray extension
47 */
48@ScriptClass("Float64Array")
49public final class NativeFloat64Array 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 = 8;
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 NativeFloat64Array(buffer, byteOffset, length);
64        }
65
66        @Override
67        public Float64ArrayData createArrayData(final ByteBuffer nb, final int start, final int length) {
68            return new Float64ArrayData(nb.asDoubleBuffer(), start, length);
69        }
70
71        @Override
72        public String getClassName() {
73            return "Float64Array";
74        }
75    };
76
77    private static final class Float64ArrayData extends TypedArrayData<DoubleBuffer> {
78
79        private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Float64ArrayData.class, "getElem", double.class, int.class).methodHandle();
80        private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Float64ArrayData.class, "setElem", void.class, int.class, double.class).methodHandle();
81
82        private Float64ArrayData(final DoubleBuffer nb, final int start, final int end) {
83            super(((DoubleBuffer)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 double.class;
99        }
100
101        private double getElem(final int index) {
102            try {
103                return nb.get(index);
104            } catch (final IndexOutOfBoundsException e) {
105                throw new ClassCastException(); //force relink - this works for unoptimistic too
106            }
107        }
108
109        private void setElem(final int index, final double elem) {
110            try {
111                nb.put(index, elem);
112            } catch (final IndexOutOfBoundsException e) {
113                //swallow valid array indexes. it's ok.
114                if (index < 0) {
115                    throw new ClassCastException();
116                }
117             }
118        }
119
120        @Override
121        public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
122            if (returnType == int.class || returnType == long.class) {
123                return null;
124            }
125            return getContinuousElementGetter(getClass(), GET_ELEM, returnType, programPoint);
126        }
127
128        @Override
129        public int getInt(final int index) {
130            return (int)getDouble(index);
131        }
132
133        @Override
134        public long getLong(final int index) {
135            return (long)getDouble(index);
136        }
137
138        @Override
139        public double getDouble(final int index) {
140            return getElem(index);
141        }
142
143        @Override
144        public Object getObject(final int index) {
145            return getDouble(index);
146        }
147
148        @Override
149        public ArrayData set(final int index, final Object value, final boolean strict) {
150            return set(index, JSType.toNumber(value), strict);
151        }
152
153        @Override
154        public ArrayData set(final int index, final int value, final boolean strict) {
155            return set(index, (double)value, strict);
156        }
157
158        @Override
159        public ArrayData set(final int index, final long value, final boolean strict) {
160            return set(index, (double)value, strict);
161        }
162
163        @Override
164        public ArrayData set(final int index, final double value, final boolean strict) {
165            setElem(index, value);
166            return this;
167        }
168    }
169
170    /**
171     * Constructor
172     *
173     * @param newObj is this typed array instantiated with the new operator
174     * @param self   self reference
175     * @param args   args
176     *
177     * @return new typed array
178     */
179    @Constructor(arity = 1)
180    public static NativeFloat64Array constructor(final boolean newObj, final Object self, final Object... args) {
181        return (NativeFloat64Array)constructorImpl(newObj, args, FACTORY);
182    }
183
184    NativeFloat64Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
185        super(buffer, byteOffset, length);
186    }
187
188    @Override
189    protected Factory factory() {
190        return FACTORY;
191    }
192
193    @Override
194    protected boolean isFloatArray() {
195        return true;
196    }
197
198    /**
199     * Set values
200     * @param self   self reference
201     * @param array  multiple values of array's type to set
202     * @param offset optional start index, interpreted  0 if undefined
203     * @return undefined
204     */
205    @Function(attributes = Attribute.NOT_ENUMERABLE)
206    protected static Object set(final Object self, final Object array, final Object offset) {
207        return ArrayBufferView.setImpl(self, array, offset);
208    }
209
210    /**
211     * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray,
212     * referencing the elements at begin, inclusive, up to end, exclusive. If either
213     * begin or end is negative, it refers to an index from the end of the array,
214     * as opposed to from the beginning.
215     * <p>
216     * If end is unspecified, the subarray contains all elements from begin to the end
217     * of the TypedArray. The range specified by the begin and end values is clamped to
218     * the valid index range for the current array. If the computed length of the new
219     * TypedArray would be negative, it is clamped to zero.
220     * <p>
221     * The returned TypedArray will be of the same type as the array on which this
222     * method is invoked.
223     *
224     * @param self self reference
225     * @param begin begin position
226     * @param end end position
227     *
228     * @return sub array
229     */
230    @Function(attributes = Attribute.NOT_ENUMERABLE)
231    protected static NativeFloat64Array subarray(final Object self, final Object begin, final Object end) {
232        return (NativeFloat64Array)ArrayBufferView.subarrayImpl(self, begin, end);
233    }
234
235    @Override
236    protected ScriptObject getPrototype(final Global global) {
237        return global.getFloat64ArrayPrototype();
238    }
239}
240