|
1 |
| -# template-for-proposals |
2 |
| - |
3 |
| -A repository template for ECMAScript proposals. |
4 |
| - |
5 |
| -## Before creating a proposal |
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 |
11 |
| - |
12 |
| -## Create your proposal repo |
13 |
| - |
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. Go to your repo settings “Options” page, under “GitHub Pages”, and set the source to the **main branch** under the root (and click Save, if it does not autosave this setting) |
17 |
| - 1. check "Enforce HTTPS" |
18 |
| - 1. On "Options", under "Features", Ensure "Issues" is checked, and disable "Wiki", and "Projects" (unless you intend to use Projects) |
19 |
| - 1. Under "Merge button", check "automatically delete head branches" |
20 |
| -<!-- |
21 |
| - 1. Avoid merge conflicts with build process output files by running: |
22 |
| - ```sh |
23 |
| - git config --local --add merge.output.driver true |
24 |
| - git config --local --add merge.output.driver true |
25 |
| - ``` |
26 |
| - 1. Add a post-rewrite git hook to auto-rebuild the output on every commit: |
27 |
| - ```sh |
28 |
| - cp hooks/post-rewrite .git/hooks/post-rewrite |
29 |
| - chmod +x .git/hooks/post-rewrite |
30 |
| - ``` |
31 |
| ---> |
32 |
| - 1. ["How to write a good explainer"][explainer] explains how to make a good first impression. |
33 |
| - |
34 |
| - > Each TC39 proposal should have a `README.md` file which explains the purpose |
35 |
| - > of the proposal and its shape at a high level. |
36 |
| - > |
37 |
| - > ... |
38 |
| - > |
39 |
| - > The rest of this page can be used as a template ... |
40 |
| -
|
41 |
| - Your explainer can point readers to the `index.html` generated from `spec.emu` |
42 |
| - via markdown like |
43 |
| - |
44 |
| - ```markdown |
45 |
| - You can browse the [ecmarkup output](https://ACCOUNT.github.io/PROJECT/) |
46 |
| - or browse the [source](https://github.com/ACCOUNT/PROJECT/blob/HEAD/spec.emu). |
47 |
| - ``` |
48 |
| - |
49 |
| - where *ACCOUNT* and *PROJECT* are the first two path elements in your project's Github URL. |
50 |
| - For example, for github.com/**tc39**/**template-for-proposals**, *ACCOUNT* is "tc39" |
51 |
| - and *PROJECT* is "template-for-proposals". |
52 |
| - |
53 |
| - |
54 |
| -## Maintain your proposal repo |
55 |
| - |
56 |
| - 1. Make your changes to `spec.emu` (ecmarkup uses HTML syntax, but is not HTML, so I strongly suggest not naming it ".html") |
57 |
| - 1. Any commit that makes meaningful changes to the spec, should run `npm run build` and commit the resulting output. |
58 |
| - 1. Whenever you update `ecmarkup`, run `npm run build` and commit any changes that come from that dependency. |
59 |
| - |
60 |
| - [explainer]: https://github.com/tc39/how-we-work/blob/HEAD/explainer.md |
| 1 | +# proposal-arraybuffer-equals |
| 2 | + |
| 3 | +This is a proposal to add a new method, `equals(b: ArrayBuffer)`, to JavaScript's `ArrayBuffer` class. It has not yet been presented to the JavaScript standards committee. |
| 4 | + |
| 5 | +## The problem |
| 6 | + |
| 7 | +```typescript |
| 8 | +const encoder = new TextEncoder(); |
| 9 | +const input = 'sample'; |
| 10 | +const a = encoder.encode(input); |
| 11 | +const b = encoder.encode(input); |
| 12 | +console.log(a === b); // returns false |
| 13 | +console.log(a == b); // returns false |
| 14 | +``` |
| 15 | + |
| 16 | +The re-definition [Abstract Equality Comparison](https://tc39.es/ecma262/#sec-abstract-equality-comparison) or [Strict Equality Comparison](https://tc39.es/ecma262/#sec-strict-equality-comparison) is break change behavior. |
| 17 | + |
| 18 | +So we need to **define a new method**, |
| 19 | + |
| 20 | +## How compare two ArrayBuffer object is equality |
| 21 | + |
| 22 | +To solve this problem, you need to define a method. |
| 23 | + |
| 24 | +```typescript |
| 25 | +// The is TypeScript code |
| 26 | +function isEquals(a: ArrayBuffer, b: ArrayBuffer) { |
| 27 | + if (!(a instanceof ArrayBuffer)) { |
| 28 | + throw new TypeError(); |
| 29 | + } else if (!(b instanceof ArrayBuffer)) { |
| 30 | + throw new TypeError(); |
| 31 | + } else if (a === b) { |
| 32 | + return true; |
| 33 | + } else if (a.byteLength !== b.byteLength) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + const view1 = new DataView(a); |
| 37 | + const view2 = new DataView(b); |
| 38 | + for (let i = 0; i < view1.byteLength; i++) { |
| 39 | + if (view1.getUint8(i) !== view2.getUint8(i)) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + return true; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## `ArrayBuffer.isEquals` |
| 48 | + |
| 49 | +To do this, we propose a new method, `ArrayBuffer.isEquals(a, b)`, which compare two array buffer is equality (bit-wise) |
| 50 | + |
| 51 | +```typescript |
| 52 | +// returns false |
| 53 | +ArrayBuffer.isEquals(Uint8Array.of(0), undefined); |
| 54 | +// returns false |
| 55 | +ArrayBuffer.isEquals(Uint8Array.of(0), null); |
| 56 | +// returns false |
| 57 | +ArrayBuffer.isEquals(Uint8Array.of(0), Uint8Array.of(0, 1)); |
| 58 | +// returns true |
| 59 | +ArrayBuffer.isEquals(Uint8Array.of(0), Uint8Array.of(0)); |
| 60 | +// returns true |
| 61 | +ArrayBuffer.isEquals( |
| 62 | + Uint32Array.from([Number.MAX_SAFE_INTEGER]).buffer, |
| 63 | + Uint8Array.of(255, 255, 255, 255).buffer, |
| 64 | +); |
| 65 | +``` |
0 commit comments