Home

Awesome

Immutable ArrayBuffers

A TC39 proposal for immutable ArrayBuffers.

Status

The TC39 Process

Stage: 2

Champions:

Specification: https://tc39.es/proposal-immutable-arraybuffer/

Presentation history

Background

Prior proposals In-Place Resizable and Growable ArrayBuffers and ArrayBuffer.prototype.transfer and friends have both reached stage 4, and so are now an official part of JavaScript. Altogether, ArrayBuffer.prototype now has the following methods:

and the following read-only accessor properties

None of the operations above enable the creation of an immutable buffer, i.e., a non-detached buffer whose contents cannot be changed, resized, or detached.

Both a DataView object and a TypedArray object are views into a buffer backing store. For a TypedArray object, the contents of the backing store appear as indexed data properties of the TypeArray object that reflect the current contents of this backing store. Currently, because there is no way to prevent the contents of the backing store from being changed, TypedArrays cannot be frozen.

Motivation

Some JavaScript implementations, like Moddable XS, bring JavaScript to embedded systems, like device controllers, where ROM is much more plentiful and cheaper than RAM. These systems need to place voluminous fixed data into ROM, and currently do so using semantics outside the official JavaScript standard.

APIs that accept ArrayBuffers and/or objects backed by them could also benefit from performance improvement by avoiding defensive copies when the input buffers are immutable (see Generic zero-copy ArrayBuffer usage for a proposed alternative solution to this problem in the Web Platform).

The OCapN network protocol treats strings and byte-arrays as distinct forms of bulk data to be transmitted by copy. At JavaScript endpoints speaking OCapN such as @endo/pass-style + @endo/marshal, JavaScript strings represent OCapN strings. The immutability of strings in the JavaScript language reflects their by-copy nature in the protocol. Likewise, to reflect an OCapN byte-array well into the JavaScript language, an immutable container of bulk binary data is required. There currently are none, but an Immutable ArrayBuffer would provide exactly the necessary low-level machinery.

Prior proposals with overlapping goals

Limited ArrayBuffer

Solution

This proposal introduces additional methods and read-only accessor properties to ArrayBuffer.prototype that fit naturally into those explained above. Just as a buffer can be resizable or not, and detached or not, this proposal enables buffers to be immutable or not. Just as transferToFixedSize moves the contents of a original buffer into a newly created non-resizable buffer, this proposal provides a transfer operation that moves the contents of an original original buffer into a newly created immutable buffer. Altogether, this proposal only adds to ArrayBuffer.prototype one method

and one read-only accessor

An immutable buffer cannot be detached, resized, or further transferred. Its maxByteLength is the same as its byteLength. A DataView or TypedArray using an immutable buffer as its backing store can be frozen and immutable. ArrayBuffers, DataViews, and TypedArrays that are frozen and immutable could be placed in ROM without going beyond JavaScript's official semantics.

The ArrayBuffer slice method and TypedArray methods that create new ArrayBuffers (filter, map, slice, toReversed, etc.) make no effort to preserve immutability, just like they make no effort to preserve resizability (although use of SpeciesConstructor in those methods means that lack of resizability/immutability in the result cannot be guaranteed for the latter).

Use cases

Represent arbitrary binary data as an immutable netstring

const consumeIntoNetstring = data => {
  // Transfer to a new ArrayBuffer with room for the netstring framing.
  // https://en.wikipedia.org/wiki/Netstring
  const prefix = new TextEncoder().encode(`${data.length}:`);
  const buf = data.buffer.transfer(prefix.length + data.length + 1);

  // Frame the data.
  const tmpArr = new Uint8Array(buf);
  tmpArr.copyWithin(prefix.length, 0);
  tmpArr.set(prefix);
  tmpArr[tmpArr.length - 1] = 0x2C;

  // Transfer to an immutable ArrayBuffer backing a frozen Uint8Array.
  const frozenNetstring = Object.freeze(new Uint8Array(buf.transferToImmutable()));
  assert(buf.detached);
  return frozenNetstring;
};

const input = new TextEncoder().encode('hello world!');
const result = consumeIntoNetstring(input);
assert(Object.isFrozen(result));
try { result[0] = 0; } catch (_err) {}
try { new Uint8Array(result.buffer)[0] = 1; } catch (_err) {}
try { result.buffer.transferToImmutable(); } catch (_err) {}
assert(String.fromCharCode(...result) === '12:hello world!,');

Implementations

Polyfill/transpiler implementations

Native implementations

Tracking issues to be added:

Q&A

<dl> <!-- Markdown rendering of HTML blocks requires a blank line before further Markdown content. --> <dt> Why can't an immutable ArrayBuffer be detached/transferred? </dt> <dd> Because that would result in observable changes to any TypedArray or DataView backed by it. </dd> <dt> Should the index properties of a TypedArray backed by an immutable ArrayBuffer be configurable and writable? </dt> <dd> No, TypedArray index properties should continue to track the state of the underlying buffer without individual bookkeeping. </dd> <dt>

Should ArrayBuffers support zero-copy slices (e.g., arrayBuffer.sliceToImmutable())?

</dt> <dd> https://github.com/tc39/proposal-immutable-arraybuffer/issues/9 </dd> <dt>

Should the new getter be name immutable or mutable?

</dt> <dd> https://github.com/tc39/proposal-immutable-arraybuffer/issues/10 </dd> <dt>

Order of operations, when to throw or silently do nothing?

</dt> <dd> https://github.com/tc39/proposal-immutable-arraybuffer/issues/16 </dd> <dl>