|
| 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 | +} |
0 commit comments