]> git.kaiwu.me - njs.git/commitdiff
Modules: improved working with external prototypes.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 9 Jul 2021 14:01:26 +0000 (14:01 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 9 Jul 2021 14:01:26 +0000 (14:01 +0000)
This patch avoids relying on the order in which external prototypes are
registered.  Instead, the returned proto_id is expected to be stored
somewhere.

nginx/ngx_http_js_module.c
nginx/ngx_js.h
nginx/ngx_js_fetch.c
nginx/ngx_stream_js_module.c

index 329ffd77337b123e7edf7bc99bb2bea7be083cb0..b9bb8fed74c96e125c7d3c21a1380f0e8e1f64e9 100644 (file)
@@ -320,6 +320,9 @@ static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;
 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 *r)
     }
 
     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_request_t *r, void *data, ngx_int_t rc)
     }
 
     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 *cf, void *conf)
     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 *cf, void *conf)
         }
     }
 
-    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;
index 395c0a120acc602902ddc2c297708a3a3dfe18a9..595e8d71703f50238c8e6036999cf08ae7ec5f00 100644 (file)
@@ -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,
index 06d8a6628ac58e7dddad2db19b58e795fbaaa229..f86b56241440993266d80f84f74d58a35c863400 100644 (file)
@@ -319,6 +319,9 @@ static njs_external_t  ngx_js_ext_http_response[] = {
 };
 
 
+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 *http)
 
         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, njs_object_prop_t *prop,
 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;
index e7b29a1d52da2bc9959fe9ec7663db3504aa88b9..66a54b1a83720ca9e0d34e6dd3b0c31832ba36b2 100644 (file)
@@ -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_t *s)
     }
 
     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 *cf, void *conf)
     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 *cf, void *conf)
         }
     }
 
-    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;