Skip to content

Commit

Permalink
Clean-up module structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Mar 13, 2023
1 parent be8ae30 commit fa74703
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/neon/src/sys/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ unsafe fn get_version(host: &libloading::Library, env: Env) -> Result<u32, liblo
Ok(version)
}

pub(super) unsafe fn load(env: Env) -> Result<(), libloading::Error> {
pub(crate) unsafe fn load(env: Env) -> Result<(), libloading::Error> {
#[cfg(not(windows))]
let host = libloading::os::unix::Library::this().into();
#[cfg(windows)]
Expand Down
14 changes: 1 addition & 13 deletions crates/neon/src/sys/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! # FFI bindings to N-API symbols
//! # FFI bindings to Node-API symbols
//!
//! These types are manually copied from bindings generated from `bindgen`. To
//! update, use the following approach:
Expand Down Expand Up @@ -175,19 +175,7 @@ macro_rules! generate {
};
}

use std::sync::Once;

pub use self::{functions::*, types::*};

mod functions;
mod types;

static SETUP: Once = Once::new();

/// Loads N-API symbols from host process.
/// Must be called at least once before using any functions in bindings or
/// they will panic.
/// Safety: `env` must be a valid `napi_env` for the current thread
pub unsafe fn setup(env: Env) {
SETUP.call_once(|| load(env).expect("Failed to load N-API symbols"));
}
26 changes: 26 additions & 0 deletions crates/neon/src/sys/bindings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Env__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Env = *mut Env__;

#[repr(C)]
Expand All @@ -16,6 +17,7 @@ pub struct Value__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Value = *mut Value__;

#[repr(C)]
Expand All @@ -25,6 +27,7 @@ pub struct CallbackInfo__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type CallbackInfo = *mut CallbackInfo__;

#[repr(C)]
Expand All @@ -34,6 +37,7 @@ pub struct EscapableHandleScope__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type EscapableHandleScope = *mut EscapableHandleScope__;

#[repr(C)]
Expand All @@ -43,6 +47,7 @@ pub struct HandleScope__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type HandleScope = *mut HandleScope__;

#[repr(C)]
Expand All @@ -52,6 +57,7 @@ pub struct Ref__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Ref = *mut Ref__;

#[cfg(feature = "napi-4")]
Expand All @@ -66,8 +72,10 @@ pub struct ThreadsafeFunction__ {
#[cfg(feature = "napi-4")]
pub type ThreadsafeFunction = *mut ThreadsafeFunction__;

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Callback = Option<unsafe extern "C" fn(env: Env, info: CallbackInfo) -> Value>;

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Finalize =
Option<unsafe extern "C" fn(env: Env, finalize_data: *mut c_void, finalize_hint: *mut c_void)>;

Expand All @@ -80,6 +88,7 @@ pub type ThreadsafeFunctionCallJs = Option<
#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub enum Status {
Ok = 0,
InvalidArg = 1,
Expand Down Expand Up @@ -108,6 +117,7 @@ pub enum Status {
#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-6")))]
pub enum ValueType {
Undefined = 0,
Null = 1,
Expand All @@ -124,6 +134,7 @@ pub enum ValueType {
#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub enum TypedArrayType {
I8 = 0,
U8 = 1,
Expand All @@ -141,6 +152,8 @@ pub enum TypedArrayType {
#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-6")))]
#[cfg(feature = "napi-6")]
pub enum KeyCollectionMode {
IncludePrototypes = 0,
OwnOnly = 1,
Expand All @@ -149,6 +162,8 @@ pub enum KeyCollectionMode {
#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-6")))]
#[cfg(feature = "napi-6")]
pub enum KeyConversion {
KeepNumbers = 0,
NumbersToStrings = 1,
Expand Down Expand Up @@ -176,9 +191,12 @@ pub enum ThreadsafeFunctionReleaseMode {

#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(docsrs, doc(cfg(feature = "napi-6")))]
#[cfg(feature = "napi-6")]
pub struct KeyFilter(pub ::std::os::raw::c_uint);

#[allow(dead_code)]
#[cfg(feature = "napi-6")]
impl KeyFilter {
pub const ALL_PROPERTIES: KeyFilter = KeyFilter(0);
pub const WRITABLE: KeyFilter = KeyFilter(1);
Expand All @@ -187,6 +205,7 @@ impl KeyFilter {
pub const SKIP_SYMBOLS: KeyFilter = KeyFilter(16);
}

#[cfg(feature = "napi-6")]
impl std::ops::BitOr<KeyFilter> for KeyFilter {
type Output = Self;
#[inline]
Expand All @@ -195,13 +214,15 @@ impl std::ops::BitOr<KeyFilter> for KeyFilter {
}
}

#[cfg(feature = "napi-6")]
impl std::ops::BitOrAssign for KeyFilter {
#[inline]
fn bitor_assign(&mut self, rhs: KeyFilter) {
self.0 |= rhs.0;
}
}

#[cfg(feature = "napi-6")]
impl std::ops::BitAnd<KeyFilter> for KeyFilter {
type Output = Self;
#[inline]
Expand All @@ -210,6 +231,7 @@ impl std::ops::BitAnd<KeyFilter> for KeyFilter {
}
}

#[cfg(feature = "napi-6")]
impl std::ops::BitAndAssign for KeyFilter {
#[inline]
fn bitand_assign(&mut self, rhs: KeyFilter) {
Expand All @@ -224,10 +246,13 @@ pub struct AsyncWork__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type AsyncWork = *mut AsyncWork__;

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type AsyncExecuteCallback = Option<unsafe extern "C" fn(env: Env, data: *mut c_void)>;

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type AsyncCompleteCallback =
Option<unsafe extern "C" fn(env: Env, status: Status, data: *mut c_void)>;

Expand All @@ -238,6 +263,7 @@ pub struct Deferred__ {
_unused: [u8; 0],
}

#[cfg_attr(docsrs, doc(cfg(feature = "napi-1")))]
pub type Deferred = *mut Deferred__;

#[cfg_attr(docsrs, doc(cfg(feature = "napi-8")))]
Expand Down
20 changes: 17 additions & 3 deletions crates/neon/src/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::mem::MaybeUninit;
use std::{mem::MaybeUninit, sync::Once};

pub use bindings::*;
// XXX: Minimize changes due to structure changes
pub(crate) use bindings::*;

pub(crate) mod array;
pub(crate) mod arraybuffer;
Expand All @@ -23,7 +24,7 @@ pub(crate) mod string;
pub(crate) mod tag;
pub(crate) mod typedarray;

mod bindings;
pub mod bindings;

#[cfg(feature = "napi-4")]
pub(crate) mod tsfn;
Expand Down Expand Up @@ -55,3 +56,16 @@ unsafe fn string(env: Env, s: impl AsRef<str>) -> raw::Local {

result.assume_init()
}

static SETUP: Once = Once::new();

/// Loads Node-API symbols from host process.
///
/// Must be called at least once before using any functions in bindings or
/// they will panic.
///
/// # Safety
/// `env` must be a valid `napi_env` for the current thread
pub unsafe fn setup(env: Env) {
SETUP.call_once(|| load(env).expect("Failed to load N-API symbols"));
}

0 comments on commit fa74703

Please sign in to comment.