From 794dc69809aa91f5c16dd5a70f895c7160ab52bb Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Thu, 23 Jul 2020 19:21:11 -0700 Subject: [PATCH] avm1: Implement isFinite --- core/src/avm1/globals.rs | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/core/src/avm1/globals.rs b/core/src/avm1/globals.rs index 2fb494523..e4936b4fd 100644 --- a/core/src/avm1/globals.rs +++ b/core/src/avm1/globals.rs @@ -2,6 +2,7 @@ use crate::avm1::activation::Activation; use crate::avm1::error::Error; use crate::avm1::function::{Executable, FunctionObject}; use crate::avm1::listeners::SystemListeners; +use crate::avm1::property::Attribute::*; use crate::avm1::{Object, ScriptObject, TObject, UpdateContext, Value}; use enumset::EnumSet; use gc_arena::Collect; @@ -54,6 +55,20 @@ pub fn random<'gc>( } } +pub fn is_finite<'gc>( + activation: &mut Activation<'_, 'gc>, + action_context: &mut UpdateContext<'_, 'gc, '_>, + _this: Object<'gc>, + args: &[Value<'gc>], +) -> Result, Error<'gc>> { + let ret = args + .get(0) + .unwrap_or(&Value::Undefined) + .coerce_to_f64(activation, action_context)? + .is_finite(); + Ok(ret.into()) +} + pub fn is_nan<'gc>( activation: &mut Activation<'_, 'gc>, action_context: &mut UpdateContext<'_, 'gc, '_>, @@ -505,6 +520,13 @@ pub fn create_globals<'gc>( )), EnumSet::empty(), ); + globals.force_set_function( + "isFinite", + is_finite, + gc_context, + DontEnum, + Some(function_proto), + ); globals.force_set_function( "isNaN", is_nan, @@ -683,6 +705,36 @@ mod tests { } ); + test_method!(is_finite, "isFinite", setup, + [19] => { + [true] => true, + [false] => true, + [10.0] => true, + [-10.0] => true, + [0.0] => true, + [std::f64::INFINITY] => false, + [std::f64::NEG_INFINITY] => false, + [std::f64::NAN] => false, + [""] => false, + ["Hello"] => false, + [" "] => false, + [" 5 "] => false, + ["0"] => true, + ["1"] => true, + ["Infinity"] => false, + ["-Infinity"] => false, + ["100a"] => false, + ["0x10"] => true, + ["0xhello"] => false, + ["0x1999999981ffffff"] => true, + ["0xUIXUIDFKHJDF012345678"] => false, + ["123e-1"] => true, + [Value::Undefined] => false, + [Value::Null] => false, + [] => false + } + ); + test_method!(number_function, "Number", setup, [5, 6] => { [true] => 1.0,