[njs] Moved njs vm to the main configuration.

Roman Arutyunyan arut at nginx.com
Mon May 14 12:17:45 UTC 2018


details:   http://hg.nginx.org/njs/rev/5abcdd645d57
branches:  
changeset: 521:5abcdd645d57
user:      Roman Arutyunyan <arut at nginx.com>
date:      Tue Apr 17 17:15:27 2018 +0300
description:
Moved njs vm to the main configuration.

Previously, vm and proto objects were located in the location (server for
stream) configuration.  Since there's only one instance of these objects, they
are moved to the main configuration.

diffstat:

 nginx/ngx_http_js_module.c   |  111 ++++++++++++++++++++++++------------------
 nginx/ngx_stream_js_module.c |  107 ++++++++++++++++++++++++-----------------
 2 files changed, 126 insertions(+), 92 deletions(-)

diffs (489 lines):

diff -r 464d7e2905fc -r 5abcdd645d57 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon May 14 14:10:23 2018 +0300
+++ b/nginx/ngx_http_js_module.c	Tue Apr 17 17:15:27 2018 +0300
@@ -14,10 +14,14 @@
 
 typedef struct {
     njs_vm_t            *vm;
-    ngx_str_t            content;
     const njs_extern_t  *req_proto;
     const njs_extern_t  *res_proto;
     const njs_extern_t  *rep_proto;
+} ngx_http_js_main_conf_t;
+
+
+typedef struct {
+    ngx_str_t            content;
 } ngx_http_js_loc_conf_t;
 
 
@@ -141,6 +145,7 @@ static char *ngx_http_js_include(ngx_con
 static char *ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+static void *ngx_http_js_create_main_conf(ngx_conf_t *cf);
 static void *ngx_http_js_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_js_merge_loc_conf(ngx_conf_t *cf, void *parent,
     void *child);
@@ -151,7 +156,7 @@ static ngx_command_t  ngx_http_js_comman
     { ngx_string("js_include"),
       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
       ngx_http_js_include,
-      NGX_HTTP_LOC_CONF_OFFSET,
+      NGX_HTTP_MAIN_CONF_OFFSET,
       0,
       NULL },
 
@@ -177,7 +182,7 @@ static ngx_http_module_t  ngx_http_js_mo
     NULL,                          /* preconfiguration */
     NULL,                          /* postconfiguration */
 
-    NULL,                          /* create main configuration */
+    ngx_http_js_create_main_conf,  /* create main configuration */
     NULL,                          /* init main configuration */
 
     NULL,                          /* create server configuration */
@@ -809,14 +814,14 @@ ngx_http_js_variable(ngx_http_request_t 
 static ngx_int_t
 ngx_http_js_init_vm(ngx_http_request_t *r)
 {
-    nxt_int_t                rc;
-    nxt_str_t                exception;
-    ngx_http_js_ctx_t       *ctx;
-    ngx_pool_cleanup_t      *cln;
-    ngx_http_js_loc_conf_t  *jlcf;
-
-    jlcf = ngx_http_get_module_loc_conf(r, ngx_http_js_module);
-    if (jlcf->vm == NULL) {
+    nxt_int_t                 rc;
+    nxt_str_t                 exception;
+    ngx_http_js_ctx_t        *ctx;
+    ngx_pool_cleanup_t       *cln;
+    ngx_http_js_main_conf_t  *jmcf;
+
+    jmcf = ngx_http_get_module_main_conf(r, ngx_http_js_module);
+    if (jmcf->vm == NULL) {
         return NGX_DECLINED;
     }
 
@@ -835,7 +840,7 @@ ngx_http_js_init_vm(ngx_http_request_t *
         return NGX_OK;
     }
 
-    ctx->vm = njs_vm_clone(jlcf->vm, r);
+    ctx->vm = njs_vm_clone(jmcf->vm, r);
     if (ctx->vm == NULL) {
         return NGX_ERROR;
     }
@@ -860,13 +865,13 @@ ngx_http_js_init_vm(ngx_http_request_t *
     }
 
     rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->args[0]),
-                                jlcf->req_proto, r);
+                                jmcf->req_proto, r);
     if (rc != NXT_OK) {
         return NGX_ERROR;
     }
 
     rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->args[1]),
-                                jlcf->res_proto, r);
+                                jmcf->res_proto, r);
     if (rc != NXT_OK) {
         return NGX_ERROR;
     }
