diff options
| author | Czarek Nakamoto <cyjan@mrcyjanek.net> | 2025-01-05 13:17:22 +0100 |
|---|---|---|
| committer | Czarek Nakamoto <cyjan@mrcyjanek.net> | 2025-01-05 13:17:22 +0100 |
| commit | 085d74b37b478be77bc873d66876247a751aa957 (patch) | |
| tree | d8434dd9c8c57df9b64ae93059d9ebb5a16b90f2 /impls/monero.ts/src/utils.ts | |
| parent | 8e7bc59509c40f00702ba568a0adcb3cf82e6e05 (diff) | |
| parent | c3dd64bdee37d361a2c1252d127fb575936e43e6 (diff) | |
Merge remote-tracking branch 'origin/develop' into rust-develop
Diffstat (limited to 'impls/monero.ts/src/utils.ts')
| -rw-r--r-- | impls/monero.ts/src/utils.ts | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/impls/monero.ts/src/utils.ts b/impls/monero.ts/src/utils.ts index e88ddcd..4323b72 100644 --- a/impls/monero.ts/src/utils.ts +++ b/impls/monero.ts/src/utils.ts @@ -1,22 +1,19 @@ -import { dylib } from "../mod.ts"; -import type { moneroSymbols, MoneroTsDylib, WowneroTsDylib } from "./symbols.ts"; - -export type Sanitizer = () => void | PromiseLike<void>; +import { fns } from "./bindings.ts"; const textEncoder = new TextEncoder(); -export function CString(string: string): Deno.PointerValue<string> { - return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`)); +export const SEPARATOR = ","; +export const C_SEPARATOR = CString(SEPARATOR); + +export function maybeMultipleStrings(input: string): string | string[]; +export function maybeMultipleStrings(input: null | string): null | string | string[]; +export function maybeMultipleStrings(input: null | string): null | string | string[] { + if (!input) return null; + const multiple = input.split(SEPARATOR); + return multiple.length === 1 ? multiple[0] : multiple; } -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]; - } +export function CString(string: string): Deno.PointerValue<string> { + return Deno.UnsafePointer.of(textEncoder.encode(`${string}\x00`)); } /** @@ -29,9 +26,8 @@ export async function readCString(pointer: Deno.PointerObject, free?: boolean): 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 getSymbol("free")(pointer); - } + if (string && free) await fns.free(pointer); return string; } |
