[njs] Fixed Array.prototype.slice() for sparse arrays.

Dmitry Volyntsev xeioex at nginx.com
Mon May 18 13:23:06 UTC 2020


details:   https://hg.nginx.org/njs/rev/80d95b2881f6
branches:  
changeset: 1389:80d95b2881f6
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon May 18 13:22:34 2020 +0000
description:
Fixed Array.prototype.slice() for sparse arrays.

diffstat:

 src/njs_array.c          |  8 +++++---
 src/test/njs_unit_test.c |  7 +++++--
 2 files changed, 10 insertions(+), 5 deletions(-)

diffs (55 lines):

diff -r 92838d964b19 -r 80d95b2881f6 src/njs_array.c
--- a/src/njs_array.c	Fri May 08 15:52:40 2020 +0000
+++ b/src/njs_array.c	Mon May 18 13:22:34 2020 +0000
@@ -686,15 +686,17 @@ njs_array_prototype_slice_copy(njs_vm_t 
 
                     /* src value may be in Array.prototype object. */
 
-                    value = &array->start[n++];
-                    ret = njs_value_property_i64(vm, this, start++, value);
+                    ret = njs_value_property_i64(vm, this, start++,
+                                                 &array->start[n]);
                     if (njs_slow_path(ret == NJS_ERROR)) {
                         return NJS_ERROR;
                     }
 
                     if (ret != NJS_OK) {
-                        njs_set_invalid(value);
+                        njs_set_invalid(&array->start[n]);
                     }
+
+                    n++;
                 }
 
                 length--;
diff -r 92838d964b19 -r 80d95b2881f6 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri May 08 15:52:40 2020 +0000
+++ b/src/test/njs_unit_test.c	Mon May 18 13:22:34 2020 +0000
@@ -4313,6 +4313,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("[0,1].slice()"),
       njs_str("0,1") },
 
+    { njs_str("[1,2,3,,,4].slice()"),
+      njs_str("1,2,3,,,4") },
+
     { njs_str("[0,1].slice(undefined)"),
       njs_str("0,1") },
 
@@ -4326,7 +4329,7 @@ static njs_unit_test_t  njs_test[] =
       njs_str("") },
 
     { njs_str("var a = [1,2,3,4,5], b = a.slice(3);"
-                 "b[0] +' '+ b[1] +' '+ b[2]"),
+              "b[0] +' '+ b[1] +' '+ b[2]"),
       njs_str("4 5 undefined") },
 
     { njs_str("var a = [1,2]; a.pop() +' '+ a.length +' '+ a"),
@@ -4336,7 +4339,7 @@ static njs_unit_test_t  njs_test[] =
       njs_str("3 3 1,2") },
 
     { njs_str("var a = [1,2], len = a.push(3,4,5);"
-                 "len +' '+ a.pop() +' '+ a"),
+              "len +' '+ a.pop() +' '+ a"),
       njs_str("5 5 1,2,3,4") },
 
     { njs_str("var x = {'0': 'a', '1': 'b', '2': 'c', 'length': 3};"


More information about the nginx-devel mailing list