[njs] Using addon module API to unify injecting of external objects.

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 30 03:39:21 UTC 2023


details:   https://hg.nginx.org/njs/rev/57ca02d7404c
branches:  
changeset: 2166:57ca02d7404c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Jun 28 22:15:57 2023 -0700
description:
Using addon module API to unify injecting of external objects.

diffstat:

 nginx/ngx_http_js_module.c    |  46 +++++++++++++++++++++------
 nginx/ngx_js.c                |  70 ++++++++++--------------------------------
 nginx/ngx_js.h                |  11 +++---
 nginx/ngx_js_fetch.c          |  38 ++++++++++------------
 nginx/ngx_js_fetch.h          |   2 +-
 nginx/ngx_stream_js_module.c  |  55 +++++++++++++++++++++++----------
 src/test/njs_externals_test.c |  18 +++++++++-
 src/test/njs_externals_test.h |   6 ++-
 src/test/njs_unit_test.c      |  39 ++++++++++-------------
 9 files changed, 151 insertions(+), 134 deletions(-)

diffs (637 lines):

diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_http_js_module.c	Wed Jun 28 22:15:57 2023 -0700
@@ -246,6 +246,7 @@ static void ngx_http_js_handle_vm_event(
 static void ngx_http_js_handle_event(ngx_http_request_t *r,
     njs_vm_event_t vm_event, njs_value_t *args, njs_uint_t nargs);
 
+static njs_int_t ngx_js_http_init(njs_vm_t *vm);
 static ngx_int_t ngx_http_js_init(ngx_conf_t *cf);
 static char *ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_http_js_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -783,6 +784,33 @@ static njs_vm_meta_t ngx_http_js_metas =
 };
 
 
