24 Structured Data

24.1 ArrayBuffer Objects

24.1.1 Abstract Operations For ArrayBuffer Objects

24.1.1.1 AllocateArrayBuffer( constructor, byteLength )

The abstract operation AllocateArrayBuffer with arguments constructor and byteLength is used to create an ArrayBuffer object. It performs the following steps:

  1. Let obj be OrdinaryCreateFromConstructor(constructor, "%ArrayBufferPrototype%", «‍[[ArrayBufferData]], [[ArrayBufferByteLength]]» ).
  2. ReturnIfAbrupt(obj).
  3. Assert: byteLength is a positive integer.
  4. Let block be CreateByteDataBlock(byteLength).
  5. ReturnIfAbrupt(block).
  6. Set obj’s [[ArrayBufferData]] internal slot to block.
  7. Set obj's [[ArrayBufferByteLength]] internal slot to byteLength.
  8. Return obj.

24.1.1.2 IsDetachedBuffer( arrayBuffer )

The abstract operation IsDetachedBuffer with argument arrayBuffer performs the following steps:

  1. Assert: Type(arrayBuffer) is Object and it has an [[ArrayBufferData]] internal slot.
  2. If arrayBuffer’s [[ArrayBufferData]] internal slot is null, return true.
  3. Return false.

24.1.1.3 DetachArrayBuffer( arrayBuffer )

The abstract operation DetachArrayBuffer with argument arrayBuffer performs the following steps:

  1. Assert: Type(arrayBuffer) is Object and it has [[ArrayBufferData]] and [[ArrayBufferByteLength]] internal slots.
  2. Set arrayBuffer’s [[ArrayBufferData]] internal slot to null.
  3. Set arrayBuffer's [[ArrayBufferByteLength]] internal slot to 0.
  4. Return NormalCompletion(null).

NOTE Detaching an ArrayBuffer instance disassociates the Data Block used as its backing store from the instance and sets the byte length of the buffer to 0. No operations defined by this specification use the DetachArrayBuffer abstract operation. However, an ECMAScript implementation or host environment may define such operations.

24.1.1.4 CloneArrayBuffer( srcBuffer, srcByteOffset [, cloneConstructor] )

The abstract operation CloneArrayBuffer takes three parameters, an ArrayBuffer srcBuffer, an integer srcByteOffset and optionally a constructor function cloneConstructor. It creates a new ArrayBuffer whose data is a copy of srcBuffer’s data starting at srcByteOffset. This operation performs the following steps:

  1. Assert: Type(srcBuffer) is Object and it has an [[ArrayBufferData]] internal slot.
  2. If cloneConstructor is not present, then
    1. Let cloneConstructor be SpeciesConstructor(srcBuffer, %ArrayBuffer%).
    2. ReturnIfAbrupt(cloneConstructor).
    3. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
  3. Else, Assert: IsConstructor(cloneConstructor) is true.
  4. Let srcLength be the value of srcBuffer's [[ArrayBufferByteLength]] internal slot.
  5. Assert: srcByteOffsetsrcLength.
  6. Let cloneLength be srcLengthsrcByteOffset.
  7. Let srcBlock be the value of srcBuffer’s [[ArrayBufferData]] internal slot.
  8. Let targetBuffer be AllocateArrayBuffer(cloneConstructor, cloneLength).
  9. ReturnIfAbrupt(targetBuffer).
  10. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
  11. Let targetBlock be the value of targetBuffer’s [[ArrayBufferData]] internal slot.
  12. Perform CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, cloneLength).
  13. Return targetBuffer.

24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isLittleEndian )

The abstract operation GetValueFromBuffer takes four parameters, an ArrayBuffer arrayBuffer, an integer byteIndex, a String type, and optionally a Boolean isLittleEndian. This operation performs the following steps:

  1. Assert: IsDetachedBuffer(arrayBuffer) is false.
  2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
  3. Assert: byteIndex is a positive integer.
  4. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
  5. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
  6. Let rawValue be a List of elementSize containing, in order, the elementSize sequence of bytes starting with block[byteIndex].
  7. If isLittleEndian is not present, set isLittleEndian to either true or false. The choice is implementation dependent and should be the alternative that is most efficient for the implementation. An implementation must use the same value each time this step is executed and the same value must be used for the corresponding step in the SetValueInBuffer abstract operation.
  8. If isLittleEndian is false, reverse the order of the elements of rawValue.
  9. If type is "Float32", then
    1. Let value be the byte elements of rawValue concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2008 binary32 value.
    2. If value is an IEEE 754-2008 binary32 NaN value, return the NaN Number value.
    3. Return the Number value that corresponds to value.
  10. If type is "Float64", then
    1. Let value be the byte elements of rawValue concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2008 binary64 value.
    2. If value is an IEEE 754-2008 binary64 NaN value, return the NaN Number value.
    3. Return the Number value that corresponds to value.
  11. If the first code unit of type is "U", then
    1. Let intValue be the byte elements of rawValue concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
  12. Else
    1. Let intValue be the byte elements of rawValue concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8.
  13. Return the Number value that corresponds to intValue.

