# HG changeset patch # User sundar # Date 1394706504 -19800 # Node ID 64a0ac7d08e7902f33325b58e6798dfbb3249844 # Parent 5a1ae83c295f0e9d29999e14b28031dcf1154239 8015958: DataView constructor is not defined Reviewed-by: attila, hannesw, lagergren diff -r 5a1ae83c295f -r 64a0ac7d08e7 src/jdk/nashorn/internal/objects/Global.java --- a/src/jdk/nashorn/internal/objects/Global.java Wed Mar 12 11:26:00 2014 +0100 +++ b/src/jdk/nashorn/internal/objects/Global.java Thu Mar 13 15:58:24 2014 +0530 @@ -226,6 +226,10 @@ @Property(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE) public volatile Object arrayBuffer; + /** DataView object */ + @Property(name = "DataView", attributes = Attribute.NOT_ENUMERABLE) + public volatile Object dataView; + /** TypedArray (int8) */ @Property(name = "Int8Array", attributes = Attribute.NOT_ENUMERABLE) public volatile Object int8Array; @@ -352,6 +356,7 @@ private ScriptObject builtinJavaImporter; private ScriptObject builtinJavaApi; private ScriptObject builtinArrayBuffer; + private ScriptObject builtinDataView; private ScriptObject builtinInt8Array; private ScriptObject builtinUint8Array; private ScriptObject builtinUint8ClampedArray; @@ -866,6 +871,10 @@ return ScriptFunction.getPrototype(builtinArrayBuffer); } + ScriptObject getDataViewPrototype() { + return ScriptFunction.getPrototype(builtinDataView); + } + ScriptObject getInt8ArrayPrototype() { return ScriptFunction.getPrototype(builtinInt8Array); } @@ -1634,6 +1643,7 @@ private void initTypedArray() { this.builtinArrayBuffer = initConstructor("ArrayBuffer"); + this.builtinDataView = initConstructor("DataView"); this.builtinInt8Array = initConstructor("Int8Array"); this.builtinUint8Array = initConstructor("Uint8Array"); this.builtinUint8ClampedArray = initConstructor("Uint8ClampedArray"); @@ -1674,6 +1684,7 @@ this.typeError = this.builtinTypeError; this.uriError = this.builtinURIError; this.arrayBuffer = this.builtinArrayBuffer; + this.dataView = this.builtinDataView; this.int8Array = this.builtinInt8Array; this.uint8Array = this.builtinUint8Array; this.uint8ClampedArray = this.builtinUint8ClampedArray; diff -r 5a1ae83c295f -r 64a0ac7d08e7 src/jdk/nashorn/internal/objects/NativeArrayBuffer.java --- a/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Wed Mar 12 11:26:00 2014 +0100 +++ b/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Thu Mar 13 15:58:24 2014 +0530 @@ -25,6 +25,7 @@ package jdk.nashorn.internal.objects; +import java.nio.ByteBuffer; import java.util.Arrays; import jdk.nashorn.internal.objects.annotations.Attribute; import jdk.nashorn.internal.objects.annotations.Constructor; @@ -128,4 +129,16 @@ public int getByteLength() { return buffer.length; } + + ByteBuffer getBuffer() { + return ByteBuffer.wrap(buffer); + } + + ByteBuffer getBuffer(final int offset) { + return ByteBuffer.wrap(buffer, offset, buffer.length - offset); + } + + ByteBuffer getBuffer(final int offset, final int length) { + return ByteBuffer.wrap(buffer, offset, length); + } } diff -r 5a1ae83c295f -r 64a0ac7d08e7 src/jdk/nashorn/internal/objects/NativeDataView.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jdk/nashorn/internal/objects/NativeDataView.java Thu Mar 13 15:58:24 2014 +0530 @@ -0,0 +1,1015 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.nashorn.internal.objects; + +import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; +import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError; +import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import jdk.nashorn.internal.objects.annotations.Attribute; +import jdk.nashorn.internal.objects.annotations.Constructor; +import jdk.nashorn.internal.objects.annotations.Function; +import jdk.nashorn.internal.objects.annotations.Property; +import jdk.nashorn.internal.objects.annotations.ScriptClass; +import jdk.nashorn.internal.objects.annotations.SpecializedConstructor; +import jdk.nashorn.internal.objects.annotations.SpecializedFunction; +import jdk.nashorn.internal.runtime.JSType; +import jdk.nashorn.internal.runtime.PropertyMap; +import jdk.nashorn.internal.runtime.ScriptObject; +import jdk.nashorn.internal.runtime.ScriptRuntime; + +/** + *

