[njs] Modules: introduced global nginx properties.

Dmitry Volyntsev xeioex at nginx.com
Thu May 18 04:51:30 UTC 2023


details:   https://hg.nginx.org/njs/rev/25b55a064e42
branches:  
changeset: 2122:25b55a064e42
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed May 17 21:16:19 2023 -0700
description:
Modules: introduced global nginx properties.

The following properties were introduced:
    ngx.build - an optional nginx build name, corresponds to
        --build=name argument of configure script, by default is "".
    ngx.conf_file_path - the file path to current nginx configuration
        file.
    ngx.error_log_path - the file path to current error log file.
    ngx.prefix - the directory that keeps server files.
    ngx.version - the nginx version as a string, for example: "1.25.0".
    ngx.version_number - the nginx version as a number, for example:
        1025000.

diffstat:

 nginx/ngx_js.c |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 121 insertions(+), 0 deletions(-)

diffs (169 lines):

diff -r ce344efc8b46 -r 25b55a064e42 nginx/ngx_js.c
--- a/nginx/ngx_js.c	Wed May 17 17:11:41 2023 -0700
+++ b/nginx/ngx_js.c	Wed May 17 21:16:19 2023 -0700
@@ -12,8 +12,20 @@
 #include "ngx_js_fetch.h"
 
 
+static njs_int_t ngx_js_ext_build(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_js_ext_conf_file_path(njs_vm_t *vm,
+    njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
+    njs_value_t *retval);
 static njs_int_t ngx_js_ext_conf_prefix(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_js_ext_error_log_path(njs_vm_t *vm,
+    njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
+    njs_value_t *retval);
+static njs_int_t ngx_js_ext_prefix(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_js_ext_version(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 static void ngx_js_cleanup_vm(void *data);
 
 
@@ -26,7 +38,26 @@ static njs_external_t  ngx_js_ext_core[]
 
     {
         .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("build"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_build,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("conf_file_path"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_conf_file_path,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
         .name.string = njs_str("conf_prefix"),
+        .enumerable = 1,
         .u.property = {
             .handler = ngx_js_ext_conf_prefix,
         }
@@ -43,6 +74,15 @@ static njs_external_t  ngx_js_ext_core[]
     },
 
     {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("error_log_path"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_error_log_path,
+        }
+    },
+
+    {
         .flags = NJS_EXTERN_METHOD,
         .name.string = njs_str("fetch"),
         .writable = 1,
@@ -76,6 +116,35 @@ static njs_external_t  ngx_js_ext_core[]
 
     {
         .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("prefix"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_prefix,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("version"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_version,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
+        .name.string = njs_str("version_number"),
+        .enumerable = 1,
+        .u.property = {
+            .handler = ngx_js_ext_constant,
+            .magic32 = nginx_version,
+            .magic16 = NGX_JS_NUMBER,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_PROPERTY,
         .name.string = njs_str("WARN"),
         .u.property = {
             .handler = ngx_js_ext_constant,
@@ -339,6 +408,31 @@ ngx_js_ext_flags(njs_vm_t *vm, njs_objec
 
 
 njs_int_t
+ngx_js_ext_build(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
+    njs_value_t *setval, njs_value_t *retval)
+{
+    return njs_vm_value_string_set(vm, retval,
+#ifdef NGX_BUILD
+                                   (u_char *) NGX_BUILD,
+                                   njs_strlen(NGX_BUILD)
+#else
+                                   (u_char *) "",
+                                   0
+#endif
+                                   );
+}
+
+
+njs_int_t
+ngx_js_ext_conf_file_path(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
+{
+    return njs_vm_value_string_set(vm, retval, ngx_cycle->conf_file.data,
+                                   ngx_cycle->conf_file.len);
+}
+
+
+njs_int_t
 ngx_js_ext_conf_prefix(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
@@ -348,6 +442,33 @@ ngx_js_ext_conf_prefix(njs_vm_t *vm, njs
 
 
 njs_int_t
+ngx_js_ext_error_log_path(njs_vm_t *vm, njs_object_prop_t *prop,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
+{
+    return njs_vm_value_string_set(vm, retval, ngx_cycle->error_log.data,
+                                   ngx_cycle->error_log.len);
+}
+
+
+njs_int_t
+ngx_js_ext_prefix(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
+    njs_value_t *setval, njs_value_t *retval)
+{
+    return njs_vm_value_string_set(vm, retval, ngx_cycle->prefix.data,
+                                   ngx_cycle->prefix.len);
+}
+
+
+njs_int_t
+ngx_js_ext_version(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
+    njs_value_t *setval, njs_value_t *retval)
+{
+    return njs_vm_value_string_set(vm, retval, (u_char *) NGINX_VERSION,
+                                   njs_strlen(NGINX_VERSION));
+}
+
+
+njs_int_t
 ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t level, njs_value_t *retval)
 {


More information about the nginx-devel mailing list