24.1.1.6 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isLittleEndian )

The abstract operation SetValueInBuffer takes five parameters, an ArrayBuffer arrayBuffer, an integer byteIndex, a String type, a Number value, and optionally a Boolean isLittleEndian. This operation performs the following steps:

  1. Assert: IsDetachedBuffer(arrayBuffer) is false.
  2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
  3. Assert: byteIndex is a positive integer.
  4. Assert: Type(value) is Number.
  5. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
  6. Assert: block is not undefined.
  7. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
  8. If isLittleEndian is not present, set isLittleEndian to either true or false. The choice is implementation dependent and should be the alternative that is most efficient for the implementation. An implementation must use the same value each time this step is executed and the same value must be used for the corresponding step in the GetValueFromBuffer abstract operation.
  9. If type is "Float32", then
    1. Set rawBytes to a List containing the 4 bytes that are the result of converting value to IEEE 754-2008 binary32 format using “Round to nearest, ties to even” rounding mode. If isLittleEndian is false, the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value is NaN, rawValue may be set to any implementation chosen IEEE 754-2008 binary64 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable NaN value.
  10. Else, if type is "Float64", then
    1. Set rawBytes to a List containing the 8 bytes that are the IEEE 754-2008 binary64 format encoding of value. If isLittleEndian is false, the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value is NaN, rawValue may be set to any implementation chosen IEEE 754-2008 binary32 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable NaN value.
  11. Else,
    1. Let n be the Number value of the Element Size specified in Table 49 for Element Type type.
    2. Let convOp be the abstract operation named in the Conversion Operation column in Table 49 for Element Type type.
    3. Let intValue be convOp(value).
    4. If intValue ≥ 0, then
      1. Let rawBytes be a List containing the n-byte binary encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
    5. Else,
      1. Let rawBytes be a List containing the n-byte binary 2's complement encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
  12. Store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
  13. Return NormalCompletion(undefined).

24.1.2 The ArrayBuffer Constructor

The ArrayBuffer constructor is the %ArrayBuffer% intrinsic object and the initial value of the ArrayBuffer property of the global object. When called as a constructor it creates and initializes a new ArrayBuffer object. ArrayBuffer is not intended to be called as a function and will throw an exception when called in that manner.

The ArrayBuffer constructor is designed to be subclassable. It may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified ArrayBuffer behaviour must include a super call to the ArrayBuffer constructor to create and initialize subclass instances with the internal state necessary to support the ArrayBuffer.prototype built-in methods.

24.1.2.1 ArrayBuffer( length )

ArrayBuffer called with argument length performs the following steps:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Let numberLength be ToNumber(length).
  3. Let byteLength be ToLength(numberLength).
  4. ReturnIfAbrupt(byteLength).
  5. If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.
  6. Return AllocateArrayBuffer(NewTarget, byteLength).

24.1.3 Properties of the ArrayBuffer Constructor

The value of the [[Prototype]] internal slot of the ArrayBuffer constructor is the intrinsic object %FunctionPrototype% (19.2.3).

Besides its length property (whose value is 1), the ArrayBuffer constructor has the following properties:

24.1.3.1 ArrayBuffer.isView ( arg )

The isView function takes one argument arg, and performs the following steps are taken:

  1. If Type(arg) is not Object, return false.
  2. If arg has a [[ViewedArrayBuffer]] internal slot, return true.
  3. Return false.

24.1.3.2 ArrayBuffer.prototype

The initial value of ArrayBuffer.prototype is the intrinsic object %ArrayBufferPrototype% (24.1.4).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

24.1.3.3 get ArrayBuffer [ @@species ]

ArrayBuffer[@@species] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Return the this value.

The value of the name property of this function is "get [Symbol.species]".

NOTE ArrayBuffer prototype methods normally use their this object’s constructor to create a derived object. However, a subclass constructor may over-ride that default behaviour by redefining its @@species property.

24.1.4 Properties of the ArrayBuffer Prototype Object

The ArrayBuffer prototype object is the intrinsic object %ArrayBufferPrototype%. The value of the [[Prototype]] internal slot of the ArrayBuffer prototype object is the intrinsic object %ObjectPrototype% (19.1.3). The ArrayBuffer prototype object is an ordinary object. It does not have an [[ArrayBufferData]] or [[ArrayBufferByteLength]] internal slot.

24.1.4.1 get ArrayBuffer.prototype.byteLength

