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:
- Let obj be ?
OrdinaryCreateFromConstructor (constructor,"%ArrayBufferPrototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]] »). - Assert: byteLength is an integer value ≥ 0.
- Let block be ?
CreateByteDataBlock (byteLength). - Set obj's [[ArrayBufferData]] internal slot to block.
- Set obj's [[ArrayBufferByteLength]] internal slot to byteLength.
- Return obj.
24.1.1.2 IsDetachedBuffer ( arrayBuffer )
The abstract operation IsDetachedBuffer with argument arrayBuffer performs the following steps:
- Assert:
Type (arrayBuffer) is Object and it has an [[ArrayBufferData]] internal slot. - If arrayBuffer's [[ArrayBufferData]] internal slot is
null , returntrue . - Return
false .
24.1.1.3 DetachArrayBuffer ( arrayBuffer )
The abstract operation DetachArrayBuffer with argument arrayBuffer performs the following steps:
- Assert:
Type (arrayBuffer) is Object and it has [[ArrayBufferData]] and [[ArrayBufferByteLength]] internal slots. - Set arrayBuffer's [[ArrayBufferData]] internal slot to
null . - Set arrayBuffer's [[ArrayBufferByteLength]] internal slot to 0.
- Return
NormalCompletion (null ).
Detaching an ArrayBuffer instance disassociates the
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:
- Assert:
Type (srcBuffer) is Object and it has an [[ArrayBufferData]] internal slot. - If cloneConstructor is not present, then
- Let cloneConstructor be ?
SpeciesConstructor (srcBuffer,%ArrayBuffer% ). - If
IsDetachedBuffer (srcBuffer) istrue , throw aTypeError exception.
- Let cloneConstructor be ?
- Else, Assert:
IsConstructor (cloneConstructor) istrue . - Let srcLength be the value of srcBuffer's [[ArrayBufferByteLength]] internal slot.
- Assert: srcByteOffset ≤ srcLength.
- Let cloneLength be srcLength - srcByteOffset.
- Let srcBlock be the value of srcBuffer's [[ArrayBufferData]] internal slot.
- Let targetBuffer be ?
AllocateArrayBuffer (cloneConstructor, cloneLength). - If
IsDetachedBuffer (srcBuffer) istrue , throw aTypeError exception. - Let targetBlock be the value of targetBuffer's [[ArrayBufferData]] internal slot.
- Perform
CopyDataBlockBytes (targetBlock, 0, srcBlock, srcByteOffset, cloneLength). - 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:
- Assert:
IsDetachedBuffer (arrayBuffer) isfalse . - Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
- Assert: byteIndex is an integer value ≥ 0.
- Let block be arrayBuffer's [[ArrayBufferData]] internal slot.
- Let elementSize be the Number value of the Element Size value specified in
Table 50 for ElementType type. - Let rawValue be a
List of elementSize containing, in order, the elementSize sequence of bytes starting with block[byteIndex]. - If isLittleEndian is not present, set isLittleEndian to either
true orfalse . 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 theSetValueInBuffer abstract operation. - If isLittleEndian is
false , reverse the order of the elements of rawValue. - If type is
"Float32", then- 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.
- If value is an IEEE 754-2008 binary32 NaN value, return the
NaN Number value. - Return the Number value that corresponds to value.
- If type is
"Float64", then- 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.
- If value is an IEEE 754-2008 binary64 NaN value, return the
NaN Number value. - Return the Number value that corresponds to value.
- If the first code unit of type is
"U", then- Let intValue be the byte elements of rawValue concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
- Else,
- 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.
- 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:
- Assert:
IsDetachedBuffer (arrayBuffer) isfalse . - Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
- Assert: byteIndex is an integer value ≥ 0.
- Assert:
Type (value) is Number. - Let block be arrayBuffer's [[ArrayBufferData]] internal slot.
- Assert: block is not
undefined . - If isLittleEndian is not present, set isLittleEndian to either
true orfalse . 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 theGetValueFromBuffer abstract operation. - If type is
"Float32", then- 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 isfalse , the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value isNaN , 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 distinguishableNaN value.
- Set rawBytes to a
- Else, if type is
"Float64", then- Set rawBytes to a
List containing the 8 bytes that are the IEEE 754-2008 binary64 format encoding of value. If isLittleEndian isfalse , the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value isNaN , 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 distinguishableNaN value.
- Set rawBytes to a
- Else,
- Let n be the Number value of the Element Size specified in
Table 50 for ElementType type. - Let convOp be the abstract operation named in the Conversion Operation column in
Table 50 for ElementType type. - Let intValue be convOp(value).
- If intValue ≥ 0, then
- Let rawBytes be a
List containing the n-byte binary encoding of intValue. If isLittleEndian isfalse , the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
- Let rawBytes be a
- Else,
- Let rawBytes be a
List containing the n-byte binary 2's complement encoding of intValue. If isLittleEndian isfalse , the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
- Let rawBytes be a
- Let n be the Number value of the Element Size specified in
- Store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
- Return
NormalCompletion (undefined ).
24.1.2 The ArrayBuffer Constructor
The ArrayBuffer constructor is the ArrayBuffer property of the 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:
- If NewTarget is
undefined , throw aTypeError exception. - Let numberLength be ?
ToNumber (length). - Let byteLength be
ToLength (numberLength). - If
SameValueZero (numberLength, byteLength) isfalse , throw aRangeError exception. - 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
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:
- If
Type (arg) is not Object, returnfalse . - If arg has a [[ViewedArrayBuffer]] internal slot, return
true . - Return
false .
24.1.3.2 ArrayBuffer.prototype
The initial value of ArrayBuffer.prototype is the intrinsic object
This property has the attributes { [[Writable]]:
24.1.3.3 get ArrayBuffer [ @@species ]
ArrayBuffer[@@species] is an accessor property whose set accessor function is
- Return the
this value.
The value of the name property of this function is "get [Symbol.species]".
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
24.1.4.1 get ArrayBuffer.prototype.byteLength
ArrayBuffer.prototype.byteLength is an accessor property whose set accessor function is
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have an [[ArrayBufferData]] internal slot, throw a
TypeError exception. - If
IsDetachedBuffer (O) istrue , throw aTypeError exception. - Let length be the value of O's [[ArrayBufferByteLength]] internal slot.
- Return length.
24.1.4.2 ArrayBuffer.prototype.constructor
The initial value of ArrayBuffer.prototype.constructor is the intrinsic object
24.1.4.3 ArrayBuffer.prototype.slice ( start, end )
The following steps are taken:
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have an [[ArrayBufferData]] internal slot, throw a
TypeError exception. - If
IsDetachedBuffer (O) istrue , throw aTypeError exception. - Let len be the value of O's [[ArrayBufferByteLength]] internal slot.
- Let relativeStart be ?
ToInteger (start). - If relativeStart < 0, let first be max((len + relativeStart), 0); else let first be min(relativeStart, len).
- If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToInteger (end). - If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let final be min(relativeEnd, len).
- Let newLen be max(final-first, 0).
- Let ctor be ?
SpeciesConstructor (O,%ArrayBuffer% ). - Let new be ?
Construct (ctor, « newLen »). - If new does not have an [[ArrayBufferData]] internal slot, throw a
TypeError exception. - If
IsDetachedBuffer (new) istrue , throw aTypeError exception. - If
SameValue (new, O) istrue , throw aTypeError exception. - If the value of new's [[ArrayBufferByteLength]] internal slot < newLen, throw a
TypeError exception. - NOTE: Side-effects of the above steps may have detached O.
- If
IsDetachedBuffer (O) istrue , throw aTypeError exception. - Let fromBuf be the value of O's [[ArrayBufferData]] internal slot.
- Let toBuf be the value of new's [[ArrayBufferData]] internal slot.
- Perform
CopyDataBlockBytes (toBuf, 0, fromBuf, first, newLen). - 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]]:
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
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:
- If
Type (view) is not Object, throw aTypeError exception. - If view does not have a [[DataView]] internal slot, throw a
TypeError exception. - Let numberIndex be ?
ToNumber (requestIndex). - Let getIndex be
ToInteger (numberIndex). - If numberIndex ≠ getIndex or getIndex < 0, throw a
RangeError exception. - Let isLittleEndian be
ToBoolean (isLittleEndian). - Let buffer be the value of view's [[ViewedArrayBuffer]] internal slot.
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let viewOffset be the value of view's [[ByteOffset]] internal slot.
- Let viewSize be the value of view's [[ByteLength]] internal slot.
- Let elementSize be the Number value of the Element Size value specified in
Table 50 for ElementType type. - If getIndex + elementSize > viewSize, throw a
RangeError exception. - Let bufferIndex be getIndex + viewOffset.
- 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:
- If
Type (view) is not Object, throw aTypeError exception. - If view does not have a [[DataView]] internal slot, throw a
TypeError exception. - Let numberIndex be ?
ToNumber (requestIndex). - Let getIndex be
ToInteger (numberIndex). - If numberIndex ≠ getIndex or getIndex < 0, throw a
RangeError exception. - Let numberValue be ?
ToNumber (value). - Let isLittleEndian be
ToBoolean (isLittleEndian). - Let buffer be the value of view's [[ViewedArrayBuffer]] internal slot.
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let viewOffset be the value of view's [[ByteOffset]] internal slot.
- Let viewSize be the value of view's [[ByteLength]] internal slot.
- Let elementSize be the Number value of the Element Size value specified in
Table 50 for ElementType type. - If getIndex + elementSize > viewSize, throw a
RangeError exception. - Let bufferIndex be getIndex + viewOffset.
- Return
SetValueInBuffer (buffer, bufferIndex, type, numberValue, isLittleEndian).
24.2.2 The DataView Constructor
The DataView constructor is the DataView property of the 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 byteLength performs the following steps:
- If NewTarget is
undefined , throw aTypeError exception. - If
Type (buffer) is not Object, throw aTypeError exception. - If buffer does not have an [[ArrayBufferData]] internal slot, throw a
TypeError exception. - Let numberOffset be ?
ToNumber (byteOffset). - Let offset be
ToInteger (numberOffset). - If numberOffset ≠ offset or offset < 0, throw a
RangeError exception. - If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] internal slot.
- If offset > bufferByteLength, throw a
RangeError exception. - If byteLength is
undefined , then- Let viewByteLength be bufferByteLength - offset.
- Else,
- Let viewByteLength be ?
ToLength (byteLength). - If offset+viewByteLength > bufferByteLength, throw a
RangeError exception.
- Let viewByteLength be ?
- Let O be ?
OrdinaryCreateFromConstructor (NewTarget,"%DataViewPrototype%", « [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]] »). - Set O's [[DataView]] internal slot to
true . - Set O's [[ViewedArrayBuffer]] internal slot to buffer.
- Set O's [[ByteLength]] internal slot to viewByteLength.
- Set O's [[ByteOffset]] internal slot to offset.
- 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
The DataView constructor has the following properties:
24.2.3.1 DataView.prototype
The initial value of DataView.prototype is the intrinsic object
This property has the attributes { [[Writable]]:
24.2.4 Properties of the DataView Prototype Object
The DataView prototype object is the intrinsic object
24.2.4.1 get DataView.prototype.buffer
DataView.prototype.buffer is an accessor property whose set accessor function is
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have a [[ViewedArrayBuffer]] internal slot, throw a
TypeError exception. - Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
- Return buffer.
24.2.4.2 get DataView.prototype.byteLength
DataView.prototype.byteLength is an accessor property whose set accessor function is
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have a [[ViewedArrayBuffer]] internal slot, throw a
TypeError exception. - Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let size be the value of O's [[ByteLength]] internal slot.
- Return size.
24.2.4.3 get DataView.prototype.byteOffset
DataView.prototype.byteOffset is an accessor property whose set accessor function is
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have a [[ViewedArrayBuffer]] internal slot, throw a
TypeError exception. - Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let offset be the value of O's [[ByteOffset]] internal slot.
- Return offset.
24.2.4.4 DataView.prototype.constructor
The initial value of DataView.prototype.constructor is the intrinsic object
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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
undefined . - 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:
- Let v be the
this value. - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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:
- Let v be the
this value. - If littleEndian is not present, let littleEndian be
false . - 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]]:
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.
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 property of the 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
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
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
- Let JText be ?
ToString (text). - Parse JText interpreted as UTF-16 encoded Unicode points (
6.1.4 ) as a JSON text as specified in ECMA-404. Throw aSyntaxError exception if JText is not a valid JSON text as defined in that specification. - Let scriptText be the result of concatenating
"(", JText, and");". - 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 ofDoubleStringCharacter provided below. The extended PropertyDefinitionEvaluation semantics defined inB.3.1 must not be used during the evaluation. - Let unfiltered be completion.[[Value]].
- Assert: unfiltered will be either a primitive value or an object that is defined by either an
ArrayLiteral or anObjectLiteral . - If
IsCallable (reviver) istrue , then- Let root be
ObjectCreate (%ObjectPrototype% ). - Let rootName be the empty String.
- Let status be
CreateDataProperty (root, rootName, unfiltered). - Assert: status is
true . - Return ?
InternalizeJSONProperty (root, rootName).
- Let root be
- Else,
- Return unfiltered.
The length property of the parse function is 2.
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
-
The SV of
is theDoubleStringCharacter :: SourceCharacter but notone of " or\ orU 0000 through+ U 001+ F UTF16Encoding of the code point value ofSourceCharacter .
The syntax of a valid JSON text is a subset of the ECMAScript
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.
- Let val be ?
Get (holder, name). - If
Type (val) is Object, then- Let isArray be ?
IsArray (val). - If isArray is
true , then- Set I to 0.
- Let len be ?
ToLength (?Get (val,"length")). - Repeat while I < len,
- Let newElement be ? InternalizeJSONProperty(val, !
ToString (I)). - If newElement is
undefined , then- Perform ? val.[[Delete]](!
ToString (I)).
- Perform ? val.[[Delete]](!
- Else,
- Perform ?
CreateDataProperty (val, !ToString (I), newElement). - NOTE This algorithm intentionally does not throw an exception if
CreateDataProperty returnsfalse .
- Perform ?
- Add 1 to I.
- Let newElement be ? InternalizeJSONProperty(val, !
- Else,
- Let keys be ?
EnumerableOwnNames (val). - For each String P in keys do,
- Let newElement be ? InternalizeJSONProperty(val, P).
- If newElement is
undefined , then- Perform ? val.[[Delete]](P).
- Else,
- Perform ?
CreateDataProperty (val, P, newElement). - NOTE This algorithm intentionally does not throw an exception if
CreateDataProperty returnsfalse .
- Perform ?
- Let keys be ?
- Let isArray be ?
- 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.
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
These are the steps in stringifying an object:
- Let stack be a new empty
List . - Let indent be the empty String.
- Let PropertyList and ReplacerFunction be
undefined . - If
Type (replacer) is Object, then- If
IsCallable (replacer) istrue , then- Let ReplacerFunction be replacer.
- Else,
- Let isArray be ?
IsArray (replacer). - If isArray is
true , then- Let PropertyList be a new empty
List . - Let len be ?
ToLength (?Get (replacer,"length")). - Let k be 0.
- Repeat while k<len,
- Let v be ?
Get (replacer, !ToString (k)). - Let item be
undefined . - If
Type (v) is String, let item be v. - Else if
Type (v) is Number, let item be !ToString (v). - Else if
Type (v) is Object, then- If v has a [[StringData]] or [[NumberData]] internal slot, let item be ?
ToString (v).
- If v has a [[StringData]] or [[NumberData]] internal slot, let item be ?
- If item is not
undefined and item is not currently an element of PropertyList, then- Append item to the end of PropertyList.
- Let k be k+1.
- Let v be ?
- Let PropertyList be a new empty
- Let isArray be ?
- If
- If
Type (space) is Object, then - If
Type (space) is Number, then- Let space be min(10,
ToInteger (space)). - 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.
- Let space be min(10,
- Else if
Type (space) is String, then- 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.
- Else,
- Set gap to the empty String.
- Let wrapper be
ObjectCreate (%ObjectPrototype% ). - Let status be
CreateDataProperty (wrapper, the empty String, value). - Assert: status is
true . - Return ?
SerializeJSONProperty (the empty String, wrapper).
The length property of the stringify function is 3.
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
a = [];
a[0] = a;
my_text = JSON.stringify(a); // This must throw a TypeError.
Symbolic primitive values are rendered as follows:
-
The
null value is rendered in JSON text as the Stringnull. -
The
undefined value is not rendered. -
The
true value is rendered in JSON text as the Stringtrue. -
The
false value is rendered in JSON text as the Stringfalse.
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).
Finite numbers are stringified as if by calling null.
Values that do not have a JSON representation (such as null. In objects an unrepresentable value causes the property to be excluded from stringification.
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:
- Let value be ?
Get (holder, key). - If
Type (value) is Object, then- Let toJSON be ?
Get (value,"toJSON"). - If
IsCallable (toJSON) istrue , then- Let value be ?
Call (toJSON, value, « key »).
- Let value be ?
- Let toJSON be ?
- If ReplacerFunction is not
undefined , then- Let value be ?
Call (ReplacerFunction, holder, « key, value »).
- Let value be ?
- If
Type (value) is Object, then - If value is
null , return"null". - If value is
true , return"true". - If value is
false , return"false". - If
Type (value) is String, returnQuoteJSONString (value). - If
Type (value) is Number, then- If value is finite, return !
ToString (value). - Else, return
"null".
- If value is finite, return !
- If
Type (value) is Object andIsCallable (value) isfalse , then- Let isArray be ?
IsArray (value). - If isArray is
true , return ?SerializeJSONArray (value). - Else, return ?
SerializeJSONObject (value).
- Let isArray be ?
- 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.
- Let product be code unit 0x0022 (QUOTATION MARK).
- For each code unit C in value
- If C is 0x0022 (QUOTATION MARK) or 0x005C (REVERSE SOLIDUS), then
- Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
- Let product be the concatenation of product and C.
- Else if C is 0x0008 (BACKSPACE), 0x000C (FORM FEED), 0x000A (LINE FEED), 0x000D (CARRIAGE RETURN), or 0x0009 (CHARACTER TABULATION), then
- Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
- 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"CHARACTER TABULATION "t" - Let product be the concatenation of product and abbrev.
- Else if C has a code unit value less than 0x0020 (SPACE), then
- Let product be the concatenation of product and code unit 0x005C (REVERSE SOLIDUS).
- Let product be the concatenation of product and
"u". - 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.
- Let product be the concatenation of product and hex.
- Else,
- Let product be the concatenation of product and C.
- If C is 0x0022 (QUOTATION MARK) or 0x005C (REVERSE SOLIDUS), then
- Let product be the concatenation of product and code unit 0x0022 (QUOTATION MARK).
- 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.
- If stack contains value, throw a
TypeError exception because the structure is cyclical. - Append value to stack.
- Let stepback be indent.
- Let indent be the concatenation of indent and gap.
- If PropertyList is not
undefined , then- Let K be PropertyList.
- Else,
- Let K be ?
EnumerableOwnNames (value).
- Let K be ?
- Let partial be a new empty
List . - For each element P of K,
- Let strP be ?
SerializeJSONProperty (P, value). - If strP is not
undefined , then- Let member be
QuoteJSONString (P). - Let member be the concatenation of member and the string
":". - If gap is not the empty String, then
- Let member be the concatenation of member and code unit 0x0020 (SPACE).
- Let member be the concatenation of member and strP.
- Append member to partial.
- Let member be
- Let strP be ?
- If partial is empty, then
- Let final be
"{}".
- Let final be
- Else,
- If gap is the empty String, then
- 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.
- Let final be the result of concatenating
"{", properties, and"}".
- Else gap is not the empty String
- Let separator be the result of concatenating code unit 0x002C (COMMA), code unit 0x000A (LINE FEED), and indent.
- 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.
- Let final be the result of concatenating
"{", code unit 0x000A (LINE FEED), indent, properties, code unit 0x000A (LINE FEED), stepback, and"}".
- If gap is the empty String, then
- Remove the last element of stack.
- Let indent be stepback.
- 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.
- If stack contains value, throw a
TypeError exception because the structure is cyclical. - Append value to stack.
- Let stepback be indent.
- Let indent be the concatenation of indent and gap.
- Let partial be a new empty
List . - Let len be ?
ToLength (?Get (value,"length")). - Let index be 0.
- Repeat while index < len
- Let strP be ?
SerializeJSONProperty (!ToString (index), value). - If strP is
undefined , then- Append
"null"to partial.
- Append
- Else,
- Append strP to partial.
- Increment index by 1.
- Let strP be ?
- If partial is empty, then
- Let final be
"[]".
- Let final be
- Else,
- If gap is the empty String, then
- 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.
- Let final be the result of concatenating
"[", properties, and"]".
- Else,
- Let separator be the result of concatenating code unit 0x002C (COMMA), code unit 0x000A (LINE FEED), and indent.
- 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.
- Let final be the result of concatenating
"[", code unit 0x000A (LINE FEED), indent, properties, code unit 0x000A (LINE FEED), stepback, and"]".
- If gap is the empty String, then
- Remove the last element of stack.
- Let indent be stepback.
- Return final.
The representation of arrays includes only the elements between zero and array.length - 1
24.3.3 JSON [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "JSON".
This property has the attributes { [[Writable]]: