gnash-commit
[Top][All Lists]
Advanced

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

[Gnash-commit] /srv/bzr/gnash/trunk r10284: libcore/as_value.{cpp, h}:


From: Bastiaan Jacques
Subject: [Gnash-commit] /srv/bzr/gnash/trunk r10284: libcore/as_value.{cpp, h}: Increase type safety by using templates to
Date: Sat, 15 Nov 2008 20:49:41 +0100
User-agent: Bazaar (1.5)

------------------------------------------------------------
revno: 10284
committer: Bastiaan Jacques <address@hidden>
branch nick: trunk
timestamp: Sat 2008-11-15 20:49:41 +0100
message:
  libcore/as_value.{cpp,h}: Increase type safety by using templates to
  prevent implicit conversions to bool. This allows us to declare a
  single numeric constructor. As a side-effect, the compiler will
  refuse to compile as_value(NULL) or as_value(0) until you resolve
  the ambiguity.
  
  This commit changes all uses of as_value(0) and adds tests.
modified:
  libcore/array.cpp
  libcore/array.h
  libcore/as_value.cpp
  libcore/as_value.h
  libcore/asobj/MovieClipLoader.cpp
  libcore/asobj/flash/display/BitmapData_as.cpp
  libcore/vm/Machine.cpp
  testsuite/libcore.all/AsValueTest.cpp
=== modified file 'libcore/array.cpp'
--- a/libcore/array.cpp 2008-09-24 15:46:11 +0000
+++ b/libcore/array.cpp 2008-11-15 19:49:41 +0000
@@ -363,7 +363,7 @@
     bool operator() (const as_value& a, const as_value& b)
     {
         as_value cmp_method(&_comp);
-        as_value ret(0);
+        as_value ret(0.0);
 
            std::auto_ptr< std::vector<as_value> > args ( new 
std::vector<as_value> );
            args->push_back(b);

=== modified file 'libcore/array.h'
--- a/libcore/array.h   2008-10-30 14:41:00 +0000
+++ b/libcore/array.h   2008-11-15 19:49:41 +0000
@@ -316,7 +316,7 @@
                nelem.sort(avc);
 
                if (std::adjacent_find(nelem.begin(), nelem.end(), ave) != 
nelem.end() )
-                       return as_value(0);
+                       return as_value(0.0);
 
                elements.resize(oldSize, false);
                size_t idx=0;
@@ -363,7 +363,7 @@
                std::sort(ielem.begin(), ielem.end(), avc);
 
                if (std::adjacent_find(ielem.begin(), ielem.end(), ave) != 
ielem.end() )
-                       return as_value(0);
+                       return as_value(0.0);
 
                return get_indices(ielem);
        }

=== modified file 'libcore/as_value.cpp'
--- a/libcore/as_value.cpp      2008-11-14 23:40:55 +0000
+++ b/libcore/as_value.cpp      2008-11-15 19:49:41 +0000
@@ -1772,52 +1772,10 @@
 {
 }
 
-as_value::as_value(bool val)
-       :
-       m_type(BOOLEAN),
-       _value(val)
-{
-}
-
-as_value::as_value(int val)
-       :
-       m_type(NUMBER),
-       _value(double(val))
-{
-}
-
-as_value::as_value(unsigned int val)
-       :
-       m_type(NUMBER),
-       _value(double(val))
-{
-}
-
-as_value::as_value(float val)
-       :
-       m_type(NUMBER),
-       _value(double(val))
-{
-}
-
-as_value::as_value(double val)
-       :
-       m_type(NUMBER),
-       _value(val)
-{
-}
-
-as_value::as_value(long val)
-       :
-       m_type(NUMBER),
-       _value(double(val))
-{
-}
-
-as_value::as_value(unsigned long val)
-       :
-       m_type(NUMBER),
-       _value(double(val))
+as_value::as_value(double num)
+       :
+       m_type(NUMBER),
+       _value(num)
 {
 }
 
@@ -1828,6 +1786,7 @@
        set_as_object(obj);
 }
 
