|
1 |
| -# template-for-proposals |
| 1 | +# Immutable ArrayBuffers |
2 | 2 |
|
3 |
| -A repository template for ECMAScript proposals. |
| 3 | +A TC39 proposal for immutable ArrayBuffers. |
4 | 4 |
|
5 |
| -## Before creating a proposal |
| 5 | +## Status |
6 | 6 |
|
7 |
| -Please ensure the following: |
8 |
| - 1. You have read the [process document](https://tc39.github.io/process-document/) |
9 |
| - 1. You have reviewed the [existing proposals](https://github.com/tc39/proposals/) |
10 |
| - 1. You are aware that your proposal requires being a member of TC39, or locating a TC39 delegate to “champion” your proposal |
| 7 | +[The TC39 Process](https://tc39.es/process-document/) |
11 | 8 |
|
12 |
| -## Create your proposal repo |
| 9 | +**Stage**: 0 |
13 | 10 |
|
14 |
| -Follow these steps: |
15 |
| - 1. Click the green [“use this template”](https://github.com/tc39/template-for-proposals/generate) button in the repo header. (Note: Do not fork this repo in GitHub's web interface, as that will later prevent transfer into the TC39 organization) |
16 |
| - 1. Update ecmarkup and the biblio to the latest version: `npm install --save-dev ecmarkup@latest && npm install --save-dev --save-exact @tc39/ecma262-biblio@latest`. |
17 |
| - 1. Go to your repo settings page: |
18 |
| - 1. Under “General”, under “Features”, ensure “Issues” is checked, and disable “Wiki”, and “Projects” (unless you intend to use Projects) |
19 |
| - 1. Under “Pull Requests”, check “Always suggest updating pull request branches” and “automatically delete head branches” |
20 |
| - 1. Under the “Pages” section on the left sidebar, and set the source to “deploy from a branch”, select “gh-pages” in the branch dropdown, and then ensure that “Enforce HTTPS” is checked. |
21 |
| - 1. Under the “Actions” section on the left sidebar, under “General”, select “Read and write permissions” under “Workflow permissions” and click “Save” |
22 |
| - 1. [“How to write a good explainer”][explainer] explains how to make a good first impression. |
| 11 | +**Champion**: Mark S. Miller (@erights) |
23 | 12 |
|
24 |
| - > Each TC39 proposal should have a `README.md` file which explains the purpose |
25 |
| - > of the proposal and its shape at a high level. |
26 |
| - > |
27 |
| - > ... |
28 |
| - > |
29 |
| - > The rest of this page can be used as a template ... |
| 13 | +**Specification**: https://papers.agoric.com/tc39-proposal-immutable-arraybuffer/ |
30 | 14 |
|
31 |
| - Your explainer can point readers to the `index.html` generated from `spec.emu` |
32 |
| - via markdown like |
| 15 | +## Presentation history |
33 | 16 |
|
34 |
| - ```markdown |
35 |
| - You can browse the [ecmarkup output](https://ACCOUNT.github.io/PROJECT/) |
36 |
| - or browse the [source](https://github.com/ACCOUNT/PROJECT/blob/HEAD/spec.emu). |
37 |
| - ``` |
| 17 | +## Background |
38 | 18 |
|
39 |
| - where *ACCOUNT* and *PROJECT* are the first two path elements in your project's Github URL. |
40 |
| - For example, for github.com/**tc39**/**template-for-proposals**, *ACCOUNT* is “tc39” |
41 |
| - and *PROJECT* is “template-for-proposals”. |
| 19 | +Prior proposals [In-Place Resizable and Growable `ArrayBuffer`s](https://github.com/tc39/proposal-resizablearraybuffer) and [ArrayBuffer.prototype.transfer and friends](https://github.com/tc39/proposal-arraybuffer-transfer) have both reached stage 4, and so are now an official part of JavaScript. Altogether, `ArrayBuffer.prototype` now has the following methods: |
| 20 | +- `transfer(newByteLength?: number) :ArrayBuffer` -- move the contents of the original buffer to a new buffer, detach the original buffer, and return the new buffer. The new buffer will be as resizable as the original was. |
| 21 | +- `transferToFixedLength(newByteLength?: number) :ArrayBuffer` -- like `transfer` but the new buffer is not resizable. |
| 22 | +- `resize(newByteLength: number) :void` -- change the size of this buffer if possible, or throw otherwise. |
| 23 | +- `slice(start?: number, end?: number) :ArrayBuffer` -- Return a new buffer whose initial contents are a copy of that region of the original buffer. The original buffer is unmodified. |
42 | 24 |
|
| 25 | +and the following read-only accessor properties |
| 26 | +- `detached: boolean` -- is this buffer detached, or are its contents still available from this buffer object? |
| 27 | +- `resizable: boolean` -- can this buffer be resized, or is it fixed-length? |
| 28 | +- `byteLength: number` -- how big are the current contents of this buffer? |
| 29 | +- `maxByteLength: number` -- how big could this buffer be resized to be? |
43 | 30 |
|
44 |
| -## Maintain your proposal repo |
| 31 | +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. |
45 | 32 |
|
46 |
| - 1. Make your changes to `spec.emu` (ecmarkup uses HTML syntax, but is not HTML, so I strongly suggest not naming it “.html”) |
47 |
| - 1. Any commit that makes meaningful changes to the spec, should run `npm run build` to verify that the build will succeed and the output looks as expected. |
48 |
| - 1. Whenever you update `ecmarkup`, run `npm run build` to verify that the build will succeed and the output looks as expected. |
| 33 | +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, `TypedArray`s cannot be frozen. |
49 | 34 |
|
50 |
| - [explainer]: https://github.com/tc39/how-we-work/blob/HEAD/explainer.md |
| 35 | +## Motivation |
| 36 | + |
| 37 | +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. |
| 38 | + |
| 39 | +The [OCapN](https://ocapn.org/) 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](https://www.npmjs.com/package/@endo/pass-style) + [@endo/marshal](https://www.npmjs.com/package/@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. |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | +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, or 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 |
| 44 | +- `transferToImmutable() :ArrayBuffer` -- move the contents of the original buffer into a new immutable buffer, detach the original buffer, and return the new buffer. |
| 45 | + |
| 46 | +and one read-only accessor |
| 47 | +- `immutable: boolean` -- is this buffer immutable, or can its contents be changed? |
| 48 | + |
| 49 | +An immutable buffer cannot be detached or resized. 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. `ArrayBuffer`s, `DataView`s, and `TypedArray`s that are frozen and immutable could be placed in ROM without going beyond JavaScript's official semantics. |
| 50 | + |
| 51 | +## Implementations |
| 52 | + |
| 53 | +### Polyfill/transpiler implementations |
| 54 | + |
| 55 | +* [endo immutable-arraybuffer](https://github.com/endojs/endo/tree/master/packages/immutable-arraybuffer) |
| 56 | + |
| 57 | +### Native implementations |
| 58 | + |
| 59 | +Tracking issues to be added: |
| 60 | +- JavaScriptCore |
| 61 | +- SpiderMonkey |
| 62 | +- XS |
| 63 | +- V8 |
| 64 | + |
| 65 | +## Q&A |
| 66 | + |
| 67 | +* Should `transferToImmutable` support a `newByteLength` argument? |
0 commit comments