X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=ts%2Fstructs%2FCommonBase.mts;h=df2cad8ec388482aa6a22c6efed2355e019cfb6d;hb=31a1b84c2ca25db2978865e9ca9573974f79cc79;hp=a7586b038f849eb3c6a2030b9fe0f4e12c1d009d;hpb=af6f9949b19c1e8d2daac5c7a94cf14d41175f2b;p=ldk-java diff --git a/ts/structs/CommonBase.mts b/ts/structs/CommonBase.mts index a7586b03..df2cad8e 100644 --- a/ts/structs/CommonBase.mts +++ b/ts/structs/CommonBase.mts @@ -355,33 +355,36 @@ import { DefaultRouter } from '../structs/DefaultRouter.mjs'; function freer(f: () => void) { f() } const finalizer = new FinalizationRegistry(freer); -function get_freeer(ptr: number, free_fn: (ptr: number) => void) { +function get_freeer(ptr: bigint, free_fn: (ptr: bigint) => void) { return () => { free_fn(ptr); } } export class CommonBase { - protected ptr: number; + protected ptr: bigint; protected ptrs_to: object[] = []; - protected constructor(ptr: number, free_fn: (ptr: number) => void) { + protected constructor(ptr: bigint, free_fn: (ptr: bigint) => void) { this.ptr = ptr; - if (Number.isFinite(ptr) && ptr != 0){ - finalizer.register(this, get_freeer(ptr, free_fn)); + if (ptr != 0n){ + finalizer.register(this, get_freeer(ptr, free_fn), this); } } // In Java, protected means "any subclass can access fields on any other subclass'" // In TypeScript, protected means "any subclass can access parent fields on instances of itself" // To work around this, we add accessors for other instances' protected fields here. protected static add_ref_from(holder: CommonBase, referent: object) { - holder.ptrs_to.push(referent); + if (holder !== null) { holder.ptrs_to.push(referent); } } protected static get_ptr_of(o: CommonBase) { return o.ptr; } protected static set_null_skip_free(o: CommonBase) { - o.ptr = 0; - finalizer.unregister(o); + o.ptr = 0n; + // @ts-ignore TypeScript is wrong about the returnvalue of unregister here! + const did_unregister: boolean = finalizer.unregister(o); + if (!did_unregister) + throw new Error("FinalizationRegistry unregister should always unregister unless you double-free'd"); } }