Skip to content

feat: Add functions script method, and create function info scaffolding #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions crates/bevy_mod_scripting_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ parking_lot = "0.12.1"
dashmap = "6"
smallvec = "1.11"
itertools = "0.13"
derivative = "2.2"

[dev-dependencies]
test_utils = { workspace = true }
Expand Down
64 changes: 0 additions & 64 deletions crates/bevy_mod_scripting_core/src/bindings/function/arg_info.rs

This file was deleted.

95 changes: 95 additions & 0 deletions crates/bevy_mod_scripting_core/src/bindings/function/arg_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//! Trait implementations to help with function dispatch.

use std::{ffi::OsString, path::PathBuf};

use crate::bindings::{script_value::ScriptValue, ReflectReference};

use super::{
from::{FromScript, Mut, Ref, Val},
into::IntoScript,
script_function::{DynamicScriptFunction, DynamicScriptFunctionMut, FunctionCallContext},
type_dependencies::GetTypeDependencies,
};

/// Marker trait for types that can be used as arguments to a script function.
pub trait ScriptArgument: ArgMeta + FromScript + GetTypeDependencies {}
impl<T: ArgMeta + FromScript + GetTypeDependencies> ScriptArgument for T {}

/// Marker trait for types that can be used as return values from a script function.
pub trait ScriptReturn: IntoScript + GetTypeDependencies {}
impl<T: IntoScript + GetTypeDependencies> ScriptReturn for T {}

/// Describes an argument to a script function. Provides necessary information for the function to handle dispatch.
pub trait ArgMeta {
fn default_value() -> Option<ScriptValue> {
None
}
}

impl ArgMeta for ScriptValue {}

macro_rules! impl_arg_info {
($($ty:ty),*) => {
$(
impl ArgMeta for $ty {}
)*
};
}

impl_arg_info!(bool, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64, usize, isize);

impl_arg_info!(String, PathBuf, OsString);

impl_arg_info!(char);

impl_arg_info!(ReflectReference);

impl<T> ArgMeta for Val<T> {}
impl<T> ArgMeta for Ref<'_, T> {}
impl<T> ArgMeta for Mut<'_, T> {}

impl<T> ArgMeta for Option<T> {
fn default_value() -> Option<ScriptValue> {
Some(ScriptValue::Unit)
}
}

impl<T> ArgMeta for Vec<T> {}
impl<T, const N: usize> ArgMeta for [T; N] {}

impl<K, V> ArgMeta for std::collections::HashMap<K, V> {}

impl_arg_info!(DynamicScriptFunction, DynamicScriptFunctionMut);

impl ArgMeta for () {
fn default_value() -> Option<ScriptValue> {
Some(ScriptValue::Unit)
}
}
impl<T> ArgMeta for (T,) {}
impl<T1, T2> ArgMeta for (T1, T2) {}
impl<T1, T2, T3> ArgMeta for (T1, T2, T3) {}
impl<T1, T2, T3, T4> ArgMeta for (T1, T2, T3, T4) {}
impl<T1, T2, T3, T4, T5> ArgMeta for (T1, T2, T3, T4, T5) {}
impl<T1, T2, T3, T4, T5, T6> ArgMeta for (T1, T2, T3, T4, T5, T6) {}
impl<T1, T2, T3, T4, T5, T6, T7> ArgMeta for (T1, T2, T3, T4, T5, T6, T7) {}
impl<T1, T2, T3, T4, T5, T6, T7, T8> ArgMeta for (T1, T2, T3, T4, T5, T6, T7, T8) {}
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> ArgMeta for (T1, T2, T3, T4, T5, T6, T7, T8, T9) {}
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ArgMeta
for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
{
}
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> ArgMeta
for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
{
}
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> ArgMeta
for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
{
}
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> ArgMeta
for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
{
}

impl ArgMeta for FunctionCallContext {}
24 changes: 24 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 @@ -421,3 +421,27 @@ impl FromScript for DynamicScriptFunction {
}
}
}

impl<V> FromScript for std::collections::HashMap<String, V>
where
V: FromScript + 'static,
for<'w> V::This<'w>: Into<V>,
{
type This<'w> = Self;

fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
match value {
ScriptValue::Map(map) => {
let mut hashmap = std::collections::HashMap::new();
for (key, value) in map {
hashmap.insert(key, V::from_script(value, world.clone())?.into());
}
Ok(hashmap)
}
_ => Err(InteropError::value_mismatch(
std::any::TypeId::of::<std::collections::HashMap<String, V>>(),
value,
)),
}
}
}
12 changes: 11 additions & 1 deletion crates/bevy_mod_scripting_core/src/bindings/function/into.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, ffi::OsString, path::PathBuf};
use std::{borrow::Cow, collections::HashMap, ffi::OsString, path::PathBuf};

use bevy::reflect::Reflect;

Expand Down Expand Up @@ -153,6 +153,16 @@ impl<T: IntoScript, const N: usize> IntoScript for [T; N] {
}
}