ArrayBuffer.prototype.byteLength is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
  4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
  5. Let length be the value of O's [[ArrayBufferByteLength]] internal slot.
  6. Return length.

24.1.4.2 ArrayBuffer.prototype.constructor

The initial value of ArrayBuffer.prototype.constructor is the intrinsic object %ArrayBuffer%.

24.1.4.3 ArrayBuffer.prototype.slice ( start, end )

The following steps are taken:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
  4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
  5. Let len be the value of O's [[ArrayBufferByteLength]] internal slot.
  6. Let relativeStart be ToInteger(start).
  7. ReturnIfAbrupt(relativeStart).
  8. If relativeStart < 0, let first be max((len + relativeStart),0); else let first be min(relativeStart, len).
  9. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
  10. ReturnIfAbrupt(relativeEnd).
  11. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
  12. Let newLen be max(final-first,0).
  13. Let ctor be SpeciesConstructor(O, %ArrayBuffer%).
  14. ReturnIfAbrupt(ctor).
  15. Let new be Construct(ctor, «newLen»).
  16. ReturnIfAbrupt(new).
  17. If new does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
  18. If IsDetachedBuffer(new) is true, throw a TypeError exception.
  19. If SameValue(new, O) is true, throw a TypeError exception.
  20. If the value of new's [[ArrayBufferByteLength]] internal slot < newLen, throw a TypeError exception.
  21. NOTE: Side-effects of the above steps may have detached O.
  22. If IsDetachedBuffer(O) is true, throw a TypeError exception.
  23. Let fromBuf be the value of O's [[ArrayBufferData]] internal slot.
  24. Let toBuf be the value of new's [[ArrayBufferData]] internal slot.
  25. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
  26. Return new.

24.1.4.4 ArrayBuffer.prototype [ @@toStringTag ]

The initial value of the @@toStringTag property is the String value "ArrayBuffer".

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

24.1.5 Properties of the ArrayBuffer Instances

ArrayBuffer instances inherit properties from the ArrayBuffer prototype object. ArrayBuffer instances each have an [[ArrayBufferData]] internal slot and an [[ArrayBufferByteLength]] internal slot.

ArrayBuffer instances whose [[ArrayBufferData]] is null are considered to be detached and all operators to access or modify data contained in the ArrayBuffer instance will fail.

24.2 DataView Objects

24.2.1 Abstract Operations For DataView Objects

24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )

The abstract operation GetViewValue with arguments view, requestIndex, isLittleEndian, and type is used by functions on DataView instances is to retrieve values from the view's buffer. It performs the following steps:

  1. If Type(view) is not Object, throw a TypeError exception.
  2. If view does not have a [[DataView]] internal slot, throw a TypeError exception.
  3. Let numberIndex be ToNumber(requestIndex).
  4. Let getIndex be ToInteger(numberIndex).
  5. ReturnIfAbrupt(getIndex).
  6. If numberIndexgetIndex or getIndex < 0, throw a RangeError exception.
  7. Let isLittleEndian be ToBoolean(isLittleEndian).
  8. Let buffer be the value of view’s [[ViewedArrayBuffer]] internal slot.
  9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  10. Let viewOffset be the value of view’s [[ByteOffset]] internal slot.
  11. Let viewSize be the value of view’s [[ByteLength]] internal slot.
  12. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
  13. If getIndex +elementSize > viewSize, throw a RangeError exception.
  14. Let bufferIndex be getIndex + viewOffset.
  15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).

24.2.1.2 SetViewValue ( view, requestIndex, isLittleEndian, type, value )

The abstract operation SetViewValue with arguments view, requestIndex, isLittleEndian, type, and value is used by functions on DataView instances to store values into the view's buffer. It performs the following steps:

  1. If Type(view) is not Object, throw a TypeError exception.
  2. If view does not have a [[DataView]] internal slot, throw a TypeError exception.
  3. Let numberIndex be ToNumber(requestIndex).
  4. Let getIndex be ToInteger(numberIndex).
  5. ReturnIfAbrupt(getIndex).
  6. If numberIndexgetIndex or getIndex < 0, throw a RangeError exception.
  7. Let isLittleEndian be ToBoolean(isLittleEndian).
  8. Let buffer be the value of view’s [[ViewedArrayBuffer]] internal slot.
  9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  10. Let viewOffset be the value of view’s [[ByteOffset]] internal slot.
  11. Let viewSize be the value of view’s [[ByteLength]] internal slot.
  12. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
  13. If getIndex +elementSize > viewSize, throw a RangeError exception.
  14. Let bufferIndex be getIndex + viewOffset.
  15. Return SetValueInBuffer(buffer, bufferIndex, type, value, isLittleEndian).

NOTE The algorithms for GetViewValue and SetViewValue are identical except for their final steps.

24.2.2 The DataView Constructor

