[njs] Resetting loaded modules in accumulative mode.

Dmitry Volyntsev xeioex at nginx.com
Mon Mar 25 14:51:28 UTC 2019


details:   https://hg.nginx.org/njs/rev/e3ee3a2d9994
branches:  
changeset: 841:e3ee3a2d9994
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Mar 25 17:51:19 2019 +0300
description:
Resetting loaded modules in accumulative mode.

This closes #113 issue on Github.

diffstat:

 njs/njs.c                        |   4 ++++
 njs/njs_module.c                 |  36 ++++++++++++++++++++++++++++++------
 njs/njs_module.h                 |   1 +
 njs/test/module/ref_exception.js |   1 +
 njs/test/njs_expect_test.exp     |  12 +++++++++---
 5 files changed, 45 insertions(+), 9 deletions(-)

diffs (115 lines):

diff -r 3aa52df685bc -r e3ee3a2d9994 njs/njs.c
--- a/njs/njs.c	Mon Mar 25 15:41:21 2019 +0300
+++ b/njs/njs.c	Mon Mar 25 17:51:19 2019 +0300
@@ -226,6 +226,10 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
         return NJS_ERROR;
     }
 
+    if (vm->modules != NULL && vm->options.accumulative) {
+        njs_module_reset(vm);
+    }
+
     parser = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_parser_t));
     if (nxt_slow_path(parser == NULL)) {
         return NJS_ERROR;
diff -r 3aa52df685bc -r e3ee3a2d9994 njs/njs_module.c
--- a/njs/njs_module.c	Mon Mar 25 15:41:21 2019 +0300
+++ b/njs/njs_module.c	Mon Mar 25 17:51:19 2019 +0300
@@ -63,22 +63,46 @@ njs_module_load(njs_vm_t *vm)
         } else {
             ret = njs_vm_invoke(vm, &module->function, NULL, 0, module->index);
             if (ret == NXT_ERROR) {
-                goto done;
+                return ret;
             }
         }
 
         item++;
     }
 
-    ret = NXT_OK;
+    return NXT_OK;
+}
+
 
-done:
+void
+njs_module_reset(njs_vm_t *vm)
+{
+    nxt_uint_t          i;
+    njs_module_t        **item, *module;
+    nxt_lvlhsh_query_t  lhq;
 
-    if (vm->options.accumulative) {
-        nxt_array_reset(vm->modules);
+    if (vm->modules == NULL) {
+        return;
     }
 
-    return ret;
+    item = vm->modules->start;
+
+    for (i = 0; i < vm->modules->items; i++) {
+        module = *item;
+
+        if (!module->function.native) {
+            lhq.key = module->name;
+            lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
+            lhq.proto = &njs_modules_hash_proto;
+            lhq.pool = vm->mem_pool;
+
+            (void) nxt_lvlhsh_delete(&vm->modules_hash, &lhq);
+        }
+
+        item++;
+    }
+
+    nxt_array_reset(vm->modules);
 }
 
 
diff -r 3aa52df685bc -r e3ee3a2d9994 njs/njs_module.h
--- a/njs/njs_module.h	Mon Mar 25 15:41:21 2019 +0300
+++ b/njs/njs_module.h	Mon Mar 25 17:51:19 2019 +0300
@@ -17,6 +17,7 @@ typedef struct {
 
 
 nxt_int_t njs_module_load(njs_vm_t *vm);
+void njs_module_reset(njs_vm_t *vm);
 nxt_int_t njs_parser_module(njs_vm_t *vm, njs_parser_t *parser);
 njs_ret_t njs_module_require(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
diff -r 3aa52df685bc -r e3ee3a2d9994 njs/test/module/ref_exception.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/njs/test/module/ref_exception.js	Mon Mar 25 17:51:19 2019 +0300
@@ -0,0 +1,1 @@
+export default {type:typeof undeclared, undeclared};
diff -r 3aa52df685bc -r e3ee3a2d9994 njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Mon Mar 25 15:41:21 2019 +0300
+++ b/njs/test/njs_expect_test.exp	Mon Mar 25 17:51:19 2019 +0300
@@ -659,9 +659,7 @@ njs_test {
 # modules
 
 njs_test {
-    {"import lib1 from 'lib1.js'\r\n"
-     "undefined\r\n"}
-    {"import lib2 from 'lib1.js'\r\n"
+    {"import lib1 from 'lib1.js'; import lib2 from 'lib1.js'\r\n"
      "undefined\r\n"}
     {"lib2.inc()\r\n"
      "undefined\r\n"}
@@ -675,6 +673,14 @@ njs_test {
      "Identifier \"default\" has already been declared in export.js:5\r\n"}
     {"import m from 'export_non_default.js'\r\n"
      "Non-default export is not supported in export_non_default.js:3\r\n"}
+    {"import ref from 'ref_exception.js'\r\n"
+     "ReferenceError: \"undeclared\" is not defined in ref_exception.js:1"}
+    {"ref\r\n"
+     "ReferenceError: \"ref\" is not defined in shell:1\r\n"}
+    {"var ref\r\n"
+     "undefined\r\n"}
+    {"import ref from 'ref_exception.js'\r\n"
+     "ReferenceError: \"undeclared\" is not defined in ref_exception.js:1"}
     {"import m from 'loading_exception.js'\r\n"
      "Error: loading exception\r\n    at module \\(loading_exception.js:1\\)"}
     {"import lib3 from 'lib1.js'\r\n"


More information about the nginx-devel mailing list