Welcome! Log In Create A New Profile

Advanced

[njs] Modules: introduced common ngx_js_retval().

Dmitry Volyntsev
October 08, 2021 09:44AM
details: https://hg.nginx.org/njs/rev/5e3973c2216d
branches:
changeset: 1716:5e3973c2216d
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Fri Oct 08 13:41:00 2021 +0000
description:
Modules: introduced common ngx_js_retval().

diffstat:

nginx/ngx_http_js_module.c | 29 ++++++++++++--------------
nginx/ngx_js.c | 47 +++++++++++++++++++++++++++++++++++++++----
nginx/ngx_js.h | 2 +
nginx/ngx_stream_js_module.c | 39 +++++++++++++++++++----------------
4 files changed, 78 insertions(+), 39 deletions(-)

diffs (332 lines):

diff -r 9291aef80a73 -r 5e3973c2216d nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c Fri Oct 08 13:40:58 2021 +0000
+++ b/nginx/ngx_http_js_module.c Fri Oct 08 13:41:00 2021 +0000
@@ -50,6 +50,7 @@ typedef struct {
ngx_log_t *log;
ngx_uint_t done;
ngx_int_t status;
+ njs_opaque_value_t retval;
njs_opaque_value_t request;
njs_opaque_value_t request_body;
njs_opaque_value_t response_body;
@@ -910,7 +911,6 @@ ngx_http_js_body_filter(ngx_http_request
size_t len;
u_char *p;
ngx_int_t rc;
- njs_str_t exception;
njs_int_t ret, pending;
ngx_buf_t *b;
ngx_chain_t *out, *cl;
@@ -988,11 +988,6 @@ ngx_http_js_body_filter(ngx_http_request
3);

if (rc == NGX_ERROR) {
- njs_vm_retval_string(ctx->vm, &exception);
-
- ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %*s",
- exception.length, exception.start);
-
return NGX_ERROR;
}

@@ -1044,7 +1039,7 @@ ngx_http_js_variable_set(ngx_http_reques

ngx_int_t rc;
njs_int_t pending;
- njs_str_t value;
+ ngx_str_t value;
ngx_http_js_ctx_t *ctx;

rc = ngx_http_js_init_vm(r);
@@ -1078,15 +1073,15 @@ ngx_http_js_variable_set(ngx_http_reques
return NGX_ERROR;
}

- if (njs_vm_retval_string(ctx->vm, &value) != NJS_OK) {
+ if (ngx_js_retval(ctx->vm, &ctx->retval, &value) != NGX_OK) {
return NGX_ERROR;
}

- v->len = value.length;
+ v->len = value.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
- v->data = value.start;
+ v->data = value.data;

return NGX_OK;
}
@@ -1123,7 +1118,7 @@ static ngx_int_t
ngx_http_js_init_vm(ngx_http_request_t *r)
{
njs_int_t rc;
- njs_str_t exception;
+ ngx_str_t exception;
ngx_http_js_ctx_t *ctx;
ngx_pool_cleanup_t *cln;
ngx_http_js_main_conf_t *jmcf;
@@ -1141,6 +1136,8 @@ ngx_http_js_init_vm(ngx_http_request_t *
return NGX_ERROR;
}

+ njs_value_invalid_set(njs_value_arg(&ctx->retval));
+
ngx_http_set_ctx(r, ctx, ngx_http_js_module);
}

@@ -1164,10 +1161,10 @@ ngx_http_js_init_vm(ngx_http_request_t *
cln->data = ctx;

if (njs_vm_start(ctx->vm) == NJS_ERROR) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "js exception: %*s", exception.length, exception.start);
+ "js exception: %V", &exception);

return NGX_ERROR;
}
@@ -3403,7 +3400,7 @@ ngx_http_js_handle_vm_event(ngx_http_req
njs_value_t *args, njs_uint_t nargs)
{
njs_int_t rc;
- njs_str_t exception;
+ ngx_str_t exception;
ngx_http_js_ctx_t *ctx;

ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
@@ -3417,10 +3414,10 @@ ngx_http_js_handle_vm_event(ngx_http_req
(ngx_int_t) rc, vm_event);

if (rc == NJS_ERROR) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "js exception: %*s", exception.length, exception.start);
+ "js exception: %V", &exception);

