From f7375c3700fcd78c9a785a45ec39f9315e322374 Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Fri, 27 Mar 2020 17:59:55 -0700 Subject: [PATCH] avm1: Use `PropertyMap::get_index` for stage object properties --- core/src/avm1/stage_object.rs | 18 +++++------------- core/src/property_map.rs | 5 +++++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/core/src/avm1/stage_object.rs b/core/src/avm1/stage_object.rs index 571f14f6b..e566721b5 100644 --- a/core/src/avm1/stage_object.rs +++ b/core/src/avm1/stage_object.rs @@ -380,18 +380,12 @@ unsafe impl<'gc> Collect for DisplayProperty<'gc> { /// The map from key/index to function pointers for special display object properties. #[derive(Collect)] #[collect(no_drop)] -pub struct DisplayPropertyMap<'gc> { - property_by_name: PropertyMap>, - property_by_index: Vec>, -} +pub struct DisplayPropertyMap<'gc>(PropertyMap>); impl<'gc> DisplayPropertyMap<'gc> { /// Creates the display property map. pub fn new(gc_context: MutationContext<'gc, '_>) -> GcCell<'gc, DisplayPropertyMap<'gc>> { - let mut property_map = DisplayPropertyMap { - property_by_name: PropertyMap::new(), - property_by_index: Vec::with_capacity(21), - }; + let mut property_map = DisplayPropertyMap(PropertyMap::new()); // Order is important: // should match the SWF specs for GetProperty/SetProperty. @@ -426,7 +420,7 @@ impl<'gc> DisplayPropertyMap<'gc> { pub fn get_by_name(&self, name: &str) -> Option<&DisplayProperty<'gc>> { // Display object properties are case insensitive, regardless of SWF version!? // TODO: Another string alloc; optimize this eventually. - self.property_by_name.get(&name, false) + self.0.get(&name, false) } /// Gets a property slot by SWF4 index. @@ -434,7 +428,7 @@ impl<'gc> DisplayPropertyMap<'gc> { /// Used by `GetProperty`/`SetProperty`. /// SWF19 pp. 85-86 pub fn get_by_index(&self, index: usize) -> Option<&DisplayProperty<'gc>> { - self.property_by_index.get(index) + self.0.get_index(index) } fn add_property( @@ -444,9 +438,7 @@ impl<'gc> DisplayPropertyMap<'gc> { set: Option>, ) { let prop = DisplayProperty { get, set }; - self.property_by_name - .insert(name.to_string(), prop.clone(), false); - self.property_by_index.push(prop); + self.0.insert(name.to_string(), prop.clone(), false); } } diff --git a/core/src/property_map.rs b/core/src/property_map.rs index aeda1e1ac..12124c0d5 100644 --- a/core/src/property_map.rs +++ b/core/src/property_map.rs @@ -71,6 +71,11 @@ impl PropertyMap { } } + /// Gets a value by index, based on insertion order. + pub fn get_index(&self, index: usize) -> Option<&V> { + self.0.get_index(index).map(|(_, v)| v) + } + pub fn insert(&mut self, key: String, value: V, case_sensitive: bool) -> Option { match self.entry(key, case_sensitive) { Entry::Occupied(entry) => Some(entry.insert(value)),