-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split out into pointer value and view
- Loading branch information
1 parent
3cfc8c4
commit 8ec3bf4
Showing
5 changed files
with
101 additions
and
37 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
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"; |
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,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); | ||
} | ||
} |
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,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(); |
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,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(); |
This file was deleted.
Oops, something went wrong.