Skip to content

Commit 6a95cf5

Browse files
committed
remove logs and add test
1 parent 1808547 commit 6a95cf5

File tree

2 files changed

+22
-57
lines changed

2 files changed

+22
-57
lines changed

crates/bevy_mod_scripting_core/src/bindings/query.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ impl ScriptComponentRegistration {
178178
)
179179
})?;
180180

181-
bevy::log::debug!("found component data");
182-
183181
// TODO: this shouldn't need entire world access it feels
184182
let type_registry = world.type_registry();
185183
world.with_global_access(|world| {
@@ -188,7 +186,6 @@ impl ScriptComponentRegistration {
188186
.map_err(|_| InteropError::missing_entity(entity))?;
189187
{
190188
let registry = type_registry.read();
191-
bevy::log::debug!("inserting component instance using component data");
192189
component_data.insert(&mut entity, instance.as_partial_reflect(), &registry);
193190
}
194191
Ok(())

crates/bevy_mod_scripting_core/src/bindings/script_component.rs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ impl WorldAccessGuard<'_> {
9191
}
9292

9393
let component_id = self.with_global_access(|w| {
94-
bevy::log::info!(
95-
"components present: {}. script: {}. World id: {:?}",
96-
w.components().len(),
97-
component_registry_read.components.len(),
98-
w.id()
99-
);
10094
let descriptor = unsafe {
10195
// Safety: same safety guarantees as ComponentDescriptor::new
10296
// we know the type in advance
@@ -108,9 +102,7 @@ impl WorldAccessGuard<'_> {
108102
needs_drop::<ScriptComponent>().then_some(|x| x.drop_as::<ScriptComponent>()),
109103
)
110104
};
111-
let o = w.register_component_with_descriptor(descriptor);
112-
bevy::log::info!("components present after: {}", w.components().len());
113-
o
105+
w.register_component_with_descriptor(descriptor)
114106
})?;
115107
drop(component_registry_read);
116108
let mut component_registry = component_registry.write();
@@ -122,12 +114,6 @@ impl WorldAccessGuard<'_> {
122114
component_id,
123115
);
124116

125-
bevy::log::debug!(
126-
"Registering dynamic script component: {}, component id assigned: {:?}",
127-
component_name,
128-
component_id
129-
);
130-
131117
let component_info = ScriptComponentInfo {
132118
name: component_name.clone(),
133119
registration: registration.clone(),
@@ -153,51 +139,33 @@ impl Plugin for DynamicScriptComponentPlugin {
153139

154140
#[cfg(test)]
155141
mod test {
156-
use std::ptr::NonNull;
157-
158142
use super::*;
159-
use bevy::{ecs::world::World, ptr::OwningPtr};
143+
use bevy::ecs::world::World;
160144

161145
#[test]
162146
fn test_script_component() {
163147
let mut world = World::new();
164-
let component_name = "MyScriptComponent";
165-
166-
#[derive(Reflect, Component)]
167-
struct UnderlyingComponent;
168-
169-
// initialize component descriptor dynamically
170-
let descriptor = unsafe {
171-
// Safety: same safety guarantees as ComponentDescriptor::new
172-
// we know the type in advance
173-
// we only use this method to name the component
174-
ComponentDescriptor::new_with_layout(
175-
component_name,
176-
UnderlyingComponent::STORAGE_TYPE,
177-
Layout::new::<UnderlyingComponent>(),
178-
needs_drop::<UnderlyingComponent>()
179-
.then_some(|x| x.drop_as::<UnderlyingComponent>()),
180-
)
148+
let registration = {
149+
let guard = WorldAccessGuard::new_exclusive(&mut world);
150+
151+
guard
152+
.register_script_component("ScriptTest".to_string())
153+
.unwrap()
181154
};
182155

183-
// register with the world
184-
let component_id = world.register_component_with_descriptor(descriptor.clone());
185-
let component_id_2 = world.register_component_with_descriptor(descriptor);
186-
assert_eq!(component_id, component_id_2); // iam getting a double free for this in scritps somehow
187-
188-
// insert into the entity
189-
let entity = world.spawn_empty().id();
190-
let mut entity = world.entity_mut(entity);
191-
192-
let value = Box::new(UnderlyingComponent);
193-
let value_ref = Box::into_raw(value).cast::<u8>();
194-
let ptr = unsafe { OwningPtr::new(NonNull::new(value_ref).unwrap()) };
195-
unsafe { entity.insert_by_id(component_id, ptr) };
196-
197-
// check it gets inserted
198-
assert!(
199-
entity.contains_id(component_id),
200-
"entity does not contain freshly inserted component"
201-
)
156+
let registry = world.get_resource::<AppScriptComponentRegistry>().unwrap();
157+
158+
let registry = registry.read();
159+
let info = registry.get("ScriptTest").unwrap();
160+
assert_eq!(info.registration.component_id, registration.component_id);
161+
assert_eq!(info.name, "ScriptTest");
162+
163+
// can get the component through the world
164+
let component = world
165+
.components()
166+
.get_info(info.registration.component_id)
167+
.unwrap();
168+
169+
assert_eq!(component.name(), "ScriptTest");
202170
}
203171
}

0 commit comments

Comments
 (0)