[njs] Modules: improved working with external prototypes.

Dmitry Volyntsev xeioex at nginx.com
Fri Jul 9 19:37:08 UTC 2021


details:   https://hg.nginx.org/njs/rev/92cb9b80cf8b
branches:  
changeset: 1674:92cb9b80cf8b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jul 09 14:01:26 2021 +0000
description:
Modules: improved working with external prototypes.

This patch avoids relying on the order in which external prototypes are
registered.  Instead, the returned proto_id is expected to be stored
somewhere.

diffstat:

 nginx/ngx_http_js_module.c   |  16 ++++++++++------
 nginx/ngx_js.h               |   3 ---
 nginx/ngx_js_fetch.c         |  14 ++++++++------
 nginx/ngx_stream_js_module.c |  12 ++++++++----
 4 files changed, 26 insertions(+), 19 deletions(-)

diffs (149 lines):

diff -r 36dd0370b121 -r 92cb9b80cf8b nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Fri Jul 09 12:03:11 2021 +0300
+++ b/nginx/ngx_http_js_module.c	Fri Jul 09 14:01:26 2021 +0000
@@ -320,6 +320,9 @@ static ngx_http_output_header_filter_pt 
 static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;
 
 
+static njs_int_t    ngx_http_js_request_proto_id;
+
+
 static njs_external_t  ngx_http_js_ext_request[] = {
 
     {
@@ -1114,7 +1117,7 @@ ngx_http_js_init_vm(ngx_http_request_t *
     }
 
     rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->request),
-                                NGX_JS_PROTO_MAIN, r, 0);
+                                ngx_http_js_request_proto_id, r, 0);
     if (rc != NJS_OK) {
         return NGX_ERROR;
     }
@@ -3141,7 +3144,7 @@ ngx_http_js_subrequest_done(ngx_http_req
     }
 
     ret = njs_vm_external_create(ctx->vm, njs_value_arg(&reply),
-                                 NGX_JS_PROTO_MAIN, r, 0);
+                                 ngx_http_js_request_proto_id, r, 0);
     if (ret != NJS_OK) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                       "js subrequest reply creation failed");
@@ -3383,7 +3386,7 @@ ngx_http_js_init_main_conf(ngx_conf_t *c
     ssize_t                n;
     ngx_fd_t               fd;
     ngx_str_t             *m, file;
-    njs_int_t              rc, proto_id;
+    njs_int_t              rc;
     njs_str_t              text, path;
     ngx_uint_t             i;
     njs_value_t           *value;
@@ -3542,9 +3545,10 @@ ngx_http_js_init_main_conf(ngx_conf_t *c
         }
     }
 
