remove wasm-init and just use inventory instead

This commit is contained in:
eugineerd 2025-02-21 19:25:58 +00:00
parent 42db5e02c6
commit 1bb9ee85de
4 changed files with 47 additions and 91 deletions

View File

@ -78,11 +78,7 @@ portable-atomic = [
"bevy_utils/portable-atomic",
]
# Enables automatic reflect registration
auto_register = [
"bevy_reflect_derive/auto_register",
"dep:inventory",
"dep:wasm-init",
]
auto_register = ["bevy_reflect_derive/auto_register", "dep:inventory"]
[dependencies]
# bevy
@ -124,15 +120,12 @@ uuid = { version = "1.13.1", default-features = false, optional = true, features
variadics_please = "1.1"
wgpu-types = { version = "24", features = ["serde"], optional = true }
# deps for automatic type registration
inventory = { version = "0.3", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
uuid = { version = "1.13.1", default-features = false, features = ["js"] }
# deps for automatic type registration
[target.'cfg(not(target_family = "wasm"))'.dependencies]
inventory = { version = "0.3", optional = true }
[target.'cfg(target_family = "wasm")'.dependencies]
wasm-init = { version = "0.2", optional = true }
[dev-dependencies]
ron = "0.8.0"

View File

@ -171,9 +171,9 @@ pub fn reflect_auto_registration(meta: &ReflectMeta) -> Option<proc_macro2::Toke
};
Some(quote! {
#bevy_reflect_path::__macro_exports::auto_register::auto_register_function!{
#bevy_reflect_path::__macro_exports::auto_register::AutomaticReflectRegistrations::add(
<#type_path as #bevy_reflect_path::__macro_exports::auto_register::RegisterForReflection>::__register
#bevy_reflect_path::__macro_exports::auto_register::inventory::submit!{
#bevy_reflect_path::__macro_exports::auto_register::AutomaticReflectRegistrations(
<#type_path as #bevy_reflect_path::__macro_exports::RegisterForReflection>::__register
)
}
})

View File

@ -718,92 +718,50 @@ pub mod __macro_exports {
/// Automatic reflect registration implementation
#[cfg(feature = "auto_register")]
pub mod auto_register {
pub use super::*;
use super::*;
/// Stores registration functions of all reflect types that can be automatically registered.
///
/// Intended to be used as follows:
/// ```rs
/// // Adding a type
/// auto_register_function!{
/// AutomaticReflectRegistrations::add(<type_registration_function>)
/// }
///
/// // Registering collected types
/// let mut registry = TypeRegistry::default();
/// AutomaticReflectRegistrations::register(&mut registry);
/// ```
pub struct AutomaticReflectRegistrations;
#[cfg(not(target_family = "wasm"))]
mod __automatic_type_registration_impl {
use super::*;
pub use inventory::submit as auto_register_function;
pub struct AutomaticReflectRegistrationsImpl(fn(&mut TypeRegistry));
impl AutomaticReflectRegistrations {
// Must be const to allow usage in static context
pub const fn add(func: fn(&mut TypeRegistry)) -> AutomaticReflectRegistrationsImpl {
AutomaticReflectRegistrationsImpl(func)
}
pub fn register(registry: &mut TypeRegistry) {
for registration_fn in inventory::iter::<AutomaticReflectRegistrationsImpl> {
registration_fn.0(registry);
}
}
}
inventory::collect!(AutomaticReflectRegistrationsImpl);
}
pub use inventory;
#[cfg(target_family = "wasm")]
mod __automatic_type_registration_impl {
use super::*;
use alloc::vec::Vec;
mod wasm_support {
use bevy_platform_support::sync::atomic::{AtomicBool, Ordering};
#[cfg(feature = "std")]
use std::sync::RwLock;
static INIT_DONE: AtomicBool = AtomicBool::new(false);
#[cfg(not(feature = "std"))]
use spin::rwlock::RwLock;
#[expect(unsafe_code, reason = "This function is generated by linker.")]
unsafe extern "C" {
fn __wasm_call_ctors();
}
pub use wasm_init::wasm_init as auto_register_function;
static AUTOMATIC_REFLECT_REGISTRATIONS: RwLock<Vec<fn(&mut TypeRegistry)>> =
RwLock::new(Vec::new());
impl AutomaticReflectRegistrations {
pub fn add(func: fn(&mut TypeRegistry)) {
#[cfg(feature = "std")]
let mut registrations = AUTOMATIC_REFLECT_REGISTRATIONS
.write()
.expect("Failed to get write lock for automatic reflect type registration");
#[cfg(not(feature = "std"))]
let mut registrations = AUTOMATIC_REFLECT_REGISTRATIONS.write();
registrations.push(func);
}
pub fn register(registry: &mut TypeRegistry) {
// wasm_init must be called at least once to run all init code.
// Calling it multiple times is ok and doesn't do anything.
wasm_init::wasm_init();
#[cfg(feature = "std")]
let registrations = AUTOMATIC_REFLECT_REGISTRATIONS
.read()
.expect("Failed to get read lock for automatic reflect type registration");
#[cfg(not(feature = "std"))]
let registrations = AUTOMATIC_REFLECT_REGISTRATIONS.read();
for registration_fn in registrations.iter() {
registration_fn(registry);
}
/// This function must be called before using [`inventory::iter`] on [`AutomaticReflectRegistrations`] to run constructors on all platforms.
pub fn init() {
if INIT_DONE.swap(true, Ordering::Relaxed) {
return;
};
// SAFETY:
// This will call constructors on wasm platforms at most once (as long as `init` is the only function that calls `__wasm_call_ctors`).
//
// For more information see: https://docs.rs/inventory/latest/inventory/#webassembly-and-constructors
#[expect(
unsafe_code,
reason = "This function must be called to use inventory on wasm."
)]
unsafe {
__wasm_call_ctors();
}
}
}
pub use __automatic_type_registration_impl::*;
/// This function must be called before using [`inventory::iter`] on [`AutomaticReflectRegistrations`] to run constructors on all platforms.
pub fn init() {
#[cfg(target_family = "wasm")]
wasm_support::init();
}
/// Stores registration function to be used during automatic reflect registration.
pub struct AutomaticReflectRegistrations(pub fn(&mut TypeRegistry));
inventory::collect!(AutomaticReflectRegistrations);
}
}

View File

@ -154,7 +154,12 @@ impl TypeRegistry {
/// ```
#[cfg(feature = "auto_register")]
pub fn register_derived_types(&mut self) {
crate::__macro_exports::auto_register::AutomaticReflectRegistrations::register(self);
crate::__macro_exports::auto_register::init();
for registration in inventory::iter::<
crate::__macro_exports::auto_register::AutomaticReflectRegistrations,
>() {
registration.0(self);
}
}
/// Attempts to register the type `T` if it has not yet been registered already.