From 19d5722aeabdb5e42bb14a340cc30854083bb795 Mon Sep 17 00:00:00 2001 From: Im-Beast Date: Tue, 15 Oct 2024 12:07:26 +0200 Subject: feat: support wownero in monero.ts bindings --- impls/monero.ts/src/utils.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'impls/monero.ts/src/utils.ts') 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; @@ -7,6 +8,17 @@ export function CString(string: string): Deno.PointerValue { return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`)); } +type SymbolWithoutPrefix = keyof typeof moneroSymbols extends `MONERO_${infer DylibSymbol}` ? DylibSymbol : never; +export function getSymbol( + 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; } -- cgit v1.2.3