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 26 package jdk.nashorn.internal.objects; 27 28 import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; 29 30 import java.lang.invoke.MethodHandle; 31 import java.lang.invoke.MethodHandles; 32 import java.nio.ByteBuffer; 33 import java.nio.ByteOrder; 34 import java.nio.IntBuffer; 35 import jdk.nashorn.internal.objects.annotations.Attribute; 36 import jdk.nashorn.internal.objects.annotations.Constructor; 37 import jdk.nashorn.internal.objects.annotations.Function; 38 import jdk.nashorn.internal.objects.annotations.Property; 39 import jdk.nashorn.internal.objects.annotations.ScriptClass; 40 import jdk.nashorn.internal.objects.annotations.Where; 41 import jdk.nashorn.internal.runtime.JSType; 42 import jdk.nashorn.internal.runtime.PropertyMap; 43 import jdk.nashorn.internal.runtime.ScriptObject; 44 import jdk.nashorn.internal.runtime.arrays.ArrayData; 45 import jdk.nashorn.internal.runtime.arrays.TypedArrayData; 46 47 /** 48 * Uint32 array for TypedArray extension 49 */ 50 @ScriptClass("Uint32Array") 51 public final class NativeUint32Array extends ArrayBufferView { 52 /** 53 * The size in bytes of each element in the array. 54 */ 55 @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) 56 public static final int BYTES_PER_ELEMENT = 4; 57 58 // initialized by nasgen 59 @SuppressWarnings("unused") 60 private static PropertyMap $nasgenmap$; 61 62 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { 63 @Override 64 public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteBegin, final int length) { 65 return new NativeUint32Array(buffer, byteBegin, length); 66 } 67 68 @Override 69 public Uint32ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { 70 return new Uint32ArrayData(nb.order(ByteOrder.nativeOrder()).asIntBuffer(), start, end); 71 } 72 73 @Override 74 public String getClassName() { 75 return "Uint32Array"; 76 } 77 }; 78 79 private static final class Uint32ArrayData extends TypedArrayData<IntBuffer> { 80 81 private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "getElem", long.class, int.class).methodHandle(); 82 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 83 84 private Uint32ArrayData(final IntBuffer nb, final int start, final int end) { 85 super(((IntBuffer)nb.position(start).limit(end)).slice(), end - start); 86 } 87 88 @Override 89 protected MethodHandle getGetElem() { 90 return GET_ELEM; 91 } 92 93 @Override 94 protected MethodHandle getSetElem() { 95 return SET_ELEM; 96 } 97 98 @Override 99 public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { 100 if (returnType == int.class) { 101 return null; 102 } 103 return getContinuousElementGetter(getClass(), GET_ELEM, returnType, programPoint); 104 } 105 106 private long getElem(final int index) { 107 try { 108 return JSType.toUint32(nb.get(index)); 109 } catch (final IndexOutOfBoundsException e) { 110 throw new ClassCastException(); //force relink - this works for unoptimistic too 111 } 112 } 113 114 private void setElem(final int index, final int elem) { 115 try { 116 if (index < nb.limit()) { 117 nb.put(index, elem); 118 } 119 } catch (final IndexOutOfBoundsException e) { 120 throw new ClassCastException(); 121 } 122 } 123 124 @Override 125 public boolean isUnsigned() { 126 return true; 127 } 128 129 @Override 130 public Class<?> getElementType() { 131 return long.class; 132 } 133 134 @Override 135 public Class<?> getBoxedElementType() { 136 return Integer.class; 137 } 138 139 @Override 140 public int getInt(final int index) { 141 return (int)getLong(index); 142 } 143 144 @Override 145 public long getLong(final int index) { 146 return getElem(index); 147 } 148 149 @Override 150 public long getLongOptimistic(final int index, final int programPoint) { 151 return getElem(index); 152 } 153 154 @Override 155 public double getDouble(final int index) { 156 return getLong(index); 157 } 158 159 @Override 160 public double getDoubleOptimistic(final int index, final int programPoint) { 161 return getLong(index); 162 } 163 164 @Override 165 public Object getObject(final int index) { 166 return getLong(index); 167 } 168 169 @Override 170 public ArrayData set(final int index, final Object value, final boolean strict) { 171 return set(index, JSType.toInt32(value), strict); 172 } 173 174 @Override 175 public ArrayData set(final int index, final int value, final boolean strict) { 176 setElem(index, value); 177 return this; 178 } 179 180 @Override 181 public ArrayData set(final int index, final long value, final boolean strict) { 182 return set(index, (int)value, strict); 183 } 184 185 @Override 186 public ArrayData set(final int index, final double value, final boolean strict) { 187 return set(index, (int)value, strict); 188 } 189 } 190 191 /** 192 * Constructor 193 * 194 * @param newObj is this typed array instantiated with the new operator 195 * @param self self reference 196 * @param args args 197 * 198 * @return new typed array 199 */ 200 @Constructor(arity = 1) 201 public static NativeUint32Array constructor(final boolean newObj, final Object self, final Object... args) { 202 return (NativeUint32Array)constructorImpl(newObj, args, FACTORY); | 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 26 package jdk.nashorn.internal.objects; 27 28 import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; 29 30 import java.lang.invoke.MethodHandle; 31 import java.lang.invoke.MethodHandles; 32 import java.nio.ByteBuffer; 33 import java.nio.ByteOrder; 34 import java.nio.IntBuffer; 35 import jdk.nashorn.internal.codegen.types.Type; 36 import jdk.nashorn.internal.objects.annotations.Attribute; 37 import jdk.nashorn.internal.objects.annotations.Constructor; 38 import jdk.nashorn.internal.objects.annotations.Function; 39 import jdk.nashorn.internal.objects.annotations.Property; 40 import jdk.nashorn.internal.objects.annotations.ScriptClass; 41 import jdk.nashorn.internal.objects.annotations.Where; 42 import jdk.nashorn.internal.runtime.JSType; 43 import jdk.nashorn.internal.runtime.PropertyMap; 44 import jdk.nashorn.internal.runtime.ScriptObject; 45 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; 46 import jdk.nashorn.internal.runtime.arrays.ArrayData; 47 import jdk.nashorn.internal.runtime.arrays.TypedArrayData; 48 49 /** 50 * Uint32 array for TypedArray extension 51 */ 52 @ScriptClass("Uint32Array") 53 public final class NativeUint32Array extends ArrayBufferView { 54 /** 55 * The size in bytes of each element in the array. 56 */ 57 @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR) 58 public static final int BYTES_PER_ELEMENT = 4; 59 60 // initialized by nasgen 61 @SuppressWarnings("unused") 62 private static PropertyMap $nasgenmap$; 63 64 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) { 65 @Override 66 public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteBegin, final int length) { 67 return new NativeUint32Array(buffer, byteBegin, length); 68 } 69 70 @Override 71 public Uint32ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) { 72 return new Uint32ArrayData(nb.order(ByteOrder.nativeOrder()).asIntBuffer(), start, end); 73 } 74 75 @Override 76 public String getClassName() { 77 return "Uint32Array"; 78 } 79 }; 80 81 private static final class Uint32ArrayData extends TypedArrayData<IntBuffer> { 82 83 private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "getElem", double.class, int.class).methodHandle(); 84 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint32ArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); 85 86 private Uint32ArrayData(final IntBuffer nb, final int start, final int end) { 87 super(((IntBuffer)nb.position(start).limit(end)).slice(), end - start); 88 } 89 90 @Override 91 protected MethodHandle getGetElem() { 92 return GET_ELEM; 93 } 94 95 @Override 96 protected MethodHandle getSetElem() { 97 return SET_ELEM; 98 } 99 100 @Override 101 public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { 102 if (returnType == int.class) { 103 return null; 104 } 105 return getContinuousElementGetter(getClass(), GET_ELEM, returnType, programPoint); 106 } 107 108 private int getRawElem(final int index) { 109 try { 110 return nb.get(index); 111 } catch (final IndexOutOfBoundsException e) { 112 throw new ClassCastException(); //force relink - this works for unoptimistic too 113 } 114 } 115 116 private double getElem(final int index) { 117 return JSType.toUint32(getRawElem(index)); 118 } 119 120 private void setElem(final int index, final int elem) { 121 try { 122 if (index < nb.limit()) { 123 nb.put(index, elem); 124 } 125 } catch (final IndexOutOfBoundsException e) { 126 throw new ClassCastException(); 127 } 128 } 129 130 @Override 131 public boolean isUnsigned() { 132 return true; 133 } 134 135 @Override 136 public Class<?> getElementType() { 137 return double.class; 138 } 139 140 @Override 141 public Class<?> getBoxedElementType() { 142 return Double.class; 143 } 144 145 @Override 146 public int getInt(final int index) { 147 return getRawElem(index); 148 } 149 150 @Override 151 public int getIntOptimistic(final int index, final int programPoint) { 152 return JSType.toUint32Optimistic(getRawElem(index), programPoint); 153 } 154 155 @Override 156 public double getDouble(final int index) { 157 return getElem(index); 158 } 159 160 @Override 161 public double getDoubleOptimistic(final int index, final int programPoint) { 162 return getElem(index); 163 } 164 165 @Override 166 public Object getObject(final int index) { 167 return getElem(index); 168 } 169 170 @Override 171 public ArrayData set(final int index, final Object value, final boolean strict) { 172 return set(index, JSType.toInt32(value), strict); 173 } 174 175 @Override 176 public ArrayData set(final int index, final int value, final boolean strict) { 177 setElem(index, value); 178 return this; 179 } 180 181 @Override 182 public ArrayData set(final int index, final double value, final boolean strict) { 183 return set(index, (int)value, strict); 184 } 185 } 186 187 /** 188 * Constructor 189 * 190 * @param newObj is this typed array instantiated with the new operator 191 * @param self self reference 192 * @param args args 193 * 194 * @return new typed array 195 */ 196 @Constructor(arity = 1) 197 public static NativeUint32Array constructor(final boolean newObj, final Object self, final Object... args) { 198 return (NativeUint32Array)constructorImpl(newObj, args, FACTORY); |