Skip to content

Commit

Permalink
feat: split out into pointer value and view
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed May 5, 2023
1 parent 3cfc8c4 commit 8ec3bf4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 37 deletions.
3 changes: 3 additions & 0 deletions types/ffi/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./pointer_value.ts";
export * from "./pointer_view.ts";
export * from "./pointer.ts";
38 changes: 38 additions & 0 deletions types/ffi/pointer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { AlignedType, SizedType } from "../types.ts";
import { pointerView } from "./pointer_view.ts";

export class Pointer<T> implements AlignedType<T> {
byteLength: number;
byteAlign: number;
type: SizedType<T>;
pointerType: AlignedType<Deno.UnsafePointerView>;

constructor(
type: SizedType<T>,
pointerType: AlignedType<Deno.UnsafePointerView> = pointerView,
) {
this.byteAlign = pointerType.byteAlign;
this.byteLength = pointerType.byteLength;
this.type = type;
this.pointerType = pointerType;
}

read(dataView: DataView, byteOffset = 0): T {
return this.type.read(
new DataView(
this.pointerType.read(dataView, byteOffset).getArrayBuffer(
this.type.byteLength,
),
),
);
}

write(value: T, dataView: DataView, byteOffset = 0) {
const buffer = new ArrayBuffer(this.type.byteLength);
this.type.write(value, new DataView(buffer));
const pointerView = new Deno.UnsafePointerView(
Deno.UnsafePointer.of(buffer)!,
);
this.pointerType.write(pointerView, dataView, byteOffset);
}
}
32 changes: 32 additions & 0 deletions types/ffi/pointer_value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { AlignedType } from "../types.ts";
import { u64 } from "../mod.ts";

export class PointerValue implements AlignedType<Deno.PointerValue> {
byteLength: number;
byteAlign: number;
pointerType: AlignedType<number | bigint>;

constructor(
pointerType: AlignedType<number | bigint> = u64,
) {
this.byteAlign = pointerType.byteAlign;
this.byteLength = pointerType.byteLength;
this.pointerType = pointerType;
}

read(dataView: DataView, byteOffset = 0): Deno.PointerValue {
return Deno.UnsafePointer.create(
this.pointerType.read(dataView, byteOffset),
);
}

write(value: Deno.PointerValue, dataView: DataView, byteOffset = 0) {
this.pointerType.write(
Deno.UnsafePointer.value(value),
dataView,
byteOffset,
);
}
}

export const pointerValue = new PointerValue();
28 changes: 28 additions & 0 deletions types/ffi/pointer_view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { AlignedType } from "../types.ts";
import { pointerValue } from "./pointer_value.ts";

export class PointerView implements AlignedType<Deno.UnsafePointerView> {
byteLength: number;
byteAlign: number;
pointerType: AlignedType<Deno.PointerValue>;

constructor(
pointerType: AlignedType<Deno.PointerValue> = pointerValue,
) {
this.byteAlign = pointerType.byteAlign;
this.byteLength = pointerType.byteLength;
this.pointerType = pointerType;
}

read(dataView: DataView, byteOffset = 0): Deno.UnsafePointerView {
return new Deno.UnsafePointerView(
this.pointerType.read(dataView, byteOffset)!,
);
}

write(value: Deno.UnsafePointerView, dataView: DataView, byteOffset = 0) {
this.pointerType.write(value.pointer, dataView, byteOffset);
}
}

export const pointerView = new PointerView();
37 changes: 0 additions & 37 deletions types/pointer/pointer.ts

This file was deleted.

0 comments on commit 8ec3bf4

Please sign in to comment.