fix(napi-derive): codegen bug when multi impl on same file

This commit is contained in:
LongYinan 2021-12-20 15:59:47 +08:00
parent ac1b52fab9
commit f7571d71c0
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
4 changed files with 19 additions and 4 deletions

View file

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use proc_macro2::{Ident, Literal, Span, TokenStream}; use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::ToTokens; use quote::ToTokens;
@ -8,6 +9,8 @@ use crate::{
BindgenResult, FnKind, NapiImpl, NapiStruct, NapiStructKind, TryToTokens, BindgenResult, FnKind, NapiImpl, NapiStruct, NapiStructKind, TryToTokens,
}; };
static NAPI_IMPL_ID: AtomicU32 = AtomicU32::new(0);
// Generate trait implementations for given Struct. // Generate trait implementations for given Struct.
fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> TokenStream { fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> TokenStream {
let name_str = name.to_string(); let name_str = name.to_string();
@ -476,7 +479,11 @@ impl NapiImpl {
let name_str = self.name.to_string(); let name_str = self.name.to_string();
let js_name = format!("{}\0", self.js_name); let js_name = format!("{}\0", self.js_name);
let mod_name = Ident::new( 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(), Span::call_site(),
); );

View file

@ -1,5 +1,5 @@
#[napi] #[napi]
fn get_words() -> Vec<&'static str> { pub fn get_words() -> Vec<&'static str> {
vec!["foo", "bar"] vec!["foo", "bar"]
} }

View file

@ -1,4 +1,4 @@
use napi::Result; use napi::{bindgen_prelude::Buffer, Result};
use crate::r#enum::Kind; 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] #[napi]
pub struct Blake2bKey(u32); pub struct Blake2bKey(u32);

View file

@ -8,7 +8,7 @@ fn get_buffer() -> Buffer {
#[napi] #[napi]
fn append_buffer(buf: Buffer) -> Buffer { fn append_buffer(buf: Buffer) -> Buffer {
let mut buf = Vec::<u8>::from(buf); let mut buf = Vec::<u8>::from(buf);
buf.push('!' as u8); buf.push(b'!' as u8);
buf.into() buf.into()
} }