Skip to content

Commit

Permalink
avm2: Use ArrayStorage::empty instead of `classes().array.construct…
Browse files Browse the repository at this point in the history
…()?`

Also make `ArrayStorage::empty` and `ArrayStorage::from_storage` infallible
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Dec 14, 2024
1 parent fd0ba04 commit 23837a4
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 84 deletions.
4 changes: 2 additions & 2 deletions core/src/avm2/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
unreachable!();
};

let args_object = ArrayObject::from_storage(self, args_array)?;
let args_object = ArrayObject::from_storage(self, args_array);

if method
.method()
Expand Down Expand Up @@ -1877,7 +1877,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
fn op_new_array(&mut self, num_args: u32) -> Result<FrameControl<'gc>, Error<'gc>> {
let args = self.pop_stack_args(num_args);
let array = ArrayStorage::from_args(&args[..]);
let array_obj = ArrayObject::from_storage(self, array)?;
let array_obj = ArrayObject::from_storage(self, array);

self.push_stack(array_obj);

Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/amf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub fn deserialize_value_impl<'gc>(
}
AmfValue::ECMAArray(id, values, elements, _) => {
let empty_storage = ArrayStorage::new(0);
let array = ArrayObject::from_storage(activation, empty_storage)?;
let array = ArrayObject::from_storage(activation, empty_storage);
object_map.insert(*id, array);

// First let's create an array out of `values` (dense portion), then we add the elements onto it.
Expand All @@ -321,7 +321,7 @@ pub fn deserialize_value_impl<'gc>(
}
AmfValue::StrictArray(id, values) => {
let empty_storage = ArrayStorage::new(0);
let array = ArrayObject::from_storage(activation, empty_storage)?;
let array = ArrayObject::from_storage(activation, empty_storage);
object_map.insert(*id, array);

let mut arr: Vec<Option<Value<'gc>>> = Vec::with_capacity(values.len());
Expand Down
10 changes: 5 additions & 5 deletions core/src/avm2/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn color_matrix_filter_to_avm2<'gc>(
let matrix = ArrayObject::from_storage(
activation,
filter.matrix.iter().map(|v| Value::from(*v)).collect(),
)?;
);
activation
.avm2()
.classes()
Expand Down Expand Up @@ -433,7 +433,7 @@ fn convolution_filter_to_avm2<'gc>(
.iter()
.map(|v| Value::from(f64::from(*v)))
.collect(),
)?;
);
activation.avm2().classes().convolutionfilter.construct(
activation,
&[
Expand Down Expand Up @@ -771,19 +771,19 @@ fn gradient_filter_to_avm2<'gc>(
.iter()
.map(|v| Value::from(v.color.to_rgb()))
.collect(),
)?;
);
let alphas = ArrayObject::from_storage(
activation,
filter
.colors
.iter()
.map(|v| Value::from(f64::from(v.color.a) / 255.0))
.collect(),
)?;
);
let ratios = ArrayObject::from_storage(
activation,
filter.colors.iter().map(|v| Value::from(v.ratio)).collect(),
)?;
);
class.construct(
activation,
&[
Expand Down
19 changes: 6 additions & 13 deletions core/src/avm2/flv.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
use crate::avm2::{
object::DateObject, Activation, ArrayObject, ArrayStorage, TObject as _, Value as Avm2Value,
};
use crate::avm2::array::ArrayStorage;
use crate::avm2::object::{ArrayObject, DateObject, ScriptObject, TObject as _};
use crate::avm2::{Activation, Value as Avm2Value};
use crate::string::AvmString;

use chrono::DateTime;
use flv_rs::{Value as FlvValue, Variable as FlvVariable};

fn avm2_object_from_flv_variables<'gc>(
activation: &mut Activation<'_, 'gc>,
variables: Vec<FlvVariable>,
) -> Avm2Value<'gc> {
let info_object = activation
.context
.avm2
.classes()
.object
.construct(activation, &[])
.expect("Object construction should succeed");
let info_object = ScriptObject::new_object(activation);

for value in variables {
let property_name = value.name;
Expand Down Expand Up @@ -43,9 +38,7 @@ fn avm2_array_from_flv_values<'gc>(
.collect::<Vec<Option<Avm2Value<'gc>>>>(),
);

ArrayObject::from_storage(activation, storage)
.unwrap()
.into()
ArrayObject::from_storage(activation, storage).into()
}

fn avm2_date_from_flv_date<'gc>(
Expand Down
18 changes: 9 additions & 9 deletions core/src/avm2/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ pub fn set_length<'gc>(
pub fn build_array<'gc>(
activation: &mut Activation<'_, 'gc>,
array: ArrayStorage<'gc>,
) -> Result<Value<'gc>, Error<'gc>> {
Ok(ArrayObject::from_storage(activation, array)?.into())
) -> Value<'gc> {
ArrayObject::from_storage(activation, array).into()
}

