[njs] Allowing to pass arbitrary pointer to object iterator handler.

Dmitry Volyntsev xeioex at nginx.com
Tue Aug 31 13:17:41 UTC 2021


details:   https://hg.nginx.org/njs/rev/4c0b2392a5ef
branches:  
changeset: 1692:4c0b2392a5ef
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Aug 31 13:16:42 2021 +0000
description:
Allowing to pass arbitrary pointer to object iterator handler.

diffstat:

 src/njs_array.c    |   8 ++++----
 src/njs_iterator.c |  13 ++++++++-----
 src/njs_iterator.h |   2 +-
 src/njs_promise.c  |  24 +++++++++++++++---------
 4 files changed, 28 insertions(+), 19 deletions(-)

diffs (195 lines):

diff -r bf1464d84fd1 -r 4c0b2392a5ef src/njs_array.c
--- a/src/njs_array.c	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_array.c	Tue Aug 31 13:16:42 2021 +0000
@@ -2256,7 +2256,7 @@ njs_array_handler_filter(njs_vm_t *vm, n
         }
 
         if (njs_is_true(&vm->retval)) {
-            ret = njs_array_add(vm, args->array, &copy);
+            ret = njs_array_add(vm, args->data, &copy);
             if (njs_slow_path(ret != NJS_OK)) {
                 return ret;
             }
@@ -2275,7 +2275,7 @@ njs_array_handler_map(njs_vm_t *vm, njs_
     njs_array_t  *retval;
     njs_value_t  this;
 
-    retval = args->array;
+    retval = args->data;
 
     if (retval->object.fast_array) {
         njs_set_invalid(&retval->start[n]);
@@ -2431,7 +2431,7 @@ njs_array_prototype_iterator(njs_vm_t *v
             }
         }
 
-        iargs.array = array;
+        iargs.data = array;
 
         break;
     }
@@ -2481,7 +2481,7 @@ done:
     case NJS_ARRAY_FILTER:
     case NJS_ARRAY_MAP:
     default:
-        njs_set_array(&vm->retval, iargs.array);
+        njs_set_array(&vm->retval, iargs.data);
     }
 
     return NJS_OK;
diff -r bf1464d84fd1 -r 4c0b2392a5ef src/njs_iterator.c
--- a/src/njs_iterator.c	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_iterator.c	Tue Aug 31 13:16:42 2021 +0000
@@ -695,8 +695,8 @@ njs_iterator_to_array(njs_vm_t *vm, njs_
         return NULL;
     }
 
