summaryrefslogtreecommitdiff
path: root/impls/monero.ts/src/utils.ts
diff options
context:
space:
mode:
authorcyan <cyjan@mrcyjanek.net>2024-10-21 11:30:40 +0200
committerGitHub <noreply@github.com>2024-10-21 11:30:40 +0200
commitd54c9023bdf089489468d86b3aedd017e8973252 (patch)
tree39d96db065dfb35cd995968ee21bff00ea836a99 /impls/monero.ts/src/utils.ts
parentf956f6b78164412c60ba6e7e368afc17635ae7ce (diff)
parentd04dcf67ec042fca76b48ffc09ea9d2ea4b5106e (diff)
Merge branch 'master' into ledgerledger
Diffstat (limited to 'impls/monero.ts/src/utils.ts')
-rw-r--r--impls/monero.ts/src/utils.ts14
1 files changed, 13 insertions, 1 deletions
diff --git a/impls/monero.ts/src/utils.ts b/impls/monero.ts/src/utils.ts
index 6fa640f..e88ddcd 100644
--- a/impls/monero.ts/src/utils.ts
+++ b/impls/monero.ts/src/utils.ts
@@ -1,4 +1,5 @@
import { dylib } from "../mod.ts";
+import type { moneroSymbols, MoneroTsDylib, WowneroTsDylib } from "./symbols.ts";
export type Sanitizer = () => void | PromiseLike<void>;
@@ -7,6 +8,17 @@ export function CString(string: string): Deno.PointerValue<string> {
return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`));
}
+type SymbolWithoutPrefix = keyof typeof moneroSymbols extends `MONERO_${infer DylibSymbol}` ? DylibSymbol : never;
+export function getSymbol<S extends SymbolWithoutPrefix>(
+ symbol: S,
+): MoneroTsDylib["symbols"][`MONERO_${S}`] | WowneroTsDylib["symbols"][`WOWNERO_${S}`] {
+ if ("MONERO_free" in dylib.symbols) {
+ return dylib.symbols[`MONERO_${symbol}` as const];
+ } else {
+ return dylib.symbols[`WOWNERO_${symbol}` as const];
+ }
+}
+
/**
* This method reads string from the given pointer and frees the string.
*
@@ -19,7 +31,7 @@ export async function readCString(pointer: Deno.PointerValue, free = true): Prom
if (!pointer) return null;
const string = new Deno.UnsafePointerView(pointer).getCString();
if (free) {
- await dylib.symbols.MONERO_free(pointer);
+ await getSymbol("free")(pointer);
}
return string;
}