+ * DataView builtin constructor. Based on the specification here: + * http://www.khronos.org/registry/typedarray/specs/latest/#8 + *

+ *

+ * An ArrayBuffer is a useful object for representing an arbitrary chunk of data. + * In many cases, such data will be read from disk or from the network, and will + * not follow the alignment restrictions that are imposed on the typed array views + * described earlier. In addition, the data will often be heterogeneous in nature + * and have a defined byte order. The DataView view provides a low-level interface + * for reading such data from and writing it to an ArrayBuffer. + *

+ *

+ * Regardless of the host computer's endianness, DataView reads or writes values + * to or from main memory with a specified endianness: big or little. + *

+ */ +@ScriptClass("DataView") +public class NativeDataView extends ScriptObject { + // initialized by nasgen + private static PropertyMap $nasgenmap$; + + // inherited ArrayBufferView properties + + /** + * Underlying ArrayBuffer storage object + */ + @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) + public final Object buffer; + + /** + * The offset in bytes from the start of the ArrayBuffer + */ + @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) + public final int byteOffset; + + /** + * The number of bytes from the offset that this DataView will reference + */ + @Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT) + public final int byteLength; + + // underlying ByteBuffer + private final ByteBuffer buf; + + private NativeDataView(NativeArrayBuffer arrBuf) { + this(arrBuf, arrBuf.getBuffer(), 0); + } + + private NativeDataView(NativeArrayBuffer arrBuf, int offset) { + this(arrBuf, bufferFrom(arrBuf, offset), offset); + } + + private NativeDataView(NativeArrayBuffer arrBuf, int offset, int length) { + this(arrBuf, bufferFrom(arrBuf, offset, length), offset, length); + } + + private NativeDataView(final NativeArrayBuffer arrBuf, final ByteBuffer buf, final int offset) { + this(arrBuf, buf, offset, buf.capacity() - offset); + } + + private NativeDataView(final NativeArrayBuffer arrBuf, final ByteBuffer buf, final int offset, final int length) { + super(Global.instance().getDataViewPrototype(), $nasgenmap$); + this.buffer = arrBuf; + this.byteOffset = offset; + this.byteLength = length; + this.buf = buf; + } + + /** + * Create a new DataView object using the passed ArrayBuffer for its + * storage. Optional byteOffset and byteLength can be used to limit the + * section of the buffer referenced. The byteOffset indicates the offset in + * bytes from the start of the ArrayBuffer, and the byteLength is the number + * of bytes from the offset that this DataView will reference. If both + * byteOffset and byteLength are omitted, the DataView spans the entire + * ArrayBuffer range. If the byteLength is omitted, the DataView extends from + * the given byteOffset until the end of the ArrayBuffer. + * + * If the given byteOffset and byteLength references an area beyond the end + * of the ArrayBuffer an exception is raised. + + * @param newObj if this constructor was invoked with 'new' or not + * @param self constructor function object + * @param args arguments to the constructor + * @return newly constructed DataView object + */ + @Constructor(arity = 1) + public static Object constructor(final boolean newObj, final Object self, final Object... args) { + if (args.length == 0 || !(args[0] instanceof NativeArrayBuffer)) { + throw typeError("not.an.arraybuffer.in.dataview"); + } + + final NativeArrayBuffer arrBuf = (NativeArrayBuffer) args[0]; + switch (args.length) { + case 1: + return new NativeDataView(arrBuf); + case 2: + return new NativeDataView(arrBuf, JSType.toInt32(args[1])); + default: + return new NativeDataView(arrBuf, JSType.toInt32(args[1]), JSType.toInt32(args[2])); + } + } + + /** + * Specialized version of DataView constructor + * + * @param newObj if this constructor was invoked with 'new' or not + * @param self constructor function object + * @param arrBuf underlying ArrayBuffer storage object + * @param offset offset in bytes from the start of the ArrayBuffer + * @return newly constructed DataView object + */ + @SpecializedConstructor + public static Object constructor(final boolean newObj, final Object self, final Object arrBuf, final int offset) { + if (!(arrBuf instanceof NativeArrayBuffer)) { + throw typeError("not.an.arraybuffer.in.dataview"); + } + return new NativeDataView((NativeArrayBuffer) arrBuf, offset); + } + + /** + * Specialized version of DataView constructor + * + * @param newObj if this constructor was invoked with 'new' or not + * @param self constructor function object + * @param arrBuf underlying ArrayBuffer storage object + * @param offset in bytes from the start of the ArrayBuffer + * @param length is the number of bytes from the offset that this DataView will reference + * @return newly constructed DataView object + */ + @SpecializedConstructor + public static Object constructor(final boolean newObj, final Object self, final Object arrBuf, final int offset, final int length) { + if (!(arrBuf instanceof NativeArrayBuffer)) { + throw typeError("not.an.arraybuffer.in.dataview"); + } + return new NativeDataView((NativeArrayBuffer) arrBuf, offset, length); + } + + // Gets the value of the given type at the specified byte offset + // from the start of the view. There is no alignment constraint; + // multi-byte values may be fetched from any offset. + // + // For multi-byte values, the optional littleEndian argument + // indicates whether a big-endian or little-endian value should be + // read. If false or undefined, a big-endian value is read. + // + // These methods raise an exception if they would read + // beyond the end of the view. + + /** + * Get 8-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 8-bit signed int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE) + public static int getInt8(final Object self, final Object byteOffset) { + try { + return getBuffer(self).get(JSType.toInt32(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 8-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 8-bit signed int value at the byteOffset + */ + @SpecializedFunction + public static int getInt8(final Object self, final int byteOffset) { + try { + return getBuffer(self).get(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 8-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 8-bit unsigned int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE) + public static int getUint8(final Object self, final Object byteOffset) { + try { + return (0xFF & getBuffer(self).get(JSType.toInt32(byteOffset))); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 8-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 8-bit unsigned int value at the byteOffset + */ + @SpecializedFunction + public static int getUint8(final Object self, final int byteOffset) { + try { + return (0xFF & getBuffer(self).get(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 16-bit signed int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static int getInt16(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return getBuffer(self, littleEndian).getShort(JSType.toInt32(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 16-bit signed int value at the byteOffset + */ + @SpecializedFunction + public static int getInt16(final Object self, final int byteOffset) { + try { + return getBuffer(self, false).getShort(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 16-bit signed int value at the byteOffset + */ + @SpecializedFunction + public static int getInt16(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return getBuffer(self, littleEndian).getShort(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 16-bit unsigned int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static int getUint16(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return (int) (0xFFFF & getBuffer(self, littleEndian).getShort(JSType.toInt32(byteOffset))); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 16-bit unsigned int value at the byteOffset + */ + @SpecializedFunction + public static int getUint16(final Object self, final int byteOffset) { + try { + return (int) (0xFFFF & getBuffer(self, false).getShort(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 16-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 16-bit unsigned int value at the byteOffset + */ + @SpecializedFunction + public static int getUint16(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return (int) (0xFFFF & getBuffer(self, littleEndian).getShort(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit signed int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static int getInt32(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return getBuffer(self, littleEndian).getInt(JSType.toInt32(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 32-bit signed int value at the byteOffset + */ + @SpecializedFunction + public static int getInt32(final Object self, final int byteOffset) { + try { + return getBuffer(self, false).getInt(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit signed int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit signed int value at the byteOffset + */ + @SpecializedFunction + public static int getInt32(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return getBuffer(self, littleEndian).getInt(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit unsigned int value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static long getUint32(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return (long) (0xFFFFFFFFL & getBuffer(self, littleEndian).getInt(JSType.toInt32(byteOffset))); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 32-bit unsigned int value at the byteOffset + */ + @SpecializedFunction + public static long getUint32(final Object self, final int byteOffset) { + try { + return (long) (0xFFFFFFFFL & getBuffer(self, false).getInt(JSType.toInt32(byteOffset))); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit unsigned int from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit unsigned int value at the byteOffset + */ + @SpecializedFunction + public static long getUint32(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return (long) (0xFFFFFFFFL & getBuffer(self, littleEndian).getInt(JSType.toInt32(byteOffset))); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit float value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static double getFloat32(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return getBuffer(self, littleEndian).getFloat(JSType.toInt32(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 32-bit float value at the byteOffset + */ + @SpecializedFunction + public static double getFloat32(final Object self, final int byteOffset) { + try { + return getBuffer(self, false).getFloat(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 32-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 32-bit float value at the byteOffset + */ + @SpecializedFunction + public static double getFloat32(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return getBuffer(self, littleEndian).getFloat(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 64-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 64-bit float value at the byteOffset + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1) + public static double getFloat64(final Object self, final Object byteOffset, final Object littleEndian) { + try { + return getBuffer(self, littleEndian).getDouble(JSType.toInt32(byteOffset)); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 64-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @return 64-bit float value at the byteOffset + */ + @SpecializedFunction + public static double getFloat64(final Object self, final int byteOffset) { + try { + return getBuffer(self, false).getDouble(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Get 64-bit float value from given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param littleEndian (optional) flag indicating whether to read in little endian order + * @return 64-bit float value at the byteOffset + */ + @SpecializedFunction + public static double getFloat64(final Object self, final int byteOffset, final boolean littleEndian) { + try { + return getBuffer(self, littleEndian).getDouble(byteOffset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + // Stores a value of the given type at the specified byte offset + // from the start of the view. There is no alignment constraint; + // multi-byte values may be stored at any offset. + // + // For multi-byte values, the optional littleEndian argument + // indicates whether the value should be stored in big-endian or + // little-endian byte order. If false or undefined, the value is + // stored in big-endian byte order. + // + // These methods raise an exception if they would write + // beyond the end of the view. + + /** + * Set 8-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param value byte value to set + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setInt8(final Object self, final Object byteOffset, final Object value) { + try { + getBuffer(self).put(JSType.toInt32(byteOffset), (byte)JSType.toInt32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 8-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to read from + * @param value byte value to set + * @return undefined + */ + @SpecializedFunction + public static Object setInt8(final Object self, final int byteOffset, final int value) { + try { + getBuffer(self).put(byteOffset, (byte)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 8-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value byte value to set + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setUint8(final Object self, final Object byteOffset, final Object value) { + try { + getBuffer(self).put(JSType.toInt32(byteOffset), (byte)JSType.toInt32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 8-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value byte value to set + * @return undefined + */ + @SpecializedFunction + public static Object setUint8(final Object self, final int byteOffset, final int value) { + try { + getBuffer(self).put(byteOffset, (byte)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setInt16(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putShort(JSType.toInt32(byteOffset), (short)JSType.toInt32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @return undefined + */ + @SpecializedFunction + public static Object setInt16(final Object self, final int byteOffset, final int value) { + try { + getBuffer(self, false).putShort(byteOffset, (short)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setInt16(final Object self, final int byteOffset, final int value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putShort(byteOffset, (short)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setUint16(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putShort(JSType.toInt32(byteOffset), (short)JSType.toInt32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @return undefined + */ + @SpecializedFunction + public static Object setUint16(final Object self, final int byteOffset, final int value) { + try { + getBuffer(self, false).putShort(byteOffset, (short)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 16-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value short value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setUint16(final Object self, final int byteOffset, final int value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putShort(byteOffset, (short)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setInt32(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putInt(JSType.toInt32(byteOffset), (int)JSType.toInt32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @return undefined + */ + @SpecializedFunction + public static Object setInt32(final Object self, final int byteOffset, final int value) { + try { + getBuffer(self, false).putInt(byteOffset, value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit signed int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setInt32(final Object self, final int byteOffset, final int value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putInt(byteOffset, value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setUint32(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putInt(JSType.toInt32(byteOffset), (int)JSType.toUint32(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @return undefined + */ + @SpecializedFunction + public static Object setUint32(final Object self, final int byteOffset, final long value) { + try { + getBuffer(self, false).putInt(byteOffset, (int)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit unsigned int at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value int value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setUint32(final Object self, final int byteOffset, final long value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putInt(byteOffset, (int)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value float value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setFloat32(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putFloat((int)JSType.toUint32(byteOffset), (float)JSType.toNumber(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value float value to set + * @return undefined + */ + @SpecializedFunction + public static Object setFloat32(final Object self, final int byteOffset, final double value) { + try { + getBuffer(self, false).putFloat(byteOffset, (float)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 32-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value float value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setFloat32(final Object self, final int byteOffset, final double value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putFloat(byteOffset, (float)value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 64-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value double value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2) + public static Object setFloat64(final Object self, final Object byteOffset, final Object value, final Object littleEndian) { + try { + getBuffer(self, littleEndian).putDouble((int)JSType.toUint32(byteOffset), JSType.toNumber(value)); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 64-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value double value to set + * @return undefined + */ + @SpecializedFunction + public static Object setFloat64(final Object self, final int byteOffset, final double value) { + try { + getBuffer(self, false).putDouble(byteOffset, value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + /** + * Set 64-bit float at the given byteOffset + * + * @param self DataView object + * @param byteOffset byte offset to write at + * @param value double value to set + * @param littleEndian (optional) flag indicating whether to write in little endian order + * @return undefined + */ + @SpecializedFunction + public static Object setFloat64(final Object self, final int byteOffset, final double value, final boolean littleEndian) { + try { + getBuffer(self, littleEndian).putDouble(byteOffset, value); + return UNDEFINED; + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.offset"); + } + } + + // internals only below this point + private static ByteBuffer bufferFrom(final NativeArrayBuffer nab, final int offset) { + try { + return nab.getBuffer(offset); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.constructor.offset"); + } + } + + private static ByteBuffer bufferFrom(final NativeArrayBuffer nab, final int offset, final int length) { + try { + return nab.getBuffer(offset, length); + } catch (final IndexOutOfBoundsException ioe) { + throw rangeError(ioe, "dataview.constructor.offset"); + } + } + + private static NativeDataView checkSelf(final Object self) { + if (!(self instanceof NativeDataView)) { + throw typeError("not.an.arraybuffer", ScriptRuntime.safeToString(self)); + } + return (NativeDataView)self; + } + + private static ByteBuffer getBuffer(final Object self) { + return checkSelf(self).buf; + } + + private static ByteBuffer getBuffer(final Object self, final Object littleEndian) { + return getBuffer(self, JSType.toBoolean(littleEndian)); + } + + private static ByteBuffer getBuffer(final Object self, final boolean littleEndian) { + return getBuffer(self).order(littleEndian? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); + } +} diff -r 5a1ae83c295f -r 64a0ac7d08e7 src/jdk/nashorn/internal/runtime/Context.java --- a/src/jdk/nashorn/internal/runtime/Context.java Wed Mar 12 11:26:00 2014 +0100 +++ b/src/jdk/nashorn/internal/runtime/Context.java Thu Mar 13 15:58:24 2014 +0530 @@ -649,7 +649,7 @@ * Checks that the given Class can be accessed from no permissions context. * * @param clazz Class object - * @throw SecurityException if not accessible + * @throws SecurityException if not accessible */ public static void checkPackageAccess(final Class clazz) { final SecurityManager sm = System.getSecurityManager(); @@ -666,7 +666,7 @@ * Checks that the given package name can be accessed from no permissions context. * * @param pkgName package name - * @throw SecurityException if not accessible + * @throws SecurityException if not accessible */ public static void checkPackageAccess(final String pkgName) { final SecurityManager sm = System.getSecurityManager(); diff -r 5a1ae83c295f -r 64a0ac7d08e7 src/jdk/nashorn/internal/runtime/resources/Messages.properties --- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties Wed Mar 12 11:26:00 2014 +0100 +++ b/src/jdk/nashorn/internal/runtime/resources/Messages.properties Thu Mar 13 15:58:24 2014 +0530 @@ -79,6 +79,7 @@ type.error.not.a.constructor={0} is not a constructor function type.error.not.a.file={0} is not a File type.error.not.a.bytebuffer={0} is not a java.nio.ByteBuffer +type.error.not.an.arraybuffer.in.dataview=First arg to DataView constructor must be an ArrayBuffer # operations not permitted on undefined type.error.cant.call.undefined=Cannot call undefined @@ -137,6 +138,9 @@ type.error.method.not.constructor=Java method {0} can't be used as a constructor. type.error.env.not.object=$ENV must be an Object. type.error.unsupported.java.to.type=Unsupported Java.to target type {0}. + +range.error.dataview.constructor.offset=Wrong offset or length in DataView constructor +range.error.dataview.offset=Offset is outside the bounds of the DataView range.error.inappropriate.array.length=inappropriate array length: {0} range.error.inappropriate.array.buffer.length=inappropriate array buffer length: {0} range.error.invalid.fraction.digits=fractionDigits argument to {0} must be in [0, 20] diff -r 5a1ae83c295f -r 64a0ac7d08e7 test/script/basic/dataview_endian.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/dataview_endian.js Thu Mar 13 15:58:24 2014 +0530 @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8015958: DataView constructor is not defined + * + * @test + * @run + */ + +// set/get endianess checks + +var buffer = new ArrayBuffer(4); +var dv = new DataView(buffer); + +// write (default) big endian, read big/little endian +dv.setUint16(0, 0xABCD); +Assert.assertEquals(dv.getUint16(0), 0xABCD); +Assert.assertEquals(dv.getUint16(0, false), 0xABCD); +Assert.assertEquals(dv.getUint16(0, true), 0xCDAB); + +// write little endian, read big/little endian +dv.setUint16(0, 0xABCD, true); +Assert.assertEquals(dv.getUint16(0), 0xCDAB); +Assert.assertEquals(dv.getUint16(0, false), 0xCDAB); +Assert.assertEquals(dv.getUint16(0, true), 0xABCD); + +// write explicit big endian, read big/little endian +dv.setUint16(0, 0xABCD, false); +Assert.assertEquals(dv.getUint16(0), 0xABCD); +Assert.assertEquals(dv.getUint16(0, false), 0xABCD); +Assert.assertEquals(dv.getUint16(0, true), 0xCDAB); + +// write (default) big endian, read big/little endian +dv.setUint32(0, 0xABCDEF89); +Assert.assertEquals(dv.getUint32(0), 0xABCDEF89); +Assert.assertEquals(dv.getUint32(0, false), 0xABCDEF89); +Assert.assertEquals(dv.getUint32(0, true), 0x89EFCDAB); + +// write little endian, read big/little endian +dv.setUint32(0, 0xABCDEF89, true); +Assert.assertEquals(dv.getUint32(0), 0x89EFCDAB); +Assert.assertEquals(dv.getUint32(0, false), 0x89EFCDAB); +Assert.assertEquals(dv.getUint32(0, true), 0xABCDEF89); + +// write explicit big endian, read big/little endian +dv.setUint32(0, 0xABCDEF89, false); +Assert.assertEquals(dv.getUint32(0), 0xABCDEF89); +Assert.assertEquals(dv.getUint32(0, false), 0xABCDEF89); +Assert.assertEquals(dv.getUint32(0, true), 0x89EFCDAB); diff -r 5a1ae83c295f -r 64a0ac7d08e7 test/script/basic/dataview_getset.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/dataview_getset.js Thu Mar 13 15:58:24 2014 +0530 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8015958: DataView constructor is not defined + * + * @test + * @run + */ + +// checking get/set of values of various types +// Also basic endianess check. + +var Float = Java.type("java.lang.Float"); +var Double = Java.type("java.lang.Double"); + +var DOUBLE_MIN = Double.MIN_VALUE; +var DOUBLE_MIN_NORMAL = Double.MIN_NORMAL; +var FLOAT_MIN = Float.MIN_VALUE; +var FLOAT_MIN_NORMAL = Float.MIN_NORMAL; + +var buffer = new ArrayBuffer(12); +var dv = new DataView(buffer); + +dv.setInt8(1, 123); +Assert.assertEquals(dv.getInt8(1), 123); +dv.setInt8(1, 123, true); +Assert.assertEquals(dv.getInt8(1, true), 123); + +dv.setUint8(1, 255); +Assert.assertEquals(dv.getUint8(1), 255); +dv.setUint8(1, 255, true); +Assert.assertEquals(dv.getUint8(1, true), 255); + +dv.setInt16(1, 1234); +Assert.assertEquals(dv.getInt16(1), 1234); +dv.setInt16(1, 1234, true); +Assert.assertEquals(dv.getInt16(1, true), 1234); + +dv.setUint16(1, 65535); +Assert.assertEquals(dv.getUint16(1), 65535); +dv.setUint16(1, 65535, true); +Assert.assertEquals(dv.getUint16(1, true), 65535); + +dv.setInt32(1, 1234); +Assert.assertEquals(dv.getInt32(1), 1234); +dv.setInt32(1, 1234, true); +Assert.assertEquals(dv.getInt32(1, true), 1234); + +dv.setUint32(1, 4294967295); +Assert.assertEquals(dv.getUint32(1), 4294967295); +dv.setUint32(1, 4294967295, true); +Assert.assertEquals(dv.getUint32(1, true), 4294967295); + +dv.setFloat64(1, Math.PI); +Assert.assertEquals(dv.getFloat64(1), Math.PI, DOUBLE_MIN); +dv.setFloat64(1, Math.PI, true); +Assert.assertEquals(dv.getFloat64(1, true), Math.PI, DOUBLE_MIN); + +dv.setFloat64(1, DOUBLE_MIN_NORMAL); +Assert.assertEquals(dv.getFloat64(1), DOUBLE_MIN_NORMAL, DOUBLE_MIN); +dv.setFloat64(1, DOUBLE_MIN_NORMAL, true); +Assert.assertEquals(dv.getFloat64(1, true), DOUBLE_MIN_NORMAL, DOUBLE_MIN); + +dv.setFloat32(1, 1.414); +Assert["assertEquals(float, float, float)"](dv.getFloat32(1), 1.414, FLOAT_MIN); +dv.setFloat32(1, 1.414, true); +Assert["assertEquals(float, float, float)"](dv.getFloat32(1, true), 1.414, FLOAT_MIN); + +dv.setFloat32(1, FLOAT_MIN_NORMAL); +Assert["assertEquals(float, float, float)"](dv.getFloat32(1), FLOAT_MIN_NORMAL, FLOAT_MIN); +dv.setFloat32(1, FLOAT_MIN_NORMAL, true); +Assert["assertEquals(float, float, float)"](dv.getFloat32(1, true), FLOAT_MIN_NORMAL, FLOAT_MIN); diff -r 5a1ae83c295f -r 64a0ac7d08e7 test/script/basic/dataview_new.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/dataview_new.js Thu Mar 13 15:58:24 2014 +0530 @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8015958: DataView constructor is not defined + * + * @test + * @run + */ + +// basic DataView constructor checks. + +// check ArrayBufferView property values of DataView instance +function check(dv, buf, offset, length) { + if (dv.buffer !== buf) { + fail("DataView.buffer is wrong"); + } + + if (dv.byteOffset != offset) { + fail("DataView.byteOffset = " + dv.byteOffset + ", expected " + offset); + } + + if (dv.byteLength != length) { + fail("DataView.byteLength = " + dv.byteLength + ", expected " + length); + } +} + +var buffer = new ArrayBuffer(12); +check(new DataView(buffer), buffer, 0, 12); +check(new DataView(buffer, 2), buffer, 2, 10); +check(new DataView(buffer, 4, 8), buffer, 4, 8); + +// make sure expected error is thrown +function checkError(callback, ErrorType) { + try { + callback(); + fail("Should have thrown " + ErrorType.name); + } catch (e) { + if (! (e instanceof ErrorType)) { + fail("Expected " + ErrorType.name + " got " + e); + } + } +} + +// non ArrayBuffer as first arg +checkError(function() { new DataView(344) }, TypeError); + +// illegal offset/length values +checkError(function() { new DataView(buffer, -1) }, RangeError); +checkError(function() { new DataView(buffer, 15) }, RangeError); +checkError(function() { new DataView(buffer, 1, 32) }, RangeError);