Skip to content

Commit e05e626

Browse files
committed
feat! add type cache table global, and refactor GetTypeDependencies to work better with nested types
WIP feat: shorten import paths (#367) where we previously did `pub use my_mod` we also use `pub use my_mod::*`, this should make imports everywhere prettier and should not be a breaking change merge re-design the type dependency trait better refactor
1 parent 7ca94df commit e05e626

File tree

25 files changed

+542
-200
lines changed

25 files changed

+542
-200
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
function on_test()
3+
local my_type = types.TestResource;
4+
assert(my_type ~= nil, "Type TestResource is not available in type cache");
5+
assert(my_type:short_name() == "TestResource", "Type t.TestResource:short_name() is not correct: " .. my_type:short_name());
6+
end

crates/bevy_api_gen/src/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ impl ReflectType<'_> {
8282
}
8383

8484
pub(crate) const DEF_PATHS_BMS_FROM_SCRIPT: [&str; 2] = [
85-
"bevy_mod_scripting_core::bindings::function::from::FromScript",
86-
"bindings::function::from::FromScript",
85+
"bevy_mod_scripting_core::bindings::FromScript",
86+
"bindings::FromScript",
8787
];
8888
pub(crate) const DEF_PATHS_BMS_INTO_SCRIPT: [&str; 2] = [
89-
"bevy_mod_scripting_core::bindings::function::into::IntoScript",
90-
"bindings::function::into::IntoScript",
89+
"bevy_mod_scripting_core::bindings::IntoScript",
90+
"bindings::IntoScript",
9191
];
9292

9393
pub(crate) const DEF_PATHS_REFLECT: [&str; 2] =

crates/bevy_api_gen/src/passes/cache_traits.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
use log::trace;
22
use rustc_hir::def_id::LOCAL_CRATE;
3+
use rustc_middle::ty::TyCtxt;
34
use rustc_span::Symbol;
45

56
use crate::{
67
Args, BevyCtxt, DEF_PATHS_BMS_FROM_SCRIPT, DEF_PATHS_BMS_INTO_SCRIPT,
78
DEF_PATHS_GET_TYPE_REGISTRATION, DEF_PATHS_REFLECT, STD_SOURCE_TRAITS,
89
};
910

11+
fn dump_traits(tcx: &TyCtxt) -> String{
12+
let mut buffer = String::new();
13+
for t in tcx.all_traits() {
14+
buffer.push_str(&tcx.def_path_str(t));
15+
buffer.push_str(", ");
16+
}
17+
buffer
18+
}
19+
1020
/// Finds and caches relevant traits, if they cannot be found throws an ICE
1121
pub(crate) fn cache_traits(ctxt: &mut BevyCtxt<'_>, _args: &Args) -> bool {
1222
let tcx = &ctxt.tcx;
@@ -35,15 +45,17 @@ pub(crate) fn cache_traits(ctxt: &mut BevyCtxt<'_>, _args: &Args) -> bool {
3545

3646
if !ctxt.cached_traits.has_all_bms_traits() {
3747
panic!(
38-
"Could not find all bms traits in crate: {}",
39-
tcx.crate_name(LOCAL_CRATE)
48+
"Could not find all bms traits in crate: {}. Available traits: {}",
49+
tcx.crate_name(LOCAL_CRATE),
50+
dump_traits(tcx)
4051
)
4152
}
4253

4354
if !ctxt.cached_traits.has_all_bevy_traits() {
4455
panic!(
45-
"Could not find all reflect traits in crate: {}, did bootstrapping go wrong?",
46-
tcx.crate_name(LOCAL_CRATE)
56+
"Could not find all reflect traits in crate: {}, did bootstrapping go wrong?. Available traits: {}",
57+
tcx.crate_name(LOCAL_CRATE),
58+
dump_traits(tcx)
4759
)
4860
}
4961

crates/bevy_mod_scripting_core/src/bindings/access_map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ impl DisplayCodeLocation for Option<std::panic::Location<'_>> {
713713
}
714714
}
715715

