-
-
Notifications
You must be signed in to change notification settings - Fork 883
avm1: Fix Object < Object comparison #6548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
617e737
0107ca8
1b63e42
40234e8
2054d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,7 +224,9 @@ impl<'gc> Value<'gc> { | |
activation: &mut Activation<'_, 'gc, '_>, | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
Ok(match self { | ||
Value::Object(object) => object.call_method("valueOf".into(), &[], activation)?, | ||
Value::Object(object) if object.as_display_object().is_none() => { | ||
object.call_method("valueOf".into(), &[], activation)? | ||
} | ||
val => val.to_owned(), | ||
}) | ||
} | ||
|
@@ -271,20 +273,35 @@ impl<'gc> Value<'gc> { | |
&self, | ||
other: Value<'gc>, | ||
activation: &mut Activation<'_, 'gc, '_>, | ||
) -> Result<Option<bool>, Error<'gc>> { | ||
) -> Result<Value<'gc>, Error<'gc>> { | ||
// If either parameter's `valueOf` results in a non-movieclip object, immediately return false. | ||
// This is the common case for objects because `Object.prototype.valueOf` returns the same object. | ||
// For example, `{} < {}` is false. | ||
let prim_self = self.to_primitive_num(activation)?; | ||
if matches!(prim_self, Value::Object(o) if o.as_display_object().is_none()) { | ||
return Ok(false.into()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like if both operands are plain objects, then both var a = { valueOf: function() { trace("in a.valueOf"); } };
var b = { valueOf: function() { trace("in b.valueOf"); } };
trace(a < b); // traces "a.valueOf", "b.valueOf"
trace(b < a); // traces "b.valueOf", "a.valueOf" If I get it right, Ruffle with this PR would bail-out early and invoke only the first
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR runs this example correctly -- the valueOfs in this case return The early-exit does happen if you add |
||
} | ||
let prim_other = other.to_primitive_num(activation)?; | ||
|
||
if let (Value::String(a), Value::String(b)) = (&prim_self, &prim_other) { | ||
return Ok(a.to_string().bytes().lt(b.to_string().bytes()).into()); | ||
if matches!(prim_other, Value::Object(o) if o.as_display_object().is_none()) { | ||
return Ok(false.into()); | ||
} | ||
|
||
let num_self = prim_self.primitive_as_number(activation); | ||
let num_other = prim_other.primitive_as_number(activation); | ||
|
||
Ok(num_self | ||
.partial_cmp(&num_other) | ||
.map(|o| o == std::cmp::Ordering::Less)) | ||
let result = match (prim_self, prim_other) { | ||
(Value::String(a), Value::String(b)) => { | ||
let a = a.to_string(); | ||
let b = b.to_string(); | ||
a.bytes().lt(b.bytes()).into() | ||
} | ||
(a, b) => { | ||
// Coerce to number and compare, with any NaN resulting in undefined. | ||
let a = a.primitive_as_number(activation); | ||
let b = b.primitive_as_number(activation); | ||
a.partial_cmp(&b).map_or(Value::Undefined, |o| { | ||
Value::Bool(o == std::cmp::Ordering::Less) | ||
}) | ||
} | ||
}; | ||
Ok(result) | ||
} | ||
|
||
/// ECMA-262 2nd edition s. 11.9.3 Abstract equality comparison algorithm | ||
|
@@ -583,19 +600,22 @@ mod test { | |
let a = Value::Number(1.0); | ||
let b = Value::Number(2.0); | ||
|
||
assert_eq!(a.abstract_lt(b, activation).unwrap(), Some(true)); | ||
assert_eq!(a.abstract_lt(b, activation).unwrap(), Value::Bool(true)); | ||
|
||
let nan = Value::Number(f64::NAN); | ||
assert_eq!(a.abstract_lt(nan, activation).unwrap(), None); | ||
assert_eq!(a.abstract_lt(nan, activation).unwrap(), Value::Undefined); | ||
|
||
let inf = Value::Number(f64::INFINITY); | ||
assert_eq!(a.abstract_lt(inf, activation).unwrap(), Some(true)); | ||
assert_eq!(a.abstract_lt(inf, activation).unwrap(), Value::Bool(true)); | ||
|
||
let neg_inf = Value::Number(f64::NEG_INFINITY); | ||
assert_eq!(a.abstract_lt(neg_inf, activation).unwrap(), Some(false)); | ||
assert_eq!( | ||
a.abstract_lt(neg_inf, activation).unwrap(), | ||
Value::Bool(false) | ||
); | ||
|
||
let zero = Value::Number(0.0); | ||
assert_eq!(a.abstract_lt(zero, activation).unwrap(), Some(false)); | ||
assert_eq!(a.abstract_lt(zero, activation).unwrap(), Value::Bool(false)); | ||
|
||
Ok(()) | ||
}); | ||
|
@@ -607,19 +627,22 @@ mod test { | |
let a = Value::Number(1.0); | ||
let b = Value::Number(2.0); | ||
|
||
assert_eq!(b.abstract_lt(a, activation).unwrap(), Some(false)); | ||
assert_eq!(b.abstract_lt(a, activation).unwrap(), Value::Bool(false)); | ||
|
||
let nan = Value::Number(f64::NAN); | ||
assert_eq!(nan.abstract_lt(a, activation).unwrap(), None); | ||
assert_eq!(nan.abstract_lt(a, activation).unwrap(), Value::Undefined); | ||
|
||
let inf = Value::Number(f64::INFINITY); | ||
assert_eq!(inf.abstract_lt(a, activation).unwrap(), Some(false)); | ||
assert_eq!(inf.abstract_lt(a, activation).unwrap(), Value::Bool(false)); | ||
|
||
let neg_inf = Value::Number(f64::NEG_INFINITY); | ||
assert_eq!(neg_inf.abstract_lt(a, activation).unwrap(), Some(true)); | ||
assert_eq!( | ||
neg_inf.abstract_lt(a, activation).unwrap(), | ||
Value::Bool(true) | ||
); | ||
|
||
let zero = Value::Number(0.0); | ||
assert_eq!(zero.abstract_lt(a, activation).unwrap(), Some(true)); | ||
assert_eq!(zero.abstract_lt(a, activation).unwrap(), Value::Bool(true)); | ||
|
||
Ok(()) | ||
}); | ||
|
@@ -631,7 +654,7 @@ mod test { | |
let a = Value::String(AvmString::new_utf8(activation.context.gc_context, "a")); | ||
let b = Value::String(AvmString::new_utf8(activation.context.gc_context, "b")); | ||
|
||
assert_eq!(a.abstract_lt(b, activation).unwrap(), Some(true)); | ||
assert_eq!(a.abstract_lt(b, activation).unwrap(), Value::Bool(true)); | ||
|
||
Ok(()) | ||
}) | ||
|
@@ -643,7 +666,7 @@ mod test { | |
let a = Value::String(AvmString::new_utf8(activation.context.gc_context, "a")); | ||
let b = Value::String(AvmString::new_utf8(activation.context.gc_context, "b")); | ||
|
||
assert_eq!(b.abstract_lt(a, activation).unwrap(), Some(false)); | ||
assert_eq!(b.abstract_lt(a, activation).unwrap(), Value::Bool(false)); | ||
|
||
Ok(()) | ||
}) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not directly related to this PR, but I think
StageObject
should be a separate enum member ofValue
rather than a "subclass" ofValue::Object
, since I have seen this special case exclusion too many times. This probably means thatStageObject
won't be able to implementTObject
anymore, so it's not trivial at all, but I still have a feeling thatStageObject
andObject
are more different than they have in common. Maybe this worth re-thinking as part of #5492.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same thought while writing this PR. This would fit nicely in with #5492.