]> git.kaiwu.me - njs.git/commitdiff
Fixed Array.prototype.pop() and shift() for sparse objects.
authorAlexander Borisov <alexander.borisov@nginx.com>
Wed, 9 Oct 2019 15:54:57 +0000 (18:54 +0300)
committerAlexander Borisov <alexander.borisov@nginx.com>
Wed, 9 Oct 2019 15:54:57 +0000 (18:54 +0300)
This closes #233 issue on GitHub.

src/njs_array.c
src/test/njs_unit_test.c

index 37459eea4bee5acdf33ac9fc78549e81929842e3..9fb178ee53cc6aedc86040aeb06e8f7e939d77d5 100644 (file)
@@ -694,7 +694,7 @@ njs_array_prototype_pop(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         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_value_t *args, njs_uint_t nargs,
     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;
     }
 
index 9d62b020f46e1ba0d52e97c351f1435156c687f1..e006e93575e0c958c19e70d70488414ba4bfd7bc 100644 (file)
@@ -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("") },