Welcome! Log In Create A New Profile

Advanced

[njs] Array.prototypes.includes() method.

October 25, 2016 11:46AM
details: http://hg.nginx.org/njs/rev/f21747d5ac83
branches:
changeset: 221:f21747d5ac83
user: Igor Sysoev <igor@sysoev.ru>
date: Tue Oct 25 17:52:18 2016 +0300
description:
Array.prototypes.includes() method.

diffstat:

njs/njs_array.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
njs/test/njs_unit_test.c | 30 ++++++++++++++++
2 files changed, 115 insertions(+), 0 deletions(-)

diffs (142 lines):

diff -r 76e57071e411 -r f21747d5ac83 njs/njs_array.c
--- a/njs/njs_array.c Mon Oct 24 19:02:31 2016 +0300
+++ b/njs/njs_array.c Tue Oct 25 17:52:18 2016 +0300
@@ -1088,6 +1088,83 @@ done:


static njs_ret_t
+njs_array_prototype_includes(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ nxt_int_t i, length;
+ njs_value_t *value, *start;
+ njs_array_t *array;
+ const njs_value_t *retval;
+
+ retval = &njs_value_false;
+
+ if (nargs < 2 || !njs_is_array(&args[0])) {
+ goto done;
+ }
+
+ array = args[0].data.u.array;
+ length = array->length;
+
+ if (length == 0) {
+ goto done;
+ }
+
+ i = 0;
+
+ if (nargs > 2) {
+ i = args[2].data.u.number;
+
+ if (i >= length) {
+ goto done;
+ }
+
+ if (i < 0) {
+ i += length;
+
+ if (i < 0) {
+ i = 0;
+ }
+ }
+ }
+
+ start = array->start;
+ value = &args[1];
+
+ if (njs_is_number(value) && njs_is_nan(value->data.u.number)) {
+
+ do {
+ value = &start[i];
+
+ if (njs_is_number(value) && njs_is_nan(value->data.u.number)) {
+ retval = &njs_value_true;
+ break;
+ }
+
+ i++;
+
+ } while (i < length);
+
+ } else {
+ do {
+ if (njs_values_strict_equal(value, &start[i])) {
+ retval = &njs_value_true;
+ break;
+ }
+
+ i++;
+
+ } while (i < length);
+ }
+
+done:
+
+ vm->retval = *retval;
+
+ return NXT_OK;
+}
+
+
+static njs_ret_t
njs_array_prototype_for_each(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
{
@@ -1804,6 +1881,14 @@ static const njs_object_prop_t njs_arra
NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG),
},

+ /* ES6. */
+ {
+ .type = NJS_METHOD,
+ .name = njs_string("includes"),
+ .value = njs_native_function(njs_array_prototype_includes, 0,
+ NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_INTEGER_ARG),
+ },
+
{
.type = NJS_METHOD,
.name = njs_string("forEach"),
diff -r 76e57071e411 -r f21747d5ac83 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Oct 24 19:02:31 2016 +0300
+++ b/njs/test/njs_unit_test.c Tue Oct 25 17:52:18 2016 +0300
@@ -2410,6 +2410,36 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("var a = [1,2,3,4,3,4]; a.lastIndexOf(3, -10)"),
nxt_string("-1") },

+ { nxt_string("[1,2,3,4].includes()"),
+ nxt_string("false") },
+
+ { nxt_string("[1,2,3,4].includes(5)"),
+ nxt_string("false") },
+
+ { nxt_string("[1,2,3,4].includes(4, 3)"),
+ nxt_string("true") },
+
+ { nxt_string("[1,2,3,4].includes(4, 4)"),
+ nxt_string("false") },
+
+ { nxt_string("[1,2,3,4,3,4].includes(3, '2')"),
+ nxt_string("true") },
+
+ { nxt_string("[1,2,3,4,3,4].includes(4, -1)"),
+ nxt_string("true") },
+
+ { nxt_string("[1,2,3,4,3,4].includes(3, -10)"),
+ nxt_string("true") },
+
+ { nxt_string("[1,2,3,NaN,3,4].includes(NaN)"),
+ nxt_string("true") },
+
+ { nxt_string("[1,2,3,4,5].includes(NaN)"),
+ nxt_string("false") },
+
+ { nxt_string("[].includes.bind(0)(0, 0)"),
+ nxt_string("false") },
+
{ nxt_string("var a = []; var s = { sum: 0 };"
"a.forEach(function(v, i, a) { this.sum += v }, s); s.sum"),
nxt_string("0") },

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Array.prototypes.includes() method.

Igor Sysoev 623 October 25, 2016 11:46AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 70
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready