TypedArrayData.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.runtime.arrays;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29
30import java.lang.invoke.MethodHandle;
31import java.nio.Buffer;
32import jdk.internal.dynalink.CallSiteDescriptor;
33import jdk.internal.dynalink.linker.GuardedInvocation;
34import jdk.internal.dynalink.linker.LinkRequest;
35import jdk.nashorn.internal.lookup.Lookup;
36
37/**
38 * The superclass of all ArrayData used by TypedArrays
39 *
40 * @param <T> buffer implementation
41 */
42public abstract class TypedArrayData<T extends Buffer> extends ContinuousArrayData {
43
44    /** wrapped native buffer */
45    protected final T nb;
46
47    /**
48     * Constructor
49     * @param nb wrapped native buffer
50     * @param elementLength length in elements
51     */
52    protected TypedArrayData(final T nb, final int elementLength) {
53        super(elementLength); //TODO is this right?
54        this.nb = nb;
55    }
56
57    /**
58     * Length in elements. Accessed from {@code ArrayBufferView}
59     * @return element length
60     */
61    public final int getElementLength() {
62        return (int)length();
63    }
64
65    /**
66     * Is this an unsigned array data?
67     * @return true if unsigned
68     */
69    public boolean isUnsigned() {
70        return false;
71    }
72
73    /**
74     * Is this a clamped array data?
75     * @return true if clamped
76     */
77    public boolean isClamped() {
78        return false;
79    }
80
81    @Override
82    public boolean canDelete(final int index, final boolean strict) {
83        return false;
84    }
85
86    @Override
87    public boolean canDelete(final long fromIndex, final long toIndex, final boolean strict) {
88        return false;
89    }
90
91    @Override
92    public ArrayData copy() {
93        throw new UnsupportedOperationException();
94    }
95
96    @Override
97    public Object[] asObjectArray() {
98        throw new UnsupportedOperationException();
99    }
100
101    @Override
102    public void shiftLeft(final int by) {
103        throw new UnsupportedOperationException();
104    }
105
106    @Override
107    public ArrayData shiftRight(final int by) {
108        throw new UnsupportedOperationException();
109    }
110
111    @Override
112    public ArrayData ensure(final long safeIndex) {
113        return this;
114    }
115
116    @Override
117    public ArrayData shrink(final long newLength) {
118        throw new UnsupportedOperationException();
119    }
120
121    @Override
122    public final boolean has(final int index) {
123        return 0 <= index && index < length();
124    }
125
126    @Override
127    public ArrayData delete(final int index) {
128        return this;
129    }
130
131    @Override
132    public ArrayData delete(final long fromIndex, final long toIndex) {
133        return this;
134    }
135
136    @Override
137    protected ArrayData convert(final Class<?> type) {
138        throw new UnsupportedOperationException();
139    }
140
141    @Override
142    public Object pop() {
143        throw new UnsupportedOperationException();
144    }
145
146    @Override
147    public ArrayData slice(final long from, final long to) {
148        throw new UnsupportedOperationException();
149    }
150
151    /**
152     * Element getter method handle
153     * @return getter
154     */
155    protected abstract MethodHandle getGetElem();
156
157    /**
158     * Element setter method handle
159     * @return setter
160     */
161    protected abstract MethodHandle getSetElem();
162
163    @Override
164    public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
165        final MethodHandle getter = getContinuousElementGetter(getClass(), getGetElem(), returnType, programPoint);
166        if (getter != null) {
167            return Lookup.filterReturnType(getter, returnType);
168        }
169        return getter;
170    }
171
172    @Override
173    public MethodHandle getElementSetter(final Class<?> elementType) {
174        return getContinuousElementSetter(getClass(), Lookup.filterArgumentType(getSetElem(), 2, elementType), elementType);
175    }
176
177    @Override
178    protected MethodHandle getContinuousElementSetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle setHas, final Class<?> elementType) {
179        final MethodHandle mh = Lookup.filterArgumentType(setHas, 2, elementType);
180        return MH.asType(mh, mh.type().changeParameterType(0, clazz));
181    }
182
183    @Override
184    public GuardedInvocation findFastGetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) {
185        final GuardedInvocation inv = super.findFastGetIndexMethod(clazz, desc, request);
186
187        if (inv != null) {
188            return inv;
189        }
190
191        return null;
192    }
193
194    @Override
195    public GuardedInvocation findFastSetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value
196        final GuardedInvocation inv = super.findFastSetIndexMethod(clazz, desc, request);
197
198        if (inv != null) {
199            return inv;
200        }
201
202        return null;
203    }
204
205}
206