[njs] Passing all args to njs_vm_create() through njs_vm_opt_t struct.

Dmitry Volyntsev xeioex at nginx.com
Thu Jul 6 16:08:36 UTC 2017


details:   http://hg.nginx.org/njs/rev/57bd01817edb
branches:  
changeset: 383:57bd01817edb
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jul 06 19:07:41 2017 +0300
description:
Passing all args to njs_vm_create() through njs_vm_opt_t struct.

diffstat:

 nginx/ngx_http_js_module.c   |  10 ++++++----
 nginx/ngx_stream_js_module.c |  10 ++++++----
 njs/njscript.c               |  18 +++++++++---------
 njs/njscript.h               |   9 +++++++--
 njs/test/njs_unit_test.c     |  20 ++++++++++++--------
 5 files changed, 40 insertions(+), 27 deletions(-)

diffs (215 lines):

diff -r f8f7540383f2 -r 57bd01817edb nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Wed Jun 28 15:31:36 2017 +0300
+++ b/nginx/ngx_http_js_module.c	Thu Jul 06 19:07:41 2017 +0300
@@ -1231,9 +1231,9 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     ngx_str_t             *value, file;
     nxt_int_t              rc;
     nxt_str_t              text, ext;
+    njs_vm_opt_t           options;
     nxt_lvlhsh_t           externals;
     ngx_file_info_t        fi;
-    njs_vm_shared_t       *shared;
     ngx_pool_cleanup_t    *cln;
     nxt_mem_cache_pool_t  *mcp;
 
@@ -1309,8 +1309,6 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     cln->handler = ngx_http_js_cleanup_mem_cache_pool;
     cln->data = mcp;
 
