CVSROOT: /sources/gnash Module name: gnash Changes by: Sandro Santilli <strk> 07/07/13 01:13:46
Modified files: . : ChangeLog server : array.cpp array.h testsuite/actionscript.all: array.as Log message: Applied patch #6081 by [EMAIL PROTECTED]: * server/array.{cpp,h}: fix user-defined comparator for sort() operation. There's a minor failure with definition of 'this' inside the comparator. * testsuite/actionscript.all/array.as: add test for sort based on custom function. CVSWeb URLs: http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.3726&r2=1.3727 http://cvs.savannah.gnu.org/viewcvs/gnash/server/array.cpp?cvsroot=gnash&r1=1.64&r2=1.65 http://cvs.savannah.gnu.org/viewcvs/gnash/server/array.h?cvsroot=gnash&r1=1.29&r2=1.30 http://cvs.savannah.gnu.org/viewcvs/gnash/testsuite/actionscript.all/array.as?cvsroot=gnash&r1=1.19&r2=1.20 Patches: Index: ChangeLog =================================================================== RCS file: /sources/gnash/gnash/ChangeLog,v retrieving revision 1.3726 retrieving revision 1.3727 diff -u -b -r1.3726 -r1.3727 --- ChangeLog 12 Jul 2007 23:22:45 -0000 1.3726 +++ ChangeLog 13 Jul 2007 01:13:45 -0000 1.3727 @@ -1,3 +1,12 @@ +2007-07-12 <[EMAIL PROTECTED]> + + Applied patch #6081 + * server/array.{cpp,h}: fix user-defined comparator for sort() + operation. There's a minor failure with definition of 'this' + inside the comparator. + * testsuite/actionscript.all/array.as: add test for sort based + on custom function. + 2007-07-12 Sandro Santilli <[EMAIL PROTECTED]> * testsuite/libbase/Makefile.am: gnash libs must be in LDADD, not Index: server/array.cpp =================================================================== RCS file: /sources/gnash/gnash/server/array.cpp,v retrieving revision 1.64 retrieving revision 1.65 diff -u -b -r1.64 -r1.65 --- server/array.cpp 5 Jul 2007 19:03:03 -0000 1.64 +++ server/array.cpp 13 Jul 2007 01:13:46 -0000 1.65 @@ -28,6 +28,7 @@ #include "as_function.h" // for sort user-defined comparator #include "fn_call.h" #include "GnashException.h" +#include "action.h" // for call_method #include <string> #include <algorithm> @@ -119,25 +120,28 @@ { public: as_function& _comp; + as_object* _object; - AsValueFuncComparator(as_function& comparator) + AsValueFuncComparator(as_function& comparator, boost::intrusive_ptr<as_object> this_ptr) : _comp(comparator) { + _object = this_ptr.get(); } bool operator() (const as_value& a, const as_value& b) { - // Ugly, but I can't see another way to - // provide fn_call a stack to work on + as_value cmp_method(&_comp); as_environment env; + as_value ret(0); + int retval; + env.push(a); env.push(b); - - as_value ret(false); // bool value - fn_call fn(NULL, &env, 2, 0); - ret = _comp(fn); - return ( ret.to_bool() ); + ret = call_method(cmp_method, &env, _object, 2, 1); + retval = (int)ret.to_number(); + if (retval > 0) return true; + return false; } }; @@ -492,7 +496,7 @@ } void -as_array_object::sort(as_function& comparator, uint8_t flags) +as_array_object::sort(as_function& comparator, boost::intrusive_ptr<as_object> this_ptr, uint8_t flags) { // use sorted_index to use this flag @@ -501,7 +505,7 @@ // Other flags are simply NOT used // (or are them ? the descending one could be!) std::sort(elements.begin(), elements.end(), - AsValueFuncComparator(comparator)); + AsValueFuncComparator(comparator, this_ptr)); } @@ -577,25 +581,36 @@ { boost::intrusive_ptr<as_array_object> array = ensureType<as_array_object>(fn.this_ptr); - uint8_t flags; + uint8_t flags = 0; - if ( fn.nargs == 1 && fn.arg(0).is_number() ) + if ( fn.nargs == 0 ) + { + array->sort(flags); + } + else if ( fn.nargs == 1 && fn.arg(0).is_number() ) { flags=static_cast<uint8_t>(fn.arg(0).to_number()); + array->sort(flags); } - else if ( fn.nargs == 0 ) + else if ( fn.arg(0).is_as_function() ) { - flags=0; + // Get comparison function + as_function* as_func = fn.arg(0).to_as_function(); + + if ( fn.nargs == 2 && fn.arg(1).is_number() ) + { + flags=static_cast<uint8_t>(fn.arg(1).to_number()); + } + array->sort(*as_func, fn.this_ptr, flags); } else { - log_unimpl("Array.sort(comparator)"); - return as_value(); + IF_VERBOSE_ASCODING_ERRORS( + log_aserror(_("Sort called with invalid arguments.")); + ) } - array->sort(flags); return as_value(); // returns void - } static as_value Index: server/array.h =================================================================== RCS file: /sources/gnash/gnash/server/array.h,v retrieving revision 1.29 retrieving revision 1.30 diff -u -b -r1.29 -r1.30 --- server/array.h 1 Jul 2007 10:54:19 -0000 1.29 +++ server/array.h 13 Jul 2007 01:13:46 -0000 1.30 @@ -153,7 +153,7 @@ const std::vector<as_value>& replacement); /// Sort the array, using given values comparator - void sort(as_function& comparator, uint8_t flags=0); + void sort(as_function& comparator, boost::intrusive_ptr<as_object> this_ptr, uint8_t flags=0); void sort(uint8_t flags=0); Index: testsuite/actionscript.all/array.as =================================================================== RCS file: /sources/gnash/gnash/testsuite/actionscript.all/array.as,v retrieving revision 1.19 retrieving revision 1.20 diff -u -b -r1.19 -r1.20 --- testsuite/actionscript.all/array.as 5 Jul 2007 19:03:03 -0000 1.19 +++ testsuite/actionscript.all/array.as 13 Jul 2007 01:13:46 -0000 1.20 @@ -5,7 +5,7 @@ // Updated with sort functions, and to use check() macro // by Mike Carlson Feb. 14th, 2006 -rcsid="$Id: array.as,v 1.19 2007/07/05 19:03:03 strk Exp $"; +rcsid="$Id: array.as,v 1.20 2007/07/13 01:13:46 strk Exp $"; #include "check.as" @@ -122,6 +122,21 @@ check_equals ( trysortarray.toString() , "But,Different,alphabet,capitalization" ); // TODO - test sort(Array.RETURNINDEXEDARRAY) +// Test sorting using a custom comparison function +function testCmp (x,y) +{ + // Gnash fails here by *requiring* a not-null 'this_ptr' in fn_call + xcheck_equals(typeof(this), 'undefined'); + + if (x.length < y.length) { return -1; } + if (x.length > y.length) { return 1; } + return 0; +} + +check_equals ( trysortarray.toString() , "But,Different,alphabet,capitalization" ); +trysortarray.sort( testCmp ); +check_equals ( trysortarray.toString() , "But,alphabet,Different,capitalization" ); + popped=b.pop(); check ( popped == 12 ); popped=b.pop(); _______________________________________________ Gnash-commit mailing list Gnash-commit@gnu.org http://lists.gnu.org/mailman/listinfo/gnash-commit