1
1
use std:: { any:: TypeId , ffi:: OsString , path:: PathBuf } ;
2
2
3
+ use bevy:: {
4
+ reflect:: { ParsedPath , PartialReflect } ,
5
+ text:: cosmic_text:: rustybuzz:: Script ,
6
+ } ;
7
+
3
8
use crate :: {
4
9
bindings:: { function:: into:: IntoScript , ReflectReference , WorldGuard } ,
5
10
error:: InteropError ,
6
11
prelude:: ScriptValue ,
7
- reflection_extensions:: TypeIdExtensions ,
12
+ reflection_extensions:: { PartialReflectExt , TypeIdExtensions } ,
8
13
} ;
9
14
10
15
/// Converts a value represented by a reference into a [`crate::bindings::function::ScriptValue`].
11
16
/// Instead of a direct conversion, the trait tries to peek into the value behind the reference and find out the most suitable representation.
12
17
///
18
+ /// Type Erased version of [`super::from::FromScript`].
19
+ ///
13
20
/// - Primitives are converted to simple values
14
21
/// - Container types are converted to references (so the references persist after accesses inside them)
15
22
pub trait IntoScriptRef {
@@ -22,12 +29,13 @@ pub trait IntoScriptRef {
22
29
macro_rules! match_by_type {
23
30
( match $on: ident { $( $id: ident : $ty: ty => $conv: expr) ,* } ) => {
24
31
$(
32
+ #[ allow( unused_variables) ]
25
33
let $id = std:: any:: TypeId :: of:: <$ty>( ) ;
26
34
) *
27
35
28
36
match $on {
29
37
$(
30
- $id => { $conv} ,
38
+ $id if $id == std :: any :: TypeId :: of :: <$ty> ( ) => { $conv} ,
31
39
) *
32
40
_ => { } ,
33
41
}
@@ -50,38 +58,54 @@ impl IntoScriptRef for ReflectReference {
50
58
self_ : ReflectReference ,
51
59
world : WorldGuard ,
52
60
) -> 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
+ }
58
64
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 ( ) ;
83
74
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 {
84
106
Ok ( ScriptValue :: Unit )
85
- } ) ?
107
+ } ;
86
108
}
109
+
110
+ Ok ( ScriptValue :: Reference ( self_) )
87
111
}
0 commit comments