@@ -1949,10 +1954,10 @@ ngx_http_js_subrequest_done(ngx_http_req
 {
     njs_vm_event_t  vm_event = data;
 
-    nxt_int_t                ret;
-    ngx_http_js_ctx_t       *ctx;
-    njs_opaque_value_t       reply;
-    ngx_http_js_loc_conf_t  *jlcf;
+    nxt_int_t                 ret;
+    ngx_http_js_ctx_t        *ctx;
+    njs_opaque_value_t        reply;
+    ngx_http_js_main_conf_t  *jmcf;
 
     if (rc != NGX_OK || r->connection->error || r->buffered) {
         return rc;
@@ -1975,7 +1980,7 @@ ngx_http_js_subrequest_done(ngx_http_req
 
     ctx->done = 1;
 
-    jlcf = ngx_http_get_module_loc_conf(r->parent, ngx_http_js_module);
+    jmcf = ngx_http_get_module_main_conf(r, ngx_http_js_module);
 
     ctx = ngx_http_get_module_ctx(r->parent, ngx_http_js_module);
 
@@ -1991,7 +1996,7 @@ ngx_http_js_subrequest_done(ngx_http_req
     }
 
     ret = njs_vm_external_create(ctx->vm, njs_value_arg(&reply),
-                                 jlcf->rep_proto, r);
+                                 jmcf->rep_proto, r);
     if (ret != NXT_OK) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                       "js subrequest reply creation failed");
@@ -2152,7 +2157,7 @@ ngx_http_js_handle_event(ngx_http_reques
 static char *
 ngx_http_js_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_js_loc_conf_t *jlcf = conf;
+    ngx_http_js_main_conf_t *jmcf = conf;
 
     size_t                 size;
     u_char                *start, *end;
@@ -2165,7 +2170,7 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     ngx_file_info_t        fi;
     ngx_pool_cleanup_t    *cln;
 
-    if (jlcf->vm) {
+    if (jmcf->vm) {
         return "is duplicate";
     }
 
@@ -2229,8 +2234,8 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     options.backtrace = 1;
     options.ops = &ngx_http_js_ops;
 
-    jlcf->vm = njs_vm_create(&options);
-    if (jlcf->vm == NULL) {
+    jmcf->vm = njs_vm_create(&options);
+    if (jmcf->vm == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to create JS VM");
         return NGX_CONF_ERROR;
     }
@@ -2241,34 +2246,34 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
     }
 
     cln->handler = ngx_http_js_cleanup_vm;
-    cln->data = jlcf->vm;
-
-    jlcf->req_proto = njs_vm_external_prototype(jlcf->vm,
+    cln->data = jmcf->vm;
+
+    jmcf->req_proto = njs_vm_external_prototype(jmcf->vm,
                                                 &ngx_http_js_externals[0]);
-    if (jlcf->req_proto == NULL) {
+    if (jmcf->req_proto == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add request proto");
         return NGX_CONF_ERROR;
     }
 
-    jlcf->res_proto = njs_vm_external_prototype(jlcf->vm,
+    jmcf->res_proto = njs_vm_external_prototype(jmcf->vm,
                                                 &ngx_http_js_externals[1]);
-    if (jlcf->res_proto == NULL) {
+    if (jmcf->res_proto == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "failed to add response proto");
         return NGX_CONF_ERROR;
     }
 
-    jlcf->rep_proto = njs_vm_external_prototype(jlcf->vm,
+    jmcf->rep_proto = njs_vm_external_prototype(jmcf->vm,
                                                 &ngx_http_js_externals[2]);
-    if (jlcf->rep_proto == NULL) {
+    if (jmcf->rep_proto == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add reply proto");
         return NGX_CONF_ERROR;
     }
 
-    rc = njs_vm_compile(jlcf->vm, &start, end);
+    rc = njs_vm_compile(jmcf->vm, &start, end);
 
     if (rc != NJS_OK) {
-        njs_vm_retval_to_ext_string(jlcf->vm, &text);
+        njs_vm_retval_to_ext_string(jmcf->vm, &text);
 
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "%*s, included",
@@ -2346,11 +2351,11 @@ ngx_http_js_content(ngx_conf_t *cf, ngx_
 
 
 static void *
-ngx_http_js_create_loc_conf(ngx_conf_t *cf)
+ngx_http_js_create_main_conf(ngx_conf_t *cf)
 {
-    ngx_http_js_loc_conf_t  *conf;
-
-    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_js_loc_conf_t));
+    ngx_http_js_main_conf_t  *conf;
+
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_js_main_conf_t));
     if (conf == NULL) {
         return NULL;
     }
@@ -2368,18 +2373,28 @@ ngx_http_js_create_loc_conf(ngx_conf_t *
 }
 
 
+static void *
+ngx_http_js_create_loc_conf(ngx_conf_t *cf)
+{
+    ngx_http_js_loc_conf_t  *conf;
+
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_js_loc_conf_t));
+    if (conf == NULL) {
+        return NULL;
+    }
+
+    /*
+     * set by ngx_pcalloc():
+     *
+     *     conf->content = { 0, NULL };
+     */
+
+    return conf;
+}
+
+
 static char *
 ngx_http_js_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
 {
-    ngx_http_js_loc_conf_t *prev = parent;
-    ngx_http_js_loc_conf_t *conf = child;
-
-    if (conf->vm == NULL) {
-        conf->vm = prev->vm;
-        conf->req_proto = prev->req_proto;
-        conf->res_proto = prev->res_proto;
-        conf->rep_proto = prev->rep_proto;
-    }
-
     return NGX_CONF_OK;
 }
diff -r 464d7e2905fc -r 5abcdd645d57 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Mon May 14 14:10:23 2018 +0300
+++ b/nginx/ngx_stream_js_module.c	Tue Apr 17 17:15:27 2018 +0300
@@ -14,10 +14,14 @@
 
 typedef struct {
     njs_vm_t              *vm;
+    const njs_extern_t    *proto;
+} ngx_stream_js_main_conf_t;
+
+
+typedef struct {
     ngx_str_t              access;
     ngx_str_t              preread;
     ngx_str_t              filter;
-    const njs_extern_t    *proto;
 } ngx_stream_js_srv_conf_t;
 
 
@@ -75,6 +79,7 @@ static char *ngx_stream_js_include(ngx_c
     void *conf);
 static char *ngx_stream_js_set(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+static void *ngx_stream_js_create_main_conf(ngx_conf_t *cf);
 static void *ngx_stream_js_create_srv_conf(ngx_conf_t *cf);
 static char *ngx_stream_js_merge_srv_conf(ngx_conf_t *cf, void *parent,
     void *child);
@@ -86,14 +91,14 @@ static ngx_command_t  ngx_stream_js_comm
     { ngx_string("js_include"),
       NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
       ngx_stream_js_include,
-      NGX_STREAM_SRV_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("js_set"),
       NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE2,
       ngx_stream_js_set,
-      NGX_STREAM_SRV_CONF_OFFSET,
+      0,
       0,
       NULL },
 
@@ -123,29 +128,29 @@ static ngx_command_t  ngx_stream_js_comm
 
 
 static ngx_stream_module_t  ngx_stream_js_module_ctx = {
-    NULL,                          /* preconfiguration */
-    ngx_stream_js_init,            /* postconfiguration */
+    NULL,                           /* preconfiguration */
+    ngx_stream_js_init,             /* postconfiguration */
 
-    NULL,                          /* create main configuration */
-    NULL,                          /* init main configuration */
+    ngx_stream_js_create_main_conf, /* create main configuration */
+    NULL,                           /* init main configuration */
 
-    ngx_stream_js_create_srv_conf, /* create server configuration */
-    ngx_stream_js_merge_srv_conf,  /* merge server configuration */
+    ngx_stream_js_create_srv_conf,  /* create server configuration */
+    ngx_stream_js_merge_srv_conf,   /* merge server configuration */
 };
 
 
 ngx_module_t  ngx_stream_js_module = {
     NGX_MODULE_V1,
-    &ngx_stream_js_module_ctx,     /* module context */
-    ngx_stream_js_commands,        /* module directives */
-    NGX_STREAM_MODULE,             /* module type */
-    NULL,                          /* init master */
-    NULL,                          /* init module */
-    NULL,                          /* init process */
-    NULL,                          /* init thread */
-    NULL,                          /* exit thread */
-    NULL,                          /* exit process */
-    NULL,                          /* exit master */
+    &ngx_stream_js_module_ctx,      /* module context */
+    ngx_stream_js_commands,         /* module directives */
+    NGX_STREAM_MODULE,              /* module type */
+    NULL,                           /* init master */
+    NULL,                           /* init module */
+    NULL,                           /* init process */
+    NULL,                           /* init thread */
+    NULL,                           /* exit thread */
+    NULL,                           /* exit process */
+    NULL,                           /* exit master */
     NGX_MODULE_V1_PADDING
 };
 
@@ -621,14 +626,14 @@ ngx_stream_js_variable(ngx_stream_sessio
 static ngx_int_t
 ngx_stream_js_init_vm(ngx_stream_session_t *s)
 {
-    nxt_int_t                  rc;
-    nxt_str_t                  exception;
-    ngx_pool_cleanup_t        *cln;
-    ngx_stream_js_ctx_t       *ctx;
-    ngx_stream_js_srv_conf_t  *jscf;
+    nxt_int_t                   rc;
+    nxt_str_t                   exception;
+    ngx_pool_cleanup_t         *cln;
+    ngx_stream_js_ctx_t        *ctx;
+    ngx_stream_js_main_conf_t  *jmcf;
 
-    jscf = ngx_stream_get_module_srv_conf(s, ngx_stream_js_module);
-    if (jscf->vm == NULL) {
+    jmcf = ngx_stream_get_module_main_conf(s, ngx_stream_js_module);
+    if (jmcf->vm == NULL) {
         return NGX_DECLINED;
     }
 
@@ -647,7 +652,7 @@ ngx_stream_js_init_vm(ngx_stream_session
         return NGX_OK;
     }
 
-    ctx->vm = njs_vm_clone(jscf->vm, s);
+    ctx->vm = njs_vm_clone(jmcf->vm, s);
     if (ctx->vm == NULL) {
         return NGX_ERROR;
     }
@@ -671,7 +676,7 @@ ngx_stream_js_init_vm(ngx_stream_session
         return NGX_ERROR;
     }
 
-    rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->arg), jscf->proto,
+    rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->arg), jmcf->proto,
                                 s);
     if (rc != NXT_OK) {
         return NGX_ERROR;
@@ -941,7 +946,7 @@ ngx_stream_js_ext_get_code(njs_vm_t *vm,
 static char *
 ngx_stream_js_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_stream_js_srv_conf_t *jscf = conf;
+    ngx_stream_js_main_conf_t *jmcf = conf;
 
     size_t                 size;
     u_char                *start, *end;
@@ -954,7 +959,7 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     ngx_file_info_t        fi;
     ngx_pool_cleanup_t    *cln;
 
-    if (jscf->vm) {
+    if (jmcf->vm) {
         return "is duplicate";
     }
 
@@ -1017,8 +1022,8 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
 
     options.backtrace = 1;
 
-    jscf->vm = njs_vm_create(&options);
-    if (jscf->vm == NULL) {
+    jmcf->vm = njs_vm_create(&options);
+    if (jmcf->vm == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to create JS VM");
         return NGX_CONF_ERROR;
     }
@@ -1029,20 +1034,20 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
     }
 
     cln->handler = ngx_stream_js_cleanup_vm;
-    cln->data = jscf->vm;
+    cln->data = jmcf->vm;
 
-    jscf->proto = njs_vm_external_prototype(jscf->vm,
+    jmcf->proto = njs_vm_external_prototype(jmcf->vm,
                                             &ngx_stream_js_externals[0]);
 
-    if (jscf->proto == NULL) {
+    if (jmcf->proto == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "failed to add stream proto");
         return NGX_CONF_ERROR;
     }
 
-    rc = njs_vm_compile(jscf->vm, &start, end);
+    rc = njs_vm_compile(jmcf->vm, &start, end);
 
     if (rc != NJS_OK) {
-        njs_vm_retval_to_ext_string(jscf->vm, &text);
+        njs_vm_retval_to_ext_string(jmcf->vm, &text);
 
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                            "%*s, included",
@@ -1098,6 +1103,27 @@ ngx_stream_js_set(ngx_conf_t *cf, ngx_co
 
 
 static void *
+ngx_stream_js_create_main_conf(ngx_conf_t *cf)
+{
+    ngx_stream_js_srv_conf_t  *conf;
+
+    conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_js_main_conf_t));
+    if (conf == NULL) {
+        return NULL;
+    }
+
+    /*
+     * set by ngx_pcalloc():
+     *
+     *     conf->vm = NULL;
+     *     conf->proto = NULL;
+     */
+
+    return conf;
+}
+
+
+static void *
 ngx_stream_js_create_srv_conf(ngx_conf_t *cf)
 {
     ngx_stream_js_srv_conf_t  *conf;
@@ -1110,8 +1136,6 @@ ngx_stream_js_create_srv_conf(ngx_conf_t
     /*
      * set by ngx_pcalloc():
      *
-     *     conf->vm = NULL;
-     *     conf->proto = NULL;
      *     conf->access = { 0, NULL };
      *     conf->preread = { 0, NULL };
      *     conf->filter = { 0, NULL };
@@ -1127,11 +1151,6 @@ ngx_stream_js_merge_srv_conf(ngx_conf_t 
     ngx_stream_js_srv_conf_t *prev = parent;
     ngx_stream_js_srv_conf_t *conf = child;
 
-    if (conf->vm == NULL) {
-        conf->vm = prev->vm;
-        conf->proto = prev->proto;
-    }
-
     ngx_conf_merge_str_value(conf->access, prev->access, "");
     ngx_conf_merge_str_value(conf->preread, prev->preread, "");
     ngx_conf_merge_str_value(conf->filter, prev->filter, "");


More information about the nginx-devel mailing list