/// Implements `Array.concat`
Expand All @@ -204,7 +204,7 @@ pub fn concat<'gc>(
}
}

build_array(activation, base_array)
Ok(build_array(activation, base_array))
}

/// Resolves array holes.
Expand Down Expand Up @@ -440,7 +440,7 @@ pub fn map<'gc>(
new_array.push(new_item);
}

build_array(activation, new_array)
Ok(build_array(activation, new_array))
}

/// Implements `Array.filter`
Expand All @@ -465,7 +465,7 @@ pub fn filter<'gc>(
}
}

build_array(activation, new_array)
Ok(build_array(activation, new_array))
}

/// Implements `Array.every`
Expand Down Expand Up @@ -713,7 +713,7 @@ pub fn slice<'gc>(
)?);
}

return build_array(activation, new_array);
return Ok(build_array(activation, new_array));
}

Ok(Value::Undefined)
Expand Down Expand Up @@ -764,7 +764,7 @@ pub fn splice<'gc>(
swap(&mut *array, &mut resolved_array)
}

return build_array(activation, removed_array);
return Ok(build_array(activation, removed_array));
}
}

Expand Down Expand Up @@ -1017,12 +1017,12 @@ fn sort_postprocess<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
if unique_satisfied {
if options.contains(SortOptions::RETURN_INDEXED_ARRAY) {
return build_array(
return Ok(build_array(
activation,
ArrayStorage::from_storage(
values.iter().map(|(i, _v)| Some((*i).into())).collect(),
),
);
));
} else {
if let Some(mut old_array) = this.as_array_storage_mut(activation.context.gc_context) {
let new_vec = values
Expand Down
20 changes: 10 additions & 10 deletions core/src/avm2/globals/avmplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ fn describe_internal_body<'gc>(

let traits = ScriptObject::new_object(activation);

let bases = ArrayObject::empty(activation)?.as_array_object().unwrap();
let interfaces = ArrayObject::empty(activation)?.as_array_object().unwrap();
let variables = ArrayObject::empty(activation)?.as_array_object().unwrap();
let accessors = ArrayObject::empty(activation)?.as_array_object().unwrap();
let methods = ArrayObject::empty(activation)?.as_array_object().unwrap();
let bases = ArrayObject::empty(activation).as_array_object().unwrap();
let interfaces = ArrayObject::empty(activation).as_array_object().unwrap();
let variables = ArrayObject::empty(activation).as_array_object().unwrap();
let accessors = ArrayObject::empty(activation).as_array_object().unwrap();
let methods = ArrayObject::empty(activation).as_array_object().unwrap();

if flags.contains(DescribeTypeFlags::INCLUDE_BASES) {
traits.set_string_property_local("bases", bases.into(), activation)?;
Expand Down Expand Up @@ -221,7 +221,7 @@ fn describe_internal_body<'gc>(
variable.set_string_property_local("metadata", Value::Null, activation)?;

if flags.contains(DescribeTypeFlags::INCLUDE_METADATA) {
let metadata_object = ArrayObject::empty(activation)?;
let metadata_object = ArrayObject::empty(activation);
if let Some(metadata) = trait_metadata {
write_metadata(metadata_object, &metadata, activation)?;
}
Expand Down Expand Up @@ -292,7 +292,7 @@ fn describe_internal_body<'gc>(
method_obj.set_string_property_local("metadata", Value::Null, activation)?;

if flags.contains(DescribeTypeFlags::INCLUDE_METADATA) {
let metadata_object = ArrayObject::empty(activation)?;
let metadata_object = ArrayObject::empty(activation);
if let Some(metadata) = trait_metadata {
write_metadata(metadata_object, &metadata, activation)?;
}
Expand Down Expand Up @@ -359,7 +359,7 @@ fn describe_internal_body<'gc>(
activation,
)?;

let metadata_object = ArrayObject::empty(activation)?;
let metadata_object = ArrayObject::empty(activation);

if let Some(get_disp_id) = get {
if let Some(metadata) = vtable.get_metadata_for_disp(get_disp_id) {
Expand Down Expand Up @@ -409,7 +409,7 @@ fn describe_internal_body<'gc>(
"with top-level metadata"
);

let metadata_object = ArrayObject::empty(activation)?;
let metadata_object = ArrayObject::empty(activation);
traits.set_string_property_local("metadata", metadata_object.into(), activation)?;
} else {
traits.set_string_property_local("metadata", Value::Null, activation)?;
Expand All @@ -433,7 +433,7 @@ fn write_params<'gc>(
method: &Method<'gc>,
activation: &mut Activation<'_, 'gc>,
) -> Result<Object<'gc>, Error<'gc>> {
let params = ArrayObject::empty(activation)?;
let params = ArrayObject::empty(activation);
let mut params_array = params
.as_array_storage_mut(activation.context.gc_context)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions core/src/avm2/globals/flash/display/bitmap_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pub fn get_pixels<'gc>(
height,
&mut storage,
)?;

let bytearray = ByteArrayObject::from_storage(activation, storage)?;
return Ok(bytearray.into());
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/globals/flash/display/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ pub fn get_filters<'gc>(
.into_iter()
.map(|f| f.as_avm2_object(activation))
.collect::<Result<ArrayStorage<'gc>, Error<'gc>>>()?;
return Ok(ArrayObject::from_storage(activation, array)?.into());
return Ok(ArrayObject::from_storage(activation, array).into());
}
Ok(ArrayObject::empty(activation)?.into())
Ok(ArrayObject::empty(activation).into())
}

fn build_argument_type_error<'gc>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub fn get_objects_under_point<'gc>(
}
}

Ok(ArrayObject::from_storage(activation, ArrayStorage::from_storage(under_point))?.into())
Ok(ArrayObject::from_storage(activation, ArrayStorage::from_storage(under_point)).into())
}

pub fn are_inaccessible_objects_under_point<'gc>(
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/globals/flash/display/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn labels_for_scene<'gc>(
Ok((
scene_name.to_string(),
*scene_length,
ArrayObject::from_storage(activation, ArrayStorage::from_storage(frame_labels))?,
ArrayObject::from_storage(activation, ArrayStorage::from_storage(frame_labels)),
))
}

Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn get_scenes<'gc>(
return Ok(ArrayObject::from_storage(
activation,
ArrayStorage::from_storage(scene_objects),
)?
)
.into());
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/flash/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn enumerate_fonts<'gc>(
}
}

