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

Wed, 03 Jul 2013 13:03:36 +0200

author
lagergren
date
Wed, 03 Jul 2013 13:03:36 +0200
changeset 405
b1980b5f00a1
parent 380
80c66d3fd872
child 414
ec84ba68ad39
permissions
-rw-r--r--

8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
Reviewed-by: sundar, attila

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.objects;
jlaskey@3 27
jlaskey@3 28 import jdk.nashorn.internal.objects.annotations.Attribute;
jlaskey@3 29 import jdk.nashorn.internal.objects.annotations.Constructor;
jlaskey@3 30 import jdk.nashorn.internal.objects.annotations.Function;
sundar@284 31 import jdk.nashorn.internal.objects.annotations.Property;
jlaskey@3 32 import jdk.nashorn.internal.objects.annotations.ScriptClass;
sundar@284 33 import jdk.nashorn.internal.objects.annotations.Where;
jlaskey@3 34 import jdk.nashorn.internal.runtime.JSType;
hannesw@380 35 import jdk.nashorn.internal.runtime.PropertyMap;
jlaskey@3 36 import jdk.nashorn.internal.runtime.ScriptObject;
jlaskey@3 37 import jdk.nashorn.internal.runtime.arrays.ArrayData;
jlaskey@3 38
jlaskey@3 39 /**
jlaskey@3 40 * Float64 array for the TypedArray extension
jlaskey@3 41 */
jlaskey@3 42 @ScriptClass("Float64Array")
sundar@82 43 public final class NativeFloat64Array extends ArrayBufferView {
sundar@292 44 /**
sundar@292 45 * The size in bytes of each element in the array.
sundar@292 46 */
sundar@284 47 @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR)
sundar@284 48 public static final int BYTES_PER_ELEMENT = 8;
sundar@284 49
hannesw@380 50 // initialized by nasgen
lagergren@405 51 @SuppressWarnings("unused")
hannesw@380 52 private static PropertyMap $nasgenmap$;
hannesw@380 53
jlaskey@3 54 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
jlaskey@3 55 @Override
jlaskey@3 56 public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
jlaskey@3 57 return new NativeFloat64Array(buffer, byteOffset, length);
jlaskey@3 58 }
jlaskey@3 59 @Override
jlaskey@3 60 public ArrayData createArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
jlaskey@3 61 return new Float64ArrayData(buffer, byteOffset, length);
jlaskey@3 62 }
jlaskey@3 63 };
jlaskey@3 64
jlaskey@3 65 private static final class Float64ArrayData extends ArrayDataImpl {
jlaskey@3 66 private Float64ArrayData(final NativeArrayBuffer buffer,
jlaskey@3 67 final int byteOffset, final int elementLength) {
jlaskey@3 68 super(buffer, byteOffset, elementLength);
jlaskey@3 69 }
jlaskey@3 70
jlaskey@3 71 @Override
jlaskey@3 72 protected int byteIndex(final int index) {
jlaskey@3 73 return index * BYTES_PER_ELEMENT + byteOffset;
jlaskey@3 74 }
jlaskey@3 75
jlaskey@3 76 @Override
jlaskey@3 77 protected double getDoubleImpl(final int index) {
jlaskey@3 78 final int byteIndex = byteIndex(index);
jlaskey@3 79 final byte[] byteArray = buffer.getByteArray();
jlaskey@3 80 final long bits;
jlaskey@3 81 bits = byteArray[byteIndex ] & 0x0000_0000_0000_00ffL |
jlaskey@3 82 (long)byteArray[byteIndex+1] << 8 & 0x0000_0000_0000_ff00L |
jlaskey@3 83 (long)byteArray[byteIndex+2] << 16 & 0x0000_0000_00ff_0000L |
jlaskey@3 84 (long)byteArray[byteIndex+3] << 24 & 0x0000_0000_ff00_0000L |
jlaskey@3 85 (long)byteArray[byteIndex+4] << 32 & 0x0000_00ff_0000_0000L |
jlaskey@3 86 (long)byteArray[byteIndex+5] << 40 & 0x0000_ff00_0000_0000L |
jlaskey@3 87 (long)byteArray[byteIndex+6] << 48 & 0x00ff_0000_0000_0000L |
jlaskey@3 88 (long)byteArray[byteIndex+7] << 56 & 0xff00_0000_0000_0000L ;
jlaskey@3 89 return Double.longBitsToDouble(bits);
jlaskey@3 90 }
jlaskey@3 91
jlaskey@3 92 @Override
jlaskey@3 93 protected int getIntImpl(final int index) {
jlaskey@3 94 return (int)getDoubleImpl(index);
jlaskey@3 95 }
jlaskey@3 96
jlaskey@3 97 @Override
jlaskey@3 98 protected long getLongImpl(final int key) {
jlaskey@3 99 return (long)getDoubleImpl(key);
jlaskey@3 100 }
jlaskey@3 101
jlaskey@3 102 @Override
jlaskey@3 103 protected Object getObjectImpl(final int key) {
jlaskey@3 104 return getDoubleImpl(key);
jlaskey@3 105 }
jlaskey@3 106
jlaskey@3 107 @Override
jlaskey@3 108 protected void setImpl(final int index, final double value) {
jlaskey@3 109 final long bits = Double.doubleToRawLongBits(value);
jlaskey@3 110 final int byteIndex = byteIndex(index);
jlaskey@3 111 @SuppressWarnings("MismatchedReadAndWriteOfArray")
jlaskey@3 112 final byte[] byteArray = buffer.getByteArray();
jlaskey@3 113 byteArray[byteIndex ] = (byte)(bits & 0xff);
jlaskey@3 114 byteArray[byteIndex+1] = (byte)(bits >>> 8 & 0xff);
jlaskey@3 115 byteArray[byteIndex+2] = (byte)(bits >>> 16 & 0xff);
jlaskey@3 116 byteArray[byteIndex+3] = (byte)(bits >>> 24 & 0xff);
jlaskey@3 117 byteArray[byteIndex+4] = (byte)(bits >>> 32 & 0xff);
jlaskey@3 118 byteArray[byteIndex+5] = (byte)(bits >>> 40 & 0xff);
jlaskey@3 119 byteArray[byteIndex+6] = (byte)(bits >>> 48 & 0xff);
jlaskey@3 120 byteArray[byteIndex+7] = (byte)(bits >>> 56 & 0xff);
jlaskey@3 121 }
jlaskey@3 122
jlaskey@3 123 @Override
jlaskey@3 124 protected void setImpl(final int key, final int value) {
jlaskey@3 125 setImpl(key, (double)value);
jlaskey@3 126 }
jlaskey@3 127
jlaskey@3 128 @Override
jlaskey@3 129 protected void setImpl(final int key, final long value) {
jlaskey@3 130 setImpl(key, (double)value);
jlaskey@3 131 }
jlaskey@3 132
jlaskey@3 133 @Override
jlaskey@3 134 protected void setImpl(final int key, final Object value) {
jlaskey@3 135 setImpl(key, JSType.toNumber(value));
jlaskey@3 136 }
jlaskey@3 137 }
jlaskey@3 138
jlaskey@3 139 /**
jlaskey@3 140 * Constructor
jlaskey@3 141 *
jlaskey@3 142 * @param newObj is this typed array instantiated with the new operator
jlaskey@3 143 * @param self self reference
jlaskey@3 144 * @param args args
jlaskey@3 145 *
jlaskey@3 146 * @return new typed array
jlaskey@3 147 */
jlaskey@3 148 @Constructor(arity = 1)
jlaskey@3 149 public static Object constructor(final boolean newObj, final Object self, final Object... args) {
jlaskey@3 150 return constructorImpl(args, FACTORY);
jlaskey@3 151 }
jlaskey@3 152
jlaskey@3 153 NativeFloat64Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
jlaskey@3 154 super(buffer, byteOffset, length);
jlaskey@3 155 }
jlaskey@3 156
jlaskey@3 157 @Override
jlaskey@3 158 protected Factory factory() {
jlaskey@3 159 return FACTORY;
jlaskey@3 160 }
jlaskey@3 161
jlaskey@3 162 @Override
jlaskey@3 163 protected boolean isFloatArray() {
jlaskey@3 164 return true;
jlaskey@3 165 }
jlaskey@3 166
jlaskey@3 167 /**
jlaskey@3 168 * Set values
jlaskey@3 169 * @param self self reference
jlaskey@3 170 * @param array multiple values of array's type to set
jlaskey@3 171 * @param offset optional start index, interpreted 0 if undefined
jlaskey@3 172 * @return undefined
jlaskey@3 173 */
jlaskey@3 174 @Function(attributes = Attribute.NOT_ENUMERABLE)
jlaskey@3 175 protected static Object set(final Object self, final Object array, final Object offset) {
jlaskey@3 176 return ArrayBufferView.setImpl(self, array, offset);
jlaskey@3 177 }
jlaskey@3 178
jlaskey@3 179 /**
jlaskey@3 180 * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray,
jlaskey@3 181 * referencing the elements at begin, inclusive, up to end, exclusive. If either
jlaskey@3 182 * begin or end is negative, it refers to an index from the end of the array,
jlaskey@3 183 * as opposed to from the beginning.
jlaskey@3 184 * <p>
jlaskey@3 185 * If end is unspecified, the subarray contains all elements from begin to the end
jlaskey@3 186 * of the TypedArray. The range specified by the begin and end values is clamped to
jlaskey@3 187 * the valid index range for the current array. If the computed length of the new
jlaskey@3 188 * TypedArray would be negative, it is clamped to zero.
jlaskey@3 189 * <p>
jlaskey@3 190 * The returned TypedArray will be of the same type as the array on which this
jlaskey@3 191 * method is invoked.
jlaskey@3 192 *
jlaskey@3 193 * @param self self reference
jlaskey@3 194 * @param begin begin position
jlaskey@3 195 * @param end end position
jlaskey@3 196 *
jlaskey@3 197 * @return sub array
jlaskey@3 198 */
jlaskey@3 199 @Function(attributes = Attribute.NOT_ENUMERABLE)
jlaskey@3 200 protected static Object subarray(final Object self, final Object begin, final Object end) {
jlaskey@3 201 return ArrayBufferView.subarrayImpl(self, begin, end);
jlaskey@3 202 }
jlaskey@3 203
jlaskey@3 204 @Override
jlaskey@3 205 protected ScriptObject getPrototype() {
jlaskey@3 206 return Global.instance().getFloat64ArrayPrototype();
jlaskey@3 207 }
jlaskey@3 208 }

mercurial