summaryrefslogtreecommitdiff
path: root/impls/monero.ts/src/utils.ts
diff options
context:
space:
mode:
authorCzarek Nakamoto <cyjan@mrcyjanek.net>2024-12-04 12:26:45 -0500
committerCzarek Nakamoto <cyjan@mrcyjanek.net>2024-12-04 12:26:45 -0500
commitecc31787c2174a829848aac403bd13e663fe33c3 (patch)
tree7f505dc9bfe9c34c36c5043911be0cfc0d146a8d /impls/monero.ts/src/utils.ts
parent24076c5a32bbec3c77cc996cb74dd08d8077a7e0 (diff)
parent40c1a1bda4b6f125c702f5a37ecc48a6ebec24b8 (diff)
Merge branch 'develop' into zano
Diffstat (limited to 'impls/monero.ts/src/utils.ts')
-rw-r--r--impls/monero.ts/src/utils.ts32
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;
}