[njs] Introduced njs_variables_copy().

Dmitry Volyntsev xeioex at nginx.com
Wed Aug 21 13:32:24 UTC 2019


details:   https://hg.nginx.org/njs/rev/91e3f7345e47
branches:  
changeset: 1139:91e3f7345e47
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Aug 20 23:03:26 2019 -0400
description:
Introduced njs_variables_copy().

diffstat:

 src/njs_parser.c   |  38 +++++++-------------------------------
 src/njs_variable.c |  36 ++++++++++++++++++++++++++++++++++++
 src/njs_variable.h |   2 ++
 3 files changed, 45 insertions(+), 31 deletions(-)

diffs (113 lines):

diff -r 4cc08cb772a2 -r 91e3f7345e47 src/njs_parser.c
--- a/src/njs_parser.c	Tue Aug 20 22:59:38 2019 -0400
+++ b/src/njs_parser.c	Tue Aug 20 23:03:26 2019 -0400
@@ -76,13 +76,9 @@ static njs_token_t njs_parser_grouping_e
 njs_int_t
 njs_parser(njs_vm_t *vm, njs_parser_t *parser, njs_parser_t *prev)
 {
-    njs_int_t           ret;
-    njs_token_t         token;
-    njs_lvlhsh_t        *variables, *prev_variables;
-    njs_variable_t      *var;
-    njs_parser_node_t   *node;
-    njs_lvlhsh_each_t   lhe;
-    njs_lvlhsh_query_t  lhq;
+    njs_int_t          ret;
+    njs_token_t        token;
+    njs_parser_node_t  *node;
 
     ret = njs_parser_scope_begin(vm, parser, NJS_SCOPE_GLOBAL);
     if (njs_slow_path(ret != NJS_OK)) {
@@ -94,30 +90,10 @@ njs_parser(njs_vm_t *vm, njs_parser_t *p
          * Copy the global scope variables from the previous
          * iteration of the accumulative mode.
          */
-        njs_lvlhsh_each_init(&lhe, &njs_variables_hash_proto);
-
-        lhq.proto = &njs_variables_hash_proto;
-        lhq.replace = 0;
-        lhq.pool = vm->mem_pool;
-
-        variables = &parser->scope->variables;
-        prev_variables = &prev->scope->variables;
-
-        for ( ;; ) {
-            var = njs_lvlhsh_each(prev_variables, &lhe);
-
-            if (var == NULL) {
-                break;
-            }
-
-            lhq.value = var;
-            lhq.key = var->name;
-            lhq.key_hash = njs_djb_hash(var->name.start, var->name.length);
-
-            ret = njs_lvlhsh_insert(variables, &lhq);
-            if (njs_slow_path(ret != NJS_OK)) {
-                return NJS_ERROR;
-            }
+        ret = njs_variables_copy(vm, &parser->scope->variables,
+                                 &prev->scope->variables);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
         }
     }
 
diff -r 4cc08cb772a2 -r 91e3f7345e47 src/njs_variable.c
--- a/src/njs_variable.c	Tue Aug 20 22:59:38 2019 -0400
+++ b/src/njs_variable.c	Tue Aug 20 23:03:26 2019 -0400
@@ -80,6 +80,42 @@ njs_variable_add(njs_vm_t *vm, njs_parse
 }
 
 
+njs_int_t
+njs_variables_copy(njs_vm_t *vm, njs_lvlhsh_t *variables,
+    njs_lvlhsh_t *prev_variables)
+{
+    njs_int_t           ret;
+    njs_variable_t      *var;
+    njs_lvlhsh_each_t   lhe;
+    njs_lvlhsh_query_t  lhq;
+
+    njs_lvlhsh_each_init(&lhe, &njs_variables_hash_proto);
+
+    lhq.proto = &njs_variables_hash_proto;
+    lhq.replace = 0;
+    lhq.pool = vm->mem_pool;
+
+    for ( ;; ) {
+        var = njs_lvlhsh_each(prev_variables, &lhe);
+
+        if (var == NULL) {
+            break;
+        }
+
+        lhq.value = var;
+        lhq.key = var->name;
+        lhq.key_hash = njs_djb_hash(var->name.start, var->name.length);
+
+        ret = njs_lvlhsh_insert(variables, &lhq);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return NJS_ERROR;
+        }
+    }
+
+    return NJS_OK;
+}
+
+
 static njs_variable_t *
 njs_variable_scope_add(njs_vm_t *vm, njs_parser_scope_t *scope,
     njs_lvlhsh_query_t *lhq, njs_variable_type_t type)
diff -r 4cc08cb772a2 -r 91e3f7345e47 src/njs_variable.h
--- a/src/njs_variable.h	Tue Aug 20 22:59:38 2019 -0400
+++ b/src/njs_variable.h	Tue Aug 20 23:03:26 2019 -0400
@@ -51,6 +51,8 @@ typedef struct {
 
 njs_variable_t *njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope,
     njs_str_t *name, uint32_t hash, njs_variable_type_t type);
+njs_int_t njs_variables_copy(njs_vm_t *vm, njs_lvlhsh_t *variables,
+    njs_lvlhsh_t *prev_variables);
 njs_variable_t * njs_label_add(njs_vm_t *vm, njs_parser_scope_t *scope,
     njs_str_t *name, uint32_t hash);
 njs_variable_t *njs_label_find(njs_vm_t *vm, njs_parser_scope_t *scope,


More information about the nginx-devel mailing list