Skip to content

Commit 79f9a6c

Browse files
committed
avm1: Fix this fallback in Function.prototype.call/apply
It should apply for all non-coercible values, not just null/undefined.
1 parent 7d66d5a commit 79f9a6c

File tree

4 files changed

+11
-153
lines changed

4 files changed

+11
-153
lines changed

core/src/avm1/globals/function.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::avm1::activation::Activation;
44
use crate::avm1::error::Error;
55
use crate::avm1::function::{ExecutionName, ExecutionReason};
6+
use crate::avm1::parameters::ParametersExt as _;
67
use crate::avm1::property_decl::{DeclContext, Declaration, SystemClass};
78
use crate::avm1::{Object, Value};
89
use crate::string::AvmString;
@@ -50,10 +51,10 @@ pub fn call<'gc>(
5051
func: Object<'gc>,
5152
myargs: &[Value<'gc>],
5253
) -> Result<Value<'gc>, Error<'gc>> {
53-
let this = match myargs.get(0).unwrap_or(&Value::Undefined) {
54-
Value::Undefined | Value::Null => activation.global_object(),
55-
this_val => this_val.coerce_to_object_or_bare(activation)?,
56-
};
54+
let this = myargs
55+
.try_get_object(activation, 0)?
56+
.unwrap_or_else(|| activation.global_object());
57+
5758
let empty = [];
5859
let args = match myargs.len() {
5960
0 => &empty,
@@ -82,10 +83,10 @@ pub fn apply<'gc>(
8283
func: Object<'gc>,
8384
myargs: &[Value<'gc>],
8485
) -> Result<Value<'gc>, Error<'gc>> {
85-
let this = match myargs.get(0).unwrap_or(&Value::Undefined) {
86-
Value::Undefined | Value::Null => activation.global_object(),
87-
this_val => this_val.coerce_to_object_or_bare(activation)?,
88-
};
86+
let this = myargs
87+
.try_get_object(activation, 0)?
88+
.unwrap_or_else(|| activation.global_object());
89+
8990
let args_object = myargs.get(1).cloned().unwrap_or(Value::Undefined);
9091
let length = match args_object {
9192
Value::Object(a) => a.length(activation)? as usize,

core/src/avm1/parameters.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,14 @@ pub trait ParametersExt<'gc> {
3131
}
3232

3333
/// Tries to get the value at the given index as an Object.
34-
/// The value will be coerced to an Object if it exists.
35-
#[expect(dead_code)]
34+
/// The value will be coerced to an Object if it exists and is coercible.
3635
fn try_get_object(
3736
&self,
3837
activation: &mut Activation<'_, 'gc>,
3938
index: usize,
40-
undefined_behaviour: UndefinedAs,
4139
) -> Result<Option<Object<'gc>>, Error<'gc>> {
4240
if let Some(value) = self.get_optional(index) {
43-
if undefined_behaviour == UndefinedAs::None && value == Value::Undefined {
44-
return Ok(None);
45-
}
46-
Ok(Some(value.coerce_to_object_or_bare(activation)?))
41+
value.coerce_to_object(activation)
4742
} else {
4843
Ok(None)
4944
}

tests/tests/swfs/avm1/coerce_to_object_monkeypatch/output.ruffle.txt

Lines changed: 0 additions & 137 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
num_ticks = 1
2-
known_failure = true

0 commit comments

Comments
 (0)