-    args.array = njs_array_alloc(vm, 1, length, 0);
-    if (njs_slow_path(args.array == NULL)) {
+    args.data = njs_array_alloc(vm, 1, length, 0);
+    if (njs_slow_path(args.data == NULL)) {
         return NULL;
     }
 
@@ -705,11 +705,11 @@ njs_iterator_to_array(njs_vm_t *vm, njs_
 
     ret = njs_object_iterate(vm, &args, njs_iterator_to_array_handler);
     if (njs_slow_path(ret == NJS_ERROR)) {
-        njs_mp_free(vm->mem_pool, args.array);
+        njs_mp_free(vm->mem_pool, args.data);
         return NULL;
     }
 
-    return args.array;
+    return args.data;
 }
 
 
@@ -717,7 +717,10 @@ static njs_int_t
 njs_iterator_to_array_handler(njs_vm_t *vm, njs_iterator_args_t *args,
     njs_value_t *value, int64_t index)
 {
-    args->array->start[index] = *value;
+    njs_array_t  *array;
+
+    array = args->data;
+    array->start[index] = *value;
 
     return NJS_OK;
 }
diff -r bf1464d84fd1 -r 4c0b2392a5ef src/njs_iterator.h
--- a/src/njs_iterator.h	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_iterator.h	Tue Aug 31 13:16:42 2021 +0000
@@ -13,7 +13,7 @@ typedef struct {
     njs_value_t     *argument;
     njs_value_t     *value;
 
-    njs_array_t     *array;
+    void            *data;
 
     int64_t         from;
     int64_t         to;
diff -r bf1464d84fd1 -r 4c0b2392a5ef src/njs_promise.c
--- a/src/njs_promise.c	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_promise.c	Tue Aug 31 13:16:42 2021 +0000
@@ -1344,8 +1344,8 @@ njs_promise_perform_all(njs_vm_t *vm, nj
         return ret;
     }
 
-    pargs->args.array = njs_array_alloc(vm, 1, length, 0);
-    if (njs_slow_path(pargs->args.array == NULL)) {
+    pargs->args.data = njs_array_alloc(vm, 1, length, 0);
+    if (njs_slow_path(pargs->args.data == NULL)) {
         return NJS_ERROR;
     }
 
@@ -1368,7 +1368,7 @@ njs_promise_perform_all(njs_vm_t *vm, nj
     if (--(*pargs->remaining) == 0) {
         njs_mp_free(vm->mem_pool, pargs->remaining);
 
-        njs_set_array(&argument, pargs->args.array);
+        njs_set_array(&argument, pargs->args.data);
 
         if (handler == njs_promise_perform_any_handler) {
             error = njs_error_alloc(vm, NJS_OBJ_TYPE_AGGREGATE_ERROR,
@@ -1398,6 +1398,7 @@ njs_promise_perform_all_handler(njs_vm_t
     njs_value_t *value, int64_t index)
 {
     njs_int_t                    ret;
+    njs_array_t                  *array;
     njs_value_t                  arguments[2], next;
     njs_function_t               *on_fulfilled;
     njs_promise_capability_t     *capability;
@@ -1408,7 +1409,8 @@ njs_promise_perform_all_handler(njs_vm_t
 
     capability = pargs->capability;
 
-    njs_set_undefined(&pargs->args.array->start[index]);
+    array = args->data;
+    njs_set_undefined(&array->start[index]);
 
     ret = njs_function_call(vm, pargs->function, pargs->constructor, value,
                             1, &next);
@@ -1429,7 +1431,7 @@ njs_promise_perform_all_handler(njs_vm_t
 
     context->already_called = 0;
     context->index = (uint32_t) index;
-    context->values = pargs->args.array;
+    context->values = pargs->args.data;
     context->capability = capability;
     context->remaining_elements = pargs->remaining;
 
@@ -1486,6 +1488,7 @@ njs_promise_perform_all_settled_handler(
     njs_value_t *value, int64_t index)
 {
     njs_int_t                    ret;
+    njs_array_t                  *array;
     njs_value_t                  arguments[2], next;
     njs_function_t               *on_fulfilled, *on_rejected;
     njs_promise_capability_t     *capability;
@@ -1496,7 +1499,8 @@ njs_promise_perform_all_settled_handler(
 
     capability = pargs->capability;
 
-    njs_set_undefined(&pargs->args.array->start[index]);
+    array = args->data;
+    njs_set_undefined(&array->start[index]);
 
     ret = njs_function_call(vm, pargs->function, pargs->constructor, value,
                             1, &next);
@@ -1514,7 +1518,7 @@ njs_promise_perform_all_settled_handler(
 
     context->already_called = 0;
     context->index = (uint32_t) index;
-    context->values = pargs->args.array;
+    context->values = pargs->args.data;
     context->capability = capability;
     context->remaining_elements = pargs->remaining;
 
@@ -1623,6 +1627,7 @@ njs_promise_perform_any_handler(njs_vm_t
     njs_value_t *value, int64_t index)
 {
     njs_int_t                    ret;
+    njs_array_t                  *array;
     njs_value_t                  arguments[2], next;
     njs_function_t               *on_rejected;
     njs_promise_capability_t     *capability;
@@ -1633,7 +1638,8 @@ njs_promise_perform_any_handler(njs_vm_t
 
     capability = pargs->capability;
 
-    njs_set_undefined(&pargs->args.array->start[index]);
+    array = pargs->args.data;
+    njs_set_undefined(&array->start[index]);
 
     ret = njs_function_call(vm, pargs->function, pargs->constructor, value, 1,
                             &next);
@@ -1654,7 +1660,7 @@ njs_promise_perform_any_handler(njs_vm_t
 
     context->already_called = 0;
     context->index = (uint32_t) index;
-    context->values = pargs->args.array;
+    context->values = pargs->args.data;
     context->capability = capability;
     context->remaining_elements = pargs->remaining;
 


More information about the nginx-devel mailing list