ngx_http_finalize_request(r, NGX_ERROR);
}
diff -r 9291aef80a73 -r 5e3973c2216d nginx/ngx_js.c
--- a/nginx/ngx_js.c Fri Oct 08 13:40:58 2021 +0000
+++ b/nginx/ngx_js.c Fri Oct 08 13:41:00 2021 +0000
@@ -69,7 +69,9 @@ ngx_int_t
ngx_js_call(njs_vm_t *vm, ngx_str_t *fname, ngx_log_t *log,
njs_opaque_value_t *args, njs_uint_t nargs)
{
- njs_str_t name, exception;
+ njs_int_t ret;
+ njs_str_t name;
+ ngx_str_t exception;
njs_function_t *func;

name.start = fname->data;
@@ -82,16 +84,51 @@ ngx_js_call(njs_vm_t *vm, ngx_str_t *fna
return NGX_ERROR;
}

- if (njs_vm_call(vm, func, njs_value_arg(args), nargs) != NJS_OK) {
- njs_vm_retval_string(vm, &exception);
+ ret = njs_vm_call(vm, func, njs_value_arg(args), nargs);
+ if (ret == NJS_ERROR) {
+ ngx_js_retval(vm, NULL, &exception);

ngx_log_error(NGX_LOG_ERR, log, 0,
- "js exception: %*s", exception.length, exception.start);
+ "js exception: %V", &exception);
+
+ return NGX_ERROR;
+ }
+
+ ret = njs_vm_run(vm);
+ if (ret == NJS_ERROR) {
+ ngx_js_retval(vm, NULL, &exception);
+
+ ngx_log_error(NGX_LOG_ERR, log, 0,
+ "js exception: %V", &exception);

return NGX_ERROR;
}

- return njs_vm_run(vm);
+ return (ret == NJS_AGAIN) ? NGX_AGAIN : NGX_OK;
+}
+
+
+ngx_int_t
+ngx_js_retval(njs_vm_t *vm, njs_opaque_value_t *retval, ngx_str_t *s)
+{
+ njs_int_t ret;
+ njs_str_t str;
+
+ if (retval != NULL && njs_value_is_valid(njs_value_arg(retval))) {
+ ret = njs_vm_value_string(vm, &str, njs_value_arg(retval));
+
+ } else {
+ ret = njs_vm_retval_string(vm, &str);
+ }
+
+ if (ret != NJS_OK) {
+ return NGX_ERROR;
+ }
+
+ s->data = str.start;
+ s->len = str.length;
+
+ return NGX_OK;
}


