1- use std:: sync:: LazyLock ;
1+ use std:: { cell :: RefCell , sync:: LazyLock } ;
22
33use napi:: {
44 bindgen_prelude:: {
@@ -150,9 +150,7 @@ impl BuildInfo {
150150 }
151151}
152152
153- fn create_known_private_properties ( env : & Env ) -> napi:: Result < Vec < Property > > {
154- let mut properties = vec ! [ ] ;
155-
153+ fn create_known_private_properties ( env : & Env , properties : & mut Vec < Property > ) -> napi:: Result < ( ) > {
156154 BUILD_INFO_ASSETS_SYMBOL . with ( |once_cell| {
157155 #[ allow( clippy:: unwrap_used) ]
158156 let symbol = once_cell. get ( ) . unwrap ( ) ;
@@ -264,7 +262,11 @@ fn create_known_private_properties(env: &Env) -> napi::Result<Vec<Property>> {
264262 Ok :: < ( ) , napi:: Error > ( ( ) )
265263 } ) ?;
266264
267- Ok ( properties)
265+ Ok ( ( ) )
266+ }
267+
268+ thread_local ! {
269+ static BUILD_INFO_PROPERTIES_BUFFER : RefCell <Vec <Property >> = const { RefCell :: new( Vec :: new( ) ) } ;
268270}
269271
270272impl ToNapiValue for BuildInfo {
@@ -278,59 +280,64 @@ impl ToNapiValue for BuildInfo {
278280 let napi_val = ToNapiValue :: to_napi_value ( env, known) ?;
279281 let mut object = Object :: from_raw ( env, napi_val) ;
280282
281- let mut properties = create_known_private_properties ( & env_wrapper) ?;
282-
283- let commit_custom_fields_fn: napi:: bindgen_prelude:: Function < ' _ , ( ) , ( ) > = env_wrapper
284- . create_function_from_closure ( "commitCustomFieldsToRust" , |ctx| {
285- let object = ctx. this :: < Object > ( ) ?;
286- let env = ctx. env ;
287- let this: & mut KnownBuildInfo = FromNapiMutRef :: from_napi_mut_ref ( env. raw ( ) , object. raw ( ) ) ?;
288-
289- this. with_mut ( |module| {
290- let mut extras = serde_json:: Map :: new ( ) ;
291- let names = Array :: from_unknown ( object. get_property_names ( ) ?. to_unknown ( ) ) ?;
292- for index in 0 ..names. len ( ) {
293- if let Some ( name) = names. get :: < String > ( index) ? {
294- if !KNOWN_BUILD_INFO_FIELD_NAMES . contains ( name. as_str ( ) ) {
295- let value = object. get_named_property :: < Unknown > ( & name) ?;
296- if let Some ( json_value) = unknown_to_json_value ( value) ? {
297- extras. insert ( name, json_value) ;
283+ BUILD_INFO_PROPERTIES_BUFFER . with ( |ref_cell| {
284+ let mut properties = ref_cell. borrow_mut ( ) ;
285+ properties. clear ( ) ;
286+ create_known_private_properties ( & env_wrapper, & mut properties) ?;
287+
288+ let commit_custom_fields_fn: napi:: bindgen_prelude:: Function < ' _ , ( ) , ( ) > = env_wrapper
289+ . create_function_from_closure ( "commitCustomFieldsToRust" , |ctx| {
290+ let object = ctx. this :: < Object > ( ) ?;
291+ let env = ctx. env ;
292+ let this: & mut KnownBuildInfo =
293+ FromNapiMutRef :: from_napi_mut_ref ( env. raw ( ) , object. raw ( ) ) ?;
294+
295+ this. with_mut ( |module| {
296+ let mut extras = serde_json:: Map :: new ( ) ;
297+ let names = Array :: from_unknown ( object. get_property_names ( ) ?. to_unknown ( ) ) ?;
298+ for index in 0 ..names. len ( ) {
299+ if let Some ( name) = names. get :: < String > ( index) ? {
300+ if !KNOWN_BUILD_INFO_FIELD_NAMES . contains ( name. as_str ( ) ) {
301+ let value = object. get_named_property :: < Unknown > ( & name) ?;
302+ if let Some ( json_value) = unknown_to_json_value ( value) ? {
303+ extras. insert ( name, json_value) ;
304+ }
298305 }
299306 }
300307 }
301- }
302308
303- module. build_info_mut ( ) . extras = extras;
304-
305- Ok ( ( ) )
306- } )
309+ module. build_info_mut ( ) . extras = extras;
310+
311+ Ok ( ( ) )
312+ } )
313+ } ) ?;
314+
315+ val. with_ref ( |module| {
316+ let extras = & module. build_info ( ) . extras ;
317+ properties. reserve ( extras. len ( ) + 1 ) ;
318+ for ( key, value) in extras {
319+ let napi_val = ToNapiValue :: to_napi_value ( env, value) ?;
320+ properties. push (
321+ Property :: new ( )
322+ . with_utf8_name ( key) ?
323+ . with_value ( & Object :: from_raw ( env, napi_val) ) ,
324+ ) ;
325+ }
326+ Ok ( ( ) )
307327 } ) ?;
308-
309- val. with_ref ( |module| {
310- let extras = & module. build_info ( ) . extras ;
311- properties. reserve ( extras. len ( ) + 1 ) ;
312- for ( key, value) in extras {
313- let napi_val = ToNapiValue :: to_napi_value ( env, value) ?;
328+ COMMIT_CUSTOM_FIELDS_SYMBOL . with ( |once_cell| {
329+ #[ allow( clippy:: unwrap_used) ]
330+ let symbol = once_cell. get ( ) . unwrap ( ) ;
314331 properties. push (
315332 Property :: new ( )
316- . with_utf8_name ( key) ?
317- . with_value ( & Object :: from_raw ( env, napi_val) ) ,
333+ . with_name ( & env_wrapper, symbol) ?
334+ . with_value ( & commit_custom_fields_fn)
335+ . with_property_attributes ( PropertyAttributes :: Configurable ) ,
318336 ) ;
319- }
320- Ok ( ( ) )
321- } ) ?;
322- COMMIT_CUSTOM_FIELDS_SYMBOL . with ( |once_cell| {
323- #[ allow( clippy:: unwrap_used) ]
324- let symbol = once_cell. get ( ) . unwrap ( ) ;
325- properties. push (
326- Property :: new ( )
327- . with_name ( & env_wrapper, symbol) ?
328- . with_value ( & commit_custom_fields_fn)
329- . with_property_attributes ( PropertyAttributes :: Configurable ) ,
330- ) ;
331- Ok :: < ( ) , napi:: Error > ( ( ) )
337+ Ok :: < ( ) , napi:: Error > ( ( ) )
338+ } ) ?;
339+ object. define_properties ( & properties)
332340 } ) ?;
333- object. define_properties ( & properties) ?;
334341
335342 Ok ( napi_val)
336343 }
0 commit comments