From bf792ae88fe843cec457982d854c7d2ca0d81a31 Mon Sep 17 00:00:00 2001 From: bing Date: Tue, 24 Mar 2026 22:17:23 +0800 Subject: [PATCH] refactor: make user provide `Ref` We still want the option of the user providing the `Ref` if they want to hold the reference to the created napi object. This is a fix for bls integration on lodestar suffering from a memory leak - because our usage of our bls bindings in lodestar-z involves us doing a `_ = Env.wrap`, we drop the reference to the napi object while increasing the refcount, which means our finalize calls from the v8 GC is never called. --- src/Env.zig | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Env.zig b/src/Env.zig index 077df04..096fbef 100644 --- a/src/Env.zig +++ b/src/Env.zig @@ -743,8 +743,8 @@ pub fn wrap( native_object: *Data, comptime finalize_cb: ?FinalizeCallback(Data), finalize_hint: ?*anyopaque, -) NapiError!Ref { - var ref_: c.napi_ref = undefined; + ref: ?*Ref, +) NapiError!void { try status.check( c.napi_wrap( self.env, @@ -752,13 +752,9 @@ pub fn wrap( native_object, if (finalize_cb) |f| wrapFinalizeCallback(Data, f) else null, finalize_hint, - &ref_, + if (ref) |r| &r.ref_ else null, ), ); - return Ref{ - .env = self.env, - .ref_ = ref_, - }; } /// https://nodejs.org/api/n-api.html#napi_unwrap