|
| 1 | +use crate::avm2::events::Event; |
| 2 | +use crate::avm2::object::TObject as _; |
| 3 | +use crate::avm2::Activation; |
| 4 | +use crate::avm2::Object; |
| 5 | +use crate::avm2::Value; |
| 6 | +use crate::display_object::DisplayObject; |
| 7 | +use crate::string::AvmString; |
| 8 | +use std::cell::{Ref, RefMut}; |
| 9 | + |
| 10 | +/// This trait is implemented for each type that can appear in the signature |
| 11 | +/// of a method annotated with `#[native]` (e.g. `bool`, `f64`, `Object`) |
| 12 | +pub trait ExtractFromVm<'a, 'gc>: Sized { |
| 13 | + /// Attempts to extract `Self` from the provided `Value`. |
| 14 | + /// If the extraction cannot be performed (or would require a coercion), |
| 15 | + /// then this should return `None`. |
| 16 | + /// |
| 17 | + /// The provided `activation` should only be used for debugging, |
| 18 | + /// or calling a method that requires a `MutationContext`. |
| 19 | + /// Any coercions (e.g. `coerce_to_string`) should have already been |
| 20 | + /// performed by the time this method is called. |
| 21 | + fn extract_from(val: &'a Value<'gc>, activation: &mut Activation<'_, 'gc>) -> Option<Self>; |
| 22 | +} |
| 23 | + |
| 24 | +/// Allows writing `arg: DisplayObject<'gc>` in a `#[native]` method |
| 25 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for DisplayObject<'gc> { |
| 26 | + fn extract_from(val: &'a Value<'gc>, _activation: &mut Activation<'_, 'gc>) -> Option<Self> { |
| 27 | + val.as_object().and_then(|o| o.as_display_object()) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Allows writing `arg: AvmString<'gc>` in a `#[native]` method |
| 32 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for AvmString<'gc> { |
| 33 | + fn extract_from(val: &'a Value<'gc>, _activation: &mut Activation<'_, 'gc>) -> Option<Self> { |
| 34 | + if let Value::String(string) = val { |
| 35 | + Some(*string) |
| 36 | + } else { |
| 37 | + None |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Allows writing `arg: f64` in a `#[native]` method |
| 43 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for f64 { |
| 44 | + fn extract_from(val: &'a Value<'gc>, _activation: &mut Activation<'_, 'gc>) -> Option<Self> { |
| 45 | + if let Value::Number(num) = val { |
| 46 | + Some(*num) |
| 47 | + } else { |
| 48 | + None |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Allows writing `arg: bool` in a `#[native]` method |
| 54 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for bool { |
| 55 | + fn extract_from(val: &'a Value<'gc>, _activation: &mut Activation<'_, 'gc>) -> Option<Self> { |
| 56 | + if let Value::Bool(val) = val { |
| 57 | + Some(*val) |
| 58 | + } else { |
| 59 | + None |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Allows writing `arg: Ref<'_, Event<'gc>>` in a `#[native]` method. |
| 65 | +/// This is a little more cumbersome for the user than allowing `&Event<'gc>`, |
| 66 | +/// but it avoids complicating the implementation. |
| 67 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for Ref<'a, Event<'gc>> { |
| 68 | + fn extract_from( |
| 69 | + val: &'a Value<'gc>, |
| 70 | + _activation: &mut Activation<'_, 'gc>, |
| 71 | + ) -> Option<Ref<'a, Event<'gc>>> { |
| 72 | + val.as_object_ref().and_then(|obj| obj.as_event()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Allows writing `arg: RefMut<'_, Event<'gc>>` in a `#[native]` method. |
| 77 | +/// This is a little more cumbersome for the user than allowing `&Event<'gc>`, |
| 78 | +/// but it avoids complicating the implementation. |
| 79 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for RefMut<'a, Event<'gc>> { |
| 80 | + fn extract_from( |
| 81 | + val: &'a Value<'gc>, |
| 82 | + activation: &mut Activation<'_, 'gc>, |
| 83 | + ) -> Option<RefMut<'a, Event<'gc>>> { |
| 84 | + val.as_object_ref() |
| 85 | + .and_then(|obj| obj.as_event_mut(activation.context.gc_context)) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Allows writing `arg: Object<'gc>` in a `#[native]` method |
| 90 | +impl<'a, 'gc> ExtractFromVm<'a, 'gc> for Object<'gc> { |
| 91 | + fn extract_from( |
| 92 | + val: &'a Value<'gc>, |
| 93 | + _activation: &mut Activation<'_, 'gc>, |
| 94 | + ) -> Option<Object<'gc>> { |
| 95 | + val.as_object() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// A helper trait to allow using both `Option<SomeNativeType>` and `SomeNativeType` |
| 100 | +/// as the receiver (`this`) argument of a `#[native]` method. |
| 101 | +pub trait ReceiverHelper<'a, 'gc>: Sized { |
| 102 | + // We take an `&Option<Value>` instead of a `Option<Object>` so that we can call |
| 103 | + // an `ExtractFromVm` impl without lifetime issues (it's impossible to turn a |
| 104 | + // `&'a Object` to an `&'a Value::Object(object)`). |
| 105 | + fn extract_from( |
| 106 | + val: &'a Option<Value<'gc>>, |
| 107 | + activation: &mut Activation<'_, 'gc>, |
| 108 | + ) -> Option<Self>; |
| 109 | +} |
| 110 | + |
| 111 | +/// Allows writing `this: SomeNativeType` in a `#[native]` method, where `SomeNativeType` |
| 112 | +/// is any type with an `ExtractFromVm` (that is, it can be used as `arg: SomeNativeType`). |
| 113 | +/// If the function is called without a receiver (e.g. `Option<Object<'gc>>` is `None`), |
| 114 | +/// the extraction fails. |
| 115 | +impl<'a, 'gc, T> ReceiverHelper<'a, 'gc> for T |
| 116 | +where |
| 117 | + T: ExtractFromVm<'a, 'gc>, |
| 118 | +{ |
| 119 | + fn extract_from( |
| 120 | + val: &'a Option<Value<'gc>>, |
| 121 | + activation: &mut Activation<'_, 'gc>, |
| 122 | + ) -> Option<Self> { |
| 123 | + if let Some(val) = val { |
| 124 | + let extracted: Option<T> = ExtractFromVm::extract_from(val, activation); |
| 125 | + extracted |
| 126 | + } else { |
| 127 | + None |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// Allows writing `this: Option<SomeNativeType>` in a `#[native]` method, where `SomeNativeType` |
| 133 | +/// is any type with an `ExtractFromVm` (that is, it can be used as `arg: SomeNativeType`). |
| 134 | +/// If the function is called without a receiver (e.g. `Option<Object<'gc>>` is `None`), |
| 135 | +/// then the `#[native]` function will be called with `None`. |
| 136 | +impl<'a, 'gc, T> ReceiverHelper<'a, 'gc> for Option<T> |
| 137 | +where |
| 138 | + T: ExtractFromVm<'a, 'gc>, |
| 139 | +{ |
| 140 | + fn extract_from( |
| 141 | + val: &'a Option<Value<'gc>>, |
| 142 | + activation: &mut Activation<'_, 'gc>, |
| 143 | + ) -> Option<Self> { |
| 144 | + if let Some(val) = val { |
| 145 | + // If the function was called with a receiver, then try to extract |
| 146 | + // a value. |
| 147 | + let extracted: Option<T> = ExtractFromVm::extract_from(val, activation); |
| 148 | + // If the extraction failed, then treat this extraction as having failed |
| 149 | + // as well. For example, if the user writes `this: Option<DisplayObject>`, |
| 150 | + // and the function is called with a Boolean receiver (e.g. `true`), then |
| 151 | + // we want an error to be produced. We do *not* want to call the user's |
| 152 | + // function with `None` (since an invalid receiver is different from no |
| 153 | + // receiver at all). |
| 154 | + extracted.map(Some) |
| 155 | + } else { |
| 156 | + // If there's no receiver, then the extraction succeeds (the outer `Some`), |
| 157 | + // and we want to call the `#[native]` method with a value of `None` for `this`. |
| 158 | + Some(None) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments