[njs] A fix in Array.slice() function.

Igor Sysoev igor at sysoev.ru
Wed Oct 12 16:30:48 UTC 2016


details:   http://hg.nginx.org/njs/rev/8046b999aaaf
branches:  
changeset: 198:8046b999aaaf
user:      Igor Sysoev <igor at sysoev.ru>
date:      Wed Oct 12 19:08:57 2016 +0300
description:
A fix in Array.slice() function.

diffstat:

 njs/njs_array.c          |  34 +++++++++++++++++++++-------------
 njs/test/njs_unit_test.c |  18 ++++++++++++++++++
 2 files changed, 39 insertions(+), 13 deletions(-)

diffs (74 lines):

diff -r beba11b9f2a1 -r 8046b999aaaf njs/njs_array.c
--- a/njs/njs_array.c	Tue Oct 11 20:09:18 2016 +0300
+++ b/njs/njs_array.c	Wed Oct 12 19:08:57 2016 +0300
@@ -387,21 +387,29 @@ njs_array_prototype_slice(njs_vm_t *vm, 
                 }
             }
 
-            end = length;
-
-            if (nargs > 2) {
-                end = args[2].data.u.number;
-
-                if (end < 0) {
-                    end += length;
-                }
-            }
-
-            length = end - start;
-
-            if (length < 0) {
+            if (start >= length) {
                 start = 0;
                 length = 0;
+
+            } else {
+                end = length;
+
+                if (nargs > 2) {
+                    end = args[2].data.u.number;
+
+                    if (end < 0) {
+                        end += length;
+                    }
+                }
+
+                if (length >= end) {
+                    length = end - start;
+
+                    if (length < 0) {
+                        start = 0;
+                        length = 0;
+                    }
+                }
             }
         }
     }
diff -r beba11b9f2a1 -r 8046b999aaaf njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Oct 11 20:09:18 2016 +0300
+++ b/njs/test/njs_unit_test.c	Wed Oct 12 19:08:57 2016 +0300
@@ -2260,6 +2260,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("a = [1,2]; a[100] = 100; a[100] +' '+ a.length"),
       nxt_string("100 101") },
 
+    { nxt_string("Array.prototype.slice(1)"),
+      nxt_string("") },
+
+    { nxt_string("Array.prototype.slice(1,2)"),
+      nxt_string("") },
+
+    { nxt_string("Array.prototype.pop()"),
+      nxt_string("undefined") },
+
+    { nxt_string("Array.prototype.shift()"),
+      nxt_string("undefined") },
+
+    { nxt_string("[0,1,2,3,4].slice(1,4)"),
+      nxt_string("1,2,3") },
+
+    { nxt_string("[0,1,2,3,4].slice(6,7)"),
+      nxt_string("") },
+
     { nxt_string("a = [1,2,3,4,5]; b = a.slice(3); b[0] +' '+ b[1] +' '+ b[2]"),
       nxt_string("4 5 undefined") },
 



More information about the nginx-devel mailing list