From bb1969973925acd923a60bdcc7221e8ed501c431 Mon Sep 17 00:00:00 2001 From: David Wendt Date: Wed, 2 Sep 2020 22:55:01 -0400 Subject: [PATCH] avm2: Add convenience method for converting a `Value` into an `EnumSet`. --- core/src/avm2/value.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/avm2/value.rs b/core/src/avm2/value.rs index e7861fcb0..740c40d6f 100644 --- a/core/src/avm2/value.rs +++ b/core/src/avm2/value.rs @@ -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( + &self, + activation: &mut Activation<'_, 'gc, '_>, + ) -> Result, 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;