[njs] Added njs_vm_array_alloc() and njs_vm_array_push() public API.

2019-10-23 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/5e136f9a5954
branches:  
changeset: 1196:5e136f9a5954
user:  Dmitry Volyntsev 
date:  Wed Oct 23 14:42:38 2019 +0300
description:
Added njs_vm_array_alloc() and njs_vm_array_push() public API.

diffstat:

 src/njs.h|   4 
 src/njs_vm.c |  39 +++
 2 files changed, 43 insertions(+), 0 deletions(-)

diffs (63 lines):

diff -r 47cdd4680fc2 -r 5e136f9a5954 src/njs.h
--- a/src/njs.h Tue Oct 22 19:58:52 2019 +0300
+++ b/src/njs.h Wed Oct 23 14:42:38 2019 +0300
@@ -305,6 +305,10 @@ NJS_EXPORT njs_int_t njs_vm_object_alloc
 NJS_EXPORT njs_value_t *njs_vm_object_prop(njs_vm_t *vm,
 const njs_value_t *value, const njs_str_t *key);
 
+NJS_EXPORT njs_int_t njs_vm_array_alloc(njs_vm_t *vm, njs_value_t *retval,
+uint32_t spare);
+NJS_EXPORT njs_value_t *njs_vm_array_push(njs_vm_t *vm, njs_value_t *value);
+
 NJS_EXPORT njs_int_t njs_vm_json_parse(njs_vm_t *vm, njs_value_t *args,
 njs_uint_t nargs);
 NJS_EXPORT njs_int_t njs_vm_json_stringify(njs_vm_t *vm, njs_value_t *args,
diff -r 47cdd4680fc2 -r 5e136f9a5954 src/njs_vm.c
--- a/src/njs_vm.c  Tue Oct 22 19:58:52 2019 +0300
+++ b/src/njs_vm.c  Wed Oct 23 14:42:38 2019 +0300
@@ -938,6 +938,45 @@ done:
 }
 
 
+njs_int_t
+njs_vm_array_alloc(njs_vm_t *vm, njs_value_t *retval, uint32_t spare)
+{
+njs_array_t  *array;
+
+array = njs_array_alloc(vm, 0, spare);
+
+if (njs_slow_path(array == NULL)) {
+return NJS_ERROR;
+}
+
+njs_set_array(retval, array);
+
+return NJS_OK;
+}
+
+
+njs_value_t *
+njs_vm_array_push(njs_vm_t *vm, njs_value_t *value)
+{
+njs_int_tret;
+njs_array_t  *array;
+
+if (njs_slow_path(!njs_is_array(value))) {
+njs_type_error(vm, "njs_vm_array_push() argument is not array");
+return NULL;
+}
+
+array = njs_array(value);
+
+ret = njs_array_expand(vm, array, 0, 1);
+if (njs_slow_path(ret != NJS_OK)) {
+return NULL;
+}
+
+return &array->start[array->length++];
+}
+
+
 njs_value_t *
 njs_vm_object_prop(njs_vm_t *vm, const njs_value_t *value, const njs_str_t 
*key)
 {
___
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel


[njs] Refactoring iteration over external objects.

2019-10-23 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/9e327cd3a33e
branches:  
changeset: 1197:9e327cd3a33e
user:  Dmitry Volyntsev 
date:  Wed Oct 23 14:42:38 2019 +0300
description:
Refactoring iteration over external objects.

Previously, two callbacks were required to support
array-like iteration for external objects (foreach, next).

Instead using only one callback (keys) to simplify.

diffstat:

 nginx/ngx_http_js_module.c   |  242 ++
 nginx/ngx_stream_js_module.c |   13 --
 src/njs.h|8 +-
 src/njs_extern.c |3 +-
 src/njs_extern.h |3 +-
 src/njs_json.c   |8 +-
 src/njs_shell.c  |6 -
 src/njs_value.c  |   42 +++
 src/njs_vmcode.c |   94 +---
 src/test/njs_unit_test.c |   61 --
 10 files changed, 187 insertions(+), 293 deletions(-)

diffs (truncated from 1094 to 1000 lines):

diff -r 5e136f9a5954 -r 9e327cd3a33e nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.cWed Oct 23 14:42:38 2019 +0300
+++ b/nginx/ngx_http_js_module.cWed Oct 23 14:42:38 2019 +0300
@@ -36,12 +36,6 @@ typedef struct {
 
 
 typedef struct {
-ngx_list_part_t *part;
-ngx_uint_t   item;
-} ngx_http_js_table_entry_t;
-
-
-typedef struct {
 ngx_http_request_t  *request;
 njs_vm_event_t   vm_event;
 void*unused;
@@ -62,10 +56,8 @@ static void ngx_http_js_cleanup_vm(void 
 
 static njs_int_t ngx_http_js_ext_get_string(njs_vm_t *vm, njs_value_t *value,
 void *obj, uintptr_t data);
-static njs_int_t ngx_http_js_ext_foreach_header(njs_vm_t *vm, void *obj,
-void *next, uintptr_t data);
-static njs_int_t ngx_http_js_ext_next_header(njs_vm_t *vm, njs_value_t *value,
-void *obj, void *next);
+static njs_int_t ngx_http_js_ext_keys_header(njs_vm_t *vm, void *obj,
+njs_value_t *keys, uintptr_t data);
 static ngx_table_elt_t *ngx_http_js_get_header(ngx_list_part_t *part,
 u_char *data, size_t len);
 static njs_int_t ngx_http_js_ext_get_header_out(njs_vm_t *vm,
@@ -74,8 +66,8 @@ static njs_int_t ngx_http_js_ext_set_hea
 uintptr_t data, njs_str_t *value);
 static njs_int_t ngx_http_js_ext_delete_header_out(njs_vm_t *vm, void *obj,
 uintptr_t data, njs_bool_t delete);
-static njs_int_t ngx_http_js_ext_foreach_header_out(njs_vm_t *vm, void *obj,
-void *next); /*FIXME*/
+static njs_int_t ngx_http_js_ext_keys_header_out(njs_vm_t *vm, void *obj,
+njs_value_t *keys); /*FIXME*/
 static njs_int_t ngx_http_js_ext_get_status(njs_vm_t *vm, njs_value_t *value,
 void *obj, uintptr_t data);
 static njs_int_t ngx_http_js_ext_set_status(njs_vm_t *vm, void *obj,
@@ -108,14 +100,12 @@ static njs_int_t ngx_http_js_ext_get_req
 njs_value_t *value, void *obj, uintptr_t data);
 static njs_int_t ngx_http_js_ext_get_header_in(njs_vm_t *vm, njs_value_t 
*value,
 void *obj, uintptr_t data);
-static njs_int_t ngx_http_js_ext_foreach_header_in(njs_vm_t *vm, void *obj,
-void *next); /*FIXME*/
+static njs_int_t ngx_http_js_ext_keys_header_in(njs_vm_t *vm, void *obj,
+njs_value_t *keys); /*FIXME*/
 static njs_int_t ngx_http_js_ext_get_arg(njs_vm_t *vm, njs_value_t *value,
 void *obj, uintptr_t data);
-static njs_int_t ngx_http_js_ext_foreach_arg(njs_vm_t *vm, void *obj,
-void *next);
-static njs_int_t ngx_http_js_ext_next_arg(njs_vm_t *vm, njs_value_t *value,
-void *obj, void *next);
+static njs_int_t ngx_http_js_ext_keys_arg(njs_vm_t *vm, void *obj,
+njs_value_t *keys);
 static njs_int_t ngx_http_js_ext_get_variable(njs_vm_t *vm, njs_value_t *value,
 void *obj, uintptr_t data);
 static njs_int_t ngx_http_js_ext_set_variable(njs_vm_t *vm, void *obj,
@@ -229,7 +219,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   offsetof(ngx_http_request_t, uri) },
 
 { njs_str("method"),
@@ -241,7 +230,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   offsetof(ngx_http_request_t, method_name) },
 
 { njs_str("httpVersion"),
@@ -253,7 +241,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   0 },
 
 { njs_str("remoteAddress"),
@@ -265,7 +252,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   0 },
 
 { njs_str("parent"),
@@ -277,7 +263,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   0 },
 
 { njs_str("requestBody"),
@@ -289,7 +274,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   0 },
 
 { njs_str("responseBody"),
@@ -301,7 +285,6 @@ static njs_external_t  ngx_http_js_ext_r
   NULL,
   NULL,
   NULL,
-  NULL,
   0 },
 
 { njs_str("headersIn"),
@@ -311,8 +294,7 @@ static njs_external_t  ngx_http_js_ext_r