Skip to content

Commit

Permalink
Uint8ClampedArray type
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Nov 21, 2024
1 parent 2b07d0a commit c13ab51
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
63 changes: 62 additions & 1 deletion member/i8a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { byteLength, byteOffset, getType, littleEndian } from '../macro.ts';
import { Struct } from '../struct.ts';

import { memberI8A, memberU8A } from './i8a.ts';
import { memberI8A, memberU8A, memberU8AC } from './i8a.ts';

Deno.test('memberI8A', () => {
class Test extends Struct {
Expand Down Expand Up @@ -130,3 +130,64 @@ Deno.test('memberU8A', () => {
assertEquals(test.alpha, source);
assertNotStrictEquals(test.alpha, source);
});

Deno.test('memberU8AC', () => {
class Test extends Struct {
declare public readonly ['constructor']: typeof Test;

declare public alpha: Uint8ClampedArray;

declare public beta: Uint8ClampedArray;

declare public gamma: Uint8ClampedArray;

public static override readonly BYTE_LENGTH: number = ((o) => {
o += memberU8AC(2, this, 'alpha', o);
o += memberU8AC(4, this, 'beta', o);
o += memberU8AC(0, this, 'gamma', o);
return o;
})(super.BYTE_LENGTH);
}

const off = {
alpha: byteOffset(Test, 'alpha'),
beta: byteOffset(Test, 'beta'),
gamma: byteOffset(Test, 'gamma'),
};

assertEquals(Test.BYTE_LENGTH, 6);
assertEquals(byteLength(Test, 'alpha'), 2);
assertEquals(byteLength(Test, 'beta'), 4);
assertEquals(byteLength(Test, 'gamma'), 0);
assertEquals(littleEndian(Test, 'alpha'), null);
assertEquals(littleEndian(Test, 'beta'), null);
assertEquals(littleEndian(Test, 'gamma'), null);
assertEquals(getType(Test, 'alpha'), Uint8ClampedArray);
assertEquals(getType(Test, 'beta'), Uint8ClampedArray);
assertEquals(getType(Test, 'gamma'), Uint8ClampedArray);

const data = new Uint8Array(Test.BYTE_LENGTH);
const test = new Test(data.buffer);
test.alpha[0] = 1;
test.alpha[1] = 0xff + 1;
test.beta[0] = 2;
test.beta[1] = 0xfe;
test.beta[2] = 3;
test.beta[3] = 0xfd;

assertEquals(test.byteLength, Test.BYTE_LENGTH);
assertEquals(test.gamma.length, 0);
assertEquals(data[off.alpha], 1);
assertEquals(data[off.alpha + 1], 0xff);
assertEquals(data[off.beta], 2);
assertEquals(data[off.beta + 1], 0xfe);
assertEquals(data[off.beta + 2], 3);
assertEquals(data[off.beta + 3], 0xfd);

assertStrictEquals(test.alpha, test.alpha);

const source = new Uint8ClampedArray([4, 252]);
test.alpha = source;
assertEquals(test.alpha, source);
assertNotStrictEquals(test.alpha, source);
});
35 changes: 35 additions & 0 deletions member/i8a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,38 @@ export function memberU8A<C extends typeof Struct>(
},
);
}

/**
* Member uint8 array clamped.
*
* @param count Array length.
* @param StructC Struct constructor.
* @param name Member name.
* @param byteOffset Byte offset.
* @returns Byte length.
*/
export function memberU8AC<C extends typeof Struct>(
count: number,
StructC: C,
name: MembersExtends<C, Uint8ClampedArray>,
byteOffset: number,
): number {
return memberView(
StructC,
name,
byteOffset,
count,
null,
Uint8ClampedArray,
function (): Uint8ClampedArray {
return new Uint8ClampedArray(
this.buffer,
this.byteOffset + byteOffset,
count,
);
},
function (value: Uint8ClampedArray): void {
assignView(this[name] as Uint8ClampedArray, value);
},
);
}
6 changes: 5 additions & 1 deletion type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export type ArrayBufferReal = ArrayBufferLike & { BYTES_PER_ELEMENT?: never };
/**
* Types of child structures.
*/
export type ChildTypes = typeof Int8Array | typeof Uint8Array | typeof Struct;
export type ChildTypes =
| typeof Int8Array
| typeof Uint8Array
| typeof Uint8ClampedArray
| typeof Struct;

/**
* Types of member type.
Expand Down

0 comments on commit c13ab51

Please sign in to comment.