Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/bevy_mod_scripting_core/src/bindings/function/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ where
}
Ok(hashmap)
}
ScriptValue::List(list) => {
let mut hashmap = std::collections::HashMap::new();
for elem in list {
let (key, val) = <(String, V)>::from_script(elem, world.clone())?;
hashmap.insert(key, val);
}
Ok(hashmap)
}
_ => Err(InteropError::value_mismatch(
std::any::TypeId::of::<std::collections::HashMap<String, V>>(),
value,
Expand Down Expand Up @@ -499,3 +507,44 @@ where
}
}
}

macro_rules! impl_from_script_tuple {
($($ty:ident),*) => {
#[allow(non_snake_case)]
impl<$($ty: FromScript),*> FromScript for ($($ty,)*)
where
Self: 'static,
$(
for<'w> $ty::This<'w>: Into<$ty>,
)*
{
type This<'w> = Self;

fn from_script(value: ScriptValue, world: WorldGuard<'_>) -> Result<Self, InteropError> {
match value {
ScriptValue::List(list) => {
let expected_arg_count = $crate::bindings::function::script_function::count!( $($ty)* );
if list.len() != expected_arg_count {
return Err(InteropError::length_mismatch(expected_arg_count, list.len()));
}

let mut iter = list.into_iter();
$(
let next_item = iter.next().ok_or_else(|| InteropError::invariant("list has right amount of elements"))?;
let $ty = $ty::from_script(next_item, world.clone())?.into();
)*


Ok(($($ty,)*))
}
_ => Err(InteropError::value_mismatch(
std::any::TypeId::of::<Self>(),
value,
)),
}
}
}
};
}

bevy::utils::all_tuples!(impl_from_script_tuple, 1, 14, T);
6 changes: 3 additions & 3 deletions crates/bevy_mod_scripting_core/src/bindings/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ mod test {
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<()>();
test_is_valid_return::<(T,)>();
test_is_valid_return::<(T, T)>();
test_is_valid_return::<(T, T, T, T, T, T, T, T, T, T)>();
test_is_valid_arg_and_return::<(T,)>();
test_is_valid_arg_and_return::<(T, T)>();
test_is_valid_arg_and_return::<(T, T, T, T, T, T, T, T, T, T)>();
}

fn test_option<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,12 @@ impl ScriptFunctionRegistry {
}

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

pub(crate) use count;

macro_rules! impl_script_function {

($( $param:ident ),* ) => {
Expand Down
19 changes: 0 additions & 19 deletions crates/bevy_mod_scripting_functions/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,25 +656,6 @@ impl GlobalNamespace {
&mut allocator,
))
}

/// Constructs a hash map. Useful for languages which do not make the distinction between lists and dictionaries.
///
/// Arguments:
/// * `map_or_list`: The list or map to convert to a hash map.
/// Returns:
/// * `hashMap`: The converted hash map
fn map(
map_or_list: Union<HashMap<String, ScriptValue>, Vec<ScriptValue>>,
) -> HashMap<String, ScriptValue> {
match map_or_list.into_left() {
Ok(map) => map,
Err(list) => list
.into_iter()
.enumerate()
.map(|(k, v)| (k.to_string(), v))
.collect(),
}
}
}

pub fn register_core_functions(app: &mut App) {
Expand Down
Loading