|
| 1 | +//! All the switchable special functions used by language implementors |
| 2 | +use super::{FromScriptRef, FunctionCallContext, IntoScriptRef}; |
| 3 | +use crate::{ |
| 4 | + bindings::{ReflectReference, ReflectionPathExt, ScriptValue}, |
| 5 | + error::InteropError, |
| 6 | + reflection_extensions::TypeIdExtensions, |
| 7 | +}; |
| 8 | +use bevy::reflect::{ParsedPath, PartialReflect}; |
| 9 | + |
| 10 | +/// A list of magic methods, these only have one replacable implementation, and apply to all `ReflectReferences`. |
| 11 | +/// It's up to the language implementer to call these in the right order (after any type specific overrides). |
| 12 | +/// |
| 13 | +/// These live in a separate mini registry since they are so commonly needed. This helps us avoid needless hashing and lookups as well as script value conversions |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct MagicFunctions { |
| 16 | + /// Indexer function |
| 17 | + pub get: |
| 18 | + fn(FunctionCallContext, ReflectReference, ScriptValue) -> Result<ScriptValue, InteropError>, |
| 19 | + /// Indexer setter function |
| 20 | + pub set: fn( |
| 21 | + FunctionCallContext, |
| 22 | + ReflectReference, |
| 23 | + ScriptValue, |
| 24 | + ScriptValue, |
| 25 | + ) -> Result<(), InteropError>, |
| 26 | +} |
| 27 | + |
| 28 | +impl MagicFunctions { |
| 29 | + /// Calls the currently set `get` function with the given arguments. |
| 30 | + pub fn get( |
| 31 | + &self, |
| 32 | + ctxt: FunctionCallContext, |
| 33 | + reference: ReflectReference, |
| 34 | + key: ScriptValue, |
| 35 | + ) -> Result<ScriptValue, InteropError> { |
| 36 | + (self.get)(ctxt, reference, key) |
| 37 | + } |
| 38 | + |
| 39 | + /// Calls the currently set `set` function with the given arguments. |
| 40 | + pub fn set( |
| 41 | + &self, |
| 42 | + ctxt: FunctionCallContext, |
| 43 | + reference: ReflectReference, |
| 44 | + key: ScriptValue, |
| 45 | + value: ScriptValue, |
| 46 | + ) -> Result<(), InteropError> { |
| 47 | + (self.set)(ctxt, reference, key, value) |
| 48 | + } |
| 49 | + |
| 50 | + /// Indexes into the given reference and if the nested type is a reference type, returns a deeper reference, otherwise |
| 51 | + /// returns the concrete value. |
| 52 | + /// |
| 53 | + /// Does not support map types at the moment, for maps see `map_get` |
| 54 | + /// |
| 55 | + /// Arguments: |
| 56 | + /// * `ctxt`: The function call context. |
| 57 | + /// * `reference`: The reference to index into. |
| 58 | + /// * `key`: The key to index with. |
| 59 | + /// |
| 60 | + /// Returns: |
| 61 | + /// * `value`: The value at the key, if the reference is indexable. |
| 62 | + pub fn default_get( |
| 63 | + ctxt: FunctionCallContext, |
| 64 | + mut reference: ReflectReference, |
| 65 | + key: ScriptValue, |
| 66 | + ) -> Result<ScriptValue, InteropError> { |
| 67 | + let mut path: ParsedPath = key.try_into()?; |
| 68 | + if ctxt.convert_to_0_indexed() { |
| 69 | + path.convert_to_0_indexed(); |
| 70 | + } |
| 71 | + reference.index_path(path); |
| 72 | + let world = ctxt.world()?; |
| 73 | + ReflectReference::into_script_ref(reference, world) |
| 74 | + } |
| 75 | + |
| 76 | + /// Sets the value under the specified path on the underlying value. |
| 77 | + /// |
| 78 | + /// Arguments: |
| 79 | + /// * `ctxt`: The function call context. |
| 80 | + /// * `reference`: The reference to set the value on. |
| 81 | + /// * `key`: The key to set the value at. |
| 82 | + /// * `value`: The value to set. |
| 83 | + /// |
| 84 | + /// Returns: |
| 85 | + /// * `result`: Nothing if the value was set successfully. |
| 86 | + pub fn default_set( |
| 87 | + ctxt: FunctionCallContext, |
| 88 | + mut reference: ReflectReference, |
| 89 | + key: ScriptValue, |
| 90 | + value: ScriptValue, |
| 91 | + ) -> Result<(), InteropError> { |
| 92 | + let world = ctxt.world()?; |
| 93 | + let mut path: ParsedPath = key.try_into()?; |
| 94 | + if ctxt.convert_to_0_indexed() { |
| 95 | + path.convert_to_0_indexed(); |
| 96 | + } |
| 97 | + reference.index_path(path); |
| 98 | + reference.with_reflect_mut(world.clone(), |r| { |
| 99 | + let target_type_id = r |
| 100 | + .get_represented_type_info() |
| 101 | + .map(|i| i.type_id()) |
| 102 | + .or_fake_id(); |
| 103 | + let other = |
| 104 | + <Box<dyn PartialReflect>>::from_script_ref(target_type_id, value, world.clone())?; |
| 105 | + r.try_apply(other.as_partial_reflect()) |
| 106 | + .map_err(|e| InteropError::external_error(Box::new(e)))?; |
| 107 | + Ok::<_, InteropError>(()) |
| 108 | + })? |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Default for MagicFunctions { |
| 113 | + fn default() -> Self { |
| 114 | + Self { |
| 115 | + get: MagicFunctions::default_get, |
| 116 | + set: MagicFunctions::default_set, |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments