[njs] Usage of njs_opaque_value_t is refactored in public API.

Dmitry Volyntsev xeioex at nginx.com
Fri May 4 17:55:21 UTC 2018


details:   http://hg.nginx.org/njs/rev/d0e244bc8760
branches:  
changeset: 518:d0e244bc8760
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 04 19:53:19 2018 +0300
description:
Usage of njs_opaque_value_t is refactored in public API.

diffstat:

 nginx/ngx_http_js_module.c   |  23 +++++++++++++----------
 nginx/ngx_stream_js_module.c |   9 +++++----
 njs/njs.c                    |  13 ++++++-------
 njs/njs.h                    |  20 ++++++++++++++------
 njs/njs_event.h              |   2 +-
 njs/njs_extern.c             |  12 +++---------
 njs/njs_shell.c              |   2 +-
 njs/test/njs_unit_test.c     |   8 ++++----
 8 files changed, 47 insertions(+), 42 deletions(-)

diffs (358 lines):

diff -r ed4721d09f60 -r d0e244bc8760 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Fri May 04 19:25:15 2018 +0300
+++ b/nginx/ngx_http_js_module.c	Fri May 04 19:53:19 2018 +0300
@@ -134,7 +134,7 @@ static void ngx_http_js_clear_timer(njs_
     njs_host_event_t event);
 static void ngx_http_js_timer_handler(ngx_event_t *ev);
 static void ngx_http_js_handle_event(ngx_http_request_t *r,
-    njs_vm_event_t vm_event, njs_opaque_value_t *args, nxt_uint_t nargs);
+    njs_vm_event_t vm_event, njs_value_t *args, nxt_uint_t nargs);
 
 static char *ngx_http_js_include(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
@@ -666,7 +666,7 @@ ngx_http_js_content_event_handler(ngx_ht
         return;
     }
 
-    if (njs_vm_call(ctx->vm, func, ctx->args, 2) != NJS_OK) {
+    if (njs_vm_call(ctx->vm, func, njs_value_arg(ctx->args), 2) != NJS_OK) {
         njs_vm_retval_to_ext_string(ctx->vm, &exception);
 
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -776,7 +776,7 @@ ngx_http_js_variable(ngx_http_request_t 
 
     pending = njs_vm_pending(ctx->vm);
 
-    if (njs_vm_call(ctx->vm, func, ctx->args, 2) != NJS_OK) {
+    if (njs_vm_call(ctx->vm, func, njs_value_arg(ctx->args), 2) != NJS_OK) {
         njs_vm_retval_to_ext_string(ctx->vm, &exception);
 
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -859,12 +859,14 @@ ngx_http_js_init_vm(ngx_http_request_t *
         return NGX_ERROR;
     }
 
-    rc = njs_vm_external_create(ctx->vm, &ctx->args[0], jlcf->req_proto, r);
+    rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->args[0]),
+                                jlcf->req_proto, r);
     if (rc != NXT_OK) {
         return NGX_ERROR;
     }
 
-    rc = njs_vm_external_create(ctx->vm, &ctx->args[1], jlcf->res_proto, r);
+    rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->args[1]),
+                                jlcf->res_proto, r);
     if (rc != NXT_OK) {
         return NGX_ERROR;
     }
@@ -1670,7 +1672,7 @@ ngx_http_js_ext_get_response(njs_vm_t *v
 
     ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
 
-    njs_vm_retval_set(ctx->vm, &ctx->args[1]);
+    njs_vm_retval_set(ctx->vm, njs_value_arg(&ctx->args[1]));
 
     return NJS_OK;
 }
@@ -1988,7 +1990,8 @@ ngx_http_js_subrequest_done(ngx_http_req
         return NGX_ERROR;
     }
 
-    ret = njs_vm_external_create(ctx->vm, &reply, jlcf->rep_proto, r);
+    ret = njs_vm_external_create(ctx->vm, njs_value_arg(&reply),
+                                 jlcf->rep_proto, r);
     if (ret != NXT_OK) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                       "js subrequest reply creation failed");
@@ -1996,7 +1999,7 @@ ngx_http_js_subrequest_done(ngx_http_req
         return NGX_ERROR;
     }
 
-    ngx_http_js_handle_event(r->parent, vm_event, &reply, 1);
+    ngx_http_js_handle_event(r->parent, vm_event, njs_value_arg(&reply), 1);
 
     return NGX_OK;
 }
@@ -2019,7 +2022,7 @@ ngx_http_js_ext_get_parent(njs_vm_t *vm,
         return NJS_ERROR;
     }
 
-    njs_vm_retval_set(ctx->vm, &ctx->args[0]);
+    njs_vm_retval_set(ctx->vm, njs_value_arg(&ctx->args[0]));
 
     return NJS_OK;
 }
@@ -2119,7 +2122,7 @@ ngx_http_js_timer_handler(ngx_event_t *e
 
 static void
 ngx_http_js_handle_event(ngx_http_request_t *r, njs_vm_event_t vm_event,
-    njs_opaque_value_t *args, nxt_uint_t nargs)
+    njs_value_t *args, nxt_uint_t nargs)
 {
     njs_ret_t           rc;
     nxt_str_t           exception;
diff -r ed4721d09f60 -r d0e244bc8760 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Fri May 04 19:25:15 2018 +0300
+++ b/nginx/ngx_stream_js_module.c	Fri May 04 19:53:19 2018 +0300
@@ -400,7 +400,7 @@ ngx_stream_js_phase_handler(ngx_stream_s
         return NGX_ERROR;
     }
 
-    if (njs_vm_call(ctx->vm, func, &ctx->arg, 1) != NJS_OK) {
+    if (njs_vm_call(ctx->vm, func, njs_value_arg(&ctx->arg), 1) != NJS_OK) {
         njs_vm_retval_to_ext_string(ctx->vm, &exception);
 
         ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %*s",
@@ -487,7 +487,7 @@ ngx_stream_js_body_filter(ngx_stream_ses
     while (in) {
         ctx->buf = in->buf;
 
-        if (njs_vm_call(ctx->vm, func, &ctx->arg, 1) != NJS_OK) {
+        if (njs_vm_call(ctx->vm, func, njs_value_arg(&ctx->arg), 1) != NJS_OK) {
             njs_vm_retval_to_ext_string(ctx->vm, &exception);
 
             ngx_log_error(NGX_LOG_ERR, c->log, 0, "js exception: %*s",
@@ -588,7 +588,7 @@ ngx_stream_js_variable(ngx_stream_sessio
 
     pending = njs_vm_pending(ctx->vm);
 
-    if (njs_vm_call(ctx->vm, func, &ctx->arg, 1) != NJS_OK) {
+    if (njs_vm_call(ctx->vm, func, njs_value_arg(&ctx->arg), 1) != NJS_OK) {
         njs_vm_retval_to_ext_string(ctx->vm, &exception);
 
         ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
@@ -671,7 +671,8 @@ ngx_stream_js_init_vm(ngx_stream_session
         return NGX_ERROR;
     }
 
-    rc = njs_vm_external_create(ctx->vm, &ctx->arg, jscf->proto, s);
+    rc = njs_vm_external_create(ctx->vm, njs_value_arg(&ctx->arg), jscf->proto,
+                                s);
     if (rc != NXT_OK) {
         return NGX_ERROR;
     }
diff -r ed4721d09f60 -r d0e244bc8760 njs/njs.c
--- a/njs/njs.c	Fri May 04 19:25:15 2018 +0300
+++ b/njs/njs.c	Fri May 04 19:53:19 2018 +0300
@@ -444,7 +444,7 @@ njs_vm_init(njs_vm_t *vm)
 
 
 nxt_int_t
-njs_vm_call(njs_vm_t *vm, njs_function_t *function, njs_opaque_value_t *args,
+njs_vm_call(njs_vm_t *vm, njs_function_t *function, njs_value_t *args,
     nxt_uint_t nargs)
 {
     u_char       *current;
@@ -460,8 +460,7 @@ njs_vm_call(njs_vm_t *vm, njs_function_t
 
     this = (njs_value_t *) &njs_value_void;
 
-    ret = njs_function_frame(vm, function, this,
-                             (njs_value_t *) args, nargs, 0);
+    ret = njs_function_frame(vm, function, this, args, nargs, 0);
     if (nxt_slow_path(ret != NXT_OK)) {
         return ret;
     }
@@ -532,7 +531,7 @@ njs_vm_pending(njs_vm_t *vm)
 
 nxt_int_t
 njs_vm_post_event(njs_vm_t *vm, njs_vm_event_t vm_event,
-    njs_opaque_value_t *args, nxt_uint_t nargs)
+    njs_value_t *args, nxt_uint_t nargs)
 {
     njs_event_t  *event;
 
@@ -541,12 +540,12 @@ njs_vm_post_event(njs_vm_t *vm, njs_vm_e
     if (nargs != 0 && !event->posted) {
         event->nargs = nargs;
         event->args = nxt_mem_cache_alloc(vm->mem_cache_pool,
-                                          sizeof(njs_opaque_value_t) * nargs);
+                                          sizeof(njs_value_t) * nargs);
         if (nxt_slow_path(event->args == NULL)) {
             return NJS_ERROR;
         }
 
-        memcpy(event->args, args, sizeof(njs_opaque_value_t) * nargs);
+        memcpy(event->args, args, sizeof(njs_value_t) * nargs);
     }
 
     if (!event->posted) {
@@ -655,7 +654,7 @@ njs_vm_retval(njs_vm_t *vm)
 
 
 nxt_noinline void
-njs_vm_retval_set(njs_vm_t *vm, njs_opaque_value_t *value)
+njs_vm_retval_set(njs_vm_t *vm, njs_value_t *value)
 {
     vm->retval = *(njs_value_t *) value;
 }
diff -r ed4721d09f60 -r d0e244bc8760 njs/njs.h
--- a/njs/njs.h	Fri May 04 19:25:15 2018 +0300
+++ b/njs/njs.h	Fri May 04 19:53:19 2018 +0300
@@ -30,6 +30,11 @@ typedef struct njs_extern_s         njs_
 typedef struct njs_function_s       njs_function_t;
 typedef struct njs_vm_shared_s      njs_vm_shared_t;
 
+/*
+ * njs_opaque_value_t is the external storage type for native njs_value_t type.
+ * sizeof(njs_opaque_value_t) == sizeof(njs_value_t).
+ */
+
 typedef struct {
     uint64_t                        filler[2];
 } njs_opaque_value_t;
@@ -42,6 +47,9 @@ typedef struct {
 #define njs_value_assign(dst, src)                                            \
     *((njs_opaque_value_t *) dst) = *((njs_opaque_value_t *) src);
 
+#define njs_value_arg(val) ((njs_value_t *) val)
+
+
 #define njs_vm_error(vm, fmt, ...)                                            \
     njs_value_error_set(vm, njs_vm_retval(vm), fmt, ##__VA_ARGS__)
 
@@ -147,7 +155,7 @@ NXT_EXPORT void njs_vm_destroy(njs_vm_t 
 NXT_EXPORT nxt_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end);
 NXT_EXPORT njs_vm_t *njs_vm_clone(njs_vm_t *vm, njs_external_ptr_t external);
 NXT_EXPORT nxt_int_t njs_vm_call(njs_vm_t *vm, njs_function_t *function,
-    njs_opaque_value_t *args, nxt_uint_t nargs);
+    njs_value_t *args, nxt_uint_t nargs);
 
 NXT_EXPORT njs_vm_event_t njs_vm_add_event(njs_vm_t *vm,
     njs_function_t *function, njs_host_event_t host_ev,
@@ -155,23 +163,23 @@ NXT_EXPORT njs_vm_event_t njs_vm_add_eve
 NXT_EXPORT void njs_vm_del_event(njs_vm_t *vm, njs_vm_event_t vm_event);
 NXT_EXPORT nxt_int_t njs_vm_pending(njs_vm_t *vm);
 NXT_EXPORT nxt_int_t njs_vm_post_event(njs_vm_t *vm, njs_vm_event_t vm_event,
-    njs_opaque_value_t *args, nxt_uint_t nargs);
+    njs_value_t *args, nxt_uint_t nargs);
 
 NXT_EXPORT nxt_int_t njs_vm_run(njs_vm_t *vm);
 
 NXT_EXPORT const njs_extern_t *njs_vm_external_prototype(njs_vm_t *vm,
     njs_external_t *external);
 NXT_EXPORT nxt_int_t njs_vm_external_create(njs_vm_t *vm,
-        njs_opaque_value_t *value, const njs_extern_t *proto, void *object);
+    njs_value_t *value, const njs_extern_t *proto, void *object);
 NXT_EXPORT nxt_int_t njs_vm_external_bind(njs_vm_t *vm,
-    const nxt_str_t *var_name, njs_opaque_value_t *value);
+    const nxt_str_t *var_name, njs_value_t *value);
 
 NXT_EXPORT void njs_disassembler(njs_vm_t *vm);
 NXT_EXPORT nxt_array_t *njs_vm_completions(njs_vm_t *vm, nxt_str_t *expression);
 
 NXT_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, nxt_str_t *name);
 NXT_EXPORT njs_value_t *njs_vm_retval(njs_vm_t *vm);
-NXT_EXPORT void njs_vm_retval_set(njs_vm_t *vm, njs_opaque_value_t *value);
+NXT_EXPORT void njs_vm_retval_set(njs_vm_t *vm, njs_value_t *value);
 
 NXT_EXPORT u_char * njs_string_alloc(njs_vm_t *vm, njs_value_t *value,
     uint32_t size, uint32_t length);
@@ -184,7 +192,7 @@ NXT_EXPORT nxt_int_t njs_value_string_co
 NXT_EXPORT njs_ret_t njs_vm_value_to_ext_string(njs_vm_t *vm, nxt_str_t *dst,
     const njs_value_t *src, nxt_uint_t handle_exception);
 NXT_EXPORT njs_ret_t njs_vm_retval_to_ext_string(njs_vm_t *vm,
-        nxt_str_t *retval);
+    nxt_str_t *retval);
 
 NXT_EXPORT void njs_vm_memory_error(njs_vm_t *vm);
 
diff -r ed4721d09f60 -r d0e244bc8760 njs/njs_event.h
--- a/njs/njs_event.h	Fri May 04 19:25:15 2018 +0300
+++ b/njs/njs_event.h	Fri May 04 19:53:19 2018 +0300
@@ -17,7 +17,7 @@
 
 typedef struct {
     njs_function_t        *function;
-    njs_opaque_value_t    *args;
+    njs_value_t           *args;
     nxt_uint_t            nargs;
     njs_host_event_t      host_event;
     njs_event_destructor  destructor;
diff -r ed4721d09f60 -r d0e244bc8760 njs/njs_extern.c
--- a/njs/njs_extern.c	Fri May 04 19:25:15 2018 +0300
+++ b/njs/njs_extern.c	Fri May 04 19:53:19 2018 +0300
@@ -150,11 +150,10 @@ njs_vm_external_prototype(njs_vm_t *vm, 
 
 
 nxt_int_t
-njs_vm_external_create(njs_vm_t *vm, njs_opaque_value_t *value,
+njs_vm_external_create(njs_vm_t *vm, njs_value_t *ext_val,
     const njs_extern_t *proto,  void *object)
 {
-    void         *obj;
-    njs_value_t  *ext_val;
+    void  *obj;
 
     if (nxt_slow_path(proto == NULL)) {
         return NXT_ERROR;
@@ -168,8 +167,6 @@ njs_vm_external_create(njs_vm_t *vm, njs
 
     memcpy(obj, &object, sizeof(void *));
 
-    ext_val = (njs_value_t *) value;
-
     ext_val->type = NJS_EXTERNAL;
     ext_val->data.truth = 1;
     ext_val->external.proto = proto;
@@ -181,15 +178,12 @@ njs_vm_external_create(njs_vm_t *vm, njs
 
 nxt_int_t
 njs_vm_external_bind(njs_vm_t *vm, const nxt_str_t *var_name,
-    njs_opaque_value_t *val)
+    njs_value_t *value)
 {
     nxt_int_t           ret;
-    njs_value_t         *value;
     njs_extern_value_t  *ev;
     nxt_lvlhsh_query_t  lhq;
 
-    value = (njs_value_t *) val;
-
     if (nxt_slow_path(!njs_is_external(value))) {
         return NXT_ERROR;
     }
diff -r ed4721d09f60 -r d0e244bc8760 njs/njs_shell.c
--- a/njs/njs_shell.c	Fri May 04 19:25:15 2018 +0300
+++ b/njs/njs_shell.c	Fri May 04 19:53:19 2018 +0300
@@ -193,8 +193,8 @@ static nxt_int_t
 njs_externals_init(njs_vm_t *vm)
 {
     nxt_uint_t          ret;
+    njs_value_t         *value;
     const njs_extern_t  *proto;
-    njs_opaque_value_t  *value;
 
     static const nxt_str_t name = nxt_string_value("console");
 
diff -r ed4721d09f60 -r d0e244bc8760 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri May 04 19:25:15 2018 +0300
+++ b/njs/test/njs_unit_test.c	Fri May 04 19:53:19 2018 +0300
@@ -9533,7 +9533,7 @@ njs_unit_test_create_external(njs_vm_t *
 {
     nxt_int_t            ret;
     nxt_str_t            uri;
-    njs_opaque_value_t   *value;
+    njs_value_t          *value;
     njs_unit_test_req_t  *r, *sr;
 
     if (nargs > 1) {
@@ -9737,15 +9737,15 @@ njs_externals_init(njs_vm_t *vm)
         requests[i].mem_cache_pool = vm->mem_cache_pool;
         requests[i].proto = proto;
 
-        ret = njs_vm_external_create(vm, &requests[i].value, proto,
-                                     &requests[i]);
+        ret = njs_vm_external_create(vm, njs_value_arg(&requests[i].value),
+                                     proto, &requests[i]);
         if (ret != NXT_OK) {
             printf("njs_vm_external_create() failed\n");
             return NXT_ERROR;
         }
 
         ret = njs_vm_external_bind(vm, &nxt_test_requests[i].name,
-                                   &requests[i].value);
+                                   njs_value_arg(&requests[i].value));
         if (ret != NXT_OK) {
             printf("njs_vm_external_bind() failed\n");
             return NXT_ERROR;


More information about the nginx-devel mailing list