+njs_module_t  ngx_js_http_module = {
+    .name = njs_str("http"),
+    .init = ngx_js_http_init,
+};
+
+
+njs_module_t *njs_http_js_addon_modules[] = {
+    /*
+     * Shared addons should be in the same order and the same positions
+     * in all nginx modules.
+     */
+    &ngx_js_ngx_module,
+    &ngx_js_fetch_module,
+#ifdef NJS_HAVE_OPENSSL
+    &njs_webcrypto_module,
+#endif
+#ifdef NJS_HAVE_XML
+    &njs_xml_module,
+#endif
+#ifdef NJS_HAVE_ZLIB
+    &njs_zlib_module,
+#endif
+    &ngx_js_http_module,
+    NULL,
+};
+
+
 static ngx_int_t
 ngx_http_js_content_handler(ngx_http_request_t *r)
 {
@@ -4103,21 +4131,17 @@ ngx_http_js_handle_event(ngx_http_reques
 }
 
 
-static ngx_int_t
-ngx_http_js_externals_init(ngx_conf_t *cf, ngx_js_loc_conf_t *conf_in)
+static njs_int_t
+ngx_js_http_init(njs_vm_t *vm)
 {
-    ngx_http_js_loc_conf_t        *conf = (ngx_http_js_loc_conf_t *) conf_in;
-
-    ngx_http_js_request_proto_id = njs_vm_external_prototype(conf->vm,
+    ngx_http_js_request_proto_id = njs_vm_external_prototype(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_ERROR;
+        return NJS_ERROR;
     }
 
-    return NGX_OK;
+    return NJS_OK;
 }
 
 
@@ -4132,11 +4156,11 @@ ngx_http_js_init_conf_vm(ngx_conf_t *cf,
     options.unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW;
     options.ops = &ngx_http_js_ops;
     options.metas = &ngx_http_js_metas;
-    options.addons = njs_js_addon_modules;
+    options.addons = njs_http_js_addon_modules;
     options.argv = ngx_argv;
     options.argc = ngx_argc;
 
-    return ngx_js_init_conf_vm(cf, conf, &options, ngx_http_js_externals_init);
+    return ngx_js_init_conf_vm(cf, conf, &options);
 }
 
 
diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_js.c
--- a/nginx/ngx_js.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_js.c	Wed Jun 28 22:15:57 2023 -0700
@@ -9,7 +9,6 @@
 #include <ngx_config.h>
 #include <ngx_core.h>
 #include "ngx_js.h"
-#include "ngx_js_fetch.h"
 
 
 static njs_int_t ngx_js_ext_build(njs_vm_t *vm, njs_object_prop_t *prop,
@@ -28,10 +27,7 @@ static njs_int_t ngx_js_ext_version(njs_
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 static void ngx_js_cleanup_vm(void *data);
 
-
-extern njs_module_t  njs_webcrypto_module;
-extern njs_module_t  njs_xml_module;
-extern njs_module_t  njs_zlib_module;
+static njs_int_t ngx_js_core_init(njs_vm_t *vm);
 
 
 static njs_external_t  ngx_js_ext_core[] = {
@@ -156,16 +152,14 @@ static njs_external_t  ngx_js_ext_core[]
 };
 
 
-njs_module_t *njs_js_addon_modules[] = {
-#ifdef NJS_HAVE_OPENSSL
-    &njs_webcrypto_module,
-#endif
-#ifdef NJS_HAVE_XML
-    &njs_xml_module,
-#endif
-#ifdef NJS_HAVE_ZLIB
-    &njs_zlib_module,
-#endif
+njs_module_t  ngx_js_ngx_module = {
+    .name = njs_str("ngx"),
+    .init = ngx_js_core_init,
+};
+
+
+njs_module_t *njs_js_addon_modules_shared[] = {
+    &ngx_js_ngx_module,
     NULL,
 };
 
@@ -279,31 +273,22 @@ ngx_js_string(njs_vm_t *vm, njs_value_t 
 }
 
 
-ngx_int_t
-ngx_js_core_init(njs_vm_t *vm, ngx_log_t *log)
+static njs_int_t
+ngx_js_core_init(njs_vm_t *vm)
 {
-    ngx_int_t           rc;
     njs_int_t           ret, proto_id;
     njs_str_t           name;
     njs_opaque_value_t  value;
 
-    rc = ngx_js_fetch_init(vm, log);
-    if (rc != NGX_OK) {
-        return NGX_ERROR;
-    }
-
     proto_id = njs_vm_external_prototype(vm, ngx_js_ext_core,
                                          njs_nitems(ngx_js_ext_core));
     if (proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0, "failed to add js core proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ret = njs_vm_external_create(vm, njs_value_arg(&value), proto_id, NULL, 1);
     if (njs_slow_path(ret != NJS_OK)) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "njs_vm_external_create() failed\n");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     name.length = 3;
@@ -311,11 +296,10 @@ ngx_js_core_init(njs_vm_t *vm, ngx_log_t
 
     ret = njs_vm_bind(vm, &name, njs_value_arg(&value), 1);
     if (njs_slow_path(ret != NJS_OK)) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0, "njs_vm_bind() failed\n");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
-    return NGX_OK;
+    return NJS_OK;
 }
 
 
@@ -772,17 +756,13 @@ ngx_js_init_preload_vm(ngx_conf_t *cf, n
     njs_vm_opt_init(&options);
 
     options.init = 1;
+    options.addons = njs_js_addon_modules_shared;
 
     vm = njs_vm_create(&options);
     if (vm == NULL) {
         goto error;
     }
 
-    ret = ngx_js_core_init(vm, cf->log);
-    if (njs_slow_path(ret != NJS_OK)) {
-        goto error;
-    }
-
     njs_str_t str = njs_str(
         "import fs from 'fs';"
 
@@ -1005,8 +985,7 @@ ngx_js_merge_vm(ngx_conf_t *cf, ngx_js_l
 
 ngx_int_t
 ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
-    njs_vm_opt_t *options,
-    ngx_int_t (*externals_init)(ngx_conf_t *cf, ngx_js_loc_conf_t *conf))
+    njs_vm_opt_t *options)
 {
     size_t                size;
     u_char               *start, *end, *p;
@@ -1110,21 +1089,6 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_
         }
     }
 
-    /*
-     * Core prototypes must be inited before externals_init() because
-     * the core prototype ids have to be identical in all the modules.
-     */
-
-    rc = ngx_js_core_init(conf->vm, cf->log);
-    if (njs_slow_path(rc != NJS_OK)) {
-        return NGX_ERROR;
-    }
-
-    rc = externals_init(cf, conf);
-    if (rc != NGX_OK) {
-        return NGX_ERROR;
-    }
-
     end = start + size;
 
     rc = njs_vm_compile(conf->vm, &start, end);
diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_js.h
--- a/nginx/ngx_js.h	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_js.h	Wed Jun 28 22:15:57 2023 -0700
@@ -13,6 +13,7 @@
 #include <ngx_config.h>
 #include <ngx_core.h>
 #include <njs.h>
+#include "ngx_js_fetch.h"
 
 
 #define NGX_JS_UNSET        0
@@ -129,8 +130,7 @@ ngx_int_t ngx_js_merge_vm(ngx_conf_t *cf
     ngx_js_loc_conf_t *prev,
     ngx_int_t (*init_vm)(ngx_conf_t *cf, ngx_js_loc_conf_t *conf));
 ngx_int_t ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
-    njs_vm_opt_t *options,
-    ngx_int_t (*externals_init)(ngx_conf_t *cf, ngx_js_loc_conf_t *conf));
+    njs_vm_opt_t *options);
 ngx_js_loc_conf_t *ngx_js_create_conf(ngx_conf_t *cf, size_t size);
 char * ngx_js_merge_conf(ngx_conf_t *cf, void *parent, void *child,
    ngx_int_t (*init_vm)(ngx_conf_t *cf, ngx_js_loc_conf_t *conf));
@@ -144,13 +144,14 @@ njs_int_t ngx_js_ext_constant(njs_vm_t *
 njs_int_t ngx_js_ext_flags(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 
-ngx_int_t ngx_js_core_init(njs_vm_t *vm, ngx_log_t *log);
-
 ngx_int_t ngx_js_string(njs_vm_t *vm, njs_value_t *value, njs_str_t *str);
 ngx_int_t ngx_js_integer(njs_vm_t *vm, njs_value_t *value, ngx_int_t *n);
 
 
-extern njs_module_t *njs_js_addon_modules[];
+extern njs_module_t  ngx_js_ngx_module;
+extern njs_module_t  njs_webcrypto_module;
+extern njs_module_t  njs_xml_module;
+extern njs_module_t  njs_zlib_module;
 
 
 #endif /* _NGX_JS_H_INCLUDED_ */
diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_js_fetch.c	Wed Jun 28 22:15:57 2023 -0700
@@ -277,6 +277,8 @@ static njs_int_t ngx_fetch_flag(njs_vm_t
 static njs_int_t ngx_fetch_flag_set(njs_vm_t *vm, const ngx_js_entry_t *entries,
      njs_value_t *value, const char *type);
 
+static njs_int_t ngx_js_fetch_init(njs_vm_t *vm);
+
 
 static const ngx_js_entry_t ngx_js_fetch_credentials[] = {
     { njs_str("same-origin"), CREDENTIALS_SAME_ORIGIN },
@@ -652,6 +654,12 @@ static njs_int_t    ngx_http_js_fetch_re
 static njs_int_t    ngx_http_js_fetch_headers_proto_id;
 
 
+njs_module_t  ngx_js_fetch_module = {
+    .name = njs_str("fetch"),
+    .init = ngx_js_fetch_init,
+};
+
+
 njs_int_t
 ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused, njs_value_t *retval)
@@ -4033,8 +4041,8 @@ ngx_js_fetch_function_bind(njs_vm_t *vm,
 }
 
 
-ngx_int_t
-ngx_js_fetch_init(njs_vm_t *vm, ngx_log_t *log)
+static njs_int_t
+ngx_js_fetch_init(njs_vm_t *vm)
 {
     njs_int_t  ret;
 
@@ -4046,52 +4054,40 @@ ngx_js_fetch_init(njs_vm_t *vm, ngx_log_
                                           ngx_js_ext_http_headers,
                                           njs_nitems(ngx_js_ext_http_headers));
     if (ngx_http_js_fetch_headers_proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to add js fetch Headers proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ngx_http_js_fetch_request_proto_id = njs_vm_external_prototype(vm,
                                           ngx_js_ext_http_request,
                                           njs_nitems(ngx_js_ext_http_request));
     if (ngx_http_js_fetch_request_proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to add js fetch Request proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ngx_http_js_fetch_response_proto_id = njs_vm_external_prototype(vm,
                                           ngx_js_ext_http_response,
                                           njs_nitems(ngx_js_ext_http_response));
     if (ngx_http_js_fetch_response_proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to add js fetch Response proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ret = ngx_js_fetch_function_bind(vm, &headers,
                                      ngx_js_ext_headers_constructor, 1);
     if (ret != NJS_OK) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to bind Headers ctor");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ret = ngx_js_fetch_function_bind(vm, &request,
                                      ngx_js_ext_request_constructor, 1);
     if (ret != NJS_OK) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to bind Request ctor");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
     ret = ngx_js_fetch_function_bind(vm, &response,
                                      ngx_js_ext_response_constructor, 1);
     if (ret != NJS_OK) {
-        ngx_log_error(NGX_LOG_EMERG, log, 0,
-                      "failed to bind Response ctor");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
-    return NGX_OK;
+    return NJS_OK;
 }
diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_js_fetch.h
--- a/nginx/ngx_js_fetch.h	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_js_fetch.h	Wed Jun 28 22:15:57 2023 -0700
@@ -12,7 +12,7 @@
 njs_int_t ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t level, njs_value_t *retval);
 
-ngx_int_t ngx_js_fetch_init(njs_vm_t *vm, ngx_log_t *log);
+extern njs_module_t  ngx_js_fetch_module;
 
 
 #endif /* _NGX_JS_FETCH_H_INCLUDED_ */
diff -r 9371326836d2 -r 57ca02d7404c nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/nginx/ngx_stream_js_module.c	Wed Jun 28 22:15:57 2023 -0700
@@ -114,6 +114,8 @@ static size_t ngx_stream_js_max_response
 static void ngx_stream_js_handle_event(ngx_stream_session_t *s,
     njs_vm_event_t vm_event, njs_value_t *args, njs_uint_t nargs);
 
+static njs_int_t ngx_js_stream_init(njs_vm_t *vm);
+static ngx_int_t ngx_stream_js_init(ngx_conf_t *cf);
 static char *ngx_stream_js_set(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
 static char *ngx_stream_js_var(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -123,7 +125,6 @@ static ngx_int_t ngx_stream_js_init_conf
 static void *ngx_stream_js_create_srv_conf(ngx_conf_t *cf);
 static char *ngx_stream_js_merge_srv_conf(ngx_conf_t *cf, void *parent,
     void *child);
-static ngx_int_t ngx_stream_js_init(ngx_conf_t *cf);
 
 static ngx_ssl_t *ngx_stream_js_ssl(njs_vm_t *vm, ngx_stream_session_t *s);
 static ngx_flag_t ngx_stream_js_ssl_verify(njs_vm_t *vm,
@@ -565,6 +566,33 @@ static njs_int_t    ngx_stream_js_sessio
 static njs_int_t    ngx_stream_js_session_flags_proto_id;
 
 
+njs_module_t  ngx_js_stream_module = {
+    .name = njs_str("stream"),
+    .init = ngx_js_stream_init,
+};
+
+
+njs_module_t *njs_stream_js_addon_modules[] = {
+    /*
+     * Shared addons should be in the same order and the same positions
+     * in all nginx modules.
+     */
+    &ngx_js_ngx_module,
+    &ngx_js_fetch_module,
+#ifdef NJS_HAVE_OPENSSL
+    &njs_webcrypto_module,
+#endif
+#ifdef NJS_HAVE_XML
+    &njs_xml_module,
+#endif
+#ifdef NJS_HAVE_ZLIB
+    &njs_zlib_module,
+#endif
+    &ngx_js_stream_module,
+    NULL,
+};
+
+
 static ngx_int_t
 ngx_stream_js_access_handler(ngx_stream_session_t *s)
 {
@@ -1669,30 +1697,24 @@ ngx_stream_js_handle_event(ngx_stream_se
 }
 
 
-static ngx_int_t
-ngx_stream_js_externals_init(ngx_conf_t *cf, ngx_js_loc_conf_t *conf_in)
+static njs_int_t
+ngx_js_stream_init(njs_vm_t *vm)
 {
-    ngx_stream_js_srv_conf_t  *conf = (ngx_stream_js_srv_conf_t *) conf_in;
-
-    ngx_stream_js_session_proto_id = njs_vm_external_prototype(conf->vm,
+    ngx_stream_js_session_proto_id = njs_vm_external_prototype(vm,
                                          ngx_stream_js_ext_session,
                                          njs_nitems(ngx_stream_js_ext_session));
     if (ngx_stream_js_session_proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                      "failed to add js session proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
-    ngx_stream_js_session_flags_proto_id = njs_vm_external_prototype(conf->vm,
+    ngx_stream_js_session_flags_proto_id = njs_vm_external_prototype(vm,
                                    ngx_stream_js_ext_session_flags,
                                    njs_nitems(ngx_stream_js_ext_session_flags));
     if (ngx_stream_js_session_flags_proto_id < 0) {
-        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                      "failed to add js session flags proto");
-        return NGX_ERROR;
+        return NJS_ERROR;
     }
 
-    return NGX_OK;
+    return NJS_OK;
 }
 
 
@@ -1707,12 +1729,11 @@ ngx_stream_js_init_conf_vm(ngx_conf_t *c
     options.unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW;
     options.ops = &ngx_stream_js_ops;
     options.metas = &ngx_stream_js_metas;
-    options.addons = njs_js_addon_modules;
+    options.addons = njs_stream_js_addon_modules;
     options.argv = ngx_argv;
     options.argc = ngx_argc;
 
-    return ngx_js_init_conf_vm(cf, conf, &options,
-                               ngx_stream_js_externals_init);
+    return ngx_js_init_conf_vm(cf, conf, &options);
 }
 
 
diff -r 9371326836d2 -r 57ca02d7404c src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/src/test/njs_externals_test.c	Wed Jun 28 22:15:57 2023 -0700
@@ -26,6 +26,8 @@ typedef struct {
 } njs_unit_test_prop_t;
 
 
+static njs_int_t njs_externals_262_init(njs_vm_t *vm);
+static njs_int_t njs_externals_shared_init(njs_vm_t *vm);
 njs_int_t njs_array_buffer_detach(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 
@@ -33,6 +35,18 @@ njs_int_t njs_array_buffer_detach(njs_vm
 static njs_int_t    njs_external_r_proto_id;
 
 
+njs_module_t  njs_unit_test_262_module = {
+    .name = njs_str("$262"),
+    .init = njs_externals_262_init,
+};
+
+
+njs_module_t  njs_unit_test_external_module = {
+    .name = njs_str("external"),
+    .init = njs_externals_shared_init,
+};
+
+
 static njs_int_t
 lvlhsh_unit_test_key_test(njs_lvlhsh_query_t *lhq, void *data)
 {
@@ -1131,7 +1145,7 @@ njs_externals_init_internal(njs_vm_t *vm
 }
 
 
-njs_int_t
+static njs_int_t
 njs_externals_262_init(njs_vm_t *vm)
 {
     njs_int_t           ret, proto_id;
@@ -1162,7 +1176,7 @@ njs_externals_262_init(njs_vm_t *vm)
 }
 
 
-njs_int_t
+static njs_int_t
 njs_externals_shared_init(njs_vm_t *vm)
 {
     return njs_externals_init_internal(vm, njs_test_requests, 1, 1);
diff -r 9371326836d2 -r 57ca02d7404c src/test/njs_externals_test.h
--- a/src/test/njs_externals_test.h	Mon Jun 26 16:47:17 2023 -0700
+++ b/src/test/njs_externals_test.h	Wed Jun 28 22:15:57 2023 -0700
@@ -24,8 +24,6 @@ typedef struct {
 } njs_external_ev_t;
 
 
-njs_int_t njs_externals_shared_init(njs_vm_t *vm);
-njs_int_t njs_externals_262_init(njs_vm_t *vm);
 njs_int_t njs_externals_init(njs_vm_t *vm);
 njs_int_t njs_external_env_init(njs_external_env_t *env);
 njs_int_t njs_external_call(njs_vm_t *vm, const njs_str_t *fname,
@@ -33,4 +31,8 @@ njs_int_t njs_external_call(njs_vm_t *vm
 njs_int_t njs_external_process_events(njs_vm_t *vm, njs_external_env_t *env);
 
 
+extern njs_module_t  njs_unit_test_262_module;
+extern njs_module_t  njs_unit_test_external_module;
+
+
 #endif /* _NJS_EXTERNALS_TEST_H_INCLUDED_ */
diff -r 9371326836d2 -r 57ca02d7404c src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Mon Jun 26 16:47:17 2023 -0700
+++ b/src/test/njs_unit_test.c	Wed Jun 28 22:15:57 2023 -0700
@@ -23683,6 +23683,19 @@ done:
 }
 
 
+njs_module_t *njs_unit_test_addon_modules[] = {
+    &njs_unit_test_262_module,
+    NULL,
+};
+
+
+njs_module_t *njs_unit_test_addon_external_modules[] = {
+    &njs_unit_test_262_module,
+    &njs_unit_test_external_module,
+    NULL,
+};
+
+
 static njs_int_t
 njs_unit_test(njs_unit_test_t tests[], size_t num, njs_str_t *name,
     njs_opts_t *opts, njs_stat_t *stat)
@@ -23716,6 +23729,8 @@ njs_unit_test(njs_unit_test_t tests[], s
         options.module = opts->module;
         options.unsafe = opts->unsafe;
         options.backtrace = opts->backtrace;
+        options.addons = opts->externals ? njs_unit_test_addon_external_modules
+                                         : njs_unit_test_addon_modules;
 
         vm = njs_vm_create(&options);
         if (vm == NULL) {
@@ -23723,18 +23738,6 @@ njs_unit_test(njs_unit_test_t tests[], s
             goto done;
         }
 
-        ret = njs_externals_262_init(vm);
-        if (ret != NJS_OK) {
-            goto done;
-        }
-
-        if (opts->externals) {
-            ret = njs_externals_shared_init(vm);
-            if (ret != NJS_OK) {
-                goto done;
-            }
-        }
-
         start = tests[i].script.start;
         end = start + tests[i].script.length;
 
@@ -23855,6 +23858,8 @@ njs_interactive_test(njs_unit_test_t tes
         options.init = 1;
         options.interactive = 1;
         options.backtrace = 1;
+        options.addons = opts->externals ? njs_unit_test_addon_external_modules
+                                         : njs_unit_test_addon_modules;
 
         vm = njs_vm_create(&options);
         if (vm == NULL) {
@@ -23862,17 +23867,7 @@ njs_interactive_test(njs_unit_test_t tes
             goto done;
         }
 
-        ret = njs_externals_262_init(vm);
-        if (ret != NJS_OK) {
-            goto done;
-        }
-
         if (opts->externals) {
-            ret = njs_externals_shared_init(vm);
-            if (ret != NJS_OK) {
-                goto done;
-            }
-
             ret = njs_externals_init(vm);
             if (ret != NJS_OK) {
                 goto done;


More information about the nginx-devel mailing list