Full qualify types
This commit is contained in:
parent
1013052de3
commit
a14cbbb836
8 changed files with 126 additions and 111 deletions
|
@ -34,7 +34,7 @@ impl NapiEnum {
|
||||||
});
|
});
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl TypeName for #name {
|
impl napi::bindgen_prelude::TypeName for #name {
|
||||||
fn type_name() -> &'static str {
|
fn type_name() -> &'static str {
|
||||||
#name_str
|
#name_str
|
||||||
}
|
}
|
||||||
|
@ -44,16 +44,22 @@ impl NapiEnum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValidateNapiValue for #name {
|
impl napi::bindgen_prelude::ValidateNapiValue for #name {
|
||||||
unsafe fn validate(env: sys::napi_env, napi_val: sys::napi_value) -> Result<()> {
|
unsafe fn validate(
|
||||||
assert_type_of!(env, napi_val, ValueType::Number)
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
|
napi_val: napi::bindgen_prelude::sys::napi_value
|
||||||
|
) -> napi::bindgen_prelude::Result<()> {
|
||||||
|
napi::bindgen_prelude::assert_type_of!(env, napi_val, napi::bindgen_prelude::ValueType::Number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromNapiValue for #name {
|
impl napi::bindgen_prelude::FromNapiValue for #name {
|
||||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
unsafe fn from_napi_value(
|
||||||
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
|
napi_val: napi::bindgen_prelude::sys::napi_value
|
||||||
|
) -> napi::bindgen_prelude::Result<Self> {
|
||||||
let val = i32::from_napi_value(env, napi_val).map_err(|e| {
|
let val = i32::from_napi_value(env, napi_val).map_err(|e| {
|
||||||
error!(
|
napi::bindgen_prelude::error!(
|
||||||
e.status,
|
e.status,
|
||||||
"Failed to convert napi value into enum `{}`. {}",
|
"Failed to convert napi value into enum `{}`. {}",
|
||||||
#name_str,
|
#name_str,
|
||||||
|
@ -64,8 +70,8 @@ impl NapiEnum {
|
||||||
match val {
|
match val {
|
||||||
#(#from_napi_branches,)*
|
#(#from_napi_branches,)*
|
||||||
_ => {
|
_ => {
|
||||||
Err(error!(
|
Err(napi::bindgen_prelude::error!(
|
||||||
Status::InvalidArg,
|
napi::bindgen_prelude::Status::InvalidArg,
|
||||||
"value `{}` does not match any variant of enum `{}`",
|
"value `{}` does not match any variant of enum `{}`",
|
||||||
val,
|
val,
|
||||||
#name_str
|
#name_str
|
||||||
|
@ -75,8 +81,11 @@ impl NapiEnum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToNapiValue for #name {
|
impl napi::bindgen_prelude::ToNapiValue for #name {
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(
|
||||||
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
|
val: Self
|
||||||
|
) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
|
||||||
let val = match val {
|
let val = match val {
|
||||||
#(#to_napi_branches,)*
|
#(#to_napi_branches,)*
|
||||||
};
|
};
|
||||||
|
@ -101,8 +110,8 @@ impl NapiEnum {
|
||||||
define_properties.push(quote! {
|
define_properties.push(quote! {
|
||||||
{
|
{
|
||||||
let name = CString::new(#name_lit).unwrap();
|
let name = CString::new(#name_lit).unwrap();
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_set_named_property(env, obj_ptr, name.as_ptr(), i32::to_napi_value(env, #val_lit)?),
|
napi::bindgen_prelude::sys::napi_set_named_property(env, obj_ptr, name.as_ptr(), i32::to_napi_value(env, #val_lit)?),
|
||||||
"Failed to defined enum `{}`",
|
"Failed to defined enum `{}`",
|
||||||
#js_name_lit
|
#js_name_lit
|
||||||
)?;
|
)?;
|
||||||
|
@ -113,16 +122,16 @@ impl NapiEnum {
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
#[ctor]
|
#[napi::bindgen_prelude::ctor]
|
||||||
fn #register_name() {
|
fn #register_name() {
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
unsafe fn cb(env: sys::napi_env) -> Result<sys::napi_value> {
|
unsafe fn cb(env: napi::bindgen_prelude::sys::napi_env) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
|
||||||
let mut obj_ptr = ptr::null_mut();
|
let mut obj_ptr = ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_create_object(env, &mut obj_ptr),
|
napi::bindgen_prelude::sys::napi_create_object(env, &mut obj_ptr),
|
||||||
"Failed to create napi object"
|
"Failed to create napi object"
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -131,7 +140,7 @@ impl NapiEnum {
|
||||||
Ok(obj_ptr)
|
Ok(obj_ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
register_module_export(#js_name_lit, cb);
|
napi::bindgen_prelude::register_module_export(#js_name_lit, cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ impl TryToTokens for NapiFn {
|
||||||
quote! { Ok(#receiver(#(#arg_names),*).await) }
|
quote! { Ok(#receiver(#(#arg_names),*).await) }
|
||||||
};
|
};
|
||||||
quote! {
|
quote! {
|
||||||
execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| {
|
napi::bindgen_prelude::execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| {
|
||||||
#ret
|
#ret
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -47,20 +47,20 @@ impl TryToTokens for NapiFn {
|
||||||
quote! { #native_call }
|
quote! { #native_call }
|
||||||
} else if self.kind == FnKind::Constructor {
|
} else if self.kind == FnKind::Constructor {
|
||||||
quote! {
|
quote! {
|
||||||
let call_from_factory = ___CALL_FROM_FACTORY.load(std::sync::atomic::Ordering::Relaxed);
|
let call_from_factory = napi::bindgen_prelude::___CALL_FROM_FACTORY.load(std::sync::atomic::Ordering::Relaxed);
|
||||||
// constructor function is called from class `factory`
|
// constructor function is called from class `factory`
|
||||||
// so we should skip the original `constructor` logic
|
// so we should skip the original `constructor` logic
|
||||||
if call_from_factory {
|
if call_from_factory {
|
||||||
return std::ptr::null_mut();
|
return std::ptr::null_mut();
|
||||||
}
|
}
|
||||||
CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| {
|
napi::bindgen_prelude::CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| {
|
||||||
#(#arg_conversions)*
|
#(#arg_conversions)*
|
||||||
#native_call
|
#native_call
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| {
|
napi::bindgen_prelude::CallbackInfo::<#args_len>::new(env, cb, None).and_then(|mut cb| {
|
||||||
#(#arg_conversions)*
|
#(#arg_conversions)*
|
||||||
#native_call
|
#native_call
|
||||||
})
|
})
|
||||||
|
@ -73,13 +73,13 @@ impl TryToTokens for NapiFn {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
extern "C" fn #intermediate_ident(
|
extern "C" fn #intermediate_ident(
|
||||||
env: sys::napi_env,
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
cb: sys::napi_callback_info
|
cb: napi::bindgen_prelude::sys::napi_callback_info
|
||||||
) -> sys::napi_value {
|
) -> napi::bindgen_prelude::sys::napi_value {
|
||||||
unsafe {
|
unsafe {
|
||||||
#function_call.unwrap_or_else(|e| {
|
#function_call.unwrap_or_else(|e| {
|
||||||
JsError::from(e).throw_into(env);
|
napi::bindgen_prelude::JsError::from(e).throw_into(env);
|
||||||
std::ptr::null_mut::<sys::napi_value__>()
|
std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_value__>()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,7 @@ impl NapiFn {
|
||||||
match arg {
|
match arg {
|
||||||
NapiFnArgKind::PatType(path) => {
|
NapiFnArgKind::PatType(path) => {
|
||||||
if &path.ty.to_token_stream().to_string() == "Env" {
|
if &path.ty.to_token_stream().to_string() == "Env" {
|
||||||
args.push(quote! { Env::from(env) });
|
args.push(quote! { napi::bindgen_prelude::Env::from(env) });
|
||||||
skipped_arg_count += 1;
|
skipped_arg_count += 1;
|
||||||
} else {
|
} else {
|
||||||
arg_conversions.push(self.gen_ty_arg_conversion(&ident, i, path));
|
arg_conversions.push(self.gen_ty_arg_conversion(&ident, i, path));
|
||||||
|
@ -149,18 +149,18 @@ impl NapiFn {
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
quote! {
|
quote! {
|
||||||
let #arg_name = <#elem as FromNapiMutRef>::from_napi_mut_ref(env, cb.get_arg(#index))?;
|
let #arg_name = <#elem as napi::bindgen_prelude::FromNapiMutRef>::from_napi_mut_ref(env, cb.get_arg(#index))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
syn::Type::Reference(syn::TypeReference { elem, .. }) => {
|
syn::Type::Reference(syn::TypeReference { elem, .. }) => {
|
||||||
quote! {
|
quote! {
|
||||||
let #arg_name = <#elem as FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?;
|
let #arg_name = <#elem as napi::bindgen_prelude::FromNapiRef>::from_napi_ref(env, cb.get_arg(#index))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let type_check = if self.strict {
|
let type_check = if self.strict {
|
||||||
quote! {
|
quote! {
|
||||||
<#ty as ValidateNapiValue>::validate(env, cb.get_arg(#index))?;
|
<#ty as napi::bindgen_prelude::ValidateNapiValue>::validate(env, cb.get_arg(#index))?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {}
|
quote! {}
|
||||||
|
@ -169,7 +169,7 @@ impl NapiFn {
|
||||||
quote! {
|
quote! {
|
||||||
let #arg_name = {
|
let #arg_name = {
|
||||||
#type_check
|
#type_check
|
||||||
<#ty as FromNapiValue>::from_napi_value(env, cb.get_arg(#index))?
|
<#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, cb.get_arg(#index))?
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,13 +183,15 @@ impl NapiFn {
|
||||||
for (i, ty) in cb.args.iter().enumerate() {
|
for (i, ty) in cb.args.iter().enumerate() {
|
||||||
let cb_arg_ident = Ident::new(&format!("callback_arg_{}", i), Span::call_site());
|
let cb_arg_ident = Ident::new(&format!("callback_arg_{}", i), Span::call_site());
|
||||||
inputs.push(quote! { #cb_arg_ident: #ty });
|
inputs.push(quote! { #cb_arg_ident: #ty });
|
||||||
arg_conversions.push(quote! { <#ty as ToNapiValue>::to_napi_value(env, #cb_arg_ident)? });
|
arg_conversions.push(
|
||||||
|
quote! { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #cb_arg_ident)? },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ret = match &cb.ret {
|
let ret = match &cb.ret {
|
||||||
Some(ty) => {
|
Some(ty) => {
|
||||||
quote! {
|
quote! {
|
||||||
let ret = <#ty as FromNapiValue>::from_napi_value(env, ret_ptr)?;
|
let ret = <#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, ret_ptr)?;
|
||||||
|
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
@ -198,7 +200,7 @@ impl NapiFn {
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
assert_type_of!(env, cb.get_arg(#index), ValueType::Function)?;
|
napi::bindgen_prelude::assert_type_of!(env, cb.get_arg(#index), napi::bindgen_prelude::ValueType::Function)?;
|
||||||
let #arg_name = |#(#inputs),*| {
|
let #arg_name = |#(#inputs),*| {
|
||||||
let args = vec![
|
let args = vec![
|
||||||
#(#arg_conversions),*
|
#(#arg_conversions),*
|
||||||
|
@ -206,8 +208,8 @@ impl NapiFn {
|
||||||
|
|
||||||
let mut ret_ptr = std::ptr::null_mut();
|
let mut ret_ptr = std::ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_call_function(
|
napi::bindgen_prelude::sys::napi_call_function(
|
||||||
env,
|
env,
|
||||||
cb.this(),
|
cb.this(),
|
||||||
cb.get_arg(#index),
|
cb.get_arg(#index),
|
||||||
|
@ -258,14 +260,14 @@ impl NapiFn {
|
||||||
} else if self.is_ret_result {
|
} else if self.is_ret_result {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
quote! {
|
quote! {
|
||||||
<#ty as ToNapiValue>::to_napi_value(env, #ret)
|
<#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #ret)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
match #ret {
|
match #ret {
|
||||||
Ok(value) => ToNapiValue::to_napi_value(env, value),
|
Ok(value) => napi::bindgen_prelude::ToNapiValue::to_napi_value(env, value),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
JsError::from(err).throw_into(env);
|
napi::bindgen_prelude::JsError::from(err).throw_into(env);
|
||||||
Ok(std::ptr::null_mut())
|
Ok(std::ptr::null_mut())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -273,12 +275,12 @@ impl NapiFn {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
<#ty as ToNapiValue>::to_napi_value(env, #ret)
|
<#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, #ret)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
<() as ToNapiValue>::to_napi_value(env, ())
|
<() as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, ())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -296,13 +298,13 @@ impl NapiFn {
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[ctor]
|
#[napi::bindgen_prelude::ctor]
|
||||||
fn #module_register_name() {
|
fn #module_register_name() {
|
||||||
unsafe fn cb(env: sys::napi_env) -> Result<sys::napi_value> {
|
unsafe fn cb(env: napi::bindgen_prelude::sys::napi_env) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
|
||||||
let mut fn_ptr = std::ptr::null_mut();
|
let mut fn_ptr = std::ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_create_function(
|
napi::bindgen_prelude::sys::napi_create_function(
|
||||||
env,
|
env,
|
||||||
#js_name.as_ptr() as *const _,
|
#js_name.as_ptr() as *const _,
|
||||||
#name_len,
|
#name_len,
|
||||||
|
@ -317,7 +319,7 @@ impl NapiFn {
|
||||||
Ok(fn_ptr)
|
Ok(fn_ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
register_module_export(#js_name, cb);
|
napi::bindgen_prelude::register_module_export(#js_name, cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use crate::{
|
||||||
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();
|
||||||
quote! {
|
quote! {
|
||||||
impl TypeName for #name {
|
impl napi::bindgen_prelude::TypeName for #name {
|
||||||
fn type_name() -> &'static str {
|
fn type_name() -> &'static str {
|
||||||
#name_str
|
#name_str
|
||||||
}
|
}
|
||||||
|
@ -24,12 +24,15 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token
|
||||||
|
|
||||||
#to_napi_val_impl
|
#to_napi_val_impl
|
||||||
|
|
||||||
impl FromNapiRef for #name {
|
impl napi::bindgen_prelude::FromNapiRef for #name {
|
||||||
unsafe fn from_napi_ref(env: sys::napi_env, napi_val: sys::napi_value) -> Result<&'static Self> {
|
unsafe fn from_napi_ref(
|
||||||
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
|
napi_val: napi::bindgen_prelude::sys::napi_value
|
||||||
|
) -> napi::bindgen_prelude::Result<&'static Self> {
|
||||||
let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut();
|
let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_unwrap(env, napi_val, &mut wrapped_val),
|
napi::bindgen_prelude::sys::napi_unwrap(env, napi_val, &mut wrapped_val),
|
||||||
"Failed to recover `{}` type from napi value",
|
"Failed to recover `{}` type from napi value",
|
||||||
#name_str,
|
#name_str,
|
||||||
)?;
|
)?;
|
||||||
|
@ -38,12 +41,15 @@ fn gen_napi_value_map_impl(name: &Ident, to_napi_val_impl: TokenStream) -> Token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromNapiMutRef for #name {
|
impl napi::bindgen_prelude::FromNapiMutRef for #name {
|
||||||
unsafe fn from_napi_mut_ref(env: sys::napi_env, napi_val: sys::napi_value) -> Result<&'static mut Self> {
|
unsafe fn from_napi_mut_ref(
|
||||||
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
|
napi_val: napi::bindgen_prelude::sys::napi_value
|
||||||
|
) -> napi::bindgen_prelude::Result<&'static mut Self> {
|
||||||
let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut();
|
let mut wrapped_val: *mut std::ffi::c_void = std::ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_unwrap(env, napi_val, &mut wrapped_val),
|
napi::bindgen_prelude::sys::napi_unwrap(env, napi_val, &mut wrapped_val),
|
||||||
"Failed to recover `{}` type from napi value",
|
"Failed to recover `{}` type from napi value",
|
||||||
#name_str,
|
#name_str,
|
||||||
)?;
|
)?;
|
||||||
|
@ -132,14 +138,14 @@ impl NapiStruct {
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
extern "C" fn constructor(
|
extern "C" fn constructor(
|
||||||
env: sys::napi_env,
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
cb: sys::napi_callback_info
|
cb: napi::bindgen_prelude::sys::napi_callback_info
|
||||||
) -> sys::napi_value {
|
) -> napi::bindgen_prelude::sys::napi_value {
|
||||||
CallbackInfo::<#fields_len>::new(env, cb, None)
|
napi::bindgen_prelude::CallbackInfo::<#fields_len>::new(env, cb, None)
|
||||||
.and_then(|cb| unsafe { cb.construct(#js_name_str, #construct) })
|
.and_then(|cb| unsafe { cb.construct(#js_name_str, #construct) })
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
unsafe { JsError::from(e).throw_into(env) };
|
unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) };
|
||||||
std::ptr::null_mut::<sys::napi_value__>()
|
std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_value__>()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,13 +194,15 @@ impl NapiStruct {
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl ToNapiValue for #name {
|
impl napi::bindgen_prelude::ToNapiValue for #name {
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, val: #name) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(
|
||||||
if let Some(ctor_ref) = get_class_constructor(#js_name_str) {
|
env: napi::bindgen_prelude::sys::napi_env, val: #name
|
||||||
|
) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
|
||||||
|
if let Some(ctor_ref) = napi::bindgen_prelude::get_class_constructor(#js_name_str) {
|
||||||
let mut ctor = std::ptr::null_mut();
|
let mut ctor = std::ptr::null_mut();
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_get_reference_value(env, ctor_ref, &mut ctor),
|
napi::bindgen_prelude::sys::napi_get_reference_value(env, ctor_ref, &mut ctor),
|
||||||
"Failed to get constructor of class `{}`",
|
"Failed to get constructor of class `{}`",
|
||||||
#js_name_str
|
#js_name_str
|
||||||
)?;
|
)?;
|
||||||
|
@ -203,15 +211,17 @@ impl NapiStruct {
|
||||||
let #destructed_fields = val;
|
let #destructed_fields = val;
|
||||||
let args = vec![#(#field_conversions),*];
|
let args = vec![#(#field_conversions),*];
|
||||||
|
|
||||||
check_status!(
|
napi::bindgen_prelude::check_status!(
|
||||||
sys::napi_new_instance(env, ctor, args.len(), args.as_ptr(), &mut result),
|
napi::bindgen_prelude::sys::napi_new_instance(env, ctor, args.len(), args.as_ptr(), &mut result),
|
||||||
"Failed to construct class `{}`",
|
"Failed to construct class `{}`",
|
||||||
#js_name_str
|
#js_name_str
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::new(Status::InvalidArg, format!("Failed to get constructor of class `{}`", #js_name_str)))
|
Err(napi::bindgen_prelude::Error::new(
|
||||||
|
napi::bindgen_prelude::Status::InvalidArg, format!("Failed to get constructor of class `{}`", #js_name_str))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,7 +265,7 @@ impl NapiStruct {
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl TypeName for #name {
|
impl napi::bindgen_prelude::TypeName for #name {
|
||||||
fn type_name() -> &'static str {
|
fn type_name() -> &'static str {
|
||||||
#name_str
|
#name_str
|
||||||
}
|
}
|
||||||
|
@ -265,22 +275,25 @@ impl NapiStruct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToNapiValue for #name {
|
impl napi::bindgen_prelude::ToNapiValue for #name {
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, val: #name) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(env: napi::bindgen_prelude::sys::napi_env, val: #name) -> napi::bindgen_prelude::Result<napi::bindgen_prelude::sys::napi_value> {
|
||||||
let env_wrapper = Env::from(env);
|
let env_wrapper = napi::bindgen_prelude::Env::from(env);
|
||||||
let mut obj = env_wrapper.create_object()?;
|
let mut obj = env_wrapper.create_object()?;
|
||||||
|
|
||||||
let #destructed_fields = val;
|
let #destructed_fields = val;
|
||||||
#(#obj_field_setters)*
|
#(#obj_field_setters)*
|
||||||
|
|
||||||
Object::to_napi_value(env, obj)
|
napi::bindgen_prelude::Object::to_napi_value(env, obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromNapiValue for #name {
|
impl napi::bindgen_prelude::FromNapiValue for #name {
|
||||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
unsafe fn from_napi_value(
|
||||||
let env_wrapper = Env::from(env);
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
let mut obj = Object::from_napi_value(env, napi_val)?;
|
napi_val: napi::bindgen_prelude::sys::napi_value
|
||||||
|
) -> napi::bindgen_prelude::Result<Self> {
|
||||||
|
let env_wrapper = napi::bindgen_prelude::Env::from(env);
|
||||||
|
let mut obj = napi::bindgen_prelude::Object::from_napi_value(env, napi_val)?;
|
||||||
|
|
||||||
#(#obj_field_getters)*
|
#(#obj_field_getters)*
|
||||||
|
|
||||||
|
@ -312,18 +325,18 @@ impl NapiStruct {
|
||||||
field.js_name.clone(),
|
field.js_name.clone(),
|
||||||
quote! {
|
quote! {
|
||||||
extern "C" fn #getter_name(
|
extern "C" fn #getter_name(
|
||||||
env: sys::napi_env,
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
cb: sys::napi_callback_info
|
cb: napi::bindgen_prelude::sys::napi_callback_info
|
||||||
) -> sys::napi_value {
|
) -> napi::bindgen_prelude::sys::napi_value {
|
||||||
CallbackInfo::<0>::new(env, cb, Some(0))
|
napi::bindgen_prelude::CallbackInfo::<0>::new(env, cb, Some(0))
|
||||||
.and_then(|mut cb| unsafe { cb.unwrap_borrow::<#struct_name>() })
|
.and_then(|mut cb| unsafe { cb.unwrap_borrow::<#struct_name>() })
|
||||||
.and_then(|obj| {
|
.and_then(|obj| {
|
||||||
let val = obj.#field_ident.to_owned();
|
let val = obj.#field_ident.to_owned();
|
||||||
unsafe { <#ty as ToNapiValue>::to_napi_value(env, val) }
|
unsafe { <#ty as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, val) }
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
unsafe { JsError::from(e).throw_into(env) };
|
unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) };
|
||||||
std::ptr::null_mut::<sys::napi_value__>()
|
std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_value__>()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -335,23 +348,23 @@ impl NapiStruct {
|
||||||
field.js_name.clone(),
|
field.js_name.clone(),
|
||||||
quote! {
|
quote! {
|
||||||
extern "C" fn #setter_name(
|
extern "C" fn #setter_name(
|
||||||
env: sys::napi_env,
|
env: napi::bindgen_prelude::sys::napi_env,
|
||||||
cb: sys::napi_callback_info
|
cb: napi::bindgen_prelude::sys::napi_callback_info
|
||||||
) -> sys::napi_value {
|
) -> napi::bindgen_prelude::sys::napi_value {
|
||||||
CallbackInfo::<1>::new(env, cb, Some(1))
|
napi::bindgen_prelude::CallbackInfo::<1>::new(env, cb, Some(1))
|
||||||
.and_then(|mut cb_info| unsafe {
|
.and_then(|mut cb_info| unsafe {
|
||||||
cb_info.unwrap_borrow_mut::<#struct_name>()
|
cb_info.unwrap_borrow_mut::<#struct_name>()
|
||||||
.and_then(|obj| {
|
.and_then(|obj| {
|
||||||
<#ty as FromNapiValue>::from_napi_value(env, cb_info.get_arg(0))
|
<#ty as napi::bindgen_prelude::FromNapiValue>::from_napi_value(env, cb_info.get_arg(0))
|
||||||
.and_then(move |val| {
|
.and_then(move |val| {
|
||||||
obj.#field_ident = val;
|
obj.#field_ident = val;
|
||||||
<() as ToNapiValue>::to_napi_value(env, ())
|
<() as napi::bindgen_prelude::ToNapiValue>::to_napi_value(env, ())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
unsafe { JsError::from(e).throw_into(env) };
|
unsafe { napi::bindgen_prelude::JsError::from(e).throw_into(env) };
|
||||||
std::ptr::null_mut::<sys::napi_value__>()
|
std::ptr::null_mut::<napi::bindgen_prelude::sys::napi_value__>()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -369,7 +382,7 @@ impl NapiStruct {
|
||||||
let mut props = vec![];
|
let mut props = vec![];
|
||||||
|
|
||||||
if self.kind == NapiStructKind::Constructor {
|
if self.kind == NapiStructKind::Constructor {
|
||||||
props.push(quote! { Property::new("constructor").unwrap().with_ctor(constructor) });
|
props.push(quote! { napi::bindgen_prelude::Property::new("constructor").unwrap().with_ctor(constructor) });
|
||||||
}
|
}
|
||||||
|
|
||||||
for field in self.fields.iter() {
|
for field in self.fields.iter() {
|
||||||
|
@ -384,7 +397,7 @@ impl NapiStruct {
|
||||||
|
|
||||||
let js_name = &field.js_name;
|
let js_name = &field.js_name;
|
||||||
let mut prop = quote! {
|
let mut prop = quote! {
|
||||||
Property::new(#js_name)
|
napi::bindgen_prelude::Property::new(#js_name)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -404,9 +417,9 @@ impl NapiStruct {
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[allow(clippy::all)]
|
#[allow(clippy::all)]
|
||||||
#[ctor]
|
#[napi::bindgen_prelude::ctor]
|
||||||
fn #struct_register_name() {
|
fn #struct_register_name() {
|
||||||
register_class(#name_str, #js_name, vec![#(#props),*]);
|
napi::bindgen_prelude::register_class(#name_str, #js_name, vec![#(#props),*]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -442,7 +455,7 @@ impl NapiImpl {
|
||||||
|
|
||||||
let prop = props.entry(&item.js_name).or_insert_with(|| {
|
let prop = props.entry(&item.js_name).or_insert_with(|| {
|
||||||
quote! {
|
quote! {
|
||||||
Property::new(#js_name).unwrap()
|
napi::bindgen_prelude::Property::new(#js_name).unwrap()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -454,7 +467,7 @@ impl NapiImpl {
|
||||||
if item.fn_self.is_some() {
|
if item.fn_self.is_some() {
|
||||||
quote! { .with_method(#intermediate_name) }
|
quote! { .with_method(#intermediate_name) }
|
||||||
} else {
|
} else {
|
||||||
quote! { .with_method(#intermediate_name).with_property_attributes(PropertyAttributes::Static) }
|
quote! { .with_method(#intermediate_name).with_property_attributes(napi::bindgen_prelude::PropertyAttributes::Static) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -473,9 +486,9 @@ impl NapiImpl {
|
||||||
use super::*;
|
use super::*;
|
||||||
#(#methods)*
|
#(#methods)*
|
||||||
|
|
||||||
#[ctor]
|
#[napi::bindgen_prelude::ctor]
|
||||||
fn #register_name() {
|
fn #register_name() {
|
||||||
register_class(#name_str, #js_name, vec![#(#props),*]);
|
napi::bindgen_prelude::register_class(#name_str, #js_name, vec![#(#props),*]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use napi::bindgen_prelude::*;
|
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
fn get_words() -> Vec<&'static str> {
|
fn get_words() -> Vec<&'static str> {
|
||||||
vec!["foo", "bar"]
|
vec!["foo", "bar"]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use napi::bindgen_prelude::*;
|
|
||||||
use napi::Result;
|
use napi::Result;
|
||||||
|
|
||||||
use crate::r#enum::Kind;
|
use crate::r#enum::Kind;
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use napi::bindgen_prelude::*;
|
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub struct ClassWithFactory {
|
pub struct ClassWithFactory {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use napi::bindgen_prelude::*;
|
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
fn map_option(val: Option<u32>) -> Option<u32> {
|
fn map_option(val: Option<u32>) -> Option<u32> {
|
||||||
val.map(|v| v + 1)
|
val.map(|v| v + 1)
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use napi::bindgen_prelude::*;
|
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
fn add(a: u32, b: u32) -> u32 {
|
fn add(a: u32, b: u32) -> u32 {
|
||||||
a + b
|
a + b
|
||||||
|
|
Loading…
Reference in a new issue