Skip to content

Commit db825a5

Browse files
committed
shift to bakery paradigm
1 parent e4cbb26 commit db825a5

File tree

7 files changed

+315
-602
lines changed

7 files changed

+315
-602
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::any::TypeId;
2+
3+
use bevy::reflect::PartialReflect;
4+
5+
use crate::{
6+
bindings::{ReflectReference, WorldGuard},
7+
error::InteropError,
8+
prelude::ScriptValue,
9+
};
10+
11+
/// Converts from a [`ScriptValue`] to a value equivalent to the given [`TypeId`].
12+
///
13+
/// Type Erased version of [`super::from::FromScript`].
14+
pub trait FromScriptRef {
15+
fn from_script_ref(
16+
target: TypeId,
17+
value: ScriptValue,
18+
world: WorldGuard,
19+
) -> Result<Self, InteropError>
20+
where
21+
Self: Sized;
22+
}
23+
24+
impl FromScriptRef for Box<dyn PartialReflect> {
25+
fn from_script_ref(
26+
target: TypeId,
27+
value: ScriptValue,
28+
world: WorldGuard,
29+
) -> Result<Self, InteropError>
30+
where
31+
Self: Sized,
32+
{
33+
todo!()
34+
}
35+
}
Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
use std::{any::TypeId, ffi::OsString, path::PathBuf};
22

3+
use bevy::{
4+
reflect::{ParsedPath, PartialReflect},
5+
text::cosmic_text::rustybuzz::Script,
6+
};
7+
38
use crate::{
49
bindings::{function::into::IntoScript, ReflectReference, WorldGuard},
510
error::InteropError,
611
prelude::ScriptValue,
7-
reflection_extensions::TypeIdExtensions,
12+
reflection_extensions::{PartialReflectExt, TypeIdExtensions},
813
};
914

1015
/// Converts a value represented by a reference into a [`crate::bindings::function::ScriptValue`].
1116
/// Instead of a direct conversion, the trait tries to peek into the value behind the reference and find out the most suitable representation.
1217
///
18+
/// Type Erased version of [`super::from::FromScript`].
19+
///
1320
/// - Primitives are converted to simple values
1421
/// - Container types are converted to references (so the references persist after accesses inside them)
1522
pub trait IntoScriptRef {
@@ -22,12 +29,13 @@ pub trait IntoScriptRef {
2229
macro_rules! match_by_type {
2330
(match $on:ident {$($id:ident : $ty:ty => $conv:expr),*}) => {
2431
$(
32+
#[allow(unused_variables)]
2533
let $id = std::any::TypeId::of::<$ty>();
2634
)*
2735

2836
match $on {
2937
$(
30-
$id => {$conv},
38+
$id if $id == std::any::TypeId::of::<$ty>() => {$conv},
3139
)*
3240
_ => {},
3341
}
@@ -50,38 +58,54 @@ impl IntoScriptRef for ReflectReference {
5058
self_: ReflectReference,
5159
world: WorldGuard,
5260
) -> 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();
61+
self_.with_reflect(world.clone(), |r| into_script_ref(self_.clone(), r, world))?
62+
}
63+
}
5864

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-
);
65+
fn into_script_ref(
66+
mut self_: ReflectReference,
67+
r: &dyn PartialReflect,
68+
world: WorldGuard,
69+
) -> Result<ScriptValue, InteropError> {
70+
let type_id = r
71+
.get_represented_type_info()
72+
.map(|i| i.type_id())
73+
.or_fake_id();
8374

75+
match_by_type! (
76+
match type_id {
77+
ta : usize => return downcast_into_value!(r, usize).into_script(world),
78+
tb : isize => return downcast_into_value!(r, isize).into_script(world),
79+
tc : u8 => return downcast_into_value!(r, u8).into_script(world),
80+
td : u16 => return downcast_into_value!(r, u16).into_script(world),
81+
te : u32 => return downcast_into_value!(r, u32).into_script(world),
82+
tf : u64 => return downcast_into_value!(r, u64).into_script(world),
83+
tg : u128 => return downcast_into_value!(r, u128).into_script(world),
84+
th : i8 => return downcast_into_value!(r, i8).into_script(world),
85+
ti : i16 => return downcast_into_value!(r, i16).into_script(world),
86+
tj : i32 => return downcast_into_value!(r, i32).into_script(world),
87+
tk : i64 => return downcast_into_value!(r, i64).into_script(world),
88+
tl : i128 => return downcast_into_value!(r, i128).into_script(world),
89+
tm : f32 => return downcast_into_value!(r, f32).into_script(world),
90+
tn : f64 => return downcast_into_value!(r, f64).into_script(world),
91+
to : bool => return downcast_into_value!(r, bool).into_script(world),
92+
tp : char => return downcast_into_value!(r, char).into_script(world),
93+
tq : String => return downcast_into_value!(r, String).clone().into_script(world),
94+
tr : PathBuf => return downcast_into_value!(r, PathBuf).clone().into_script(world),
95+
ts : OsString=> return downcast_into_value!(r, OsString).clone().into_script(world),
96+
tn : () => return Ok(ScriptValue::Unit)
97+
}
98+
);
99+
100+
// either return nil or ref into
101+
if let Ok(as_option) = r.as_option() {
102+
return if let Some(s) = as_option {
103+
self_.index_path(ParsedPath::parse_static(".0").expect("invariant"));
104+
into_script_ref(self_, s, world)
105+
} else {
84106
Ok(ScriptValue::Unit)
85-
})?
107+
};
86108
}
109+
110+
Ok(ScriptValue::Reference(self_))
87111
}

0 commit comments

Comments
 (0)