-    proto_id = njs_vm_external_prototype(jmcf->vm, ngx_http_js_ext_request,
-                                         njs_nitems(ngx_http_js_ext_request));
-    if (proto_id < 0) {
+    ngx_http_js_request_proto_id = njs_vm_external_prototype(jmcf->vm,
+                                           ngx_http_js_ext_request,
+                                           njs_nitems(ngx_http_js_ext_request));
+    if (ngx_http_js_request_proto_id < 0) {
         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                       "failed to add js request proto");
         return NGX_CONF_ERROR;
diff -r 36dd0370b121 -r 92cb9b80cf8b nginx/ngx_js.h
--- a/nginx/ngx_js.h	Fri Jul 09 12:03:11 2021 +0300
+++ b/nginx/ngx_js.h	Fri Jul 09 14:01:26 2021 +0000
@@ -19,9 +19,6 @@
 #define NGX_JS_STRING  1
 #define NGX_JS_BUFFER  2
 
-#define NGX_JS_PROTO_MAIN      0
-#define NGX_JS_PROTO_RESPONSE  1
-
 
 typedef ngx_pool_t *(*ngx_external_pool_pt)(njs_vm_t *vm, njs_external_ptr_t e);
 typedef void (*ngx_js_event_handler_pt)(njs_external_ptr_t e,
diff -r 36dd0370b121 -r 92cb9b80cf8b nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c	Fri Jul 09 12:03:11 2021 +0300
+++ b/nginx/ngx_js_fetch.c	Fri Jul 09 14:01:26 2021 +0000
@@ -319,6 +319,9 @@ static njs_external_t  ngx_js_ext_http_r
 };
 
 
+static njs_int_t    ngx_http_js_fetch_proto_id;
+
+
 njs_int_t
 ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
@@ -1189,7 +1192,7 @@ ngx_js_http_process_body(ngx_js_http_t *
 
         if (size == http->http_parse.content_length_n) {
             ret = njs_vm_external_create(http->vm, njs_value_arg(&http->reply),
-                                         NGX_JS_PROTO_RESPONSE, http, 0);
+                                         ngx_http_js_fetch_proto_id, http, 0);
             if (ret != NJS_OK) {
                 ngx_js_http_error(http, 0, "fetch object creation failed");
                 return NGX_ERROR;
@@ -2212,11 +2215,10 @@ ngx_response_js_ext_type(njs_vm_t *vm, n
 ngx_int_t
 ngx_js_fetch_init(njs_vm_t *vm, ngx_log_t *log)
 {
-    njs_int_t  proto_id;
-
-    proto_id = njs_vm_external_prototype(vm, ngx_js_ext_http_response,
-                                         njs_nitems(ngx_js_ext_http_response));
-    if (proto_id != NGX_JS_PROTO_RESPONSE) {
+    ngx_http_js_fetch_proto_id = njs_vm_external_prototype(vm,
+                                        ngx_js_ext_http_response,
+                                        njs_nitems(ngx_js_ext_http_response));
+    if (ngx_http_js_fetch_proto_id < 0) {
         ngx_log_error(NGX_LOG_EMERG, log, 0,
                       "failed to add js http.response proto");
         return NGX_ERROR;
diff -r 36dd0370b121 -r 92cb9b80cf8b nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Fri Jul 09 12:03:11 2021 +0300
+++ b/nginx/ngx_stream_js_module.c	Fri Jul 09 14:01:26 2021 +0000
@@ -419,6 +419,9 @@ static njs_vm_meta_t ngx_stream_js_metas
 static ngx_stream_filter_pt  ngx_stream_next_filter;
 
 
+static njs_int_t    ngx_stream_js_session_proto_id;
+
+
 static ngx_int_t
 ngx_stream_js_access_handler(ngx_stream_session_t *s)
 {
@@ -757,7 +760,7 @@ ngx_stream_js_init_vm(ngx_stream_session
     }
 
     rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->args[0]),
-                                NGX_JS_PROTO_MAIN, s, 0);
+                                ngx_stream_js_session_proto_id, s, 0);
     if (rc != NJS_OK) {
         return NGX_ERROR;
     }
@@ -1400,7 +1403,7 @@ ngx_stream_js_init_main_conf(ngx_conf_t 
     ssize_t                  n;
     ngx_fd_t                 fd;
     ngx_str_t               *m, file;
-    njs_int_t                rc, proto_id;
+    njs_int_t                rc;
     njs_str_t                text, path;
     ngx_uint_t               i;
     njs_value_t             *value;
@@ -1559,9 +1562,10 @@ ngx_stream_js_init_main_conf(ngx_conf_t 
         }
     }
 
-    proto_id = njs_vm_external_prototype(jmcf->vm, ngx_stream_js_ext_session,
+    ngx_stream_js_session_proto_id = njs_vm_external_prototype(jmcf->vm,
+                                         ngx_stream_js_ext_session,
                                          njs_nitems(ngx_stream_js_ext_session));
-    if (proto_id < 0) {
+    if (ngx_stream_js_session_proto_id < 0) {
         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                       "failed to add js request proto");
         return NGX_CONF_ERROR;


More information about the nginx-devel mailing list