Skip to content
Merged
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
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rotu/structview",
"version": "0.14.1",
"version": "0.15.0",
"license": "MIT",
"tasks": {
"dev": "deno test --watch",
Expand Down
29 changes: 26 additions & 3 deletions fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,36 @@ export function bool(fieldOffset: number): StructPropertyDescriptor<boolean> {

/**
* Define a descriptor based on a dataview of the struct
* @param fieldGetter function which, given a dataview, returns
* @returns
* @param fieldGetter function which, given a dataview, returns the field value
* @param fieldSetter optional function which, given a dataview and a value, sets the field value
* @returns an enumerable property descriptor; readonly if no setter is provided
*/
export function fromDataView<T>(
fieldGetter: (dv: DataView) => T,
): StructPropertyDescriptor<T> & ReadOnlyAccessorDescriptor<T> {
fieldSetter: (dv: DataView, value: T) => void,
): StructPropertyDescriptor<T>
export function fromDataView<T>(
fieldGetter: (dv: DataView) => T,
): StructPropertyDescriptor<T> & ReadOnlyAccessorDescriptor<T>
export function fromDataView<T>(
fieldGetter: (dv: DataView) => T,
fieldSetter?: (dv: DataView, value: T) => void,
): StructPropertyDescriptor<T> {
if (fieldSetter !== undefined) {
return {
enumerable: true,
get() {
const dv = structDataView(this)
return fieldGetter(dv)
},
set(value) {
const dv = structDataView(this)
fieldSetter(dv, value)
},
}
}
return {
enumerable: true,
get() {
const dv = structDataView(this)
return fieldGetter(dv)
Expand Down
47 changes: 47 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
f16,
f32,
f64,
fromDataView,
i16,
i32,
i64,
Expand Down Expand Up @@ -449,3 +450,49 @@ Deno.test("alloc", () => {
// ensure correct typing (that alloc doesn't return a bare Struct)
const _zz: Sized = z
})

Deno.test("fromDataView getter-only is readonly and enumerable", () => {
class S extends defineStruct({
val: fromDataView((dv) => dv.getUint8(0)),
}) {}
const buf = new Uint8Array([42])
const obj = new S(buf)
assertEquals(obj.val, 42)

// type test: val is readonly
assertThrows(() => {
// @ts-expect-error assigning to readonly property
obj.val = 1
})

// the descriptor should be enumerable
const keys: string[] = []
for (const k in S.prototype) {
keys.push(k)
}
assert(keys.includes("val"))
})

Deno.test("fromDataView with setter is writable and enumerable", () => {
class S extends defineStruct({
val: fromDataView(
(dv) => dv.getUint8(0),
(dv, v) => dv.setUint8(0, v),
),
}) {}
const buf = new Uint8Array([0])
const obj = new S(buf)
obj.val = 99
assertEquals(obj.val, 99)
assertEquals(buf[0], 99)

// type test: val is writable (no @ts-expect-error needed)
const _: number = obj.val

// the descriptor should be enumerable
const keys: string[] = []
for (const k in S.prototype) {
keys.push(k)
}
assert(keys.includes("val"))
})
Loading