The DataView constructor is the %DataView% intrinsic object and the initial value of the DataView property of the global object. When called as a constructor it creates and initializes a new DataView object. DataView is not intended to be called as a function and will throw an exception when called in that manner.

The DataView constructor is designed to be subclassable. It may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified DataView behaviour must include a super call to the DataView constructor to create and initialize subclass instances with the internal state necessary to support the DataView.prototype built-in methods.

24.2.2.1 DataView (buffer [ , byteOffset [ , byteLength ] ] )

DataView called with arguments buffer, byteOffset, and length performs the following steps:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. If Type(buffer) is not Object, throw a TypeError exception.
  3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
  4. Let numberOffset be ToNumber(byteOffset).
  5. Let offset be ToInteger(numberOffset).
  6. ReturnIfAbrupt(offset).
  7. If numberOffsetoffset or offset < 0, throw a RangeError exception.
  8. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  9. Let bufferByteLength be the value of buffer’s [[ArrayBufferByteLength]] internal slot.
  10. If offset > bufferByteLength, throw a RangeError exception.
  11. If byteLength is undefined, then
    1. Let viewByteLength be bufferByteLengthoffset.
  12. Else,
    1. Let viewByteLength be ToLength(byteLength).
    2. ReturnIfAbrupt(viewByteLength).
    3. If offset+viewByteLength > bufferByteLength, throw a RangeError exception.
  13. Let O be OrdinaryCreateFromConstructor(NewTarget, "%DataViewPrototype%", «‍[[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]]» ).
  14. ReturnIfAbrupt(O).
  15. Set O’s [[DataView]] internal slot to true.
  16. Set O’s [[ViewedArrayBuffer]] internal slot to buffer.
  17. Set O's [[ByteLength]] internal slot to viewByteLength.
  18. Set O's [[ByteOffset]] internal slot to offset.
  19. Return O.

24.2.3 Properties of the DataView Constructor

The value of the [[Prototype]] internal slot of the DataView constructor is the intrinsic object %FunctionPrototype% (19.2.3).

Besides the length property (whose value is 3), the DataView constructor has the following properties:

24.2.3.1 DataView.prototype

The initial value of DataView.prototype is the intrinsic object %DataViewPrototype% (24.2.4).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

24.2.4 Properties of the DataView Prototype Object

The DataView prototype object is the intrinsic object %DataViewPrototype%. The value of the [[Prototype]] internal slot of the DataView prototype object is the intrinsic object %ObjectPrototype% (19.1.3). The DataView prototype object is an ordinary object. It does not have a [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], or [[ByteOffset]] internal slot.

24.2.4.1 get DataView.prototype.buffer

DataView.prototype.buffer is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. Return buffer.

24.2.4.2 get DataView.prototype.byteLength

DataView.prototype.byteLength is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  6. Let size be the value of O's [[ByteLength]] internal slot.
  7. Return size.

24.2.4.3 get DataView.prototype.byteOffset

DataView.prototype.byteOffset is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  6. Let offset be the value of O's [[ByteOffset]] internal slot.
  7. Return offset.

24.2.4.4 DataView.prototype.constructor

The initial value of DataView.prototype.constructor is the intrinsic object %DataView%.

24.2.4.5 DataView.prototype.getFloat32 ( byteOffset [ , littleEndian ] )

When the getFloat32 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Float32").

24.2.4.6 DataView.prototype.getFloat64 ( byteOffset [ , littleEndian ] )

When the getFloat64 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Float64").

24.2.4.7 DataView.prototype.getInt8 ( byteOffset )

When the getInt8 method is called with argument byteOffset the following steps are taken:

  1. Let v be the this value.
  2. Return GetViewValue(v, byteOffset, true, "Int8").

24.2.4.8 DataView.prototype.getInt16 ( byteOffset [ , littleEndian ] )

When the getInt16 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Int16").

24.2.4.9 DataView.prototype.getInt32 ( byteOffset [ , littleEndian ] )

When the getInt32 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be undefined.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Int32").

24.2.4.10 DataView.prototype.getUint8 ( byteOffset )

When the getUint8 method is called with argument byteOffset the following steps are taken:

  1. Let v be the this value.
  2. Return GetViewValue(v, byteOffset, true, "Uint8").

24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )

When the getUint16 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Uint16").

24.2.4.12 DataView.prototype.getUint32 ( byteOffset [ , littleEndian ] )

When the getUint32 method is called with argument byteOffset and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return GetViewValue(v, byteOffset, littleEndian, "Uint32").

24.2.4.13 DataView.prototype.setFloat32 ( byteOffset, value [ , littleEndian ] )

When the setFloat32 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Float32", value).

24.2.4.14 DataView.prototype.setFloat64 ( byteOffset, value [ , littleEndian ] )

When the setFloat64 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Float64", value).

