gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash server/asobj/Math.cpp ChangeLog


From: Martin Guy
Subject: [Gnash-commit] gnash server/asobj/Math.cpp ChangeLog
Date: Sat, 17 Feb 2007 20:18:09 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Martin Guy <martinwguy> 07/02/17 20:18:09

Modified files:
        server/asobj   : Math.cpp 
        .              : ChangeLog 

Log message:
        Don't abort on missing args; return NAN

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/server/asobj/Math.cpp?cvsroot=gnash&r1=1.17&r2=1.18
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.2387&r2=1.2388

Patches:
Index: server/asobj/Math.cpp
===================================================================
RCS file: /sources/gnash/gnash/server/asobj/Math.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- server/asobj/Math.cpp       17 Feb 2007 17:11:16 -0000      1.17
+++ server/asobj/Math.cpp       17 Feb 2007 20:18:09 -0000      1.18
@@ -16,13 +16,22 @@
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 //
 
-/* $Id: Math.cpp,v 1.17 2007/02/17 17:11:16 martinwguy Exp $ */
+/* $Id: Math.cpp,v 1.18 2007/02/17 20:18:09 martinwguy Exp $ */
+
+//
+// This file implements methods of the ActionScript Math class.
+//
+// They are all static methods; there is no Math class object as such.
+//
+// TODO:
+//     min(), max() and pow(1) return NaN here; they should return
+//     Infinity, -Infinity and 1 respectively
+//
 
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
-
 #include <cmath>
 #include <string>
 #include "tu_random.h"
@@ -30,6 +39,12 @@
 #include "GMath.h"
 #include "log.h"
 
+#ifndef NAN
+// This throws a warning with some compilers. If that bugs you, use
+// static double nan_zero = 0.0; and (nan_zero/nan_zero)
+# define NAN (0.0/0.0)
+#endif
+
 using namespace std;
 
 namespace gnash {
@@ -63,15 +78,25 @@
 }
 
 //
-// Macro to wrap math library functions as method implementation functions
+// Macros to wrap C math library functions as ActionScript Math methods
 //
 
+//
 // One-argument simple functions.
+//
+// All one-argument Math functions called with no args return NaN
+//
+
 #define MATH_WRAP_FUNC1(funcname)                              \
        void    math_##funcname(const fn_call& fn)              \
        {                                                       \
+               double result;                                  \
+               if (fn.nargs < 1) result = NAN;                 \
+               else {                                          \
                double  arg = fn.arg(0).to_number();            \
-               fn.result->set_double(funcname(arg));           \
+                       result = funcname(arg);                 \
+               }                                               \
+               fn.result->set_double(result);                  \
        }
 
 #ifndef __GNUC__  //Some hacks are ugly and dirty, we call them 'fulhack'.
@@ -93,12 +118,27 @@
 MATH_WRAP_FUNC1(tan)
 
 // Two-argument functions.
+//
+// In general, two-argument functions called with no or one argument
+// return NaN.
+// This is always true for atan2, but there are the following exceptions:
+// pow(1) == 1, max() == -Infinity and min() == Infinity
+//
+// Flash's pow() is clever cos it copes with negative numbers to an integral
+// power, and can do pow(-2, -1) == -0.5 and pow(-2, -2) == 0.25.
+// Fortunately, pow() in the cmath library works the same way.
+
 #define MATH_WRAP_FUNC2_EXP(funcname, expr)                    \
        void    math_##funcname(const fn_call& fn)              \
        {                                                       \
+               double result;                                  \
+               if (fn.nargs < 2) result = NAN;                 \
+               else {                                          \
                double  arg0 = fn.arg(0).to_number();           \
                double  arg1 = fn.arg(1).to_number();           \
-               fn.result->set_double(expr);                    \
+                       result = (expr);                        \
+               }                                               \
+               fn.result->set_double(result);                  \
        }
 
 MATH_WRAP_FUNC2_EXP(atan2, (atan2(arg0, arg1)))
@@ -115,9 +155,15 @@
 
 void   math_round(const fn_call& fn)
 {
-    // round argument to nearest int.
+       // round argument to nearest int. 0.5 goes to 1 and -0.5 goes to 0
+       double result;
+
+       if (fn.nargs < 1) result = NAN;
+       else {
     double     arg0 = fn.arg(0).to_number();
-    fn.result->set_double(floor(arg0 + 0.5));
+               result = floor(arg0 + 0.5);
+       }
+       fn.result->set_double(result);
 }
        
 
@@ -139,7 +185,7 @@
        init_member("SQRT2", 1.4142135623730950488);
 
        // math methods, 1-arg
-       init_member("abs", &math_fabs);         // AS "abs" is math "fabs"
+       init_member("abs", &math_fabs);  // ActionScript "abs" is math "fabs"
        init_member("acos", &math_acos);
        init_member("asin", &math_asin);
        init_member("atan", &math_atan);
@@ -162,6 +208,4 @@
 }
 
 
-
 } // end of gnash namespace
-

Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.2387
retrieving revision 1.2388
diff -u -b -r1.2387 -r1.2388
--- ChangeLog   17 Feb 2007 17:11:16 -0000      1.2387
+++ ChangeLog   17 Feb 2007 20:18:09 -0000      1.2388
@@ -6,6 +6,7 @@
          (see http://www.brajeshwar.com/reference/as2)
        * server/asobj/Date.cpp: Document incompatabilities with FlashPlayer.
        * server/asobj/Math.cpp: Remove unused and duplicate code.
+       * server/asobj/Math.cpp: Don't abort on missing args; return NAN
 
 2007-02-16 Sandro Santilli <address@hidden>
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]