diff options
| author | cyan <cyjan@mrcyjanek.net> | 2024-09-19 10:59:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-19 10:59:07 +0200 |
| commit | d1940944943cd79ce1f2b2080456dd28bec5c36f (patch) | |
| tree | 8a2a29c5de6cec47ccefff3472f38a8ecb9fb8af /impls/monero.ts/src/utils.ts | |
| parent | 0b91e91d19d4a11d753f9bdda56398bcfcdb4260 (diff) | |
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 <franik.mateusz@gmail.com>
Diffstat (limited to 'impls/monero.ts/src/utils.ts')
| -rw-r--r-- | impls/monero.ts/src/utils.ts | 25 |
1 files changed, 25 insertions, 0 deletions
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<void>; + +const textEncoder = new TextEncoder(); +export function CString(string: string): Deno.PointerValue<string> { + 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<string>; +export async function readCString(pointer: Deno.PointerValue, free?: boolean): Promise<string | null>; +export async function readCString(pointer: Deno.PointerValue, free = true): Promise<string | null> { + if (!pointer) return null; + const string = new Deno.UnsafePointerView(pointer).getCString(); + if (free) { + await dylib.symbols.MONERO_free(pointer); + } + return string; +} |