24.2.4.15 DataView.prototype.setInt8 ( byteOffset, value )

When the setInt8 method is called with arguments byteOffset and value the following steps are taken:

  1. Let v be the this value.
  2. Return SetViewValue(v, byteOffset, true, "Int8", value).

24.2.4.16 DataView.prototype.setInt16 ( byteOffset, value [ , littleEndian ] )

When the setInt16 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Int16", value).

24.2.4.17 DataView.prototype.setInt32 ( byteOffset, value [ , littleEndian ] )

When the setInt32 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Int32", value).

24.2.4.18 DataView.prototype.setUint8 ( byteOffset, value )

When the setUint8 method is called with arguments byteOffset and value the following steps are taken:

  1. Let v be the this value.
  2. Return SetViewValue(v, byteOffset, true, "Uint8", value).

24.2.4.19 DataView.prototype.setUint16 ( byteOffset, value [ , littleEndian ] )

When the setUint16 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Uint16", value).

24.2.4.20 DataView.prototype.setUint32 ( byteOffset, value [ , littleEndian ] )

When the setUint32 method is called with arguments byteOffset and value and optional argument littleEndian the following steps are taken:

  1. Let v be the this value.
  2. If littleEndian is not present, let littleEndian be false.
  3. Return SetViewValue(v, byteOffset, littleEndian, "Uint32", value).

24.2.4.21 DataView.prototype[ @@toStringTag ]

The initial value of the @@toStringTag property is the String value "DataView".

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

24.2.5 Properties of DataView Instances

DataView instances are ordinary objects that inherit properties from the DataView prototype object. DataView instances each have [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], and [[ByteOffset]] internal slots.

NOTE The value of the [[DataView]] internal slot is not used within this specification. The simple presence of that internal slot is used within the specification to identify objects created using the DataView constructor.

24.3 The JSON Object

The JSON object is the %JSON% intrinsic object and the initial value of the JSON property of the global object. The JSON object is a single ordinary object that contains two functions, parse and stringify, that are used to parse and construct JSON texts. The JSON Data Interchange Format is defined in ECMA-404. The JSON interchange format used in this specification is exactly that described by ECMA-404.

Conforming implementations of JSON.parse and JSON.stringify must support the exact interchange format described in the ECMA-404 specification without any deletions or extensions to the format.

The value of the [[Prototype]] internal slot of the JSON object is the intrinsic object %ObjectPrototype% (19.1.3). The value of the [[Extensible]] internal slot of the JSON object is set to true.

The JSON object does not have a [[Construct]] internal method; it is not possible to use the JSON object as a constructor with the new operator.

The JSON object does not have a [[Call]] internal method; it is not possible to invoke the JSON object as a function.

24.3.1 JSON.parse ( text [ , reviver ] )

The parse function parses a JSON text (a JSON-formatted String) and produces an ECMAScript value. The JSON format is a subset of the syntax for ECMAScript literals, Array Initializers and Object Initializers. After parsing, JSON objects are realized as ECMAScript objects. JSON arrays are realized as ECMAScript Array instances. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and null.

The optional reviver parameter is a function that takes two parameters, key and value. It can filter and transform the results. It is called with each of the key/value pairs produced by the parse, and its return value is used instead of the original value. If it returns what it received, the structure is not modified. If it returns undefined then the property is deleted from the result.

  1. Let JText be ToString(text).
  2. ReturnIfAbrupt(JText).
  3. Parse JText interpreted as UTF-16 encoded Unicode points (6.1.4) as a JSON text as specified in
    ECMA-404. Throw a SyntaxError exception if JText is not a valid JSON text as defined in that specification.
  4. Let scriptText be the result of concatenating "(", JText, and ");".
  5. Let completion be the result of parsing and evaluating scriptText as if it was the source text of an ECMAScript Script. but using the alternative definition of DoubleStringCharacter provided below. The extended PropertyDefinitionEvaluation semantics defined in B.3.1 must not be used during the evaluation.
  6. Let unfiltered be completion.[[value]].
  7. Assert: unfiltered will be either a primitive value or an object that is defined by either an ArrayLiteral or an ObjectLiteral.
  8. If IsCallable(reviver) is true, then
    1. Let root be ObjectCreate(%ObjectPrototype%).
    2. Let rootName be the empty String.
    3. Let status be CreateDataProperty(root, rootName, unfiltered).
    4. Assert: status is true.
    5. Return InternalizeJSONProperty(root, rootName).
  9. Else
    1. Return unfiltered.

JSON allows Unicode code units 0x2028 (LINE SEPARATOR) and 0x2029 (PARAGRAPH SEPARATOR) to directly appear in String literals without using an escape sequence. This is enabled by using the following alternative definition of DoubleStringCharacter when parsing scriptText in step 5:

