[njs] Fixed Array.prototype.pop() and shift() for sparse objects.

Alexander Borisov alexander.borisov at nginx.com
Wed Oct 9 15:56:20 UTC 2019


details:   https://hg.nginx.org/njs/rev/fa75ca7ed851
branches:  
changeset: 1178:fa75ca7ed851
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Wed Oct 09 18:54:57 2019 +0300
description:
Fixed Array.prototype.pop() and shift() for sparse objects.

This closes #233 issue on GitHub.

diffstat:

 src/njs_array.c          |   4 ++--
 src/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diffs (50 lines):

diff -r 6ed84cda7426 -r fa75ca7ed851 src/njs_array.c
--- a/src/njs_array.c	Wed Oct 09 18:54:54 2019 +0300
+++ b/src/njs_array.c	Wed Oct 09 18:54:57 2019 +0300
@@ -694,7 +694,7 @@ njs_array_prototype_pop(njs_vm_t *vm, nj
         njs_uint32_to_string(&index, --length);
 
         ret = njs_value_property_delete(vm, value, &index, &vm->retval);
-        if (njs_slow_path(ret != NJS_OK)) {
+        if (njs_slow_path(ret == NJS_ERROR)) {
             return ret;
         }
     }
@@ -900,7 +900,7 @@ njs_array_prototype_shift(njs_vm_t *vm, 
     njs_uint32_to_string(&index, 0);
 
     ret = njs_value_property_delete(vm, value, &index, &vm->retval);
-    if (njs_slow_path(ret != NJS_OK)) {
+    if (njs_slow_path(ret == NJS_ERROR)) {
         return ret;
     }
 
diff -r 6ed84cda7426 -r fa75ca7ed851 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Oct 09 18:54:54 2019 +0300
+++ b/src/test/njs_unit_test.c	Wed Oct 09 18:54:57 2019 +0300
@@ -4093,6 +4093,12 @@ static njs_unit_test_t  njs_test[] =
               "catch (e) {i += '; ' + e} i"),
       njs_str("1; TypeError: Cannot set property \"length\" of object which has only a getter") },
 
+    { njs_str("Array.prototype.pop.call({ length: 3 })"),
+      njs_str("undefined") },
+
+    { njs_str("var o = { length: 3 }; Array.prototype.pop.call(o); o.length"),
+      njs_str("2") },
+
     { njs_str("Array.prototype.shift()"),
       njs_str("undefined") },
 
@@ -4200,6 +4206,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var a=[0], n = 64; while(--n) {a.push(n); a.shift()}; a"),
       njs_str("1") },
 
+    { njs_str("Array.prototype.shift.call({ length: 3 })"),
+      njs_str("undefined") },
+
+    { njs_str("var o = { length: 3 }; Array.prototype.shift.call(o); o.length"),
+      njs_str("2") },
+
     { njs_str("var a = []; a.splice()"),
       njs_str("") },
 


More information about the nginx-devel mailing list