details: http://hg.nginx.org/njs/rev/ef2b708510b1
branches:
changeset: 115:ef2b708510b1
user: Igor Sysoev <igor@sysoev.ru>
date: Wed Jun 29 13:38:20 2016 +0300
description:
Array.reverse() method.
diffstat:
njs/njs_array.c | 40 ++++++++++++++++++++++++++++++++++++++++
njs/test/njs_unit_test.c | 18 ++++++++++++++++++
2 files changed, 58 insertions(+), 0 deletions(-)
diffs (92 lines):
diff -r 41404eee5063 -r ef2b708510b1 njs/njs_array.c
--- a/njs/njs_array.c Tue Jun 28 19:28:00 2016 +0300
+++ b/njs/njs_array.c Wed Jun 29 13:38:20 2016 +0300
@@ -464,6 +464,39 @@ njs_array_prototype_shift(njs_vm_t *vm,
}
+static njs_ret_t
+njs_array_prototype_reverse(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ nxt_uint_t i, n, length;
+ njs_value_t value;
+ njs_array_t *array;
+
+ if (njs_is_array(&args[0])) {
+ array = args[0].data.u.array;
+ length = array->length;
+
+ if (length > 1) {
+ for (i = 0, n = length - 1; i < n; i++, n--) {
+ value = array->start[i];
+ array->start[i] = array->start[n];
+ array->start[n] = value;
+ }
+ }
+
+ vm->retval.data.u.array = array;
+ vm->retval.type = NJS_ARRAY;
+ vm->retval.data.truth = 1;
+
+ } else {
+ /* STUB */
+ vm->retval = args[0];
+ }
+
+ return NXT_OK;
+}
+
+
/*
* ECMAScript 5.1: try first to use object method "join", then
* use the standard built-in method Object.prototype.toString().
@@ -971,6 +1004,13 @@ static const njs_object_prop_t njs_arra
{
.type = NJS_METHOD,
+ .name = njs_string("reverse"),
+ .value = njs_native_function(njs_array_prototype_reverse, 0,
+ NJS_OBJECT_ARG),
+ },
+
+ {
+ .type = NJS_METHOD,
.name = njs_string("toString"),
.value = njs_native_function(njs_array_prototype_to_string,
NJS_CONTINUATION_SIZE, 0),
diff -r 41404eee5063 -r ef2b708510b1 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Tue Jun 28 19:28:00 2016 +0300
+++ b/njs/test/njs_unit_test.c Wed Jun 29 13:38:20 2016 +0300
@@ -2186,6 +2186,21 @@ static njs_unit_test_t njs_test[] =
"len +' '+ a +' '+ a.shift()"),
nxt_string("5 3,4,5,1,2 3") },
+ { nxt_string("var a = []; a.reverse()"),
+ nxt_string("") },
+
+ { nxt_string("var a = [1]; a.reverse()"),
+ nxt_string("1") },
+
+ { nxt_string("var a = [1,2]; a.reverse()"),
+ nxt_string("2,1") },
+
+ { nxt_string("var a = [1,2,3]; a.reverse()"),
+ nxt_string("3,2,1") },
+
+ { nxt_string("var a = [1,2,3,4]; a.reverse()"),
+ nxt_string("4,3,2,1") },
+
{ nxt_string("var a = []; var s = { sum: 0 };"
"a.forEach(function(v, i, a) { this.sum += v }, s); s.sum"),
nxt_string("0") },
@@ -2966,6 +2981,9 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("'abc'.split(/abc/)"),
nxt_string(",") },
+ { nxt_string("'0123456789'.split('').reverse().join('')"),
+ nxt_string("9876543210") },
+
/* Functions. */
{ nxt_string("function f() { } f()"),
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel