Welcome! Log In Create A New Profile

Advanced

[njs] Now njs_vm_compile() returns all global functions via the ...

October 10, 2016 10:22AM
details: http://hg.nginx.org/njs/rev/03d12ab0961e
branches:
changeset: 190:03d12ab0961e
user: Igor Sysoev <igor@sysoev.ru>
date: Mon Oct 10 16:33:56 2016 +0300
description:
Now njs_vm_compile() returns all global functions via the export
array.

diffstat:

nginx/ngx_http_js_module.c | 4 +-
nginx/ngx_stream_js_module.c | 4 +-
njs/njs_variable.c | 59 ++++++++++++++++++++++++++++++++++++++++++++
njs/njs_variable.h | 7 +++++
njs/njscript.c | 7 ++++-
njs/njscript.h | 2 +-
njs/test/njs_unit_test.c | 8 ++--
7 files changed, 81 insertions(+), 10 deletions(-)

diffs (207 lines):

diff -r 769173883f87 -r 03d12ab0961e nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c Mon Oct 10 13:08:40 2016 +0300
+++ b/nginx/ngx_http_js_module.c Mon Oct 10 16:33:56 2016 +0300
@@ -1225,7 +1225,7 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
ngx_fd_t fd;
ngx_str_t *value, file;
nxt_int_t rc;
- nxt_str_t text, ext;
+ nxt_str_t text, ext, *export;
nxt_lvlhsh_t externals;
ngx_file_info_t fi;
njs_vm_shared_t *shared;
@@ -1321,7 +1321,7 @@ ngx_http_js_include(ngx_conf_t *cf, ngx_
return NGX_CONF_ERROR;
}

- rc = njs_vm_compile(jlcf->vm, &start, end, NULL);
+ rc = njs_vm_compile(jlcf->vm, &start, end, NULL, &export);

if (rc != NJS_OK) {
njs_vm_exception(jlcf->vm, &text);
diff -r 769173883f87 -r 03d12ab0961e nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c Mon Oct 10 13:08:40 2016 +0300
+++ b/nginx/ngx_stream_js_module.c Mon Oct 10 16:33:56 2016 +0300
@@ -933,7 +933,7 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
ngx_fd_t fd;
ngx_str_t *value, file;
nxt_int_t rc;
- nxt_str_t text, ext;
+ nxt_str_t text, ext, *export;
nxt_lvlhsh_t externals;
ngx_file_info_t fi;
njs_vm_shared_t *shared;
@@ -1029,7 +1029,7 @@ ngx_stream_js_include(ngx_conf_t *cf, ng
return NGX_CONF_ERROR;
}

- rc = njs_vm_compile(jscf->vm, &start, end, NULL);
+ rc = njs_vm_compile(jscf->vm, &start, end, NULL, &export);

if (rc != NJS_OK) {
njs_vm_exception(jscf->vm, &text);
diff -r 769173883f87 -r 03d12ab0961e njs/njs_variable.c
--- a/njs/njs_variable.c Mon Oct 10 13:08:40 2016 +0300
+++ b/njs/njs_variable.c Mon Oct 10 16:33:56 2016 +0300
@@ -153,6 +153,65 @@ njs_parser_variable(njs_vm_t *vm, njs_pa
}


+nxt_str_t *
+njs_vm_export_functions(njs_vm_t *vm)
+{
+ size_t n;
+ nxt_str_t *ex, *export;
+ njs_value_t *value;
+ njs_variable_t *var;
+ nxt_lvlhsh_each_t lhe;
+
+ n = 1;
+
+ memset(&lhe, 0, sizeof(nxt_lvlhsh_each_t));
+ lhe.proto = &njs_variables_hash_proto;
+
+ for ( ;; ) {
+ var = nxt_lvlhsh_each(&vm->variables_hash, &lhe);
+ if (var == NULL) {
+ break;
+ }
+
+ value = njs_global_variable_value(vm, var);
+
+ if (njs_is_function(value) && !value->data.u.function->native) {
+ n++;
+ }
+ }
+
+ export = nxt_mem_cache_alloc(vm->mem_cache_pool, n * sizeof(nxt_str_t));
+ if (nxt_slow_path(export == NULL)) {
+ return NULL;
+ }
+
+ memset(&lhe, 0, sizeof(nxt_lvlhsh_each_t));
+ lhe.proto = &njs_variables_hash_proto;
+
+ ex = export;
+
+ for ( ;; ) {
+ var = nxt_lvlhsh_each(&vm->variables_hash, &lhe);
+ if (var == NULL) {
+ break;
+ }
+
+ value = njs_global_variable_value(vm, var);
+
+ if (njs_is_function(value) && !value->data.u.function->native) {
+ ex->length = var->name_len;
+ ex->start = var->name_start;
+ ex++;
+ }
+ }
+
+ ex->length = 0;
+ ex->start = NULL;
+
+ return export;
+}
+
+
njs_function_t *
njs_vm_function(njs_vm_t *vm, nxt_str_t *name)
{
diff -r 769173883f87 -r 03d12ab0961e njs/njs_variable.h
--- a/njs/njs_variable.h Mon Oct 10 13:08:40 2016 +0300
+++ b/njs/njs_variable.h Mon Oct 10 16:33:56 2016 +0300
@@ -26,10 +26,17 @@ typedef struct {
} njs_variable_t;


+#define njs_global_variable_value(vm, var) \
+ (njs_value_t *) ((u_char *) vm->global_scope \
+ + njs_offset((var)->index) - NJS_INDEX_GLOBAL_OFFSET)
+
+
+
njs_variable_t *njs_parser_name_alloc(njs_vm_t *vm, njs_parser_t *parser);
njs_variable_t *njs_parser_variable(njs_vm_t *vm, njs_parser_t *parser,
nxt_uint_t *level);
njs_value_t *njs_variable_value(njs_parser_t *parser, njs_index_t index);
+nxt_str_t *njs_vm_export_functions(njs_vm_t *vm);


#endif /* _NJS_VARIABLE_H_INCLUDED_ */
diff -r 769173883f87 -r 03d12ab0961e njs/njscript.c
--- a/njs/njscript.c Mon Oct 10 13:08:40 2016 +0300
+++ b/njs/njscript.c Mon Oct 10 16:33:56 2016 +0300
@@ -175,7 +175,7 @@ njs_vm_destroy(njs_vm_t *vm)

nxt_int_t
njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end,
- njs_function_t **function)
+ njs_function_t **function, nxt_str_t **export)
{
nxt_int_t ret;
njs_lexer_t *lexer;
@@ -251,6 +251,11 @@ njs_vm_compile(njs_vm_t *vm, u_char **st

vm->parser = NULL;

+ *export = njs_vm_export_functions(vm);
+ if (nxt_slow_path(*export == NULL)) {
+ return NJS_ERROR;
+ }
+
return NJS_OK;
}

diff -r 769173883f87 -r 03d12ab0961e njs/njscript.h
--- a/njs/njscript.h Mon Oct 10 13:08:40 2016 +0300
+++ b/njs/njscript.h Mon Oct 10 16:33:56 2016 +0300
@@ -84,7 +84,7 @@ NXT_EXPORT njs_vm_t *njs_vm_create(nxt_m
NXT_EXPORT void njs_vm_destroy(njs_vm_t *vm);

NXT_EXPORT nxt_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end,
- njs_function_t **function);
+ njs_function_t **function, nxt_str_t **export);
NXT_EXPORT njs_vm_t *njs_vm_clone(njs_vm_t *vm, nxt_mem_cache_pool_t *mcp,
void **external);
NXT_EXPORT nxt_int_t njs_vm_call(njs_vm_t *vm, njs_function_t *function,
diff -r 769173883f87 -r 03d12ab0961e njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Oct 10 13:08:40 2016 +0300
+++ b/njs/test/njs_unit_test.c Mon Oct 10 16:33:56 2016 +0300
@@ -5484,7 +5484,7 @@ njs_unit_test(nxt_bool_t disassemble)
u_char *start;
njs_vm_t *vm, *nvm;
nxt_int_t ret;
- nxt_str_t s, r_name;
+ nxt_str_t s, r_name, *export;
nxt_uint_t i;
nxt_bool_t success;
nxt_lvlhsh_t externals;
@@ -5532,7 +5532,7 @@ njs_unit_test(nxt_bool_t disassemble)
start = njs_test[i].script.start;

ret = njs_vm_compile(vm, &start, start + njs_test[i].script.length,
- &function);
+ &function, &export);

if (ret == NXT_OK) {
if (disassemble) {
@@ -5611,7 +5611,7 @@ njs_unit_test_benchmark(nxt_str_t *scrip
njs_vm_t *vm, *nvm;
uint64_t us;
nxt_int_t ret;
- nxt_str_t s;
+ nxt_str_t s, *export;
nxt_uint_t i;
nxt_bool_t success;
nxt_lvlhsh_t externals;
@@ -5645,7 +5645,7 @@ njs_unit_test_benchmark(nxt_str_t *scrip

start = script->start;

- ret = njs_vm_compile(vm, &start, start + script->length, NULL);
+ ret = njs_vm_compile(vm, &start, start + script->length, NULL, &export);
if (ret != NXT_OK) {
return NXT_ERROR;
}

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Now njs_vm_compile() returns all global functions via the ...

Igor Sysoev 759 October 10, 2016 10:22AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 125
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready