src/jdk/nashorn/internal/objects/NativeUint32Array.java

Thu, 24 May 2018 16:39:31 +0800

author
aoqi
date
Thu, 24 May 2018 16:39:31 +0800
changeset 1959
61ffdd1b89f2
parent 1720
c09b105e7be5
parent 1490
d85f981c8cf8
permissions
-rw-r--r--

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

mercurial