-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of TypedArray support. (#11)
- Loading branch information
Showing
14 changed files
with
470 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
apps/typed-binary-docs/src/content/docs/guides/typed-arrays.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Typed Arrays | ||
description: A guide on how typed arrays can be represented in Typed Binary | ||
--- | ||
|
||
Sometimes binary is the format that we want to work with directly. [Typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays) schemas | ||
allow that data to be nested in plain objects or arrays. | ||
|
||
```ts | ||
const Image = object({ | ||
size: tupleOf([u32, u32]), | ||
bitmap: u8Array(256 * 256), | ||
}); | ||
|
||
// { | ||
// size: [number, number]; | ||
// bitmap: Uint8Array; | ||
// } | ||
const image = Image.read(...); | ||
|
||
image.size // [number, number] | ||
image.bitmap // Uint8Array | ||
``` | ||
|
||
Below is the list of available typed array schemas. | ||
|
||
Schema constructor | Encoded as | JavaScript value | ||
---|---|--- | ||
`u8Array` | consecutive 8-bit unsigned integers | `Uint8Array` | ||
`u8ClampedArray` | consecutive 8-bit unsigned integers | `Uint8ClampedArray` | ||
`u16Array` | consecutive 16-bit unsigned integers | `Uint16Array` | ||
`u32Array` | consecutive 32-bit unsigned integers | `Uint32Array` | ||
`i8Array` | consecutive 8-bit signed integers | `Int8Array` | ||
`i16Array` | consecutive 16-bit signed integers | `Int16Array` | ||
`i32Array` | consecutive 32-bit signed integers | `Int32Array` | ||
`f32Array` | consecutive 32-bit floats | `Float32Array` | ||
`f64Array` | consecutive 64-bit floats | `Float64Array` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { BufferReader } from 'typed-binary'; | ||
|
||
describe('BufferReader', () => { | ||
it('reads a uint32 from an ArrayBuffer', () => { | ||
const buffer = new ArrayBuffer(64); | ||
const u32Array = new Uint32Array(buffer); | ||
u32Array[0] = 256; | ||
|
||
const reader = new BufferReader(buffer); | ||
expect(reader.readUint32()).to.equal(256); | ||
}); | ||
|
||
it('reads an int32 array from an ArrayBuffer', () => { | ||
const buffer = new ArrayBuffer(3 * 4); | ||
const i32View = new Int32Array(buffer); | ||
|
||
i32View[0] = 1; | ||
i32View[1] = 2; | ||
i32View[2] = 3; | ||
|
||
const i32Array = new Int32Array(3); | ||
const reader = new BufferReader(buffer); | ||
reader.readSlice(i32Array, 0, i32Array.byteLength); | ||
|
||
expect([...i32Array]).to.deep.eq([1, 2, 3]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
import { BufferReader, BufferWriter } from 'typed-binary'; | ||
import { BufferWriter } from 'typed-binary'; | ||
|
||
describe('BufferWriter', () => { | ||
it('writes and reads from an ArrayBuffer', () => { | ||
it('writes a uint32 from an ArrayBuffer', () => { | ||
const buffer = new ArrayBuffer(64); | ||
const writer = new BufferWriter(buffer); | ||
|
||
writer.writeUint32(256); | ||
|
||
const reader = new BufferReader(buffer); | ||
expect(reader.readUint32()).to.equal(256); | ||
const u32View = new Uint32Array(buffer); | ||
expect(u32View[0]).to.equal(256); | ||
}); | ||
|
||
it('writes an int32 array to an ArrayBuffer', () => { | ||
const buffer = new ArrayBuffer(64); | ||
const writer = new BufferWriter(buffer); | ||
|
||
const i32Array = new Int32Array([1, 2, 3]); | ||
writer.writeSlice(i32Array); | ||
|
||
const i32View = new Int32Array(buffer); | ||
expect(i32View[0]).to.equal(1); | ||
expect(i32View[1]).to.equal(2); | ||
expect(i32View[2]).to.equal(3); | ||
}); | ||
|
||
it('writes a uint32 array to an ArrayBuffer', () => { | ||
const buffer = new ArrayBuffer(64); | ||
const writer = new BufferWriter(buffer); | ||
|
||
const u32Array = new Uint32Array([1, 2, 3]); | ||
writer.writeSlice(u32Array); | ||
|
||
const u32View = new Uint32Array(buffer); | ||
expect(u32View[0]).to.equal(1); | ||
expect(u32View[1]).to.equal(2); | ||
expect(u32View[2]).to.equal(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.