impl<V: IntoScript> IntoScript for HashMap<String, V> {
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
let mut map = HashMap::new();
for (key, value) in self {
map.insert(key, value.into_script(world.clone())?);
}
Ok(ScriptValue::Map(map))
}
}

impl IntoScript for InteropError {
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
Ok(ScriptValue::Error(self))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::OsString, path::PathBuf};
use std::{borrow::Cow, ffi::OsString, path::PathBuf};

use bevy::reflect::{Access, PartialReflect};

Expand Down Expand Up @@ -90,6 +90,13 @@ fn into_script_ref(
to : bool => return downcast_into_value!(r, bool).into_script(world),
tp : char => return downcast_into_value!(r, char).into_script(world),
tq : String => return downcast_into_value!(r, String).clone().into_script(world),
tcs: Cow<'static, str> => match r.try_downcast_ref::<Cow<'static, str>>() {
Some(cow) => return Ok(ScriptValue::String(cow.clone())),
None => return Err(InteropError::type_mismatch(
std::any::TypeId::of::<Cow<str>>(),
r.get_represented_type_info().map(|i| i.type_id()),
)),
},
tr : PathBuf => return downcast_into_value!(r, PathBuf).clone().into_script(world),
ts : OsString=> return downcast_into_value!(r, OsString).clone().into_script(world),
tn : () => return Ok(ScriptValue::Unit)
Expand Down
134 changes: 133 additions & 1 deletion crates/bevy_mod_scripting_core/src/bindings/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,139 @@
pub mod arg_info;
pub mod arg_meta;
pub mod from;
pub mod from_ref;
pub mod into;
pub mod into_ref;
pub mod namespace;
pub mod script_function;
pub mod type_dependencies;

#[cfg(test)]
#[allow(dead_code)]
mod test {
use bevy::reflect::{FromReflect, GetTypeRegistration, Typed};

use crate::{
bindings::function::from::{Ref, Val},
error::InteropError,
};

use super::arg_meta::{ScriptArgument, ScriptReturn};

fn test_is_valid_return<T: ScriptReturn>() {}
fn test_is_valid_arg<T: ScriptArgument>() {}
fn test_is_valid_arg_and_return<T: ScriptArgument + ScriptReturn>() {}

#[test]
fn primitives_are_valid_args() {
test_is_valid_arg_and_return::<bool>();
test_is_valid_arg_and_return::<i8>();
test_is_valid_arg_and_return::<i16>();
test_is_valid_arg_and_return::<i32>();
test_is_valid_arg_and_return::<i64>();
test_is_valid_arg_and_return::<i128>();
test_is_valid_arg_and_return::<u8>();
test_is_valid_arg_and_return::<u16>();
test_is_valid_arg_and_return::<u32>();
test_is_valid_arg_and_return::<u64>();
test_is_valid_arg_and_return::<u128>();
test_is_valid_arg_and_return::<f32>();
test_is_valid_arg_and_return::<f64>();
test_is_valid_arg_and_return::<usize>();
test_is_valid_arg_and_return::<isize>();
}

#[test]
fn strings_are_valid_args() {
test_is_valid_arg_and_return::<String>();
test_is_valid_arg_and_return::<std::path::PathBuf>();
test_is_valid_arg_and_return::<std::ffi::OsString>();
test_is_valid_arg_and_return::<char>();
}

#[test]
fn composites_are_valid_args() {
fn test_val<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect,
{
test_is_valid_arg_and_return::<Val<T>>();
}

fn test_ref<T>()
where
T: ScriptArgument,
T: GetTypeRegistration + FromReflect + Typed,
{
test_is_valid_arg::<Ref<'_, T>>();
}

fn test_mut<T>()
where
T: ScriptArgument,
T: GetTypeRegistration + FromReflect + Typed,
{
test_is_valid_arg::<Ref<'_, T>>();
}

test_is_valid_return::<InteropError>();

fn test_array<T, const N: usize>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<[T; N]>();
}

fn test_tuple<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed,
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)>();
}

fn test_option<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<Option<T>>();
}

fn test_vec<T>()
where
T: ScriptArgument + ScriptReturn,
T: GetTypeRegistration + FromReflect + Typed,
for<'a> T::This<'a>: Into<T>,
{
test_is_valid_arg_and_return::<Vec<T>>();
}

fn test_hashmap<V>()
where
V: ScriptArgument + ScriptReturn,
V: GetTypeRegistration + FromReflect + Typed,
for<'a> V::This<'a>: Into<V>,
{
test_is_valid_arg_and_return::<std::collections::HashMap<String, V>>();
}
}

#[test]
fn test_dynamic_functions() {
test_is_valid_arg_and_return::<
crate::bindings::function::script_function::DynamicScriptFunction,
>();
test_is_valid_arg_and_return::<
crate::bindings::function::script_function::DynamicScriptFunctionMut,
>();
}
}
Loading
Loading