Skip to content

Commit e4cbb26

Browse files
committed
remove older stuff
1 parent 56fa757 commit e4cbb26

File tree

11 files changed

+103
-1531
lines changed

11 files changed

+103
-1531
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ macro_rules! impl_from_with_downcast {
4949
ScriptValue::Integer(i) => Ok(i as $ty),
5050
ScriptValue::Float(i) => Ok(i as $ty),
5151
ScriptValue::Reference(r) => r.downcast::<Self>(world),
52+
ScriptValue::Bool(b) => Ok(b as usize as $ty),
5253
_ => Err(InteropError::value_mismatch(std::any::TypeId::of::<Self>(), value)),
5354
}
5455
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ impl IntoScript for () {
2929
}
3030
}
3131

32+
impl IntoScript for bool {
33+
fn into_script(self, world: WorldGuard) -> Result<ScriptValue, InteropError> {
34+
Ok(ScriptValue::Bool(self))
35+
}
36+
}
37+
3238
macro_rules! impl_into_with_downcast {
3339
($variant:tt as $cast:ty [$($ty:ty),*]) => {
3440
$(
@@ -63,6 +69,7 @@ impl_into_stringlike!(
6369
s,
6470
[
6571
(String => s),
72+
(char => s.to_string()),
6673
(PathBuf => s.to_string_lossy().to_string()),
6774
(OsString => s.into_string().map_err(|e| InteropError::unsupported_operation(None, Some(Box::new(e)), "Could not convert OsString to String".to_owned()))?)
6875
]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::{any::TypeId, ffi::OsString, path::PathBuf};
2+
3+
use crate::{
4+
bindings::{function::into::IntoScript, ReflectReference, WorldGuard},
5+
error::InteropError,
6+
prelude::ScriptValue,
7+
reflection_extensions::TypeIdExtensions,
8+
};
9+
10+
/// Converts a value represented by a reference into a [`crate::bindings::function::ScriptValue`].
11+
/// Instead of a direct conversion, the trait tries to peek into the value behind the reference and find out the most suitable representation.
12+
///
13+
/// - Primitives are converted to simple values
14+
/// - Container types are converted to references (so the references persist after accesses inside them)
15+
pub trait IntoScriptRef {
16+
fn into_script_ref(
17+
self_: ReflectReference,
18+
world: WorldGuard,
19+
) -> Result<ScriptValue, InteropError>;
20+
}
21+
22+
macro_rules! match_by_type {
23+
(match $on:ident {$($id:ident : $ty:ty => $conv:expr),*}) => {
24+
$(
25+
let $id = std::any::TypeId::of::<$ty>();
26+
)*
27+
28+
match $on {
29+
$(
30+
$id => {$conv},
31+
)*
32+
_ => {},
33+
}
34+
};
35+
}
36+
37+
macro_rules! downcast_into_value {
38+
($r:ident, $ty:ty) => {
39+
*$r.try_downcast_ref::<$ty>().ok_or_else(|| {
40+
InteropError::type_mismatch(
41+
std::any::TypeId::of::<$ty>(),
42+
$r.get_represented_type_info().map(|i| i.type_id()),
43+
)
44+
})?
45+
};
46+
}
47+
48+
impl IntoScriptRef for ReflectReference {
49+
fn into_script_ref(
50+
self_: ReflectReference,
51+
world: WorldGuard,
52+
) -> Result<ScriptValue, InteropError> {
53+
self_.with_reflect(world.clone(), |r| {
54+
let type_id = r
55+
.get_represented_type_info()
56+
.map(|i| i.type_id())
57+
.or_fake_id();
58+
59+
match_by_type! (
60+
match type_id {
61+
ta : usize => return downcast_into_value!(r, usize).into_script(world),
62+
tb : isize => return downcast_into_value!(r, isize).into_script(world),
63+
tc : u8 => return downcast_into_value!(r, u8).into_script(world),
64+
td : u16 => return downcast_into_value!(r, u16).into_script(world),
65+
te : u32 => return downcast_into_value!(r, u32).into_script(world),
66+
tf : u64 => return downcast_into_value!(r, u64).into_script(world),
67+
tg : u128 => return downcast_into_value!(r, u128).into_script(world),
68+
th : i8 => return downcast_into_value!(r, i8).into_script(world),
69+
ti : i16 => return downcast_into_value!(r, i16).into_script(world),
70+
tj : i32 => return downcast_into_value!(r, i32).into_script(world),
71+
tk : i64 => return downcast_into_value!(r, i64).into_script(world),
72+
tl : i128 => return downcast_into_value!(r, i128).into_script(world),
73+
tm : f32 => return downcast_into_value!(r, f32).into_script(world),
74+
tn : f64 => return downcast_into_value!(r, f64).into_script(world),
75+
to : bool => return downcast_into_value!(r, bool).into_script(world),
76+
tp : char => return downcast_into_value!(r, char).into_script(world),
77+
tq : String => return downcast_into_value!(r, String).clone().into_script(world),
78+
tr : PathBuf => return downcast_into_value!(r, PathBuf).clone().into_script(world),
79+
ts : OsString=> return downcast_into_value!(r, OsString).clone().into_script(world),
80+
tn : () => return Ok(ScriptValue::Unit)
81+
}
82+
);
83+
84+
Ok(ScriptValue::Unit)
85+
})?
86+
}
87+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{any::TypeId, borrow::Cow, ops::Deref, sync::Arc};
22

33
pub mod from;
44
pub mod into;
5+
pub mod into_ref;
56
pub mod script_function;
67

78
use bevy::reflect::{
@@ -18,9 +19,7 @@ use crate::{
1819
};
1920

2021
use super::{
21-
access_map::ReflectAccessId,
22-
pretty_print::DisplayWithWorld,
23-
script_value::{FromScriptValue, IntoScriptValue, ScriptValue},
22+
access_map::ReflectAccessId, pretty_print::DisplayWithWorld, script_value::ScriptValue,
2423
ReflectBase, ReflectReference, WorldAccessGuard, WorldCallbackAccess, WorldGuard,
2524
};
2625

crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl ReflectReferencePrinter {
117117
let type_id = v
118118
.get_represented_type_info()
119119
.map(|t| t.type_id())
120-
.type_id_or_fake_id();
120+
.or_fake_id();
121121

122122
output.push_str("Reflect(");
123123
match type_id {

0 commit comments

Comments
 (0)