From d1940944943cd79ce1f2b2080456dd28bec5c36f Mon Sep 17 00:00:00 2001 From: cyan Date: Thu, 19 Sep 2024 10:59:07 +0200 Subject: feat: deno ffi bindings (#40) * initial ts commit * monero.ts improvements * test on latest, build on debian:bookworm * feat: upstream changes to bindings * chore: update checksums * feat: allow manually loading dylib * chore: add readme * fix: free strings after being read to prevent potential memory leaks * fix: load dylib * fix: checksum checks segfaulting because of freeing const variables --------- Co-authored-by: Mateusz Franik <47059999+Im-Beast@users.noreply.github.com> Co-authored-by: Im-Beast --- impls/monero.ts/src/utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 impls/monero.ts/src/utils.ts (limited to 'impls/monero.ts/src/utils.ts') diff --git a/impls/monero.ts/src/utils.ts b/impls/monero.ts/src/utils.ts new file mode 100644 index 0000000..6fa640f --- /dev/null +++ b/impls/monero.ts/src/utils.ts @@ -0,0 +1,25 @@ +import { dylib } from "../mod.ts"; + +export type Sanitizer = () => void | PromiseLike; + +const textEncoder = new TextEncoder(); +export function CString(string: string): Deno.PointerValue { + return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`)); +} + +/** + * This method reads string from the given pointer and frees the string. + * + * SAFETY: Do not use readCString twice on the same pointer as it will cause double free\ + * If you want to read CString without freeing it set the {@linkcode free} parameter to false + */ +export async function readCString(pointer: Deno.PointerObject, free?: boolean): Promise; +export async function readCString(pointer: Deno.PointerValue, free?: boolean): Promise; +export async function readCString(pointer: Deno.PointerValue, free = true): Promise { + if (!pointer) return null; + const string = new Deno.UnsafePointerView(pointer).getCString(); + if (free) { + await dylib.symbols.MONERO_free(pointer); + } + return string; +} -- cgit v1.2.3