From f7571d71c0570c6bf105332ca486e2b9f0dd8af4 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 20 Dec 2021 15:59:47 +0800 Subject: [PATCH] fix(napi-derive): codegen bug when multi impl on same file --- crates/backend/src/codegen/struct.rs | 9 ++++++++- examples/napi/src/array.rs | 2 +- examples/napi/src/class.rs | 10 +++++++++- examples/napi/src/typed_array.rs | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/backend/src/codegen/struct.rs b/crates/backend/src/codegen/struct.rs index 887833c9..3ed55f91 100644 --- a/crates/backend/src/codegen/struct.rs +++ b/crates/backend/src/codegen/struct.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::sync::atomic::{AtomicU32, Ordering}; use proc_macro2::{Ident, Literal, Span, TokenStream}; use quote::ToTokens; @@ -8,6 +9,8 @@ use crate::{ BindgenResult, FnKind, NapiImpl, NapiStruct, NapiStructKind, TryToTokens, }; +static NAPI_IMPL_ID: AtomicU32 = AtomicU32::new(0); + // Generate trait implementations for given Struct. fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> TokenStream { let name_str = name.to_string(); @@ -476,7 +479,11 @@ impl NapiImpl { let name_str = self.name.to_string(); let js_name = format!("{}\0", self.js_name); let mod_name = Ident::new( - &format!("__napi_impl_helper__{}", name_str), + &format!( + "__napi_impl_helper__{}__{}", + name_str, + NAPI_IMPL_ID.fetch_add(1, Ordering::SeqCst) + ), Span::call_site(), ); diff --git a/examples/napi/src/array.rs b/examples/napi/src/array.rs index 9dda022d..01d5f546 100644 --- a/examples/napi/src/array.rs +++ b/examples/napi/src/array.rs @@ -1,5 +1,5 @@ #[napi] -fn get_words() -> Vec<&'static str> { +pub fn get_words() -> Vec<&'static str> { vec!["foo", "bar"] } diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 063310f6..d4b864c2 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -1,4 +1,4 @@ -use napi::Result; +use napi::{bindgen_prelude::Buffer, Result}; use crate::r#enum::Kind; @@ -75,6 +75,14 @@ impl Blake2bHasher { } } +#[napi] +impl Blake2bHasher { + #[napi] + pub fn update(&mut self, data: Buffer) { + self.0 += data.len() as u32; + } +} + #[napi] pub struct Blake2bKey(u32); diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs index ba779e9a..297c597e 100644 --- a/examples/napi/src/typed_array.rs +++ b/examples/napi/src/typed_array.rs @@ -8,7 +8,7 @@ fn get_buffer() -> Buffer { #[napi] fn append_buffer(buf: Buffer) -> Buffer { let mut buf = Vec::::from(buf); - buf.push('!' as u8); + buf.push(b'!' as u8); buf.into() }