diff -r 9291aef80a73 -r 5e3973c2216d nginx/ngx_js.h
--- a/nginx/ngx_js.h Fri Oct 08 13:40:58 2021 +0000
+++ b/nginx/ngx_js.h Fri Oct 08 13:41:00 2021 +0000
@@ -51,6 +51,8 @@ typedef ngx_ssl_t *(*ngx_external_ssl_pt

ngx_int_t ngx_js_call(njs_vm_t *vm, ngx_str_t *fname, ngx_log_t *log,
njs_opaque_value_t *args, njs_uint_t nargs);
+ngx_int_t ngx_js_retval(njs_vm_t *vm, njs_opaque_value_t *retval,
+ ngx_str_t *s);

njs_int_t ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t level);
diff -r 9291aef80a73 -r 5e3973c2216d nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c Fri Oct 08 13:40:58 2021 +0000
+++ b/nginx/ngx_stream_js_module.c Fri Oct 08 13:41:00 2021 +0000
@@ -52,6 +52,7 @@ typedef struct {

typedef struct {
njs_vm_t *vm;
+ njs_opaque_value_t retval;
njs_opaque_value_t args[3];
ngx_buf_t *buf;
ngx_chain_t **last_out;
@@ -511,7 +512,7 @@ ngx_stream_js_preread_handler(ngx_stream
static ngx_int_t
ngx_stream_js_phase_handler(ngx_stream_session_t *s, ngx_str_t *name)
{
- njs_str_t exception;
+ ngx_str_t exception;
njs_int_t ret;
ngx_int_t rc;
ngx_connection_t *c;
@@ -550,10 +551,10 @@ ngx_stream_js_phase_handler(ngx_stream_s

ret = ngx_stream_js_run_event(s, ctx, &ctx->events[NGX_JS_EVENT_UPLOAD]);
if (ret != NJS_OK) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

- ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %*s",
- exception.length, exception.start);
+ ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
+ &exception);

return NGX_ERROR;
}
@@ -583,7 +584,7 @@ static ngx_int_t
ngx_stream_js_body_filter(ngx_stream_session_t *s, ngx_chain_t *in,
ngx_uint_t from_upstream)
{
- njs_str_t exception;
+ ngx_str_t exception;
njs_int_t ret;
ngx_int_t rc;
ngx_chain_t *out, *cl, **busy;
@@ -635,10 +636,10 @@ ngx_stream_js_body_filter(ngx_stream_ses
if (event->ev != NULL) {
ret = ngx_stream_js_run_event(s, ctx, event);
if (ret != NJS_OK) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

- ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %*s",
- exception.length, exception.start);
+ ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %V",
+ &exception);

return NGX_ERROR;
}
@@ -693,7 +694,7 @@ ngx_stream_js_variable_set(ngx_stream_se

ngx_int_t rc;
njs_int_t pending;
- njs_str_t value;
+ ngx_str_t value;
ngx_stream_js_ctx_t *ctx;

rc = ngx_stream_js_init_vm(s);
@@ -727,15 +728,15 @@ ngx_stream_js_variable_set(ngx_stream_se
return NGX_ERROR;
}

- if (njs_vm_retval_string(ctx->vm, &value) != NJS_OK) {
+ if (ngx_js_retval(ctx->vm, &ctx->retval, &value) != NGX_OK) {
return NGX_ERROR;
}

- v->len = value.length;
+ v->len = value.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
- v->data = value.start;
+ v->data = value.data;

return NGX_OK;
}
@@ -772,7 +773,7 @@ static ngx_int_t
ngx_stream_js_init_vm(ngx_stream_session_t *s)
{
njs_int_t rc;
- njs_str_t exception;
+ ngx_str_t exception;
ngx_pool_cleanup_t *cln;
ngx_stream_js_ctx_t *ctx;
ngx_stream_js_main_conf_t *jmcf;
@@ -790,6 +791,8 @@ ngx_stream_js_init_vm(ngx_stream_session
return NGX_ERROR;
}

+ njs_value_invalid_set(njs_value_arg(&ctx->retval));
+
ngx_stream_set_ctx(s, ctx, ngx_stream_js_module);
}

@@ -811,10 +814,10 @@ ngx_stream_js_init_vm(ngx_stream_session
cln->data = s;

if (njs_vm_start(ctx->vm) == NJS_ERROR) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "js exception: %*s", exception.length, exception.start);
+ "js exception: %V", &exception);

return NGX_ERROR;
}
@@ -1433,7 +1436,7 @@ ngx_stream_js_handle_event(ngx_stream_se
njs_value_t *args, njs_uint_t nargs)
{
njs_int_t rc;
- njs_str_t exception;
+ ngx_str_t exception;
ngx_stream_js_ctx_t *ctx;

ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
@@ -1443,10 +1446,10 @@ ngx_stream_js_handle_event(ngx_stream_se
rc = njs_vm_run(ctx->vm);

if (rc == NJS_ERROR) {
- njs_vm_retval_string(ctx->vm, &exception);
+ ngx_js_retval(ctx->vm, NULL, &exception);

ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "js exception: %*s", exception.length, exception.start);
+ "js exception: %V", &exception);

ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Modules: introduced common ngx_js_retval().

Dmitry Volyntsev 149 October 08, 2021 09:44AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 81
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready