String.slice() and Function.apply()

Harri Porten porten at froglogic.com
Thu Nov 20 10:53:04 CET 2003


Hi,

almost forgot to post two patches that we applied ~2 days ago. One of them
fixes String.slice() (similar to the Array fixed before) and the other
prevents a crash when apply() is called with an invalid argument.

Harri.

-------------- next part --------------
Index: string_object.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/string_object.cpp,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -3 -p -r1.79 -r1.80
--- string_object.cpp	4 Nov 2003 07:42:54 -0000	1.79
+++ string_object.cpp	17 Nov 2003 21:19:06 -0000	1.80
@@ -396,20 +396,18 @@ Value StringProtoFuncImp::call(ExecState
   case Slice: // http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1194366 or 15.5.4.13
     {
         // The arg processing is very much like ArrayProtoFunc::Slice
-        // We return a new array
-        result = exec->interpreter()->builtinArray().construct(exec,List::empty());
         int begin = args[0].toUInt32(exec);
+        if (begin < 0)
+          begin = maxInt(begin + len, 0);
+        else
+          begin = minInt(begin, len);
         int end = len;
-        if (args[1].type() != UndefinedType)
-        {
-          end = args[1].toUInt32(exec);
-          if ( end < 0 )
-            end += len;
-        }
-        // safety tests
-        if ( begin < 0 || end < 0 || begin >= end ) {
-            result = String();
-            break;
+        if (args[1].type() != UndefinedType) {
+          end = args[1].toInteger(exec);
+          if (end < 0)
+            end = maxInt(len + end, 0);
+          else
+            end = minInt(end, len);
         }
         //printf( "Slicing from %d to %d \n", begin, end );
         result = String(s.substr(begin, end-begin));
-------------- next part --------------
diff -b -p -u -r1.44 -r1.45
--- function_object.cpp	30 Oct 2003 00:30:55 -0000	1.44
+++ function_object.cpp	17 Nov 2003 20:55:09 -0000	1.45
@@ -137,9 +137,9 @@ Value FunctionProtoFuncImp::call(ExecSta
 
     List applyArgs;
     if (!argArray.isA(NullType) && !argArray.isA(UndefinedType)) {
-      if ((argArray.isA(ObjectType) &&
-           Object::dynamicCast(argArray).inherits(&ArrayInstanceImp::info)) ||
-           Object::dynamicCast(argArray).inherits(&ArgumentsImp::info)) {
+      if (argArray.isA(ObjectType) &&
+           (Object::dynamicCast(argArray).inherits(&ArrayInstanceImp::info) ||
+            Object::dynamicCast(argArray).inherits(&ArgumentsImp::info))) {
 
         Object argArrayObj = Object::dynamicCast(argArray);
         unsigned int length = argArrayObj.get(exec,lengthPropertyName).toUInt32(exec);


More information about the Khtml-devel mailing list