Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions core/src/avm2/globals/Number.as
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,78 @@ package {
[API("680")]
public static const LOG10E:Number = 0.4342944819032518;

[API("680")]
[Ruffle(FastCall)]
public static native function abs(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function acos(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function asin(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function atan(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function atan2(y:Number, x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function ceil(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function cos(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function exp(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function floor(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function log(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function max(x:Number = NEGATIVE_INFINITY, y:Number = NEGATIVE_INFINITY, ...rest):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function min(x:Number = POSITIVE_INFINITY, y:Number = POSITIVE_INFINITY, ...rest):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function pow(x:Number, y:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function random():Number;

[API("680")]
[Ruffle(FastCall)]
public static native function round(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function sin(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function sqrt(x:Number):Number;

[API("680")]
[Ruffle(FastCall)]
public static native function tan(x:Number):Number;

{
prototype.toExponential = function(digits:* = 0):String {
var self:Number = this;
Expand Down
90 changes: 82 additions & 8 deletions core/src/avm2/globals/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::avm2::error::type_error;
use crate::avm2::object::Object;
use crate::avm2::value::Value;
use crate::avm2::{ClassObject, Error};
use num_traits::ToPrimitive;
use rand::Rng;

macro_rules! wrap_std {
Expand All @@ -20,14 +21,11 @@ macro_rules! wrap_std {
};
}

wrap_std!(abs, f64::abs);
wrap_std!(acos, f64::acos);
wrap_std!(asin, f64::asin);
wrap_std!(atan, f64::atan);
wrap_std!(ceil, f64::ceil);
wrap_std!(cos, f64::cos);
wrap_std!(exp, f64::exp);
wrap_std!(floor, f64::floor);
wrap_std!(log, f64::ln);
wrap_std!(sin, f64::sin);
wrap_std!(sqrt, f64::sqrt);
Expand Down Expand Up @@ -66,7 +64,10 @@ pub fn round<'gc>(
// Note that Flash Math.round always rounds toward infinity,
// unlike Rust f32::round which rounds away from zero.
let ret = (x + 0.5).floor();
Ok(ret.into())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably also return Ok(ret.into()) directly- as Adrian said, it'd be better to keep the Number -> int optimization a general optimization and implement it in a separate PR

match ret.to_i32() {
Some(num) => Ok(Value::Integer(num)),
None => Ok(Value::Number(ret)),
}
}

pub fn atan2<'gc>(
Expand All @@ -90,7 +91,9 @@ pub fn max<'gc>(
let val = arg.coerce_to_number(activation)?;
if val.is_nan() {
return Ok(f64::NAN.into());
} else if val > cur_max {
} else if val > cur_max
|| (val == cur_max && !val.is_sign_negative() && cur_max.is_sign_negative())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an oddly specific condition. Why does it have to be here? Can't it be somehow generalized? (Similarly the min one.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It handles signed zero cases. In max, positive zero takes priority, while in min, the negative zero takes priority. Rust cannot compare positive zero with negative zero, so that's why it is needed.

{
cur_max = val;
};
}
Expand All @@ -107,7 +110,9 @@ pub fn min<'gc>(
let val = arg.coerce_to_number(activation)?;
if val.is_nan() {
return Ok(f64::NAN.into());
} else if val < cur_min {
} else if val < cur_min
|| (val == cur_min && val.is_sign_negative() && !cur_min.is_sign_negative())
{
cur_min = val;
}
}
Expand All @@ -121,8 +126,16 @@ pub fn pow<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
let n = args[0].as_f64();
let p = args[1].as_f64();

Ok(f64::powf(n, p).into())
match (n, p) {
(_, _) if p.is_nan() => Ok(f64::NAN.into()),
// Special case: If p is ±Infinity and n is ±1, the result is NaN.
(1.0, _) | (-1.0, _) if p.is_infinite() => Ok(f64::NAN.into()),
// Special case: If n is -Infinity and p < 0 and p is a negative even integer, Flash Player returns -0.
(f64::NEG_INFINITY, _) if p.to_i64().is_some_and(|i| i % 2 == 0 && i < 0) => {
Ok(Value::Number(-0.0))
}
(_, _) => Ok(f64::powf(n, p).into()),
}
}

pub fn random<'gc>(
Expand All @@ -136,3 +149,64 @@ pub fn random<'gc>(
let rand = activation.context.rng.random_range(0..MAX_VAL);
Ok(((rand as f64) / (MAX_VAL as f64 + 1f64)).into())
}

pub fn abs<'gc>(
_activation: &mut Activation<'_, 'gc>,
_this: Value<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let input = args[0];
match input {
Value::Integer(i) => Ok(i.abs().into()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have dedicated Integer cases? How is that observable from AS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a pushbyte instruction in the test from_avmplus/as3/Types/Number/abs, which (if I understand it correctly) pushes a byte (and therefore an integer) to the abs function. And Ruffle does seem to separate Value::Number and Value::Integer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the test verifies (when 1 is passed or when Number(-0.0) is passed), if it returns int. In other cases, it returns Number.

Copy link
Collaborator

@adrian17 adrian17 Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should need a special handling per-method. Just like @Lord-McSweeney said, there should probably be a "if the float is integral*, generate a Value::Integer" in impl From<f64> for Value instead. (for avmplus reference, see AvmCore::doubleToAtom which does exactly that)

(*and within 1<<28 range)

IMO adding missing methods to Number and adding new logic to core Value should go into separate PRs?

Value::Number(-0.0) => Ok(Value::Integer(0)),
_ => Ok(f64::abs(input.as_f64()).into()),
}
}

pub fn ceil<'gc>(
_activation: &mut Activation<'_, 'gc>,
_this: Value<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let input = args[0].as_f64();

if input.is_nan() {
Ok(Value::Number(f64::NAN))
} else if input.is_infinite() {
Ok(Value::Number(input))
} else {
let ceiled = input.ceil();
// Special case: if input was negative and ceil result is 0, preserve -0.0
if ceiled == 0.0 && input.is_sign_negative() {
Ok(Value::Number(-0.0))
} else if ceiled >= i32::MIN as f64 && ceiled <= i32::MAX as f64 {
Ok(Value::Integer(ceiled as i32))
} else {
Ok(Value::Number(ceiled))
}
}
}

pub fn floor<'gc>(
_activation: &mut Activation<'_, 'gc>,
_this: Value<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let input = args[0];
let num = input.as_f64();

if num.is_nan() {
Ok(Value::Number(f64::NAN))
} else if num.is_infinite() {
Ok(Value::Number(num))
} else if num == 0.0 && num.is_sign_negative() {
Ok(Value::Number(-0.0))
} else {
let floored = num.floor();
if floored >= i32::MIN as f64 && floored <= i32::MAX as f64 {
Ok(Value::Integer(floored as i32))
} else {
Ok(Value::Number(floored))
}
}
}
21 changes: 21 additions & 0 deletions core/src/avm2/globals/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::avm2::value::Value;
use crate::avm2::{AvmString, Error};
use ruffle_macros::istr;

use crate::avm2::globals::math;

pub fn number_constructor<'gc>(
activation: &mut Activation<'_, 'gc>,
args: &[Value<'gc>],
Expand All @@ -33,6 +35,25 @@ pub fn call_handler<'gc>(
.into())
}

macro_rules! define_math_functions {
($($name:ident),* $(,)?) => {
$(
pub fn $name<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Value<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
math::$name(activation, this, args)
}
)*
};
}

define_math_functions!(
abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random, round, sin,
sqrt, tan
);

/// Implements `Number.toExponential`
pub fn to_exponential<'gc>(
activation: &mut Activation<'_, 'gc>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
num_ticks = 1
known_failure = true

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
2 changes: 1 addition & 1 deletion tests/tests/swfs/from_avmplus/regress/bug_551587/test.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
num_ticks = 1
known_failure = true