Skip to content

Commit a147891

Browse files
authored
fix: fix global type cache not containing generic types (#388)
# Summary `types["GenericType<String>"]` should work now, this was using `ident` instead of `short_path` by mistake
1 parent e806daa commit a147891

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

assets/tests/globals/type_cache_available.lua

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ function on_test()
33
local my_type = types.TestResource;
44
assert(my_type ~= nil, "Type TestResource is not available in type cache");
55
assert(my_type:short_name() == "TestResource", "Type t.TestResource:short_name() is not correct: " .. my_type:short_name());
6+
7+
local my_generic_type = types["GenericComponent<String>"];
8+
assert(my_generic_type ~= nil, "Type GenericComponent<String> is not available in type cache");
9+
assert(my_generic_type:short_name() == "GenericComponent<String>", "Type t.GenericComponent<String>:short_name() is not correct: " .. my_generic_type:short_name());
610
end

assets/tests/globals/type_cache_available.rhai

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ fn on_test() {
22
let my_type = types.TestResource;
33
assert(type_of(my_type) != "()", "Type TestResource is not available in type cache");
44
assert(my_type.short_name.call() == "TestResource", "Type t.TestResource:short_name() is not correct: " + my_type.short_name.call());
5+
6+
let my_type = types["GenericComponent<String>"];
7+
assert(type_of(my_type) != "()", "Type GenericComponent<String> is not available in type cache");
8+
assert(my_type.short_name.call() == "GenericComponent<String>", "Type t.GenericComponent<String>:short_name() is not correct: " + my_type.short_name.call());
59
}

crates/bevy_mod_scripting_core/src/bindings/globals/core.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ impl CoreGlobals {
8787
let type_registry = type_registry.read();
8888
let mut type_cache = HashMap::<String, _>::default();
8989
for registration in type_registry.iter() {
90-
if let Some(ident) = registration.type_info().type_path_table().ident() {
91-
let registration = ScriptTypeRegistration::new(Arc::new(registration.clone()));
92-
let registration = guard.clone().get_type_registration(registration)?;
93-
let registration =
94-
registration.map_both(Val::from, |u| u.map_both(Val::from, Val::from));
95-
type_cache.insert(ident.to_string(), registration);
96-
}
90+
let type_path = registration.type_info().type_path_table().short_path();
91+
let registration = ScriptTypeRegistration::new(Arc::new(registration.clone()));
92+
let registration = guard.clone().get_type_registration(registration)?;
93+
let registration =
94+
registration.map_both(Val::from, |u| u.map_both(Val::from, Val::from));
95+
type_cache.insert(type_path.to_owned(), registration);
9796
}
9897

9998
Ok(type_cache)

crates/testing_crates/script_integration_test_harness/src/lib.rs

-27
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,6 @@ fn dummy_startup_system<T>() {}
3838
fn dummy_before_post_update_system() {}
3939
fn dummy_post_update_system() {}
4040

41-
pub trait Benchmarker: 'static + Send + Sync {
42-
fn bench(
43-
&self,
44-
label: &str,
45-
f: &dyn Fn() -> Result<ScriptValue, ScriptError>,
46-
) -> Result<ScriptValue, ScriptError>;
47-
48-
fn clone_box(&self) -> Box<dyn Benchmarker>;
49-
}
50-
51-
#[derive(Clone)]
52-
pub struct NoOpBenchmarker;
53-
54-
impl Benchmarker for NoOpBenchmarker {
55-
fn bench(
56-
&self,
57-
_label: &str,
58-
f: &dyn Fn() -> Result<ScriptValue, ScriptError>,
59-
) -> Result<ScriptValue, ScriptError> {
60-
f()
61-
}
62-
63-
fn clone_box(&self) -> Box<dyn Benchmarker> {
64-
Box::new(self.clone())
65-
}
66-
}
67-
6841
#[derive(Event)]
6942
struct TestEventFinished;
7043

crates/testing_crates/test_utils/src/test_data.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ impl TestComponent {
2323
}
2424
}
2525

26+
#[derive(Component, Reflect, PartialEq, Eq, Debug, Default)]
27+
#[reflect(Component)]
28+
pub struct GenericComponent<T: Default> {
29+
pub value: T,
30+
}
31+
32+
impl GenericComponent<String> {
33+
pub fn init() -> Self {
34+
Self {
35+
value: "Initial Value".to_string(),
36+
}
37+
}
38+
}
39+
2640
/// Test Resource with Reflect and ReflectResource registered
2741
#[derive(Resource, Reflect, Default, PartialEq, Eq, Debug)]
2842
#[reflect(Resource)]
@@ -278,11 +292,12 @@ impl_test_component_ids!(
278292
SimpleStruct => 6,
279293
SimpleTupleStruct => 7,
280294
SimpleEnum => 8,
295+
GenericComponent<String> => 9,
281296
],
282297
[
283-
TestResource => 9,
284-
ResourceWithDefault => 10,
285-
TestResourceWithVariousFields => 11,
298+
TestResource => 10,
299+
ResourceWithDefault => 11,
300+
TestResourceWithVariousFields => 12,
286301
]
287302
);
288303

0 commit comments

Comments
 (0)