Skip to content

Commit ca1ea68

Browse files
committed
avm2: Simplify BitmapData.perlinNoise impl
By collapsing ifs we can get rid of duplicate code and indentation.
1 parent 82ad26f commit ca1ea68

1 file changed

Lines changed: 48 additions & 57 deletions

File tree

core/src/avm2/globals/flash/display/bitmap_data.rs

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,63 +1303,54 @@ pub fn perlin_noise<'gc>(
13031303
) -> Result<Value<'gc>, Error<'gc>> {
13041304
let this = this.as_object().unwrap();
13051305

1306-
if let Some(bitmap_data) = this.as_bitmap_data() {
1307-
if !bitmap_data.disposed() {
1308-
let base_x = args.get_f64(0);
1309-
let base_y = args.get_f64(1);
1310-
let num_octaves = args.get_u32(2) as usize;
1311-
let seed = args.get_i32(3) as i64;
1312-
let stitch = args.get_bool(4);
1313-
let fractal_noise = args.get_bool(5);
1314-
let channel_options = ChannelOptions::from_bits_truncate(args.get_i32(6) as u8);
1315-
let grayscale = args.get_bool(7);
1316-
let offsets = args.try_get_object(8);
1317-
1318-
let point_class = activation.avm2().classes().point.inner_class_definition();
1319-
1320-
let octave_offsets: Result<Vec<_>, Error<'gc>> = (0..num_octaves)
1321-
.map(|i| {
1322-
if let Some(offsets) = offsets {
1323-
if let Some(offsets) = offsets.as_array_storage() {
1324-
if let Some(Value::Object(point)) = offsets.get(i) {
1325-
if point.is_of_type(point_class) {
1326-
let x = point
1327-
.get_slot(point_slots::X)
1328-
.coerce_to_number(activation)?;
1329-
let y = point
1330-
.get_slot(point_slots::Y)
1331-
.coerce_to_number(activation)?;
1332-
1333-
Ok((x, y))
1334-
} else {
1335-
Ok((0.0, 0.0))
1336-
}
1337-
} else {
1338-
Ok((0.0, 0.0))
1339-
}
1340-
} else {
1341-
Ok((0.0, 0.0))
1342-
}
1343-
} else {
1344-
Ok((0.0, 0.0))
1345-
}
1346-
})
1347-
.collect();
1348-
let octave_offsets = octave_offsets?;
1349-
1350-
operations::perlin_noise(
1351-
activation.gc(),
1352-
bitmap_data,
1353-
(base_x, base_y),
1354-
num_octaves,
1355-
seed,
1356-
stitch,
1357-
fractal_noise,
1358-
channel_options,
1359-
grayscale,
1360-
octave_offsets,
1361-
);
1362-
}
1306+
if let Some(bitmap_data) = this.as_bitmap_data()
1307+
&& !bitmap_data.disposed()
1308+
{
1309+
let base_x = args.get_f64(0);
1310+
let base_y = args.get_f64(1);
1311+
let num_octaves = args.get_u32(2) as usize;
1312+
let seed = args.get_i32(3) as i64;
1313+
let stitch = args.get_bool(4);
1314+
let fractal_noise = args.get_bool(5);
1315+
let channel_options = ChannelOptions::from_bits_truncate(args.get_i32(6) as u8);
1316+
let grayscale = args.get_bool(7);
1317+
let offsets = args.try_get_object(8);
1318+
1319+
let point_class = activation.avm2().classes().point.inner_class_definition();
1320+
1321+
let octave_offsets: Result<Vec<_>, Error<'gc>> = (0..num_octaves)
1322+
.map(|i| {
1323+
if let Some(offsets) = offsets
1324+
&& let Some(offsets) = offsets.as_array_storage()
1325+
&& let Some(Value::Object(point)) = offsets.get(i)
1326+
&& point.is_of_type(point_class)
1327+
{
1328+
let x = point
1329+
.get_slot(point_slots::X)
1330+
.coerce_to_number(activation)?;
1331+
let y = point
1332+
.get_slot(point_slots::Y)
1333+
.coerce_to_number(activation)?;
1334+
1335+
Ok((x, y))
1336+
} else {
1337+
Ok((0.0, 0.0))
1338+
}
1339+
})
1340+
.collect();
1341+
1342+
operations::perlin_noise(
1343+
activation.gc(),
1344+
bitmap_data,
1345+
(base_x, base_y),
1346+
num_octaves,
1347+
seed,
1348+
stitch,
1349+
fractal_noise,
1350+
channel_options,
1351+
grayscale,
1352+
octave_offsets?,
1353+
);
13631354
}
13641355

13651356
Ok(Value::Undefined)

0 commit comments

Comments
 (0)