Ok(ArrayObject::from_storage(activation, storage)?.into())
Ok(ArrayObject::from_storage(activation, storage).into())
}

/// `Font.registerFont`
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/flash/text/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ pub fn get_text_runs<'gc>(
textrun_class.construct(activation, &[start.into(), end.into(), tf.into()])
})
.collect::<Result<ArrayStorage<'gc>, Error<'gc>>>()?;
Ok(ArrayObject::from_storage(activation, array)?.into())
Ok(ArrayObject::from_storage(activation, array).into())
}

pub fn get_line_index_of_char<'gc>(
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/flash/text/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ pub fn get_tab_stops<'gc>(
.as_ref()
.map_or(Ok(Value::Null), |tab_stops| {
let tab_stop_storage = tab_stops.iter().copied().collect();
Ok(ArrayObject::from_storage(activation, tab_stop_storage)?.into())
Ok(ArrayObject::from_storage(activation, tab_stop_storage).into())
});
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn deserialize_json_inner<'gc>(
arr.push(Some(mapped_val));
}
let storage = ArrayStorage::from_storage(arr);
let array = ArrayObject::from_storage(activation, storage)?;
let array = ArrayObject::from_storage(activation, storage);
array.into()
}
})
Expand Down
2 changes: 1 addition & 1 deletion core/src/avm2/globals/reg_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn exec<'gc>(
None => return Ok(Value::Null),
};

let object = ArrayObject::from_storage(activation, storage)?;
let object = ArrayObject::from_storage(activation, storage);

object.set_string_property_local("index", Value::Number(index as f64), activation)?;

Expand Down
14 changes: 5 additions & 9 deletions core/src/avm2/globals/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,8 @@ fn match_s<'gc>(
if old_last_index == regexp.last_index() {
regexp.set_last_index(1);
}
return Ok(ArrayObject::from_storage(activation, storage)
.unwrap()
.into());

return Ok(ArrayObject::from_storage(activation, storage).into());
} else {
let old = regexp.last_index();
regexp.set_last_index(0);
Expand All @@ -326,9 +325,8 @@ fn match_s<'gc>(
storage.push(AvmString::new(activation.context.gc_context, substring).into());
}
regexp.set_last_index(old);
return Ok(ArrayObject::from_storage(activation, storage)
.unwrap()
.into());

return Ok(ArrayObject::from_storage(activation, storage).into());
} else {
regexp.set_last_index(old);
// If the pattern parameter is a String or a non-global regular expression
Expand Down Expand Up @@ -495,9 +493,7 @@ fn split<'gc>(
.collect()
};

Ok(ArrayObject::from_storage(activation, storage)
.unwrap()
.into())
Ok(ArrayObject::from_storage(activation, storage).into())
}

/// Implements `String.substr`
Expand Down
Loading

0 comments on commit 23837a4

Please sign in to comment.