[njs] Improved working with scope indexes of variables.

Dmitry Volyntsev xeioex at nginx.com
Sun Dec 30 11:13:30 UTC 2018


details:   https://hg.nginx.org/njs/rev/5cf68fb9152e
branches:  
changeset: 711:5cf68fb9152e
user:      hongzhidao <hongzhidao at gmail.com>
date:      Sat Dec 29 16:03:57 2018 +0800
description:
Improved working with scope indexes of variables.

diffstat:

 njs/njs_parser.h   |   7 +++----
 njs/njs_variable.c |  35 +++++++++++++++++------------------
 njs/njs_variable.h |   1 +
 3 files changed, 21 insertions(+), 22 deletions(-)

diffs (115 lines):

diff -r 157bbb5d50eb -r 5cf68fb9152e njs/njs_parser.h
--- a/njs/njs_parser.h	Sat Dec 29 15:16:59 2018 +0300
+++ b/njs/njs_parser.h	Sat Dec 29 16:03:57 2018 +0800
@@ -238,10 +238,9 @@ struct njs_parser_scope_s {
     nxt_lvlhsh_t                    variables;
     nxt_lvlhsh_t                    references;
 
-    /*
-     * 0: local scope index;
-     * 1: closure scope index.
-     */
+#define NJS_SCOPE_INDEX_LOCAL       0
+#define NJS_SCOPE_INDEX_CLOSURE     1
+
     nxt_array_t                     *values[2];  /* Array of njs_value_t. */
     njs_index_t                     next_index[2];
 
diff -r 157bbb5d50eb -r 5cf68fb9152e njs/njs_variable.c
--- a/njs/njs_variable.c	Sat Dec 29 15:16:59 2018 +0300
+++ b/njs/njs_variable.c	Sat Dec 29 16:03:57 2018 +0800
@@ -9,7 +9,7 @@
 #include <string.h>
 
 
-static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
+static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *node_scope,
     njs_variable_reference_t *vr);
 static njs_variable_t *njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name,
     njs_variable_type_t type);
@@ -196,15 +196,7 @@ njs_variables_scope_resolve(njs_vm_t *vm
                     continue;
                 }
 
-                if (vr->scope->type == NJS_SCOPE_GLOBAL) {
-                    continue;
-                }
-
-                if (node->scope->nesting == vr->scope->nesting) {
-                    /*
-                     * A variable is referenced locally here, but may be
-                     * referenced non-locally in other places, skipping.
-                     */
+                if (vr->scope_index == NJS_SCOPE_INDEX_LOCAL) {
                     continue;
                 }
             }
@@ -308,18 +300,16 @@ njs_variable_get(njs_vm_t *vm, njs_parse
         goto not_found;
     }
 
-    scope_index = 0;
-
-    if (vr->scope->type > NJS_SCOPE_GLOBAL) {
-        scope_index = (node->scope->nesting != vr->scope->nesting);
-    }
+    scope_index = vr->scope_index;
 
     var = vr->variable;
     index = var->index;
 
     if (index != NJS_INDEX_NONE) {
 
-        if (scope_index == 0 || njs_scope_type(index) != NJS_SCOPE_ARGUMENTS) {
+        if (scope_index == NJS_SCOPE_INDEX_LOCAL
+            || njs_scope_type(index) != NJS_SCOPE_ARGUMENTS)
+        {
             node->index = index;
 
             return var;
@@ -400,16 +390,17 @@ not_found:
 
 
 static njs_ret_t
-njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
+njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *node_scope,
     njs_variable_reference_t *vr)
 {
     nxt_lvlhsh_query_t  lhq;
-    njs_parser_scope_t  *parent, *previous;
+    njs_parser_scope_t  *scope, *parent, *previous;
 
     lhq.key_hash = vr->hash;
     lhq.key = vr->name;
     lhq.proto = &njs_variables_hash_proto;
 
+    scope = node_scope;
     previous = NULL;
 
     for ( ;; ) {
@@ -431,6 +422,14 @@ njs_variable_find(njs_vm_t *vm, njs_pars
 
             vr->scope = scope;
 
+            vr->scope_index = NJS_SCOPE_INDEX_LOCAL;
+
+            if (vr->scope->type > NJS_SCOPE_GLOBAL
+                && node_scope->nesting != vr->scope->nesting)
+            {
+                vr->scope_index = NJS_SCOPE_INDEX_CLOSURE;
+            }
+
             return NXT_OK;
         }
 
diff -r 157bbb5d50eb -r 5cf68fb9152e njs/njs_variable.h
--- a/njs/njs_variable.h	Sat Dec 29 15:16:59 2018 +0300
+++ b/njs/njs_variable.h	Sat Dec 29 16:03:57 2018 +0800
@@ -48,6 +48,7 @@ typedef struct {
     nxt_str_t             name;
     njs_variable_t        *variable;
     njs_parser_scope_t    *scope;
+    nxt_uint_t            scope_index;  /* NJS_SCOPE_INDEX_... */
 } njs_variable_reference_t;
 
 


More information about the nginx-devel mailing list