2021-11-22 17:51:21 +09:00
|
|
|
use proc_macro2::{Ident, Literal, TokenStream};
|
2021-11-10 20:04:36 +09:00
|
|
|
use quote::ToTokens;
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
use crate::{
|
|
|
|
codegen::{get_register_ident, js_mod_to_token_stream},
|
|
|
|
BindgenResult, NapiConst, TryToTokens,
|
|
|
|
};
|
2021-11-10 20:04:36 +09:00
|
|
|
|
|
|
|
impl TryToTokens for NapiConst {
|
|
|
|
fn try_to_tokens(&self, tokens: &mut TokenStream) -> BindgenResult<()> {
|
|
|
|
let register = self.gen_module_register();
|
|
|
|
(quote! {
|
|
|
|
#register
|
|
|
|
})
|
|
|
|
.to_tokens(tokens);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NapiConst {
|
|
|
|
fn gen_module_register(&self) -> TokenStream {
|
|
|
|
let name_str = self.name.to_string();
|
|
|
|
let name_ident = self.name.clone();
|
2021-11-22 17:51:21 +09:00
|
|
|
let js_name_lit = Literal::string(&format!("{}\0", self.name));
|
2021-11-10 20:04:36 +09:00
|
|
|
let register_name = get_register_ident(&name_str);
|
|
|
|
let type_name = &self.type_name;
|
2021-11-22 17:51:21 +09:00
|
|
|
let cb_name = Ident::new(
|
|
|
|
&format!("__register__const__{}_callback__", register_name),
|
|
|
|
self.name.span(),
|
|
|
|
);
|
2021-11-23 20:00:31 +09:00
|
|
|
let js_mod_ident = js_mod_to_token_stream(self.js_mod.as_ref());
|
2023-03-20 19:42:27 +09:00
|
|
|
crate::codegen::REGISTER_IDENTS.with(|c| {
|
|
|
|
c.borrow_mut().push(register_name.to_string());
|
|
|
|
});
|
2021-11-10 20:04:36 +09:00
|
|
|
quote! {
|
2021-11-22 19:31:51 +09:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[allow(clippy::all)]
|
2021-11-22 17:51:21 +09:00
|
|
|
unsafe fn #cb_name(env: napi::sys::napi_env) -> napi::Result<napi::sys::napi_value> {
|
|
|
|
<#type_name as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #name_ident)
|
|
|
|
}
|
2021-11-10 20:04:36 +09:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[allow(clippy::all)]
|
2023-11-03 13:09:06 +09:00
|
|
|
#[cfg(all(not(test), not(feature = "noop"), not(target_os = "wasi")))]
|
2021-11-10 20:04:36 +09:00
|
|
|
#[napi::bindgen_prelude::ctor]
|
|
|
|
fn #register_name() {
|
2021-11-23 20:00:31 +09:00
|
|
|
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_name);
|
2021-11-10 20:04:36 +09:00
|
|
|
}
|
2023-03-20 19:42:27 +09:00
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[allow(clippy::all)]
|
2023-11-03 13:09:06 +09:00
|
|
|
#[cfg(all(not(test), not(feature = "noop"), target_os = "wasi"))]
|
2023-03-20 19:42:27 +09:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn #register_name() {
|
|
|
|
napi::bindgen_prelude::register_module_export(#js_mod_ident, #js_name_lit, #cb_name);
|
|
|
|
}
|
2021-11-10 20:04:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|