[njs] Array.prototype.fill() method.
Andrey Zelenkov
zelenkov at nginx.com
Fri Mar 17 15:52:29 UTC 2017
details: http://hg.nginx.org/njs/rev/5c42125bd99c
branches:
changeset: 314:5c42125bd99c
user: Andrey Zelenkov <zelenkov at nginx.com>
date: Fri Mar 17 18:18:01 2017 +0300
description:
Array.prototype.fill() method.
diffstat:
njs/njs_array.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
njs/test/njs_unit_test.c | 54 ++++++++++++++++++++++++++++++++++++
2 files changed, 126 insertions(+), 0 deletions(-)
diffs (153 lines):
diff -r 243c9c96511a -r 5c42125bd99c njs/njs_array.c
--- a/njs/njs_array.c Mon Mar 13 16:32:40 2017 +0300
+++ b/njs/njs_array.c Fri Mar 17 18:18:01 2017 +0300
@@ -1292,6 +1292,69 @@ njs_array_prototype_every_continuation(n
static njs_ret_t
+njs_array_prototype_fill(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ nxt_int_t start, end, length;
+ njs_array_t *array;
+
+ vm->retval = args[0];
+
+ if (nargs < 2 || !njs_is_array(&args[0])) {
+ return NXT_OK;
+ }
+
+ array = args[0].data.u.array;
+ length = array->length;
+
+ if (length == 0) {
+ return NXT_OK;
+ }
+
+ start = 0;
+ end = length;
+
+ if (nargs > 2) {
+ start = args[2].data.u.number;
+
+ if (start > length) {
+ start = length;
+ }
+
+ if (start < 0) {
+ start += length;
+
+ if (start < 0) {
+ start = 0;
+ }
+ }
+
+ if (nargs > 3) {
+ end = args[3].data.u.number;
+
+ if (end > length) {
+ end = length;
+ }
+
+ if (end < 0) {
+ end += length;
+
+ if (end < 0) {
+ end = 0;
+ }
+ }
+ }
+ }
+
+ while (start < end) {
+ array->start[start++] = args[1];
+ }
+
+ return NXT_OK;
+}
+
+
+static njs_ret_t
njs_array_prototype_filter(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
{
@@ -1911,6 +1974,15 @@ static const njs_object_prop_t njs_arra
njs_continuation_size(njs_array_iter_t), 0),
},
+ /* ES6. */
+ {
+ .type = NJS_METHOD,
+ .name = njs_string("fill"),
+ .value = njs_native_function(njs_array_prototype_fill, 0,
+ NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_NUMBER_ARG,
+ NJS_NUMBER_ARG),
+ },
+
{
.type = NJS_METHOD,
.name = njs_string("filter"),
diff -r 243c9c96511a -r 5c42125bd99c njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Mar 13 16:32:40 2017 +0300
+++ b/njs/test/njs_unit_test.c Fri Mar 17 18:18:01 2017 +0300
@@ -2728,6 +2728,60 @@ static njs_unit_test_t njs_test[] =
"a.every(function(v, i, a) { return v > 0 })"),
nxt_string("true") },
+ { nxt_string("[].fill(1);"),
+ nxt_string("") },
+
+ { nxt_string("[1,2,3].fill(5);"),
+ nxt_string("5,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 0);"),
+ nxt_string("5,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 1);"),
+ nxt_string("1,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 4);"),
+ nxt_string("1,2,3") },
+
+ { nxt_string("[1,2,3].fill(5, -2);"),
+ nxt_string("1,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, -3);"),
+ nxt_string("5,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, -4);"),
+ nxt_string("5,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 1, 0);"),
+ nxt_string("1,2,3") },
+
+ { nxt_string("[1,2,3].fill(5, 1, 1);"),
+ nxt_string("1,2,3") },
+
+ { nxt_string("[1,2,3].fill(5, 1, 2);"),
+ nxt_string("1,5,3") },
+
+ { nxt_string("[1,2,3].fill(5, 1, 3);"),
+ nxt_string("1,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 1, 4);"),
+ nxt_string("1,5,5") },
+
+ { nxt_string("[1,2,3].fill(5, 1, -1);"),
+ nxt_string("1,5,3") },
+
+ { nxt_string("[1,2,3].fill(5, 1, -3);"),
+ nxt_string("1,2,3") },
+
+ { nxt_string("[1,2,3].fill(5, 1, -4);"),
+ nxt_string("1,2,3") },
+
+ { nxt_string("[1,2,3].fill(\"a\", 1, 2);"),
+ nxt_string("1,a,3") },
+
+ { nxt_string("[1,2,3].fill({a:\"b\"}, 1, 2);"),
+ nxt_string("1,[object Object],3") },
+
{ nxt_string("var a = [];"
"a.filter(function(v, i, a) { return v > 1 })"),
nxt_string("") },
More information about the nginx-devel
mailing list