716-
#[macro_export]
717716
/// A macro for claiming access to a value for reading
718717
macro_rules! with_access_read {
719718
($access_map:expr, $id:expr, $msg:expr, $body:block) => {{
@@ -731,7 +730,7 @@ macro_rules! with_access_read {
731730
}};
732731
}
733732

734-
#[macro_export]
733+
pub(crate) use with_access_read;
735734
/// A macro for claiming access to a value for writing
736735
macro_rules! with_access_write {
737736
($access_map:expr, $id:expr, $msg:expr, $body:block) => {
@@ -748,8 +747,8 @@ macro_rules! with_access_write {
748747
}
749748
};
750749
}
750+
pub(crate) use with_access_write;
751751

752-
#[macro_export]
753752
/// A macro for claiming global access
754753
macro_rules! with_global_access {
755754
($access_map:expr, $msg:expr, $body:block) => {
@@ -771,6 +770,8 @@ macro_rules! with_global_access {
771770
};
772771
}
773772

773+
pub(crate) use with_global_access;
774+
774775
#[cfg(test)]
775776
mod test {
776777
use super::*;

crates/bevy_mod_scripting_core/src/bindings/function/arg_meta.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{ffi::OsString, path::PathBuf};
44

55
use crate::{
66
bindings::{script_value::ScriptValue, ReflectReference},
7-
docgen::typed_through::TypedThrough,
7+
docgen::typed_through::TypedThrough, error::InteropError,
88
};
99

1010
use super::{
@@ -77,6 +77,8 @@ impl<T> ArgMeta for Val<T> {}
7777
impl<T> ArgMeta for Ref<'_, T> {}
7878
impl<T> ArgMeta for Mut<'_, T> {}
7979

80+
impl<T> ArgMeta for Result<T, InteropError> {}
81+
8082
impl<T> ArgMeta for Option<T> {
8183
fn default_value() -> Option<ScriptValue> {
8284
Some(ScriptValue::Unit)

crates/bevy_mod_scripting_core/src/bindings/function/from.rs

+19
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,17 @@ where
469469
pub struct Union<T1, T2>(Result<T1, T2>);
470470

471471
impl<T1, T2> Union<T1, T2> {
472+
/// Create a new union with the left value.
473+
pub fn new_left(value: T1) -> Self {
474+
Union(Ok(value))
475+
}
476+
477+
/// Create a new union with the right value.
478+
pub fn new_right(value: T2) -> Self {
479+
Union(Err(value))
480+
}
481+
482+
472483
/// Try interpret the union as the left type
473484
pub fn into_left(self) -> Result<T1, T2> {
474485
match self.0 {
@@ -484,6 +495,14 @@ impl<T1, T2> Union<T1, T2> {
484495
Ok(l) => Err(l),
485496
}
486497
}
498+
499+
/// Map the union to another type
500+
pub fn map_both<U1, U2, F: Fn(T1) -> U1, G: Fn(T2) -> U2>(self, f: F, g: G) -> Union<U1, U2> {
501+
match self.0 {
502+
Ok(t) => Union(Ok(f(t))),
503+
Err(t) => Union(Err(g(t))),
504+
}
505+
}
487506
}
488507

489508
impl<T1: FromScript, T2: FromScript> FromScript for Union<T1, T2>

crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
//! Contains the [`FromScriptRef`] trait and its implementations.
22
33
use std::{any::TypeId, ffi::OsString, path::PathBuf};
4-
54
use bevy::reflect::{
65
DynamicEnum, DynamicList, DynamicMap, DynamicTuple, DynamicVariant, Map, PartialReflect,
76
};
8-
97
use crate::{
10-
bindings::{function::from::FromScript, WorldGuard},
8+
bindings::{match_by_type, WorldGuard, FromScript},
119
error::InteropError,
12-
match_by_type,
1310
reflection_extensions::TypeInfoExtensions,
1411
ScriptValue,
1512
};

crates/bevy_mod_scripting_core/src/bindings/function/into.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
//! Implementations of the [`IntoScript`] trait for various types.
22
33
use std::{borrow::Cow, collections::HashMap, ffi::OsString, path::PathBuf};
4-
54
use bevy::reflect::Reflect;
65

76
use crate::{
87
bindings::{script_value::ScriptValue, ReflectReference, WorldGuard},
98
error::InteropError,
10-
self_type_dependency_only,
119
};
12-
1310
use super::{
14-
from::Val,
11+
from::{Union, Val},
1512
script_function::{DynamicScriptFunction, DynamicScriptFunctionMut},
1613
};
1714

@@ -35,14 +32,13 @@ impl IntoScript for ScriptValue {
3532
}
3633
}
3734

38-
self_type_dependency_only!(ScriptValue);
3935

4036
impl IntoScript for () {
4137
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
4238
Ok(ScriptValue::Unit)
4339
}
4440
}
45-
self_type_dependency_only!(());
41+
4642

4743
impl IntoScript for DynamicScriptFunctionMut {
4844
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
@@ -56,14 +52,12 @@ impl IntoScript for DynamicScriptFunction {
5652
}
5753
}
5854

59-
self_type_dependency_only!(DynamicScriptFunctionMut, DynamicScriptFunction);
6055

6156
impl IntoScript for bool {
6257
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
6358
Ok(ScriptValue::Bool(self))
6459
}
6560
}
66-
self_type_dependency_only!(bool);
6761

6862
macro_rules! impl_into_with_downcast {
6963
($variant:tt as $cast:ty [$($ty:ty),*]) => {
@@ -80,9 +74,7 @@ macro_rules! impl_into_with_downcast {
8074

8175
impl_into_with_downcast!(Integer as i64 [i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize]);
8276
impl_into_with_downcast!(Float as f64 [f32, f64]);
83-
self_type_dependency_only!(
84-
i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize, f32, f64
85-
);
77+
8678

8779
macro_rules! impl_into_stringlike {
8880
($id:ident,[ $(($ty:ty => $conversion:expr)),*]) => {
@@ -108,15 +100,14 @@ impl_into_stringlike!(
108100
]
109101
);
110102

111-
self_type_dependency_only!(String, char, PathBuf, OsString);
112103

113104
impl IntoScript for &'static str {
114105
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
115106
Ok(ScriptValue::String(Cow::Borrowed(self)))
116107
}
117108
}
118109

119-
self_type_dependency_only!(&'static str);
110+
120111

121112
impl IntoScript for ReflectReference {
122113
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
@@ -165,6 +156,15 @@ impl<T: IntoScript, const N: usize> IntoScript for [T; N] {
165156
}
166157
}
167158

159+
impl <T1: IntoScript, T2: IntoScript> IntoScript for Union<T1,T2> {
160+
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
161+
match self.into_left() {
162+
Ok(left) => left.into_script(world),
163+
Err(right) => right.into_script(world),
164+
}
165+
}
166+
}
167+
168168
impl<V: IntoScript> IntoScript for HashMap<String, V> {
169169
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
170170
let mut map = HashMap::new();

crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub trait IntoScriptRef {
2626
) -> Result<ScriptValue, InteropError>;
2727
}
2828

29-
#[macro_export]
3029
/// a utility for matching types by their [`std::any::TypeId`]
30+
#[macro_export]
3131
macro_rules! match_by_type {
3232
(match $on:ident {$($id:ident : $ty:ty => $conv:expr),*}) => {
3333
match $on {
@@ -39,6 +39,8 @@ macro_rules! match_by_type {
3939
};
4040
}
4141

42+
pub use match_by_type;
43+
4244
#[macro_export]
4345
/// Downcasts a reference into a value of a given type or returns an error if the downcast fails.
4446
macro_rules! downcast_into_value {

crates/bevy_mod_scripting_core/src/bindings/function/mod.rs

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
//! Abstractions to do with dynamic script functions
22
3-
pub mod arg_meta;
4-
pub mod from;
5-
pub mod from_ref;
6-
pub mod into;
7-
pub mod into_ref;
8-
pub mod namespace;
9-
pub mod script_function;
10-
pub mod type_dependencies;
3+
crate::private::export_all_in_modules!{
4+
arg_meta,
5+
from,
6+
from_ref,
7+
into,
8+
into_ref,
9+
namespace,
10+
script_function,
11+
type_dependencies
12+
}
1113

1214
#[cfg(test)]
1315
#[allow(dead_code)]
1416
mod test {
1517
use bevy::reflect::{FromReflect, GetTypeRegistration, Reflect, Typed};
1618
use bevy_mod_scripting_derive::script_bindings;
1719

18-
use crate::{
19-
bindings::{
20+
use crate::bindings::{
2021
function::{
2122
from::{Ref, Union, Val},
2223
namespace::IntoNamespace,
2324
script_function::AppScriptFunctionRegistry,
24-
},
25-
script_value::ScriptValue,
26-
},
27-
docgen::typed_through::TypedThrough,
28-
};
25+
}, script_value::ScriptValue
26+
};
2927

3028
use super::arg_meta::{ScriptArgument, ScriptReturn, TypedScriptArgument, TypedScriptReturn};
3129

@@ -135,19 +133,29 @@ mod test {
135133
test_is_valid_arg::<Ref<'_, T>>();
136134
}
137135

136+
fn test_union<T>()
137+
where
138+
T: TypedScriptArgument + TypedScriptReturn,
139+
T::Underlying: FromReflect + Typed + GetTypeRegistration,
140+
for<'a> T::This<'a>: Into<T>,
141+
{
142+
test_is_valid_arg_and_return::<Union<T, T>>();
143+
test_is_valid_arg_and_return::<Union<T, Union<T, T>>>();
144+
}
145+
138146
fn test_array<T, const N: usize>()
139147
where
140-
T: ScriptArgument + ScriptReturn,
141-
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
148+
T: TypedScriptArgument + TypedScriptReturn + 'static,
149+
T::Underlying: FromReflect + Typed + GetTypeRegistration,
142150
for<'a> T::This<'a>: Into<T>,
143151
{
144152
test_is_valid_arg_and_return::<[T; N]>();
145153
}
146154

147155
fn test_tuple<T>()
148156
where
149-
T: ScriptArgument + ScriptReturn,
150-
T: GetTypeRegistration + FromReflect + TypedThrough + Typed,
157+
T: TypedScriptArgument + TypedScriptReturn + 'static,
158+
T::Underlying: FromReflect + Typed + GetTypeRegistration,
151159
for<'a> T::This<'a>: Into<T>,
152160
{
153161
test_is_valid_arg_and_return::<()>();
@@ -158,26 +166,26 @@ mod test {
158166

159167
fn test_option<T>()
160168
where
161-
T: ScriptArgument + ScriptReturn,
162-
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
169+
T: TypedScriptArgument + TypedScriptReturn,
170+
T::Underlying: FromReflect + Typed + GetTypeRegistration,
163171
for<'a> T::This<'a>: Into<T>,
164172
{
165173
test_is_valid_arg_and_return::<Option<T>>();
166174
}
167175

168176
fn test_vec<T>()
169177
where
170-
T: ScriptArgument + ScriptReturn,
171-
T: GetTypeRegistration + FromReflect + Typed + TypedThrough,
178+
T: TypedScriptArgument + TypedScriptReturn + 'static,
179+
T::Underlying: FromReflect + Typed + GetTypeRegistration,
172180
for<'a> T::This<'a>: Into<T>,
173181
{
174182
test_is_valid_arg_and_return::<Vec<T>>();
175183
}
176184

177185
fn test_hashmap<V>()
178186
where
179-
V: ScriptArgument + ScriptReturn,
180-
V: GetTypeRegistration + FromReflect + Typed + TypedThrough,
187+
V: TypedScriptArgument + TypedScriptReturn + 'static,
188+
V::Underlying: FromReflect + Typed + GetTypeRegistration + Eq,
181189
for<'a> V::This<'a>: Into<V>,
182190
{
183191
test_is_valid_arg_and_return::<std::collections::HashMap<String, V>>();

0 commit comments

Comments
 (0)