-    shared = NULL;
-
     nxt_lvlhsh_init(&externals);
 
     if (njs_vm_external_add(&externals, mcp, 0, ngx_http_js_externals,
@@ -1321,7 +1319,11 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
         return NGX_CONF_ERROR;
     }
 
-    jlcf->vm = njs_vm_create(mcp, &shared, &externals);
+    options.mcp = mcp;
+    options.shared = NULL;
+    options.externals = &externals;
+
+    jlcf->vm = njs_vm_create(&options);
     if (jlcf->vm == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to create JS VM");
         return NGX_CONF_ERROR;
diff -r f8f7540383f2 -r 57bd01817edb nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Wed Jun 28 15:31:36 2017 +0300
+++ b/nginx/ngx_stream_js_module.c	Thu Jul 06 19:07:41 2017 +0300
@@ -941,9 +941,9 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     ngx_str_t             *value, file;
     nxt_int_t              rc;
     nxt_str_t              text, ext;
+    njs_vm_opt_t           options;
     nxt_lvlhsh_t           externals;
     ngx_file_info_t        fi;
-    njs_vm_shared_t       *shared;
     ngx_pool_cleanup_t    *cln;
     nxt_mem_cache_pool_t  *mcp;
 
@@ -1019,8 +1019,6 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     cln->handler = ngx_stream_js_cleanup_mem_cache_pool;
     cln->data = mcp;
 
-    shared = NULL;
-
     nxt_lvlhsh_init(&externals);
 
     if (njs_vm_external_add(&externals, mcp, 0, ngx_stream_js_externals,
@@ -1031,7 +1029,11 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
         return NGX_CONF_ERROR;
     }
 
-    jscf->vm = njs_vm_create(mcp, &shared, &externals);
+    options.mcp = mcp;
+    options.shared = NULL;
+    options.externals = &externals;
+
+    jscf->vm = njs_vm_create(&options);
     if (jscf->vm == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to create JS VM");
         return NGX_CONF_ERROR;
diff -r f8f7540383f2 -r 57bd01817edb njs/njscript.c
--- a/njs/njscript.c	Wed Jun 28 15:31:36 2017 +0300
+++ b/njs/njscript.c	Thu Jul 06 19:07:41 2017 +0300
@@ -99,13 +99,15 @@ const nxt_mem_proto_t  njs_array_mem_pro
 
 
 njs_vm_t *
-njs_vm_create(nxt_mem_cache_pool_t *mcp, njs_vm_shared_t **shared,
-    nxt_lvlhsh_t *externals)
+njs_vm_create(njs_vm_opt_t *options)
 {
     njs_vm_t              *vm;
     nxt_int_t             ret;
+    nxt_mem_cache_pool_t  *mcp;
     njs_regexp_pattern_t  *pattern;
 
+    mcp = options->mcp;
+
     if (mcp == NULL) {
         mcp = nxt_mem_cache_pool_create(&njs_vm_mem_cache_pool_proto, NULL,
                                         NULL, 2 * nxt_pagesize(), 128, 512, 16);
@@ -124,8 +126,8 @@ njs_vm_create(nxt_mem_cache_pool_t *mcp,
             return NULL;
         }
 
-        if (shared != NULL && *shared != NULL) {
-            vm->shared = *shared;
+        if (options->shared != NULL) {
+            vm->shared = options->shared;
 
         } else {
             vm->shared = nxt_mem_cache_zalloc(mcp, sizeof(njs_vm_shared_t));
@@ -133,9 +135,7 @@ njs_vm_create(nxt_mem_cache_pool_t *mcp,
                 return NULL;
             }
 
-            if (shared != NULL) {
-                *shared = vm->shared;
-            }
+            options->shared = vm->shared;
 
             nxt_lvlhsh_init(&vm->shared->keywords_hash);
 
@@ -162,8 +162,8 @@ njs_vm_create(nxt_mem_cache_pool_t *mcp,
 
         nxt_lvlhsh_init(&vm->values_hash);
 
-        if (externals != NULL) {
-            vm->externals_hash = *externals;
+        if (options->externals != NULL) {
+            vm->externals_hash = *options->externals;
         }
 
         vm->trace.level = NXT_LEVEL_TRACE;
diff -r f8f7540383f2 -r 57bd01817edb njs/njscript.h
--- a/njs/njscript.h	Wed Jun 28 15:31:36 2017 +0300
+++ b/njs/njscript.h	Thu Jul 06 19:07:41 2017 +0300
@@ -65,6 +65,12 @@ struct njs_external_s {
     uintptr_t                       data;
 };
 
+typedef struct {
+    nxt_lvlhsh_t                    *externals;
+    njs_vm_shared_t                 *shared;
+    nxt_mem_cache_pool_t            *mcp;
+} njs_vm_opt_t;
+
 
 #define NJS_OK                      NXT_OK
 #define NJS_ERROR                   NXT_ERROR
@@ -79,8 +85,7 @@ NXT_EXPORT nxt_int_t njs_vm_external_add
 NXT_EXPORT nxt_int_t njs_vm_external(njs_vm_t *vm, njs_opaque_value_t *object,
     nxt_str_t *property, njs_opaque_value_t *value);
 
-NXT_EXPORT njs_vm_t *njs_vm_create(nxt_mem_cache_pool_t *mcp,
-    njs_vm_shared_t **shared, nxt_lvlhsh_t *externals);
+NXT_EXPORT njs_vm_t *njs_vm_create(njs_vm_opt_t *options);
 NXT_EXPORT void njs_vm_destroy(njs_vm_t *vm);
 
 NXT_EXPORT nxt_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end);
diff -r f8f7540383f2 -r 57bd01817edb njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Jun 28 15:31:36 2017 +0300
+++ b/njs/test/njs_unit_test.c	Thu Jul 06 19:07:41 2017 +0300
@@ -8121,8 +8121,8 @@ njs_unit_test(nxt_bool_t disassemble)
     nxt_str_t             s;
     nxt_uint_t            i;
     nxt_bool_t            success;
+    njs_vm_opt_t          options;
     nxt_lvlhsh_t          externals;
-    njs_vm_shared_t       *shared;
     njs_unit_test_req     r;
     nxt_mem_cache_pool_t  *mcp;
 
@@ -8133,8 +8133,6 @@ njs_unit_test(nxt_bool_t disassemble)
     (void) putenv((char *) "TZ=Pacific/Chatham");
     tzset();
 
-    shared = NULL;
-
     mcp = nxt_mem_cache_pool_create(&njs_mem_cache_pool_proto, NULL, NULL,
                                     2 * nxt_pagesize(), 128, 512, 16);
     if (nxt_slow_path(mcp == NULL)) {
@@ -8157,7 +8155,11 @@ njs_unit_test(nxt_bool_t disassemble)
                (int) njs_test[i].script.length, njs_test[i].script.start);
         fflush(stdout);
 
-        vm = njs_vm_create(mcp, &shared, &externals);
+        options.mcp = mcp;
+        options.shared = NULL;
+        options.externals = &externals;
+
+        vm = njs_vm_create(&options);
         if (vm == NULL) {
             return NXT_ERROR;
         }
@@ -8235,13 +8237,11 @@ njs_unit_test_benchmark(nxt_str_t *scrip
     nxt_uint_t            i;
     nxt_bool_t            success;
     nxt_lvlhsh_t          externals;
+    njs_vm_opt_t          options;
     struct rusage         usage;
-    njs_vm_shared_t       *shared;
     njs_unit_test_req     r;
     nxt_mem_cache_pool_t  *mcp;
 
-    shared = NULL;
-
     mcp = nxt_mem_cache_pool_create(&njs_mem_cache_pool_proto, NULL, NULL,
                                     2 * nxt_pagesize(), 128, 512, 16);
     if (nxt_slow_path(mcp == NULL)) {
@@ -8258,7 +8258,11 @@ njs_unit_test_benchmark(nxt_str_t *scrip
         return NXT_ERROR;
     }
 
-    vm = njs_vm_create(mcp, &shared, &externals);
+    options.mcp = mcp;
+    options.shared = NULL;
+    options.externals = &externals;
+
+    vm = njs_vm_create(&options);
     if (vm == NULL) {
         return NXT_ERROR;
     }


More information about the nginx-devel mailing list