[njs] Added njs_vm_add_module().

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 19 04:06:56 UTC 2023


details:   https://hg.nginx.org/njs/rev/6bcfa2934487
branches:  
changeset: 2025:6bcfa2934487
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jan 06 20:39:01 2023 -0800
description:
Added njs_vm_add_module().

The new API does not require for njs_mod_t structure to be disclosed.

diffstat:

 external/njs_crypto_module.c       |   6 ++----
 external/njs_fs_module.c           |   5 +----
 external/njs_query_string_module.c |   6 ++----
 src/njs.h                          |   2 ++
 src/njs_buffer.c                   |   6 ++----
 src/njs_module.c                   |  17 +++++++++--------
 src/njs_module.h                   |   2 +-
 src/njs_vm.c                       |   9 ++++++++-
 8 files changed, 27 insertions(+), 26 deletions(-)

diffs (162 lines):

diff -r a98b63e87688 -r 6bcfa2934487 external/njs_crypto_module.c
--- a/external/njs_crypto_module.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/external/njs_crypto_module.c	Fri Jan 06 20:39:01 2023 -0800
@@ -646,13 +646,11 @@ njs_crypto_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    module = njs_module_add(vm, &njs_str_value("crypto"));
+    module = njs_vm_add_module(vm, &njs_str_value("crypto"),
+                               njs_value_arg(&value));
     if (njs_slow_path(module == NULL)) {
         return NJS_ERROR;
     }
 
-    njs_value_assign(&module->value, &value);
-    module->function.native = 1;
-
     return NJS_OK;
 }
diff -r a98b63e87688 -r 6bcfa2934487 external/njs_fs_module.c
--- a/external/njs_fs_module.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/external/njs_fs_module.c	Fri Jan 06 20:39:01 2023 -0800
@@ -3762,13 +3762,10 @@ njs_fs_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    module = njs_module_add(vm, &njs_str_value("fs"));
+    module = njs_vm_add_module(vm, &njs_str_value("fs"), njs_value_arg(&value));
     if (njs_slow_path(module == NULL)) {
         return NJS_ERROR;
     }
 
-    njs_value_assign(&module->value, &value);
-    module->function.native = 1;
-
     return NJS_OK;
 }
diff -r a98b63e87688 -r 6bcfa2934487 external/njs_query_string_module.c
--- a/external/njs_query_string_module.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/external/njs_query_string_module.c	Fri Jan 06 20:39:01 2023 -0800
@@ -998,13 +998,11 @@ njs_query_string_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    module = njs_module_add(vm, &njs_str_value("querystring"));
+    module = njs_vm_add_module(vm, &njs_str_value("querystring"),
+                               njs_value_arg(&value));
     if (njs_slow_path(module == NULL)) {
         return NJS_ERROR;
     }
 
-    njs_value_assign(&module->value, &value);
-    module->function.native = 1;
-
     return NJS_OK;
 }
diff -r a98b63e87688 -r 6bcfa2934487 src/njs.h
--- a/src/njs.h	Fri Jan 06 16:50:46 2023 -0800
+++ b/src/njs.h	Fri Jan 06 20:39:01 2023 -0800
@@ -310,6 +310,8 @@ NJS_EXPORT njs_vm_t *njs_vm_create(njs_v
 NJS_EXPORT void njs_vm_destroy(njs_vm_t *vm);
 
 NJS_EXPORT njs_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end);
+NJS_EXPORT njs_mod_t *njs_vm_add_module(njs_vm_t *vm, njs_str_t *name,
+    njs_value_t *value);
 NJS_EXPORT njs_mod_t *njs_vm_compile_module(njs_vm_t *vm, njs_str_t *name,
     u_char **start, u_char *end);
 NJS_EXPORT njs_vm_t *njs_vm_clone(njs_vm_t *vm, njs_external_ptr_t external);
diff -r a98b63e87688 -r 6bcfa2934487 src/njs_buffer.c
--- a/src/njs_buffer.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/src/njs_buffer.c	Fri Jan 06 20:39:01 2023 -0800
@@ -2602,13 +2602,11 @@ njs_buffer_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    module = njs_module_add(vm, &njs_str_value("buffer"));
+    module = njs_vm_add_module(vm, &njs_str_value("buffer"),
+                               njs_value_arg(&value));
     if (njs_slow_path(module == NULL)) {
         return NJS_ERROR;
     }
 
-    njs_value_assign(&module->value, &value);
-    module->function.native = 1;
-
     return NJS_OK;
 }
diff -r a98b63e87688 -r 6bcfa2934487 src/njs_module.c
--- a/src/njs_module.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/src/njs_module.c	Fri Jan 06 20:39:01 2023 -0800
@@ -283,7 +283,7 @@ njs_module_find(njs_vm_t *vm, njs_str_t 
 
 
 njs_mod_t *
-njs_module_add(njs_vm_t *vm, njs_str_t *name)
+njs_module_add(njs_vm_t *vm, njs_str_t *name, njs_value_t *value)
 {
     njs_int_t           ret;
     njs_mod_t           *module;
@@ -309,16 +309,17 @@ njs_module_add(njs_vm_t *vm, njs_str_t *
     lhq.proto = &njs_modules_hash_proto;
 
     ret = njs_lvlhsh_insert(&vm->shared->modules_hash, &lhq);
-    if (njs_fast_path(ret == NJS_OK)) {
-        return module;
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_internal_error(vm, "lvlhsh insert failed");
+        return NULL;
     }
 
-    njs_mp_free(vm->mem_pool, module->name.start);
-    njs_mp_free(vm->mem_pool, module);
+    if (value != NULL) {
+        njs_value_assign(&module->value, value);
+        module->function.native = 1;
+    }
 
-    njs_internal_error(vm, "lvlhsh insert failed");
-
-    return NULL;
+    return module;
 }
 
 
diff -r a98b63e87688 -r 6bcfa2934487 src/njs_module.h
--- a/src/njs_module.h	Fri Jan 06 16:50:46 2023 -0800
+++ b/src/njs_module.h	Fri Jan 06 20:39:01 2023 -0800
@@ -16,7 +16,7 @@ struct njs_mod_s {
 };
 
 
-njs_mod_t *njs_module_add(njs_vm_t *vm, njs_str_t *name);
+njs_mod_t *njs_module_add(njs_vm_t *vm, njs_str_t *name, njs_value_t *value);
 njs_mod_t *njs_module_find(njs_vm_t *vm, njs_str_t *name,
     njs_bool_t shared);
 njs_mod_t *njs_parser_module(njs_parser_t *parser, njs_str_t *name);
diff -r a98b63e87688 -r 6bcfa2934487 src/njs_vm.c
--- a/src/njs_vm.c	Fri Jan 06 16:50:46 2023 -0800
+++ b/src/njs_vm.c	Fri Jan 06 20:39:01 2023 -0800
@@ -239,6 +239,13 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
 
 
 njs_mod_t *
+njs_vm_add_module(njs_vm_t *vm, njs_str_t *name, njs_value_t *value)
+{
+    return njs_module_add(vm, name, value);
+}
+
+
+njs_mod_t *
 njs_vm_compile_module(njs_vm_t *vm, njs_str_t *name, u_char **start,
     u_char *end)
 {
@@ -256,7 +263,7 @@ njs_vm_compile_module(njs_vm_t *vm, njs_
         return module;
     }
 
-    module = njs_module_add(vm, name);
+    module = njs_module_add(vm, name, NULL);
     if (njs_slow_path(module == NULL)) {
         return NULL;
     }


More information about the nginx-devel mailing list