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]], [[ArrayBufferDetachKey]] »). Assert : byteLength is an integer value ≥ 0.- Let block be ?
CreateByteDataBlock (byteLength). Set obj.[[ArrayBufferData]] to block.Set obj.[[ArrayBufferByteLength]] to byteLength.- Return obj.
24.1.1.2 IsDetachedBuffer ( arrayBuffer )
24.1.1.3 DetachArrayBuffer ( arrayBuffer [ , key ] )
The abstract operation DetachArrayBuffer with argument arrayBuffer and optional argument key performs the following steps:
Assert :Type (arrayBuffer) is Object and it has [[ArrayBufferData]], [[ArrayBufferByteLength]], and [[ArrayBufferDetachKey]] internal slots.Assert :IsSharedArrayBuffer (arrayBuffer) isfalse .- If key is not present, set key to
undefined . - If
SameValue (arrayBuffer.[[ArrayBufferDetachKey]], key) isfalse , throw aTypeError exception. Set arrayBuffer.[[ArrayBufferData]] tonull .Set arrayBuffer.[[ArrayBufferByteLength]] to 0.- Return
NormalCompletion (null ).
Detaching an ArrayBuffer instance disassociates the
24.1.1.4 CloneArrayBuffer ( srcBuffer, srcByteOffset, srcLength, cloneConstructor )
The abstract operation CloneArrayBuffer takes four parameters, an ArrayBuffer srcBuffer, an integer offset srcByteOffset, an integer length srcLength, and a
Assert :Type (srcBuffer) is Object and it has an [[ArrayBufferData]] internal slot.Assert :IsConstructor (cloneConstructor) istrue .- Let targetBuffer be ?
AllocateArrayBuffer (cloneConstructor, srcLength). - If
IsDetachedBuffer (srcBuffer) istrue , throw aTypeError exception. - Let srcBlock be srcBuffer.[[ArrayBufferData]].
- Let targetBlock be targetBuffer.[[ArrayBufferData]].
- Perform
CopyDataBlockBytes (targetBlock, 0, srcBlock, srcByteOffset, srcLength). - Return targetBuffer.
24.1.1.5 RawBytesToNumber ( type, rawBytes, isLittleEndian )
The abstract operation RawBytesToNumber takes three parameters, a String type, a
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If isLittleEndian is
false , reverse the order of the elements of rawBytes. - If type is
"Float32", then- Let value be the byte elements of rawBytes 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 rawBytes 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 the code unit 0x0055 (LATIN CAPITAL LETTER U), then
- Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
- Else,
- Let intValue be the byte elements of rawBytes 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 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isTypedArray, order [ , isLittleEndian ] )
The abstract operation GetValueFromBuffer takes six parameters, an ArrayBuffer or SharedArrayBuffer arrayBuffer, an integer byteIndex, a String type, a Boolean isTypedArray, a String order, 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.[[ArrayBufferData]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If
IsSharedArrayBuffer (arrayBuffer) istrue , then- Let execution be the [[CandidateExecution]] field of the
surrounding agent 'sAgent Record . - Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is
AgentSignifier (). - If isTypedArray is
true and type is"Int8","Uint8","Int16","Uint16","Int32", or"Uint32", let noTear betrue ; otherwise let noTear befalse . - Let rawValue be a
List of length elementSize of nondeterministically chosen byte values. - NOTE: In implementations, rawValue is the result of a non-atomic or atomic read instruction on the underlying hardware. The nondeterminism is a semantic prescription of the
memory model to describe observable behaviour of hardware with weak consistency. - Let readEvent be
ReadSharedMemory { [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize }. - Append readEvent to eventList.
- Append
Chosen Value Record { [[Event]]: readEvent, [[ChosenValue]]: rawValue } to execution.[[ChosenValues]].
- Let execution be the [[CandidateExecution]] field of the
- Else, 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 the value of the [[LittleEndian]] field of the
surrounding agent 'sAgent Record . - Return
RawBytesToNumber (type, rawValue, isLittleEndian).
24.1.1.7 NumberToRawBytes ( type, value, isLittleEndian )
The abstract operation NumberToRawBytes takes three parameters, a String type, a Number value, and a Boolean isLittleEndian. This operation performs the following steps:
- If type is
"Float32", then- Let rawBytes be 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 , rawBytes 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.
- Let rawBytes be a
- Else if type is
"Float64", then- Let rawBytes be 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 , rawBytes 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.
- Let rawBytes be a
- Else,
- Let n be the Number value of the Element Size specified in
Table 59 for ElementType type. - Let convOp be the abstract operation named in the Conversion Operation column in
Table 59 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
- Return rawBytes.
24.1.1.8 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] )
The abstract operation SetValueInBuffer takes seven parameters, an ArrayBuffer or SharedArrayBuffer arrayBuffer, an integer byteIndex, a String type, a Number value, a Boolean isTypedArray, a String order, 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.[[ArrayBufferData]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If isLittleEndian is not present, set isLittleEndian to the value of the [[LittleEndian]] field of the
surrounding agent 'sAgent Record . - Let rawBytes be
NumberToRawBytes (type, value, isLittleEndian). - If
IsSharedArrayBuffer (arrayBuffer) istrue , then- Let execution be the [[CandidateExecution]] field of the
surrounding agent 'sAgent Record . - Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is
AgentSignifier (). - If isTypedArray is
true and type is"Int8","Uint8","Int16","Uint16","Int32", or"Uint32", let noTear betrue ; otherwise let noTear befalse . - Append
WriteSharedMemory { [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize, [[Payload]]: rawBytes } to eventList.
- Let execution be the [[CandidateExecution]] field of the
- Else, store the individual bytes of rawBytes into block, in order, starting at block[byteIndex].
- Return
NormalCompletion (undefined ).
24.1.1.9 GetModifySetValueInBuffer ( arrayBuffer, byteIndex, type, value, op [ , isLittleEndian ] )
The abstract operation GetModifySetValueInBuffer takes six parameters, a SharedArrayBuffer arrayBuffer, a nonnegative integer byteIndex, a String type, a Number value, a semantic function op, and optionally a Boolean isLittleEndian. This operation performs the following steps:
Assert :IsSharedArrayBuffer (arrayBuffer) istrue .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.[[ArrayBufferData]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If isLittleEndian is not present, set isLittleEndian to the value of the [[LittleEndian]] field of the
surrounding agent 'sAgent Record . - Let rawBytes be
NumberToRawBytes (type, value, isLittleEndian). - Let execution be the [[CandidateExecution]] field of the
surrounding agent 'sAgent Record . - Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is
AgentSignifier (). - Let rawBytesRead be a
List of length elementSize of nondeterministically chosen byte values. - NOTE: In implementations, rawBytesRead is the result of a load-link, of a load-exclusive, or of an operand of a read-modify-write instruction on the underlying hardware. The nondeterminism is a semantic prescription of the
memory model to describe observable behaviour of hardware with weak consistency. - Let rmwEvent be
ReadModifyWriteSharedMemory { [[Order]]:"SeqCst", [[NoTear]]:true , [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize, [[Payload]]: rawBytes, [[ModifyOp]]: op }. - Append rmwEvent to eventList.
- Append
Chosen Value Record { [[Event]]: rmwEvent, [[ChosenValue]]: rawBytesRead } to execution.[[ChosenValues]]. - Return
RawBytesToNumber (type, rawBytesRead, isLittleEndian).
24.1.2 The ArrayBuffer Constructor
The ArrayBuffer
- is the intrinsic object
%ArrayBuffer% . - is the initial value of the
ArrayBufferproperty of theglobal object . - creates and initializes a new ArrayBuffer object when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- is designed to be subclassable. It may be used as the value of an
extendsclause of a class definition. Subclass constructors that intend to inherit the specifiedArrayBufferbehaviour must include asupercall to theArrayBufferconstructor to create and initialize subclass instances with the internal state necessary to support theArrayBuffer.prototypebuilt-in methods.
24.1.2.1 ArrayBuffer ( length )
When the ArrayBuffer function is called with argument length, the following steps are taken:
- If NewTarget is
undefined , throw aTypeError exception. - Let byteLength be ?
ToIndex (length). - Return ?
AllocateArrayBuffer (NewTarget, byteLength).
24.1.3 Properties of the ArrayBuffer Constructor
The ArrayBuffer
- has a [[Prototype]] internal slot whose value is the intrinsic object
%FunctionPrototype% . - has the following properties:
24.1.3.1 ArrayBuffer.isView ( arg )
The isView function takes one argument arg, and performs the following steps:
- 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
- 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
24.1.4 Properties of the ArrayBuffer Prototype Object
The ArrayBuffer prototype object:
- is the intrinsic object
%ArrayBufferPrototype% . - has a [[Prototype]] internal slot whose value is the intrinsic object
%ObjectPrototype% . - is an ordinary object.
- does not have an [[ArrayBufferData]] or [[ArrayBufferByteLength]] internal slot.
24.1.4.1 get ArrayBuffer.prototype.byteLength
ArrayBuffer.prototype.byteLength is an
- 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
IsSharedArrayBuffer (O) istrue , throw aTypeError exception. - If
IsDetachedBuffer (O) istrue , throw aTypeError exception. - Let length be O.[[ArrayBufferByteLength]].
- 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
IsSharedArrayBuffer (O) istrue , throw aTypeError exception. - If
IsDetachedBuffer (O) istrue , throw aTypeError exception. - Let len be O.[[ArrayBufferByteLength]].
- Let relativeStart be ?
ToInteger (start). - If relativeStart < 0, let first be
max ((len + relativeStart), 0); else let first bemin (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 bemin (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
IsSharedArrayBuffer (new) istrue , throw aTypeError exception. - If
IsDetachedBuffer (new) istrue , throw aTypeError exception. - If
SameValue (new, O) istrue , throw aTypeError exception. - If new.[[ArrayBufferByteLength]] < 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 O.[[ArrayBufferData]].
- Let toBuf be new.[[ArrayBufferData]].
- 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 ArrayBuffer Instances
ArrayBuffer instances inherit properties from the ArrayBuffer prototype object. ArrayBuffer instances each have an [[ArrayBufferData]] internal slot, an [[ArrayBufferByteLength]] internal slot, and an [[ArrayBufferDetachKey]] internal slot.
ArrayBuffer instances whose [[ArrayBufferData]] is
ArrayBuffer instances whose [[ArrayBufferDetachKey]] is set to a value other than
24.2 SharedArrayBuffer Objects
24.2.1 Abstract Operations for SharedArrayBuffer Objects
24.2.1.1 AllocateSharedArrayBuffer ( constructor, byteLength )
The abstract operation AllocateSharedArrayBuffer with arguments constructor and byteLength is used to create a SharedArrayBuffer object. It performs the following steps:
- Let obj be ?
OrdinaryCreateFromConstructor (constructor,"%SharedArrayBufferPrototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]] »). Assert : byteLength is a nonnegative integer.- Let block be ?
CreateSharedByteDataBlock (byteLength). Set obj.[[ArrayBufferData]] to block.Set obj.[[ArrayBufferByteLength]] to byteLength.- Return obj.
24.2.1.2 IsSharedArrayBuffer ( obj )
IsSharedArrayBuffer tests whether an object is an ArrayBuffer, a SharedArrayBuffer, or a subtype of either. It performs the following steps:
Assert :Type (obj) is Object and it has an [[ArrayBufferData]] internal slot.- Let bufferData be obj.[[ArrayBufferData]].
- If bufferData is
null , returnfalse . - If bufferData is a
Data Block , returnfalse . Assert : bufferData is aShared Data Block .- Return
true .
24.2.2 The SharedArrayBuffer Constructor
The SharedArrayBuffer
- is the intrinsic object
%SharedArrayBuffer% . - is the initial value of the
SharedArrayBufferproperty of theglobal object . - creates and initializes a new SharedArrayBuffer object when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- is designed to be subclassable. It may be used as the value of an
extendsclause of a class definition. Subclass constructors that intend to inherit the specifiedSharedArrayBufferbehaviour must include asupercall to theSharedArrayBufferconstructor to create and initialize subclass instances with the internal state necessary to support theSharedArrayBuffer.prototypebuilt-in methods.
Unlike an ArrayBuffer, a SharedArrayBuffer cannot become detached, and its internal [[ArrayBufferData]] slot is never
24.2.2.1 SharedArrayBuffer ( [ length ] )
When the SharedArrayBuffer function is called with optional argument length, the following steps are taken:
- If NewTarget is
undefined , throw aTypeError exception. - Let byteLength be ?
ToIndex (length). - Return ?
AllocateSharedArrayBuffer (NewTarget, byteLength).
24.2.3 Properties of the SharedArrayBuffer Constructor
The SharedArrayBuffer
- has a [[Prototype]] internal slot whose value is the intrinsic object
%FunctionPrototype% . - has the following properties:
24.2.3.1 SharedArrayBuffer.prototype
The initial value of SharedArrayBuffer.prototype is the intrinsic object
This property has the attributes { [[Writable]]:
24.2.3.2 get SharedArrayBuffer [ @@species ]
SharedArrayBuffer[@@species] is an
- Return the
this value.
The value of the name property of this function is "get [Symbol.species]".
24.2.4 Properties of the SharedArrayBuffer Prototype Object
The SharedArrayBuffer prototype object:
- is the intrinsic object
%SharedArrayBufferPrototype% . - has a [[Prototype]] internal slot whose value is the intrinsic object
%ObjectPrototype% . - is an ordinary object.
- does not have an [[ArrayBufferData]] or [[ArrayBufferByteLength]] internal slot.
24.2.4.1 get SharedArrayBuffer.prototype.byteLength
SharedArrayBuffer.prototype.byteLength is an
- 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
IsSharedArrayBuffer (O) isfalse , throw aTypeError exception. - Let length be O.[[ArrayBufferByteLength]].
- Return length.
24.2.4.2 SharedArrayBuffer.prototype.constructor
The initial value of SharedArrayBuffer.prototype.constructor is the intrinsic object
24.2.4.3 SharedArrayBuffer.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
IsSharedArrayBuffer (O) isfalse , throw aTypeError exception. - Let len be O.[[ArrayBufferByteLength]].
- Let relativeStart be ?
ToInteger (start). - If relativeStart < 0, let first be
max ((len + relativeStart), 0); else let first bemin (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 bemin (relativeEnd, len). - Let newLen be
max (final - first, 0). - Let ctor be ?
SpeciesConstructor (O,%SharedArrayBuffer% ). - Let new be ?
Construct (ctor, « newLen »). - If new does not have an [[ArrayBufferData]] internal slot, throw a
TypeError exception. - If
IsSharedArrayBuffer (new) isfalse , throw aTypeError exception. - If new.[[ArrayBufferData]] and O.[[ArrayBufferData]] are the same
Shared Data Block values, throw aTypeError exception. - If new.[[ArrayBufferByteLength]] < newLen, throw a
TypeError exception. - Let fromBuf be O.[[ArrayBufferData]].
- Let toBuf be new.[[ArrayBufferData]].
- Perform
CopyDataBlockBytes (toBuf, 0, fromBuf, first, newLen). - Return new.
24.2.4.4 SharedArrayBuffer.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "SharedArrayBuffer".
This property has the attributes { [[Writable]]:
24.2.5 Properties of SharedArrayBuffer Instances
SharedArrayBuffer instances inherit properties from the SharedArrayBuffer prototype object. SharedArrayBuffer instances each have an [[ArrayBufferData]] internal slot and an [[ArrayBufferByteLength]] internal slot.
SharedArrayBuffer instances, unlike ArrayBuffer instances, are never detached.
24.3 DataView Objects
24.3.1 Abstract Operations For DataView Objects
24.3.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 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. Assert : view has a [[ViewedArrayBuffer]] internal slot.- Let getIndex be ?
ToIndex (requestIndex). Set isLittleEndian toToBoolean (isLittleEndian).- Let buffer be view.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let viewOffset be view.[[ByteOffset]].
- Let viewSize be view.[[ByteLength]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If getIndex + elementSize > viewSize, throw a
RangeError exception. - Let bufferIndex be getIndex + viewOffset.
- Return
GetValueFromBuffer (buffer, bufferIndex, type,false ,"Unordered", isLittleEndian).
24.3.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. Assert : view has a [[ViewedArrayBuffer]] internal slot.- Let getIndex be ?
ToIndex (requestIndex). - Let numberValue be ?
ToNumber (value). Set isLittleEndian toToBoolean (isLittleEndian).- Let buffer be view.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let viewOffset be view.[[ByteOffset]].
- Let viewSize be view.[[ByteLength]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for ElementType type. - If getIndex + elementSize > viewSize, throw a
RangeError exception. - Let bufferIndex be getIndex + viewOffset.
- Return
SetValueInBuffer (buffer, bufferIndex, type, numberValue,false ,"Unordered", isLittleEndian).
24.3.2 The DataView Constructor
The DataView
- is the intrinsic object
%DataView% . - is the initial value of the
DataViewproperty of theglobal object . - creates and initializes a new DataView object when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
- is designed to be subclassable. It may be used as the value of an
extendsclause of a class definition. Subclass constructors that intend to inherit the specifiedDataViewbehaviour must include asupercall to theDataViewconstructor to create and initialize subclass instances with the internal state necessary to support theDataView.prototypebuilt-in methods.
24.3.2.1 DataView ( buffer [ , byteOffset [ , byteLength ] ] )
When the DataView function is called with at least one argument buffer, the following steps are taken:
- 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 offset be ?
ToIndex (byteOffset). - If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let bufferByteLength be buffer.[[ArrayBufferByteLength]].
- If offset > bufferByteLength, throw a
RangeError exception. - If byteLength is either not present or
undefined , then- Let viewByteLength be bufferByteLength - offset.
- Else,
- Let viewByteLength be ?
ToIndex (byteLength). - If offset + viewByteLength > bufferByteLength, throw a
RangeError exception.
- Let viewByteLength be ?
- Let O be ?
OrdinaryCreateFromConstructor (NewTarget,"%DataViewPrototype%", « [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]] »). - If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. Set O.[[ViewedArrayBuffer]] to buffer.Set O.[[ByteLength]] to viewByteLength.Set O.[[ByteOffset]] to offset.- Return O.
24.3.3 Properties of the DataView Constructor
The DataView
- has a [[Prototype]] internal slot whose value is the intrinsic object
%FunctionPrototype% . - has the following properties:
24.3.3.1 DataView.prototype
The initial value of DataView.prototype is the intrinsic object
This property has the attributes { [[Writable]]:
24.3.4 Properties of the DataView Prototype Object
The DataView prototype object:
- is the intrinsic object
%DataViewPrototype% . - has a [[Prototype]] internal slot whose value is the intrinsic object
%ObjectPrototype% . - is an ordinary object.
- does not have a [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], or [[ByteOffset]] internal slot.
24.3.4.1 get DataView.prototype.buffer
DataView.prototype.buffer is an
24.3.4.2 get DataView.prototype.byteLength
DataView.prototype.byteLength is an
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have a [[DataView]] internal slot, throw a
TypeError exception. Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let size be O.[[ByteLength]].
- Return size.
24.3.4.3 get DataView.prototype.byteOffset
DataView.prototype.byteOffset is an
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If O does not have a [[DataView]] internal slot, throw a
TypeError exception. Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let offset be O.[[ByteOffset]].
- Return offset.
24.3.4.4 DataView.prototype.constructor
The initial value of DataView.prototype.constructor is the intrinsic object
24.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Float32").
24.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Float64").
24.3.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.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Int16").
24.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Int32").
24.3.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.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Uint16").
24.3.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, set littleEndian to
false . - Return ?
GetViewValue (v, byteOffset, littleEndian,"Uint32").
24.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Float32", value).
24.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Float64", value).
24.3.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.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Int16", value).
24.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Int32", value).
24.3.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.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Uint16", value).
24.3.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, set littleEndian to
false . - Return ?
SetViewValue (v, byteOffset, littleEndian,"Uint32", value).
24.3.4.21 DataView.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "DataView".
This property has the attributes { [[Writable]]:
24.3.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
24.4 The Atomics Object
The Atomics object:
- is the intrinsic object
%Atomics% . - is the initial value of the
Atomicsproperty of theglobal object . - is an ordinary object.
- has a [[Prototype]] internal slot whose value is the intrinsic object
%ObjectPrototype% . - does not have a [[Construct]] internal method; it cannot be used as a
constructor with thenewoperator. - does not have a [[Call]] internal method; it cannot be invoked as a function.
The Atomics object provides functions that operate indivisibly (atomically) on shared memory array cells as well as functions that let agents wait for and dispatch primitive events. When used with discipline, the Atomics functions allow multi-
For informative guidelines for programming and implementing shared memory in ECMAScript, please see the notes at the end of the
24.4.1 Abstract Operations for Atomics
24.4.1.1 ValidateSharedIntegerTypedArray ( typedArray [ , onlyInt32 ] )
The abstract operation ValidateSharedIntegerTypedArray takes one argument typedArray and an optional Boolean onlyInt32. It performs the following steps:
- If onlyInt32 is not present, set onlyInt32 to
false . - If
Type (typedArray) is not Object, throw aTypeError exception. - If typedArray does not have a [[TypedArrayName]] internal slot, throw a
TypeError exception. - Let typeName be typedArray.[[TypedArrayName]].
- If onlyInt32 is
true , then- If typeName is not
"Int32Array", throw aTypeError exception.
- If typeName is not
- Else,
- If typeName is not
"Int8Array","Uint8Array","Int16Array","Uint16Array","Int32Array", or"Uint32Array", throw aTypeError exception.
- If typeName is not
Assert : typedArray has a [[ViewedArrayBuffer]] internal slot.- Let buffer be typedArray.[[ViewedArrayBuffer]].
- If
IsSharedArrayBuffer (buffer) isfalse , throw aTypeError exception. - Return buffer.
24.4.1.2 ValidateAtomicAccess ( typedArray, requestIndex )
The abstract operation ValidateAtomicAccess takes two arguments, typedArray and requestIndex. It performs the following steps:
24.4.1.3 GetWaiterList ( block, i )
A WaiterList is a semantic object that contains an ordered list of those agents that are waiting on a location (block, i) in shared memory; block is a
The
Operations on a WaiterList -- adding and removing waiting agents, traversing the list of agents, suspending and notifying agents on the list -- may only be performed by agents that have entered the WaiterList's critical section.
The abstract operation GetWaiterList takes two arguments, a
Assert : block is aShared Data Block .Assert : i and i + 3 are valid byte offsets within the memory of block.Assert : i is divisible by 4.- Return the WaiterList that is referenced by the pair (block, i).
24.4.1.4 EnterCriticalSection ( WL )
The abstract operation EnterCriticalSection takes one argument, a
Assert : The callingagent is not in the critical section for anyWaiterList .- Wait until no
agent is in the critical section for WL, then enter the critical section for WL (without allowing any otheragent to enter).
24.4.1.5 LeaveCriticalSection ( WL )
The abstract operation LeaveCriticalSection takes one argument, a
24.4.1.6 AddWaiter ( WL, W )
The abstract operation AddWaiter takes two arguments, a
Assert : The callingagent is in the critical section for WL.Assert : W is not on the list of waiters in anyWaiterList .- Add W to the end of the list of waiters in WL.
24.4.1.7 RemoveWaiter ( WL, W )
The abstract operation RemoveWaiter takes two arguments, a
24.4.1.8 RemoveWaiters ( WL, c )
The abstract operation RemoveWaiters takes two arguments, a
24.4.1.9 Suspend ( WL, W, timeout )
The abstract operation Suspend takes three arguments, a
Assert : The callingagent is in the critical section for WL.Assert : W is equal toAgentSignifier ().Assert : W is on the list of waiters in WL.Assert :AgentCanSuspend () istrue .- Perform
LeaveCriticalSection (WL) and suspend W for up to timeout milliseconds, performing the combined operation in such a way that a notification that arrives after the critical section is exited but before the suspension takes effect is not lost. W can notify either because the timeout expired or because it was notified explicitly by anotheragent callingNotifyWaiter (WL, W), and not for any other reasons at all. - Perform
EnterCriticalSection (WL). - If W was notified explicitly by another
agent callingNotifyWaiter (WL, W), returntrue . - Return
false .
24.4.1.10 NotifyWaiter ( WL, W )
The abstract operation NotifyWaiter takes two arguments, a
Assert : The callingagent is in the critical section for WL.Assert : W is on the list of waiters in WL.- Let execution be the [[CandidateExecution]] field of the
surrounding agent 'sAgent Record . - Let eventsRecord be the
Agent Events Record in execution.[[EventsRecords]] whose [[AgentSignifier]] isAgentSignifier (). - Let agentSynchronizesWith be eventsRecord.[[AgentSynchronizesWith]].
- Let notifierEventList be eventsRecord.[[EventList]].
- Let waiterEventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is W.
- Let notifyEvent and waitEvent be new
Synchronize events. - Append notifyEvent to notifierEventList.
- Append waitEvent to waiterEventList.
- Append (notifyEvent, waitEvent) to agentSynchronizesWith.
- Notify the
agent W.
The embedding may delay notifying W, e.g. for resource management reasons, but W must eventually be notified in order to guarantee forward progress.
24.4.1.11 AtomicReadModifyWrite ( typedArray, index, value, op )
The abstract operation AtomicReadModifyWrite takes four arguments, typedArray, index, value, and a pure combining operation op. The pure combining operation op takes two
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray). - Let i be ?
ValidateAtomicAccess (typedArray, index). - Let v be ?
ToInteger (value). - Let arrayTypeName be typedArray.[[TypedArrayName]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for arrayTypeName. - Let elementType be the String value of the Element
Type value inTable 59 for arrayTypeName. - Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × elementSize) + offset.
- Return
GetModifySetValueInBuffer (buffer, indexedPosition, elementType, v, op).
24.4.1.12 AtomicLoad ( typedArray, index )
The abstract operation AtomicLoad takes two arguments, typedArray, index. The operation atomically loads a value and returns the loaded value. It performs the following steps:
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray). - Let i be ?
ValidateAtomicAccess (typedArray, index). - Let arrayTypeName be typedArray.[[TypedArrayName]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for arrayTypeName. - Let elementType be the String value of the Element
Type value inTable 59 for arrayTypeName. - Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × elementSize) + offset.
- Return
GetValueFromBuffer (buffer, indexedPosition, elementType,true ,"SeqCst").
24.4.2 Atomics.add ( typedArray, index, value )
Let add denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,add).
24.4.3 Atomics.and ( typedArray, index, value )
Let and denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,and).
24.4.4 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue )
The following steps are taken:
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray). - Let i be ?
ValidateAtomicAccess (typedArray, index). - Let expected be ?
ToInteger (expectedValue). - Let replacement be ?
ToInteger (replacementValue). - Let arrayTypeName be typedArray.[[TypedArrayName]].
- Let elementType be the String value of the Element
Type value inTable 59 for arrayTypeName. - Let isLittleEndian be the value of the [[LittleEndian]] field of the
surrounding agent 'sAgent Record . - Let expectedBytes be
NumberToRawBytes (elementType, expected, isLittleEndian). - Let elementSize be the Number value of the Element Size value specified in
Table 59 for arrayTypeName. - Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × elementSize) + offset.
- Let
compareExchangedenote a semantic function of twoList of byte values arguments that returns the second argument if the first argument is element-wise equal to expectedBytes. - Return
GetModifySetValueInBuffer (buffer, indexedPosition, elementType, replacement,compareExchange).
24.4.5 Atomics.exchange ( typedArray, index, value )
Let second denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,second).
24.4.6 Atomics.isLockFree ( size )
The following steps are taken:
- Let n be ?
ToInteger (size). - Let AR be the
Agent Record of thesurrounding agent . - If n equals 1, return AR.[[IsLockFree1]].
- If n equals 2, return AR.[[IsLockFree2]].
- If n equals 4, return
true . - Return
false .
Atomics.isLockFree() is an optimization primitive. The intuition is that if the atomic step of an atomic primitive (compareExchange, load, store, add, sub, and, or, xor, or exchange) on a datum of size n bytes will be performed without the calling Atomics.isLockFree(n) will return
Atomics.isLockFree(4) always returns
24.4.7 Atomics.load ( typedArray, index )
The following steps are taken:
- Return ?
AtomicLoad (typedArray, index).
24.4.8 Atomics.or ( typedArray, index, value )
Let or denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,or).
24.4.9 Atomics.store ( typedArray, index, value )
The following steps are taken:
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray). - Let i be ?
ValidateAtomicAccess (typedArray, index). - Let v be ?
ToInteger (value). - Let arrayTypeName be typedArray.[[TypedArrayName]].
- Let elementSize be the Number value of the Element Size value specified in
Table 59 for arrayTypeName. - Let elementType be the String value of the Element
Type value inTable 59 for arrayTypeName. - Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × elementSize) + offset.
- Perform
SetValueInBuffer (buffer, indexedPosition, elementType, v,true ,"SeqCst"). - Return v.
24.4.10 Atomics.sub ( typedArray, index, value )
Let subtract denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,subtract).
24.4.11 Atomics.wait ( typedArray, index, value, timeout )
Atomics.wait puts the calling
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray,true ). - Let i be ?
ValidateAtomicAccess (typedArray, index). - Let v be ?
ToInt32 (value). - Let q be ?
ToNumber (timeout). - If q is
NaN , let t be+∞ , else let t bemax (q, 0). - Let B be
AgentCanSuspend (). - If B is
false , throw aTypeError exception. - Let block be buffer.[[ArrayBufferData]].
- Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × 4) + offset.
- Let WL be
GetWaiterList (block, indexedPosition). - Perform
EnterCriticalSection (WL). - Let w be !
AtomicLoad (typedArray, i). - If v is not equal to w, then
- Perform
LeaveCriticalSection (WL). - Return the String
"not-equal".
- Perform
- Let W be
AgentSignifier (). - Perform
AddWaiter (WL, W). - Let notified be
Suspend (WL, W, t). - If notified is
true , thenAssert : W is not on the list of waiters in WL.
- Else,
- Perform
RemoveWaiter (WL, W).
- Perform
- Perform
LeaveCriticalSection (WL). - If notified is
true , return the String"ok". - Return the String
"timed-out".
24.4.12 Atomics.notify ( typedArray, index, count )
Atomics.notify notifies some agents that are sleeping in the wait queue. The following steps are taken:
- Let buffer be ?
ValidateSharedIntegerTypedArray (typedArray,true ). - Let i be ?
ValidateAtomicAccess (typedArray, index). - If count is
undefined , let c be+∞ . - Else,
- Let block be buffer.[[ArrayBufferData]].
- Let offset be typedArray.[[ByteOffset]].
- Let indexedPosition be (i × 4) + offset.
- Let WL be
GetWaiterList (block, indexedPosition). - Let n be 0.
- Perform
EnterCriticalSection (WL). - Let S be
RemoveWaiters (WL, c). - Repeat, while S is not an empty
List ,- Let W be the first
agent in S. - Remove W from the front of S.
- Perform
NotifyWaiter (WL, W). - Add 1 to n.
- Let W be the first
- Perform
LeaveCriticalSection (WL). - Return n.
24.4.13 Atomics.xor ( typedArray, index, value )
Let xor denote a semantic function of two
The following steps are taken:
- Return ?
AtomicReadModifyWrite (typedArray, index, value,xor).
24.4.14 Atomics [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "Atomics".
This property has the attributes { [[Writable]]:
24.5 The JSON Object
The JSON object:
- is the intrinsic object
%JSON% . - is the initial value of the
JSONproperty of theglobal object . - is an ordinary object.
- contains two functions,
parseandstringify, that are used to parse and construct JSON texts. - has a [[Prototype]] internal slot whose value is the intrinsic object
%ObjectPrototype% . - does not have a [[Construct]] internal method; it cannot be used as a
constructor with thenewoperator. - does not have a [[Call]] internal method; it cannot be invoked as a function.
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.
24.5.1 JSON.parse ( text [ , reviver ] )
The parse function parses a JSON text (a JSON-formatted String) and produces an ECMAScript value. The JSON format represents literals, arrays, and objects with a syntax similar to 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
string-concatenation of"(", JText, and");". - Let completion be the result of parsing and evaluating scriptText as if it was the source text of an ECMAScript
Script . The extended PropertyDefinitionEvaluation semantics defined inB.3.1 must not be used during the evaluation. - Let unfiltered be completion.[[Value]].
Assert : unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either anArrayLiteral 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 istrue .- Return ?
InternalizeJSONProperty (root, rootName).
- Let root be
- Else,
- Return unfiltered.
This function is the
The "length" property of the parse function is 2.
Valid JSON text is a subset of the ECMAScript
24.5.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- Let I be 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 ?
EnumerableOwnPropertyNames (val,"key" ). - 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.5.2 JSON.stringify ( value [ , replacer [ , space ] ] )
The stringify function returns a String in UTF-16 encoded JSON format representing an ECMAScript value, 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 , thenSet ReplacerFunction to replacer.
- Else,
- Let isArray be ?
IsArray (replacer). - If isArray is
true , thenSet PropertyList to a new emptyList .- 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, set item to v. - Else if
Type (v) is Number, set item to !ToString (v). - Else if
Type (v) is Object, then- If v has a [[StringData]] or [[NumberData]] internal slot, set item to ?
ToString (v).
- If v has a [[StringData]] or [[NumberData]] internal slot, set item to ?
- If item is not
undefined and item is not currently an element of PropertyList, then- Append item to the end of PropertyList.
- Increase k by 1.
- Let v be ?
- Let isArray be ?
- If
- If
Type (space) is Object, then - If
Type (space) is Number, then - Else if
Type (space) is String, then- If the length of space is 10 or less, let gap be space; otherwise let gap be the String value consisting of the first 10 code units of space.
- Else,
- Let gap be the empty String.
- Let wrapper be
ObjectCreate (%ObjectPrototype% ). - Let status be
CreateDataProperty (wrapper, the empty String, value). Assert : status istrue .- 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
24.5.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 toJSON be ?
- If ReplacerFunction is not
undefined , then - 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). - 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). - Return ?
SerializeJSONObject (value).
- Let isArray be ?
- Return
undefined .
24.5.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.
This operation interprets a String value as a sequence of UTF-16 encoded code points, as described in
- Let product be the String value consisting solely of the code unit 0x0022 (QUOTATION MARK).
- Let cpList be a
List containing in order the code points of value when interpreted as a sequence of UTF-16 encoded code points as described in6.1.4 . - For each code point C in cpList, do
- If C is listed in the Code Point column of
Table 62 , thenSet product to thestring-concatenation of product and the Escape Sequence for C as specified inTable 62 .
- Else if C has a numeric value less than 0x0020 (SPACE), or if C has the same numeric value as a
leading surrogate ortrailing surrogate , then- Let unit be the code unit whose numeric value is that of C.
Set product to thestring-concatenation of product andUnicodeEscape (unit).
- Else,
Set product to thestring-concatenation of product and theUTF16Encoding of C.
- If C is listed in the Code Point column of
Set product to thestring-concatenation of product and the code unit 0x0022 (QUOTATION MARK).- Return product.
| Code Point | Unicode Character Name | Escape Sequence |
|---|---|---|
| U+0008 | BACKSPACE |
\b
|
| U+0009 | CHARACTER TABULATION |
\t
|
| U+000A | LINE FEED (LF) |
\n
|
| U+000C | FORM FEED (FF) |
\f
|
| U+000D | CARRIAGE RETURN (CR) |
\r
|
| U+0022 | QUOTATION MARK |
\"
|
| U+005C | REVERSE SOLIDUS |
\\
|
24.5.2.3 Runtime Semantics: UnicodeEscape ( C )
The abstract operation UnicodeEscape takes a code unit argument C and represents it as a Unicode escape sequence.
- Let n be the numeric value of C.
Assert : n ≤ 0xFFFF.- Return the
string-concatenation of:- the code unit 0x005C (REVERSE SOLIDUS)
"u"- the String representation of n, formatted as a four-digit lowercase hexadecimal number, padded to the left with zeroes if necessary
24.5.2.4 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.
Set indent to thestring-concatenation of indent and gap.- If PropertyList is not
undefined , then- Let K be PropertyList.
- Else,
- Let K be ?
EnumerableOwnPropertyNames (value,"key" ).
- Let K be ?
- Let partial be a new empty
List . - For each element P of K, do
- Let strP be ?
SerializeJSONProperty (P, value). - If strP is not
undefined , then- Let member be
QuoteJSONString (P). Set member to thestring-concatenation of member and":".- If gap is not the empty String, then
Set member to thestring-concatenation of member and the code unit 0x0020 (SPACE).
Set member to thestring-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 the String value formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with the code unit 0x002C (COMMA). A comma is not inserted either before the first String or after the last String.
- Let final be the
string-concatenation of"{", properties, and"}".
- Else gap is not the empty String,
- Let separator be the
string-concatenation of the code unit 0x002C (COMMA), the code unit 0x000A (LINE FEED), and indent. - Let properties be the String value 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
string-concatenation of"{", the code unit 0x000A (LINE FEED), indent, properties, the code unit 0x000A (LINE FEED), stepback, and"}".
- Let separator be the
- If gap is the empty String, then
- Remove the last element of stack.
Set indent to stepback.- Return final.
24.5.2.5 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.
Set indent to thestring-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 the String value formed by concatenating all the element Strings of partial with each adjacent pair of Strings separated with the code unit 0x002C (COMMA). A comma is not inserted either before the first String or after the last String.
- Let final be the
string-concatenation of"[", properties, and"]".
- Else,
- Let separator be the
string-concatenation of the code unit 0x002C (COMMA), the code unit 0x000A (LINE FEED), and indent. - Let properties be the String value 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
string-concatenation of"[", the code unit 0x000A (LINE FEED), indent, properties, the code unit 0x000A (LINE FEED), stepback, and"]".
- Let separator be the
- If gap is the empty String, then
- Remove the last element of stack.
Set indent to stepback.- Return final.
The representation of arrays includes only the elements between zero and array.length - 1
24.5.3 JSON [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value "JSON".
This property has the attributes { [[Writable]]: