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