Thu, 24 May 2018 16:39:31 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package jdk.nashorn.internal.objects; |
aoqi@0 | 27 | |
attila@963 | 28 | import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; |
attila@1056 | 29 | |
attila@963 | 30 | import java.lang.invoke.MethodHandle; |
attila@963 | 31 | import java.lang.invoke.MethodHandles; |
attila@963 | 32 | import java.nio.ByteBuffer; |
aoqi@0 | 33 | import jdk.nashorn.internal.objects.annotations.Attribute; |
aoqi@0 | 34 | import jdk.nashorn.internal.objects.annotations.Constructor; |
aoqi@0 | 35 | import jdk.nashorn.internal.objects.annotations.Function; |
aoqi@0 | 36 | import jdk.nashorn.internal.objects.annotations.Property; |
aoqi@0 | 37 | import jdk.nashorn.internal.objects.annotations.ScriptClass; |
aoqi@0 | 38 | import jdk.nashorn.internal.objects.annotations.Where; |
attila@963 | 39 | import jdk.nashorn.internal.runtime.JSType; |
aoqi@0 | 40 | import jdk.nashorn.internal.runtime.PropertyMap; |
aoqi@0 | 41 | import jdk.nashorn.internal.runtime.ScriptObject; |
aoqi@0 | 42 | import jdk.nashorn.internal.runtime.arrays.ArrayData; |
attila@963 | 43 | import jdk.nashorn.internal.runtime.arrays.TypedArrayData; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * Int8Array for the TypedArray extension |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | @ScriptClass("Int8Array") |
aoqi@0 | 49 | public final class NativeInt8Array extends ArrayBufferView { |
aoqi@0 | 50 | /** |
aoqi@0 | 51 | * The size in bytes of each element in the array. |
aoqi@0 | 52 | */ |
aoqi@0 | 53 | @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) |
aoqi@0 | 54 | public static final int BYTES_PER_ELEMENT = 1; |
aoqi@0 | 55 | |
aoqi@0 | 56 | // initialized by nasgen |
aoqi@0 | 57 | @SuppressWarnings("unused") |
aoqi@0 | 58 | private static PropertyMap $nasgenmap$; |
aoqi@0 | 59 | |
aoqi@0 | 60 | private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { |
aoqi@0 | 61 | @Override |
aoqi@0 | 62 | public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
aoqi@0 | 63 | return new NativeInt8Array(buffer, byteOffset, length); |
aoqi@0 | 64 | } |
aoqi@0 | 65 | |
aoqi@0 | 66 | @Override |
attila@963 | 67 | public Int8ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { |
attila@963 | 68 | return new Int8ArrayData(nb, start, end); |
attila@963 | 69 | } |
attila@963 | 70 | |
attila@963 | 71 | @Override |
attila@963 | 72 | public String getClassName() { |
attila@963 | 73 | return "Int8Array"; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | }; |
aoqi@0 | 76 | |
attila@963 | 77 | private static final class Int8ArrayData extends TypedArrayData<ByteBuffer> { |
attila@963 | 78 | |
attila@963 | 79 | private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Int8ArrayData.class, "getElem", int.class, int.class).methodHandle(); |
attila@963 | 80 | private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Int8ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); |
attila@963 | 81 | |
attila@963 | 82 | private Int8ArrayData(final ByteBuffer nb, final int start, final int end) { |
attila@963 | 83 | super(((ByteBuffer)nb.position(start).limit(end)).slice(), end - start); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | @Override |
attila@963 | 87 | protected MethodHandle getGetElem() { |
attila@963 | 88 | return GET_ELEM; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | @Override |
attila@963 | 92 | protected MethodHandle getSetElem() { |
attila@963 | 93 | return SET_ELEM; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | @Override |
lagergren@1028 | 97 | public Class<?> getElementType() { |
lagergren@1028 | 98 | return int.class; |
lagergren@1028 | 99 | } |
lagergren@1028 | 100 | |
lagergren@1071 | 101 | @Override |
lagergren@1071 | 102 | public Class<?> getBoxedElementType() { |
lagergren@1071 | 103 | return Integer.class; |
lagergren@1071 | 104 | } |
lagergren@1071 | 105 | |
attila@963 | 106 | private int getElem(final int index) { |
attila@963 | 107 | try { |
attila@963 | 108 | return nb.get(index); |
attila@963 | 109 | } catch (final IndexOutOfBoundsException e) { |
attila@963 | 110 | throw new ClassCastException(); //force relink - this works for unoptimistic too |
attila@963 | 111 | } |
attila@963 | 112 | } |
attila@963 | 113 | |
attila@963 | 114 | private void setElem(final int index, final int elem) { |
attila@963 | 115 | try { |
hannesw@1272 | 116 | if (index < nb.limit()) { |
hannesw@1272 | 117 | nb.put(index, (byte) elem); |
hannesw@1272 | 118 | } |
attila@963 | 119 | } catch (final IndexOutOfBoundsException e) { |
hannesw@1272 | 120 | throw new ClassCastException(); |
attila@963 | 121 | } |
sundar@774 | 122 | } |
sundar@774 | 123 | |
sundar@774 | 124 | @Override |
attila@963 | 125 | public int getInt(final int index) { |
attila@963 | 126 | return getElem(index); |
attila@963 | 127 | } |
attila@963 | 128 | |
attila@963 | 129 | @Override |
attila@1056 | 130 | public int getIntOptimistic(final int index, final int programPoint) { |
attila@1056 | 131 | return getElem(index); |
attila@1056 | 132 | } |
attila@1056 | 133 | |
attila@1056 | 134 | @Override |
attila@963 | 135 | public double getDouble(final int index) { |
attila@963 | 136 | return getInt(index); |
attila@963 | 137 | } |
attila@963 | 138 | |
attila@963 | 139 | @Override |
attila@1056 | 140 | public double getDoubleOptimistic(final int index, final int programPoint) { |
attila@1056 | 141 | return getElem(index); |
attila@1056 | 142 | } |
attila@1056 | 143 | |
attila@1056 | 144 | @Override |
attila@963 | 145 | public Object getObject(final int index) { |
attila@963 | 146 | return getInt(index); |
attila@963 | 147 | } |
attila@963 | 148 | |
attila@963 | 149 | @Override |
attila@963 | 150 | public ArrayData set(final int index, final Object value, final boolean strict) { |
attila@963 | 151 | return set(index, JSType.toInt32(value), strict); |
attila@963 | 152 | } |
attila@963 | 153 | |
attila@963 | 154 | @Override |
attila@963 | 155 | public ArrayData set(final int index, final int value, final boolean strict) { |
attila@963 | 156 | setElem(index, value); |
attila@963 | 157 | return this; |
attila@963 | 158 | } |
attila@963 | 159 | |
attila@963 | 160 | @Override |
attila@963 | 161 | public ArrayData set(final int index, final double value, final boolean strict) { |
attila@963 | 162 | return set(index, (int)value, strict); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
attila@963 | 166 | |
aoqi@0 | 167 | /** |
aoqi@0 | 168 | * Constructor |
aoqi@0 | 169 | * |
aoqi@0 | 170 | * @param newObj is this typed array instantiated with the new operator |
aoqi@0 | 171 | * @param self self reference |
aoqi@0 | 172 | * @param args args |
aoqi@0 | 173 | * |
aoqi@0 | 174 | * @return new typed array |
aoqi@0 | 175 | */ |
aoqi@0 | 176 | @Constructor(arity = 1) |
aoqi@0 | 177 | public static NativeInt8Array constructor(final boolean newObj, final Object self, final Object... args) { |
attila@963 | 178 | return (NativeInt8Array)constructorImpl(newObj, args, FACTORY); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | NativeInt8Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) { |
aoqi@0 | 182 | super(buffer, byteOffset, length); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | @Override |
aoqi@0 | 186 | protected Factory factory() { |
aoqi@0 | 187 | return FACTORY; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** |
aoqi@0 | 191 | * Set values |
aoqi@0 | 192 | * @param self self reference |
aoqi@0 | 193 | * @param array multiple values of array's type to set |
aoqi@0 | 194 | * @param offset optional start index, interpreted 0 if undefined |
aoqi@0 | 195 | * @return undefined |
aoqi@0 | 196 | */ |
aoqi@0 | 197 | @Function(attributes = Attribute.NOT_ENUMERABLE) |
aoqi@0 | 198 | protected static Object set(final Object self, final Object array, final Object offset) { |
aoqi@0 | 199 | return ArrayBufferView.setImpl(self, array, offset); |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | /** |
aoqi@0 | 203 | * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray, |
aoqi@0 | 204 | * referencing the elements at begin, inclusive, up to end, exclusive. If either |
aoqi@0 | 205 | * begin or end is negative, it refers to an index from the end of the array, |
aoqi@0 | 206 | * as opposed to from the beginning. |
aoqi@0 | 207 | * <p> |
aoqi@0 | 208 | * If end is unspecified, the subarray contains all elements from begin to the end |
aoqi@0 | 209 | * of the TypedArray. The range specified by the begin and end values is clamped to |
aoqi@0 | 210 | * the valid index range for the current array. If the computed length of the new |
aoqi@0 | 211 | * TypedArray would be negative, it is clamped to zero. |
aoqi@0 | 212 | * <p> |
aoqi@0 | 213 | * The returned TypedArray will be of the same type as the array on which this |
aoqi@0 | 214 | * method is invoked. |
aoqi@0 | 215 | * |
aoqi@0 | 216 | * @param self self reference |
aoqi@0 | 217 | * @param begin begin position |
aoqi@0 | 218 | * @param end end position |
aoqi@0 | 219 | * |
aoqi@0 | 220 | * @return sub array |
aoqi@0 | 221 | */ |
aoqi@0 | 222 | @Function(attributes = Attribute.NOT_ENUMERABLE) |
aoqi@0 | 223 | protected static NativeInt8Array subarray(final Object self, final Object begin, final Object end) { |
aoqi@0 | 224 | return (NativeInt8Array)ArrayBufferView.subarrayImpl(self, begin, end); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | @Override |
aoqi@0 | 228 | protected ScriptObject getPrototype(final Global global) { |
aoqi@0 | 229 | return global.getInt8ArrayPrototype(); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |