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);
};
+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)
{
}
-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;
}
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);
}
#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,
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[] = {
};
-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,
};
}
-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;
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;
}
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';"
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;
}
}
- /*
- * 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);
#include <ngx_config.h>
#include <ngx_core.h>
#include <njs.h>
+#include "ngx_js_fetch.h"
#define NGX_JS_UNSET 0
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));
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_ */
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 },
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)
}
-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;
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;
}
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_ */
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,
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,
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)
{
}
-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;
}
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);
}
} 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);
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)
{
}
-njs_int_t
+static njs_int_t
njs_externals_262_init(njs_vm_t *vm)
{
njs_int_t ret, proto_id;
}
-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);
} 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,
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_ */
}
+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)
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) {
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;
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) {
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;