The current design is tried to match the
Readonly Collections proposal. But ArrayBuffer
is not a
a collection so the design won't be the same.
- Freeze the
ArrayBuffer
. - Read-only
TypedArray
/DataView
to anArrayBuffer
. - Unescapable slice of
TypedArray
/DataView
to anArrayBuffer
. - Not creating new code branches on a hot path when it is unnecessary.
- Have similar API design with Readonly collections proposal
Note: issue #9, suggests removing this function based on the following reasoning: If all read-write view to an ArrayBuffer has been GC, the engine can assume the underlying buffer is read-only. But I'm not favoring this because it is an implicit opt-in.
After calling this function on ArrayBuffer A:
- [[GetOwnProperty]]:
Return
[[Writable]]: false, [[Configurable]]: false
for existing numeric keys. - [[DefineOwnProperty]]:
Change the behavior to accept descriptor with
[[Writable]]: false
or[[Configurable]]: false
; Throw when trying to define a different[[Value]]
. - [[Set]]: Throw.
- [[Delete]]: Throw.
- setBigInt64, setBigUint64, setFloat32, setFloat64, setInt8, setInt16, setInt32, setUint8, setUint16, setUint32: Throw.
- If a Host API need to write an ArrayBuffer A,
- if A is frozen, throws.
- Prevent A to be frozen.
- Host may re-allow freeze A after the host no longer needs the write access.
Possible choices:
- Throw? (do not allow freeze on SABs).
- Froze the SAB? (May impact usability).
- Freeze the ResizableArrayBuffer.
- Throw when trying to resize.
- Return true if the this value has been frozen.
This API design is trying to match the Readonly collection proposal.
Note: It provides a read-only view to a possibly mutable ArrayBuffer.
%TypedArray%.prototype.readOnlyView()
returns a new%TypedArray%
object with same offset and length. (Or we can create a newReadonlyTypedArray
type for it).- If a
%TypedArray%
is already a readonly view, callingreadonlyView()
on it will return it self. [[GetOwnProperty]]
: Return[[Writable]]: false, [[Configurable]]: true
for existing numeric keys. (NOT SAME as[[GetOwnProperty]]
above.)[[DefineOwnProperty]]
: Throw.[[Set]]
: Throw.[[Delete]]
: Throw.- get
%TypedArray%.prototype.buffer
: Return undefine or throw. (If we haveArrayBufferSlice
, return a new slice that is read-only). - Any new
%TypedArray%
created based on this one is also read-only.
Same design as %TypedArray%.prototype.readOnlyView()
.
This API design is trying to match the Readonly collection proposal.
Clone a new read-only ArrayBuffer
. If the current ArrayBuffer
is already read-only, it will return itself.
This API is required if we do not have ArrayBufferSlice
because %TypedArray%.prototype.buffer
will return undefined/throw therefore it's impossible to create a snaphost on a read-only typed array view.
readonlyU8Array.buffer.snapshot()
// Throw: Cannot read property snapshot of undefined
This API design is trying to match the Readonly collection proposal.
Clone a new mutable ArrayBuffer
(even if the current ArrayBuffer
is read-only).
This API is required if we do not have ArrayBufferSlice
because %TypedArray%.prototype.buffer
will return undefined/throw therefore it's impossible to create a diverge on a read-only typed array view.
readonlyU8Array.buffer.diverge()
// Throw: Cannot read property snapshot of undefined
ArrayBuffer
itself is not a view to a collection. It needs to be viewed with TypedArray
or DataView
.
- New primitive type
arraybuffer
. - Always immutable.
- Can generate from
ArrayBuffer.prototype.toPrimitive()
- Has prototype
ArrayBuffer.prototype
. ToObject
make it anArrayBuffer
object.
This part is intended to resolve the use case of issue #11.
One wants the slice to be read-only in order to prevent writing to the memory, and one doesn't want to move around the entire ArrayBuffer object, as that would allow reading into the memory at practically arbitrary locations.
- Can be created by
ArrayBuffer.prototype.placeholder_name_to_create_a_new_slice(offset, length)
- Calling
.freeze()
on anArrayBufferSlice
will throw. %TypedArray%
,DataView
and host APIs can also acceptArrayBufferSlice
whenArrayBuffer
is accepted.- Cannot get the wider view based on the
ArrayBufferSlice
. - Can create a read-only slice and keep the underlying
ArrayBuffer
mutable.