From 6638be8d812a58bb26377e2c9cb3d67657feb05f Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 9 Jul 2021 14:01:26 +0000 Subject: [PATCH] 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. --- 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(-) diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index 329ffd77..b9bb8fed 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -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; diff --git a/nginx/ngx_js.h b/nginx/ngx_js.h index 395c0a12..595e8d71 100644 --- a/nginx/ngx_js.h +++ b/nginx/ngx_js.h @@ -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 --git a/nginx/ngx_js_fetch.c b/nginx/ngx_js_fetch.c index 06d8a662..f86b5624 100644 --- a/nginx/ngx_js_fetch.c +++ b/nginx/ngx_js_fetch.c @@ -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; diff --git a/nginx/ngx_stream_js_module.c b/nginx/ngx_stream_js_module.c index e7b29a1d..66a54b1a 100644 --- a/nginx/ngx_stream_js_module.c +++ b/nginx/ngx_stream_js_module.c @@ -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; -- 2.47.3