Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update design #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 73 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,90 @@ Stage: 1

## Presentations

- [For stage 1 on 82th tc39 meeting (Apr 2021)](https://docs.google.com/presentation/d/1TGLvflOG63C5iHush597ffKTenoYowc3MivQEhAM20w/edit?usp=sharing)
- [TC39 meeting notes on 82th tc39 meeting](https://github.com/tc39/notes/blob/master/meetings/2021-04/apr-21.md#read-only-arraybuffer-and-fixed-view-of-arraybuffer-for-stage-1)
- [TC39 meeting notes on 82th tc39 meeting](https://github.com/tc39/notes/blob/main/meetings/2021-04/apr-21.md#read-only-arraybuffer-and-fixed-view-of-arraybuffer-for-stage-1) ([slides](https://docs.google.com/presentation/d/1TGLvflOG63C5iHush597ffKTenoYowc3MivQEhAM20w/edit?usp=sharing))
- [TC39 meeting notes on 106 tc39 meeting](https://github.com/tc39/notes/blob/main/meetings/2025-02/) ([slides](https://docs.google.com/presentation/d/1u6JsSeInvm6F4OrmCSLubtDvFVdjw1ESeE5-c_YflHE/))

## Problem to be resolved

All of the following are helpful to archive the minimal permission/information principle.

1. Cannot make an `ArrayBuffer` read-only.
2. Cannot give others a read-only view to the `ArrayBuffer` and keep the read-write permission internally.
3. Cannot give others a view that range limited (only a small area of the whole buffer is visible).
1. Cannot give others a read-only view to the `ArrayBuffer` and keep the read-write view internally.
2. Cannot give others a view that range is limited.

```js
function createView() {
const buffer = new ArrayBuffer(128)
const offset = 4
const length = 16
const view = new Uint8Array(buffer, offset, length)
view[0] = 0x12
return view
}

const view = createView()
// oops!
const wholeBufferView = new Uint8Array(view.buffer)
```

## Design goal

1. Freeze the `ArrayBuffer`.
1. Like `Object.freeze`, there is no way back once frozen.
2. Any `TypedArray`/`DataView` to the freezed `ArrayBuffer` is read-only too.
3. [Optional] Keep frozen when sent across Realm (HTML intergration).
2. Read-only `TypedArray`/`DataView` to a read-write `ArrayBuffer`.
1. Read-only `TypedArray`/`DataView` to a read-write `ArrayBuffer`.
1. Must not be able to construct a read-write view from a read-only view.
3. [Optional] Range-limited `TypedArray`/`DataView` to a read-write `ArrayBuffer` ([CrimsonCodes0](https://github.com/CrimsonCodes0)'s [use case on WebAssembly](https://github.com/tc39/proposal-limited-arraybuffer/issues/11)).
1. Range-limited `TypedArray`/`DataView` to a read-write `ArrayBuffer`.
1. Must not be able to construct a bigger view range from a smaller view range.
4. Not adding too much complexity to the implementor.
1. Not adding too much complexity to the implementor.

## Proposed changes

1. New API on the `TypedArray`/`DataView` constructor.

```ts
interface TypedArrayConstructor {
new (buffer: ArrayBuffer, options?: TypedArrayConstructorOptionsBag): Uint8Array<ArrayBuffer>;
}
interface DataViewConstructor {
new (buffer: ArrayBuffer, options?: ViewConstructorOptionsBag): DataView<ArrayBuffer>;
}
interface ViewConstructorOptionsBag {
byteOffset?: number;
length?: number;
readonly?: boolean;
limited?: boolean;
}
```

1. If `readonly` or `limited` is set, the view returned does not have a `.buffer` property on it.

```ts
const buffer = new ArrayBuffer(1024);
const view = new Uint8Array(buffer, { limited: true, byteOffset: 12 });
view.buffer; // undefined
```

1. If `readonly` is set, the `TypedArray` returned is an exotic object (behaves like a [`ModuleNamespaceExoticObject`](https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#module-namespace-exotic-object)), the returned `DataView` throws for set functions.

```ts
const buffer = new ArrayBuffer(8);
const view = new Uint8Array(buffer, { readonly: true });
view.buffer; // undefined
// view[0] = 1; // Error
Object.getOwnPropertyDescriptor(view, 0);
// { configurable: false, writable: true, enumerable: true, value: 0 }

const view2 = new DataView(buffer, { readonly: true });
view2.buffer; // undefined
// view2.setUint8(0, 0); // Error
```

## Pros
1. Change the constructor of `TypedArray` and `DataView`, to accept `TypedArray` and `DataView` as source.

1. Minimal permission/information principle works on `ArrayBuffer`.
2. Embedded JS engines can represent ROMs as read-only `ArrayBuffer`.
```ts
const buffer = new ArrayBuffer(128)
const view = new Uint8Array(buffer, { offset: 16, length: 32, readonly: true })

## API design
const dataView = new DataView(view)
dataView.setUint32(0, 1) // throws, inherits readonly, offset and length of `view`

See [design.md](./design.md)
const dataView2 = new DataView(view, 16, 16)
// inherits readonly, offset += 16 (32 in total), length = 16
```
135 changes: 0 additions & 135 deletions design.md

This file was deleted.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@
</ul></div><div id="menu-toggle"><svg xmlns="http://www.w3.org/2000/svg" style="width:100%; height:100%; stroke:currentColor" viewBox="0 0 120 120">
<title>Menu</title>
<path stroke-width="10" stroke-linecap="round" d="M30,60 h60 M30,30 m0,5 h60 M30,90 m0,-5 h60"></path>
</svg></div><div id="menu-spacer"></div><div id="menu"><div id="menu-search"><input type="text" id="menu-search-box" placeholder="Search..."><div id="menu-search-results" class="inactive"></div></div><div id="menu-pins"><div class="menu-pane-header">Pins</div><ul id="menu-pins-list"></ul></div><div class="menu-pane-header">Table of Contents</div><div id="menu-toc"><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-demo-clause" title="This is an emu-clause"><span class="secnum">1</span> This is an emu-clause</a></li><li><span class="item-toggle-none"></span><a href="#sec-copyright-and-software-license" title="Copyright &amp; Software License"><span class="secnum">A</span> Copyright &amp; Software License</a></li></ol></div></div><div id="spec-container"><h1 class="version">Stage -1 Draft / February 7, 2022</h1><h1 class="title">Proposal Title Goes Here</h1>
</svg></div><div id="menu-spacer"></div><div id="menu"><div id="menu-search"><input type="text" id="menu-search-box" placeholder="Search..."><div id="menu-search-results" class="inactive"></div></div><div id="menu-pins"><div class="menu-pane-header">Pins</div><ul id="menu-pins-list"></ul></div><div class="menu-pane-header">Table of Contents</div><div id="menu-toc"><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-demo-clause" title="This is an emu-clause"><span class="secnum">1</span> This is an emu-clause</a></li><li><span class="item-toggle-none"></span><a href="#sec-copyright-and-software-license" title="Copyright &amp; Software License"><span class="secnum">A</span> Copyright &amp; Software License</a></li></ol></div></div><div id="spec-container"><h1 class="version">Stage -1 Draft / February 22, 2025</h1><h1 class="title">Proposal Title Goes Here</h1>

<emu-clause id="sec-demo-clause">
<h1><span class="secnum">1</span> This is an emu-clause</h1>
Expand All @@ -2424,7 +2424,7 @@ <h1><span class="secnum">1</span> This is an emu-clause</h1>
<h1><span class="secnum">A</span> Copyright &amp; Software License</h1>

<h2>Copyright Notice</h2>
<p>© 2022 Your Name(s) Here</p>
<p>© 2025 Your Name(s) Here</p>

<h2>Software License</h2>
<p>All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.</p>
Expand Down