From 8bad014bbff38bb82fe271f6eaa49d223723dfa8 Mon Sep 17 00:00:00 2001 From: Alexander Borisov Date: Wed, 9 Oct 2019 18:54:57 +0300 Subject: [PATCH] Fixed Array.prototype.pop() and shift() for sparse objects. This closes #233 issue on GitHub. --- src/njs_array.c | 4 ++-- src/test/njs_unit_test.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/njs_array.c b/src/njs_array.c index 37459eea..9fb178ee 100644 --- a/src/njs_array.c +++ b/src/njs_array.c @@ -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; } diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 9d62b020..e006e935 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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("") }, -- 2.47.3