+
 /// Chad: Document this
 as_value::as_value(asNamespace &)
 {

=== modified file 'libcore/as_value.h'
--- a/libcore/as_value.h        2008-11-14 23:40:55 +0000
+++ b/libcore/as_value.h        2008-11-15 19:49:41 +0000
@@ -146,36 +146,25 @@
        /// Construct an UNDEFINED value
        as_value();
 
-       /// Copy-construct a STRING value 
-       as_value(const as_value& v);
+       /// Copy constructor.
+       as_value(const as_value& value);
 
        /// Construct a STRING value 
        as_value(const char* str);
-
-       /// Construct a STRING value 
        as_value(const std::string& str);
 
        /// Construct a BOOLEAN value
-       as_value(bool val);
-
-       /// Construct a NUMBER value
-       as_value(int val);
-
-       /// Construct a NUMBER value
-       as_value(unsigned int val);
-
-       /// Construct a NUMBER value
-       as_value(float val);
+       template <typename T>
+       as_value(T val, typename boost::enable_if<boost::is_same<bool, T> 
>::type* dummy = 0)
+               : m_type(BOOLEAN),
+                 _value(val)
+       {
+               UNUSED(dummy);
+       }
 
        /// Construct a NUMBER value
        as_value(double val);
 
-       /// Construct a NUMBER value
-       as_value(long val);
-       
-       /// Construct a NUMBER value
-       as_value(unsigned long val);
-
        /// Chad: Document this
        as_value(asNamespace &);
 

=== modified file 'libcore/asobj/MovieClipLoader.cpp'
--- a/libcore/asobj/MovieClipLoader.cpp 2008-10-28 15:32:20 +0000
+++ b/libcore/asobj/MovieClipLoader.cpp 2008-11-15 19:49:41 +0000
@@ -209,7 +209,7 @@
                // TODO: find semantic of last argument
                as_value met("onLoadError");
                as_value arg1("Failed to load movie or jpeg");
-               as_value arg2(0);
+               as_value arg2(0.0);
                callMethod(NSV::PROP_BROADCAST_MESSAGE, met, targetVal, arg1, 
arg2);
 
                return false;
@@ -234,7 +234,7 @@
 
        // Dispatch onLoadComplete
        callMethod(NSV::PROP_BROADCAST_MESSAGE, as_value("onLoadComplete"), 
targetVal,
-               as_value(0)); // TODO: find semantic of last arg
+               as_value(0.0)); // TODO: find semantic of last arg
 
        /// This event must be dispatched when actions
        /// in first frame of loaded clip have been executed.

=== modified file 'libcore/asobj/flash/display/BitmapData_as.cpp'
--- a/libcore/asobj/flash/display/BitmapData_as.cpp     2008-09-20 09:19:09 
+0000
+++ b/libcore/asobj/flash/display/BitmapData_as.cpp     2008-11-15 19:49:41 
+0000
@@ -522,8 +522,8 @@
 
        boost::intrusive_ptr<as_object> obj = init_Rectangle_instance();
 
-       obj->set_member(NSV::PROP_X, 0);
-       obj->set_member(NSV::PROP_Y, 0);
+       obj->set_member(NSV::PROP_X, 0.0);
+       obj->set_member(NSV::PROP_Y, 0.0);
        obj->set_member(NSV::PROP_WIDTH, ptr->getWidth());
        obj->set_member(NSV::PROP_HEIGHT, ptr->getHeight());
 

=== modified file 'libcore/vm/Machine.cpp'
--- a/libcore/vm/Machine.cpp    2008-11-14 23:40:55 +0000
+++ b/libcore/vm/Machine.cpp    2008-11-15 19:49:41 +0000
@@ -959,7 +959,7 @@
                {
                        mStack.top(0).set_bool(false);
                        mFrame.value(oindex).set_null();
-                       mFrame.value(iindex) = 0;
+                       mFrame.value(iindex) = 0.0;
                }
                break;
        }

=== modified file 'testsuite/libcore.all/AsValueTest.cpp'
--- a/testsuite/libcore.all/AsValueTest.cpp     2008-10-28 15:32:20 +0000
+++ b/testsuite/libcore.all/AsValueTest.cpp     2008-11-15 19:49:41 +0000
@@ -57,6 +57,7 @@
 static void test_el();
 static void test_obj();
 static void test_isnan();
+static void test_conversion();
 
 // Enable the display of memory allocation and timing data
 static bool memdebug = false;
@@ -118,7 +119,68 @@
     test_isnan();
     test_el();
     test_obj();
-}
+    test_conversion();
+   
+}
+
+void
+test_bool(as_value boolval)
+{
+    if (boolval.is_bool()) {
+        runtest.pass("as_value(bool)");
+    } else {
+        runtest.fail("as_value(bool)");
+    }
+}
+
+void
+test_int(as_value val)
+{
+    if (val.is_number()) {
+        runtest.pass("as_value(int)");
+    } else {
+        runtest.fail("as_value(int)");
+    }
+}
+
+void
+test_string(as_value val)
+{
+    if (val.is_string()) {
+        runtest.pass("as_value(string)");
+    } else {
+        runtest.fail("as_value(string)");
+    }
+}
+
+
+typedef enum {
+    ONE = 0,
+    TWO = 1,
+    THREE = 2
+} enumbers;
+
+void
+test_conversion()
+{
+    test_bool(true);
+    test_bool(false);
+    test_int(5);
+    test_int(1);
+    test_int(double(0));
+    test_int(0.0);
+
+    test_int(THREE);
+    test_int(ONE);
+    test_int(TWO);
+
+    test_string(std::string("lar"));
+
+    test_string("lar");
+
+    
+}
+
 
 void
 test_el()


reply via email to

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