[njs] Array.filter() function.
Igor Sysoev
igor at sysoev.ru
Tue Aug 9 12:49:57 UTC 2016
details: http://hg.nginx.org/njs/rev/6e212d387b88
branches:
changeset: 145:6e212d387b88
user: Igor Sysoev <igor at sysoev.ru>
date: Tue Aug 09 14:10:29 2016 +0300
description:
Array.filter() function.
diffstat:
njs/njs_array.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++
njs/test/njs_unit_test.c | 32 ++++++++++++++++++
2 files changed, 116 insertions(+), 0 deletions(-)
diffs (171 lines):
diff -r 2f40b153a7c1 -r 6e212d387b88 njs/njs_array.c
--- a/njs/njs_array.c Tue Aug 09 14:10:21 2016 +0300
+++ b/njs/njs_array.c Tue Aug 09 14:10:29 2016 +0300
@@ -43,6 +43,8 @@ typedef struct {
*/
njs_value_t retval;
+ njs_value_t value;
+ njs_array_t *array;
uint32_t next_index;
uint32_t length;
} njs_array_iter_t;
@@ -61,6 +63,8 @@ static nxt_noinline njs_ret_t njs_array_
njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
static nxt_noinline njs_ret_t njs_array_prototype_every_cont(njs_vm_t *vm,
njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
+static njs_ret_t njs_array_prototype_filter_cont(njs_vm_t *vm,
+ njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
static nxt_noinline njs_ret_t njs_array_iterator_args(njs_vm_t *vm,
njs_value_t *args, nxt_uint_t nargs);
static nxt_noinline uint32_t njs_array_iterator_next(njs_array_t *array,
@@ -100,6 +104,25 @@ njs_array_alloc(njs_vm_t *vm, uint32_t l
}
+static njs_ret_t
+njs_array_add(njs_vm_t *vm, njs_array_t *array, njs_value_t *value)
+{
+ njs_ret_t ret;
+
+ if (array->size == array->length) {
+ ret = njs_array_realloc(vm, array, 0, array->size + 1);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ return ret;
+ }
+ }
+
+ /* GC: retain value. */
+ array->start[array->length++] = *value;
+
+ return NXT_OK;
+}
+
+
njs_ret_t
njs_array_string_add(njs_vm_t *vm, njs_array_t *array, u_char *start,
size_t size, size_t length)
@@ -904,6 +927,59 @@ njs_array_prototype_every_cont(njs_vm_t
}
+static njs_ret_t
+njs_array_prototype_filter(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ nxt_int_t ret;
+ njs_array_iter_t *iter;
+
+ ret = njs_array_iterator_args(vm, args, nargs);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ return ret;
+ }
+
+ iter = njs_continuation(vm->frame);
+ iter->u.cont.function = njs_array_prototype_filter_cont;
+ iter->retval.data.truth = 0;
+
+ iter->array = njs_array_alloc(vm, 0, NJS_ARRAY_SPARE);
+ if (nxt_slow_path(iter->array == NULL)) {
+ return NXT_ERROR;
+ }
+
+ return njs_array_prototype_filter_cont(vm, args, nargs, unused);
+}
+
+
+static njs_ret_t
+njs_array_prototype_filter_cont(njs_vm_t *vm, njs_value_t *args,
+ nxt_uint_t nargs, njs_index_t unused)
+{
+ nxt_int_t ret;
+ njs_array_iter_t *iter;
+
+ iter = njs_continuation(vm->frame);
+
+ if (njs_is_true(&iter->retval)) {
+ ret = njs_array_add(vm, iter->array, &iter->value);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ return ret;
+ }
+ }
+
+ if (iter->next_index >= args[0].data.u.array->length) {
+ vm->retval.data.u.array = iter->array;
+ vm->retval.type = NJS_ARRAY;
+ vm->retval.data.truth = 1;
+
+ return NXT_OK;
+ }
+
+ return njs_array_iterator_apply(vm, iter, args, nargs);
+}
+
+
static nxt_noinline njs_ret_t
njs_array_iterator_args(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs)
{
@@ -966,6 +1042,7 @@ njs_array_iterator_apply(njs_vm_t *vm, n
*/
array = args[0].data.u.array;
n = iter->next_index;
+ iter->value = array->start[n];
arguments[1] = array->start[n];
njs_number_set(&arguments[2], n);
@@ -1066,6 +1143,13 @@ static const njs_object_prop_t njs_arra
.value = njs_native_function(njs_array_prototype_every,
njs_continuation_size(njs_array_iter_t), 0),
},
+
+ {
+ .type = NJS_METHOD,
+ .name = njs_string("filter"),
+ .value = njs_native_function(njs_array_prototype_filter,
+ njs_continuation_size(njs_array_iter_t), 0),
+ },
};
diff -r 2f40b153a7c1 -r 6e212d387b88 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Tue Aug 09 14:10:21 2016 +0300
+++ b/njs/test/njs_unit_test.c Tue Aug 09 14:10:29 2016 +0300
@@ -2333,6 +2333,38 @@ static njs_unit_test_t njs_test[] =
"a.every(function(v, i, a) { return v > 0 })"),
nxt_string("true") },
+ { nxt_string("var a = [];"
+ "a.filter(function(v, i, a) { return v > 1 })"),
+ nxt_string("") },
+
+ { nxt_string("var a = [1,2,3,-1,5];"
+ "a.filter(function(v, i, a) { return v > 1 })"),
+ nxt_string("2,3,5") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7,8];"
+ "a.filter(function(v, i, a) { a.pop(); return v > 1 })"),
+ nxt_string("2,3,4") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7,8];"
+ "a.filter(function(v, i, a) { a.shift(); return v > 1 })"),
+ nxt_string("3,5,7") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7];"
+ "a.filter(function(v, i, a) { a.pop(); return v > 1 })"),
+ nxt_string("2,3,4") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7];"
+ "a.filter(function(v, i, a) { a.shift(); return v > 1 })"),
+ nxt_string("3,5,7") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7];"
+ "a.filter(function(v, i, a) { a[i] = v + 1; return true })"),
+ nxt_string("1,2,3,4,5,6,7") },
+
+ { nxt_string("var a = [1,2,3,4,5,6,7];"
+ "a.filter(function(v, i, a) { a[i+1] = v+10; return true })"),
+ nxt_string("1,11,21,31,41,51,61") },
+
/* Strings. */
{ nxt_string("var a = '0123456789' + '012345'"
More information about the nginx-devel
mailing list