DoubleStringCharacter ::
SourceCharacter but not one of " or \ or U+0000 through U+001F
\ EscapeSequence
  • The SV of DoubleStringCharacter :: SourceCharacter but not one of " or \ or U+0000 through U+001F is the UTF16Encoding (10.1.1) of the code point value of SourceCharacter.

NOTE The syntax of a valid JSON text is a subset of the ECMAScript PrimaryExpression syntax. Hence a valid JSON text is also a valid PrimaryExpression. Step 3 above verifies that JText conforms to that subset. When scriptText is parsed and evaluated as a Script the result will be either a String, Number, Boolean, or Null primitive value or an Object defined as if by an ArrayLiteral or ObjectLiteral.

24.3.1.1 Runtime Semantics: InternalizeJSONProperty( holder, name)

The abstract operation InternalizeJSONProperty is a recursive abstract operation that takes two parameters: a holder object and the String name of a property in that object. InternalizeJSONProperty uses the value of reviver that was originally passed to the above parse function.

  1. Let val be Get(holder, name).
  2. ReturnIfAbrupt(val).
  3. If Type(val) is Object, then
    1. Let isArray be IsArray(val).
    2. ReturnIfAbrupt(isArray).
    3. If isArray is true, then
      1. Set I to 0.
      2. Let len be ToLength(Get(val, "length")).
      3. ReturnIfAbrupt(len).
      4. Repeat while I < len,
        1. Let newElement be InternalizeJSONProperty(val, ToString(I)).
        2. ReturnIfAbrupt(newElement).
        3. If newElement is undefined, then
          1. Let status be val.[[Delete]](ToString(I)).
        4. Else
          1. Let status be CreateDataProperty(val, ToString(I), newElement).
          2. NOTE This algorithm intentionally does not throw an exception if status is false.
        5. ReturnIfAbrupt(status).
        6. Add 1 to I.
    4. Else
      1. Let keys be EnumerableOwnNames(val).
      2. ReturnIfAbrupt(keys).
      3. For each String P in keys do,
        1. Let newElement be InternalizeJSONProperty(val, P).
        2. ReturnIfAbrupt(newElement).
        3. If newElement is undefined, then
          1. Let status be val.[[Delete]](P).
        4. Else
          1. Let status be CreateDataProperty(val, P, newElement).
          2. NOTE This algorithm intentionally does not throw an exception if status is false.
        5. ReturnIfAbrupt(status).
  4. Return Call(reviver, holder, «name, val»).

It is not permitted for a conforming implementation of JSON.parse to extend the JSON grammars. If an implementation wishes to support a modified or extended JSON interchange format it must do so by defining a different parse function.

NOTE In the case where there are duplicate name Strings within an object, lexically preceding values for the same key shall be overwritten.

24.3.2 JSON.stringify ( value [ , replacer [ , space ] ] )

The stringify function returns a String in UTF-16 encoded JSON format representing an ECMAScript value. It can take three parameters. The value parameter is an ECMAScript value, which is usually an object or array, although it can also be a String, Boolean, Number or null. The optional replacer parameter is either a function that alters the way objects and arrays are stringified, or an array of Strings and Numbers that acts as a white list for selecting the object properties that will be stringified. The optional space parameter is a String or Number that allows the result to have white space injected into it to improve human readability.

These are the steps in stringifying an object:

  1. Let stack be an empty List.
  2. Let indent be the empty String.
  3. Let PropertyList and ReplacerFunction be undefined.
  4. If Type(replacer) is Object, then
    1. If IsCallable(replacer) is true, then
      1. Let ReplacerFunction be replacer.
    2. Else,
      1. Let isArray be IsArray(replacer).
      2. ReturnIfAbrupt(isArray).
      3. If isArray is true, then
        1. Let PropertyList be an empty List
        2. Let len be ToLength(Get(replacer, "length")).
        3. ReturnIfAbrupt(len).
        4. Let k be 0.
        5. Repeat while k<len.
          1. Let v be Get(replacer, ToString(k)).
          2. ReturnIfAbrupt(v).
          3. Let item be undefined.
          4. If Type(v) is String, let item be v.
          5. Else if Type(v) is Number, let item be ToString(v).
          6. Else if Type(v) is Object, then
            1. If v has a [[StringData]] or [[NumberData]] internal slot, let item be ToString(v).
            2. ReturnIfAbrupt(item).
          7. If item is not undefined and item is not currently an element of PropertyList, then
            1. Append item to the end of PropertyList.
          8. Let k be k+1.
  5. If Type(space) is Object, then
    1. If space has a [[NumberData]] internal slot, then
      1. Let space be ToNumber(space).
      2. ReturnIfAbrupt(space).
    2. Else if space has a [[StringData]] internal slot, then
      1. Let space be ToString(space).
      2. ReturnIfAbrupt(space).
  6. If Type(space) is Number, then
    1. Let space be min(10, ToInteger(space)).
    2. Set gap to a String containing space occurrences of code unit 0x0020 (SPACE). This will be the empty String if space is less than 1.
  7. Else if Type(space) is String, then
    1. If the number of elements in space is 10 or less, set gap to space otherwise set gap to a String consisting of the first 10 elements of space.
  8. Else
    1. Set gap to the empty String.
  9. Let wrapper be ObjectCreate(%ObjectPrototype%).
  10. Let status be CreateDataProperty(wrapper, the empty String, value).
  11. Assert: status is true.
  12. Return SerializeJSONProperty(the empty String, wrapper).

