avm2: Add convenience method for converting a `Value` into an `EnumSet`.

This commit is contained in:
David Wendt 2020-09-02 22:55:01 -04:00 committed by Mike Welsh
parent a653a62a93
commit bb19699739
1 changed files with 15 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use crate::avm2::script::TranslationUnit;
use crate::avm2::string::AvmString;
use crate::avm2::{Avm2, Error};
use crate::ecma_conversions::{f64_to_wrapping_i32, f64_to_wrapping_u32};
use enumset::{EnumSet, EnumSetType};
use gc_arena::{Collect, MutationContext};
use std::cell::Ref;
use std::f64::NAN;
@ -445,6 +446,20 @@ impl<'gc> Value<'gc> {
Ok(f64_to_wrapping_i32(self.coerce_to_number(activation)?))
}
/// Coerce the value to an EnumSet of a particular type.
///
/// This function will ignore invalid bits of the value when interpreting
/// the enum.
pub fn coerce_to_enumset<E>(
&self,
activation: &mut Activation<'_, 'gc, '_>,
) -> Result<EnumSet<E>, Error>
where
E: EnumSetType,
{
Ok(EnumSet::from_u32_truncated(self.coerce_to_u32(activation)?))
}
/// Mininum number of digits after which numbers are formatted as
/// exponential strings.
const MIN_DIGITS: f64 = -6.0;