Skip to content

Commit 3bfee7a

Browse files
authored
Merge branch 'main' into unit-struct-test
2 parents 6001547 + 8b588b4 commit 3bfee7a

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,14 @@ where
449449
}
450450
Ok(hashmap)
451451
}
452+
ScriptValue::List(list) => {
453+
let mut hashmap = std::collections::HashMap::new();
454+
for elem in list {
455+
let (key, val) = <(String, V)>::from_script(elem, world.clone())?;
456+
hashmap.insert(key, val);
457+
}
458+
Ok(hashmap)
459+
}
452460
_ => Err(InteropError::value_mismatch(
453461
std::any::TypeId::of::<std::collections::HashMap<String, V>>(),
454462
value,
@@ -499,3 +507,44 @@ where
499507
}
500508
}
501509
}
510+
511+
macro_rules! impl_from_script_tuple {
512+
($($ty:ident),*) => {
513+
#[allow(non_snake_case)]
514+
impl<$($ty: FromScript),*> FromScript for ($($ty,)*)
515+
where
516+
Self: 'static,
517+
$(
518+
for<'w> $ty::This<'w>: Into<$ty>,
519+
)*
520+
{
521+
type This<'w> = Self;
522+
523+
fn from_script(value: ScriptValue, world: WorldGuard<'_>) -> Result<Self, InteropError> {
524+
match value {
525+
ScriptValue::List(list) => {
526+
let expected_arg_count = $crate::bindings::function::script_function::count!( $($ty)* );
527+
if list.len() != expected_arg_count {
528+
return Err(InteropError::length_mismatch(expected_arg_count, list.len()));
529+
}
530+
531+
let mut iter = list.into_iter();
532+
$(
533+
let next_item = iter.next().ok_or_else(|| InteropError::invariant("list has right amount of elements"))?;
534+
let $ty = $ty::from_script(next_item, world.clone())?.into();
535+
)*
536+
537+
538+
Ok(($($ty,)*))
539+
}
540+
_ => Err(InteropError::value_mismatch(
541+
std::any::TypeId::of::<Self>(),
542+
value,
543+
)),
544+
}
545+
}
546+
}
547+
};
548+
}
549+
550+
bevy::utils::all_tuples!(impl_from_script_tuple, 1, 14, T);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ mod test {
151151
for<'a> T::This<'a>: Into<T>,
152152
{
153153
test_is_valid_arg_and_return::<()>();
154-
test_is_valid_return::<(T,)>();
155-
test_is_valid_return::<(T, T)>();
156-
test_is_valid_return::<(T, T, T, T, T, T, T, T, T, T)>();
154+
test_is_valid_arg_and_return::<(T,)>();
155+
test_is_valid_arg_and_return::<(T, T)>();
156+
test_is_valid_arg_and_return::<(T, T, T, T, T, T, T, T, T, T)>();
157157
}
158158

159159
fn test_option<T>()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,12 @@ impl ScriptFunctionRegistry {
530530
}
531531

532532
macro_rules! count {
533-
() => (0usize);
534-
( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
533+
() => (0usize);
534+
( $x:tt $($xs:tt)* ) => (1usize + $crate::bindings::function::script_function::count!($($xs)*));
535535
}
536536

537+
pub(crate) use count;
538+
537539
macro_rules! impl_script_function {
538540

539541
($( $param:ident ),* ) => {

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -656,25 +656,6 @@ impl GlobalNamespace {
656656
&mut allocator,
657657
))
658658
}
659-
660-
/// Constructs a hash map. Useful for languages which do not make the distinction between lists and dictionaries.
661-
///
662-
/// Arguments:
663-
/// * `map_or_list`: The list or map to convert to a hash map.
664-
/// Returns:
665-
/// * `hashMap`: The converted hash map
666-
fn map(
667-
map_or_list: Union<HashMap<String, ScriptValue>, Vec<ScriptValue>>,
668-
) -> HashMap<String, ScriptValue> {
669-
match map_or_list.into_left() {
670-
Ok(map) => map,
671-
Err(list) => list
672-
.into_iter()
673-
.enumerate()
674-
.map(|(k, v)| (k.to_string(), v))
675-
.collect(),
676-
}
677-
}
678659
}
679660

680661
pub fn register_core_functions(app: &mut App) {

0 commit comments

Comments
 (0)