]> git.kaiwu.me - njs.git/commitdiff
Fixed Array.prototype.slice() for sparse arrays.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 18 May 2020 13:22:34 +0000 (13:22 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 18 May 2020 13:22:34 +0000 (13:22 +0000)
src/njs_array.c
src/test/njs_unit_test.c

index 13be922f08175bbba023889d5abd13c5357fd765..3bfcf4c0f552676870e657250ef644c1401856dd 100644 (file)
@@ -686,15 +686,17 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
 
                     /* 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--;
index 3642c6fc83e622e371ebd8f3403f0d077780caed..d468f0bc776a02b64d7576082a7f3b39b8099473 100644 (file)
@@ -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};"