NOTE 1 JSON structures are allowed to be nested to any depth, but they must be acyclic. If value is or contains a cyclic structure, then the stringify function must throw a TypeError exception. This is an example of a value that cannot be stringified:

a = [];
a[0] = a;
my_text = JSON.stringify(a); // This must throw a TypeError.

NOTE 2 Symbolic primitive values are rendered as follows:

  • The null value is rendered in JSON text as the String null.
  • The undefined value is not rendered.
  • The true value is rendered in JSON text as the String true.
  • The false value is rendered in JSON text as the String false.

NOTE 3 String values are wrapped in QUOTATION MARK (") code units. The code units " and \ are escaped with \ prefixes. Control characters code units are replaced with escape sequences \uHHHH, or with the shorter forms, \b (BACKSPACE), \f (FORM FEED), \n (LINE FEED), \r (CARRIAGE RETURN), \t (CHARACTER TABULATION).

NOTE 4 Finite numbers are stringified as if by calling ToString(number). NaN and Infinity regardless of sign are represented as the String null.

NOTE 5 Values that do not have a JSON representation (such as undefined and functions) do not produce a String. Instead they produce the undefined value. In arrays these values are represented as the String null. In objects an unrepresentable value causes the property to be excluded from stringification.

NOTE 6 An object is rendered as U+007B (LEFT CURLY BRACKET) followed by zero or more properties, separated with a U+002C (COMMA), closed with a U+007D (RIGHT CURLY BRACKET). A property is a quoted String representing the key or property name, a U+003A (COLON), and then the stringified property value. An array is rendered as an opening U+005B (LEFT SQUARE BRACKET followed by zero or more values, separated with a U+002C (COMMA), closed with a U+005D (RIGHT SQUARE BRACKET).

24.3.2.1 Runtime Semantics: SerializeJSONProperty ( key, holder )

The abstract operation SerializeJSONProperty with arguments key, and holder has access to ReplacerFunction from the invocation of the stringify method. Its algorithm is as follows:

  1. Let value be Get(holder, key).
  2. ReturnIfAbrupt(value).
  3. If Type(value) is Object, then
    1. Let toJSON be Get(value, "toJSON").
    2. ReturnIfAbrupt(toJSON).
    3. If IsCallable(toJSON) is true
      1. Let value be Call(toJSON, value, «key»).
      2. ReturnIfAbrupt(value).
  4. If ReplacerFunction is not undefined, then
    1. Let value be Call(ReplacerFunction, holder, «key, value»).
    2. ReturnIfAbrupt(value).
  5. If Type(value) is Object, then
    1. If value has a [[NumberData]] internal slot, then
      1. Let value be ToNumber(value).
      2. ReturnIfAbrupt(value).
    2. Else if value has a [[StringData]] internal slot, then
      1. Let value be ToString(value).
      2. ReturnIfAbrupt(value).
    3. Else if value has a [[BooleanData]] internal slot, then
      1. Let value be the value of the [[BooleanData]] internal slot of value.
  6. If value is null, return "null".
  7. If value is true, return "true".
  8. If value is false, return "false".
  9. If Type(value) is String, return QuoteJSONString(value).
  10. If Type(value) is Number, then
    1. If value is finite, return ToString(value).
    2. Else, return "null".
  11. If Type(value) is Object, and IsCallable(value) is false, then
    1. Let isArray be IsArray(value).
    2. ReturnIfAbrupt(isArray).
    3. If isArray is true, return SerializeJSONArray(value).
    4. Else, return SerializeJSONObject(value).
  12. Return undefined.

24.3.2.2 Runtime Semantics: QuoteJSONString ( value )

The abstract operation QuoteJSONString with argument value wraps a String value in QUOTATION MARK code units and escapes certain other code units within it.

  1. Let product be code unit 0x0022 (QUOTATION MARK).
  2. For each code unit C in value
    1. If C is 0x0022 (QUOTATION MARK) or 0x005C (REVERSE SOLIDUS), then
      1. Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
      2. Let product be the concatenation of product and C.
    2. Else if C is 0x0008 (BACKSPACE), 0x000C (FORM FEED), 0x000A (LINE FEED), 0x000D (CARRIAGE RETURN), or 0x000B (LINE TABULATION), then
      1. Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
      2. Let abbrev be the String value corresponding to the value of C as follows:
        BACKSPACE "b"
        FORM FEED (FF) "f"
        LINE FEED (LF) "n"
        CARRIAGE RETURN (CR) "r"
        LINE TABULATION "t"
      3. Let product be the concatenation of product and abbrev.
    3. Else if C has a code unit value less than 0x0020 (SPACE), then
      1. Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
      2. Let product be the concatenation of product and "u".
      3. Let hex be the string result of converting the numeric code unit value of C to a String of four hexadecimal digits. Alphabetic hexadecimal digits are presented as lowercase Latin letters.
      4. Let product be the concatenation of product and hex.
    4. Else,
      1. Let product be the concatenation of product and C.
  3. Let product be the concatenation of product and code unit 0x0022 (QUOTATION MARK).
  4. Return product.

24.3.2.3 Runtime Semantics: SerializeJSONObject ( value )

The abstract operation SerializeJSONObject with argument value serializes an object. It has access to the stack, indent, gap, and PropertyList values of the current invocation of the stringify method.

  1. If stack contains value, throw a TypeError exception because the structure is cyclical.
  2. Append value to stack.
  3. Let stepback be indent.
  4. Let indent be the concatenation of indent and gap.
  5. If PropertyList is not undefined, then
    1. Let K be PropertyList.
  6. Else,
    1. Let K be EnumerableOwnNames(value).
  7. Let partial be an empty List.
  8. For each element P of K,
    1. Let strP be SerializeJSONProperty(P, value).
    2. ReturnIfAbrupt(strP).
    3. If strP is not undefined, then
      1. Let member be QuoteJSONString(P).
      2. Let member be the concatenation of member and the string ":".
      3. If gap is not the empty String, then
        1. Let member be the concatenation of member and code unit 0x0020 (SPACE).
      4. Let member be the concatenation of member and strP.
      5. Append member to partial.
  9. If partial is empty, then
    1. Let final be "{}".
  10. Else,
    1. If gap is the empty String, then
      1. Let properties be a String formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with code unit 0x002C (COMMA). A comma is not inserted either before the first String or after the last String.
      2. Let final be the result of concatenating "{", properties, and "}".
    2. Else gap is not the empty String
      1. Let separator be the result of concatenating code unit 0x002C (COMMA), code unit 0x000A (LINE FEED), and indent.
      2. Let properties be a String formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with separator. The separator String is not inserted either before the first String or after the last String.
      3. Let final be the result of concatenating "{", code unit 0x000A (LINE FEED), indent, properties, code unit 0x000A, stepback, and "}".
  11. Remove the last element of stack.
  12. Let indent be stepback.
  13. Return final.

24.3.2.4 Runtime Semantics: SerializeJSONArray ( value )

The abstract operation SerializeJSONArray with argument value serializes an array. It has access to the stack, indent, and gap values of the current invocation of the stringify method.

  1. If stack contains value, throw a TypeError exception because the structure is cyclical.
  2. Append value to stack.
  3. Let stepback be indent.
  4. Let indent be the concatenation of indent and gap.
  5. Let partial be an empty List.
  6. Let len be ToLength(Get(value, "length")).
  7. ReturnIfAbrupt(len).
  8. Let index be 0.
  9. Repeat while index < len
    1. Let strP be SerializeJSONProperty(ToString(index), value).
    2. ReturnIfAbrupt(strP).
    3. If strP is undefined, then
      1. Append "null" to partial.
    4. Else,
      1. Append strP to partial.
    5. Increment index by 1.
  10. If partial is empty, then
    1. Let final be "[]".
  11. Else,
    1. If gap is the empty String, then
      1. Let properties be a String formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with code unit 0x002C (COMMA). A comma is not inserted either before the first String or after the last String.
      2. Let final be the result of concatenating "[", properties, and "]".
    2. Else,
      1. Let separator be the result of concatenating code unit 0x002C (COMMA), code unit 0x000A (LINE FEED), and indent.
      2. Let properties be a String formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with separator. The separator String is not inserted either before the first String or after the last String.
      3. Let final be the result of concatenating "[", code unit 0x000A (LINE FEED), indent, properties, code unit 0x000A, stepback, and "]".
  12. Remove the last element of stack.
  13. Let indent be stepback.
  14. Return final.

NOTE The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Properties whose keys are not array indexes are excluded from the stringification. An array is stringified as an opening LEFT SQUARE BRACKET, elements separated by COMMA, and a closing RIGHT SQUARE BRACKET.

24.3.3 JSON [ @@toStringTag ]

The initial value of the @@toStringTag property is the String value "JSON".

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.