Skip to content

Commit

Permalink
feat: pointers (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Sjögreen <[email protected]>
  • Loading branch information
JinWeiTan and eliassjogreen authored May 5, 2023
1 parent 8aa2df9 commit bca52d5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.vscode
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();

0 comments on commit bca52d5

Please sign in to comment.