[njs] Refactored variable reference.

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 18 17:20:44 UTC 2019


details:   https://hg.nginx.org/njs/rev/ec44a042cf15
branches:  
changeset: 909:ec44a042cf15
user:      hongzhidao <hongzhidao at gmail.com>
date:      Fri Apr 19 00:28:31 2019 +0800
description:
Refactored variable reference.

diffstat:

 njs/njs_generator.c |   9 ++++-----
 njs/njs_parser.c    |  24 ++++++++++++++++++++++--
 njs/njs_variable.c  |  10 +---------
 3 files changed, 27 insertions(+), 16 deletions(-)

diffs (110 lines):

diff -r fca54d073408 -r ec44a042cf15 njs/njs_generator.c
--- a/njs/njs_generator.c	Thu Apr 18 19:17:22 2019 +0300
+++ b/njs/njs_generator.c	Fri Apr 19 00:28:31 2019 +0800
@@ -1648,13 +1648,11 @@ njs_generate_assignment(njs_vm_t *vm, nj
 
     if (lvalue->token == NJS_TOKEN_NAME) {
 
-        index = njs_variable_index(vm, lvalue);
-        if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
-            return NXT_ERROR;
+        ret = njs_generate_variable(vm, generator, lvalue);
+        if (nxt_slow_path(ret != NXT_OK)) {
+            return ret;
         }
 
-        lvalue->index = index;
-
         expr->dest = lvalue;
 
         ret = njs_generator(vm, generator, expr);
@@ -1757,6 +1755,7 @@ njs_generate_operation_assignment(njs_vm
     lvalue = node->left;
 
     if (lvalue->token == NJS_TOKEN_NAME) {
+
         ret = njs_generate_variable(vm, generator, lvalue);
         if (nxt_slow_path(ret != NXT_OK)) {
             return ret;
diff -r fca54d073408 -r ec44a042cf15 njs/njs_parser.c
--- a/njs/njs_parser.c	Thu Apr 18 19:17:22 2019 +0300
+++ b/njs/njs_parser.c	Fri Apr 19 00:28:31 2019 +0800
@@ -522,6 +522,19 @@ njs_parser_variable_node(njs_vm_t *vm, n
         return NULL;
     }
 
+    if (njs_is_null(&var->value)) {
+
+        switch (type) {
+
+        case NJS_VARIABLE_VAR:
+            var->value = njs_value_undefined;
+            break;
+
+        default:
+            break;
+        }
+    }
+
     node = njs_parser_node_new(vm, parser, NJS_TOKEN_NAME);
     if (nxt_slow_path(node == NULL)) {
         return NULL;
@@ -1389,7 +1402,7 @@ njs_parser_for_statement(njs_vm_t *vm, n
             init = parser->node;
 
             if (init->token == NJS_TOKEN_FOR_IN) {
-                return token;
+                goto done;
             }
 
         } else {
@@ -1404,7 +1417,12 @@ njs_parser_for_statement(njs_vm_t *vm, n
             init = parser->node;
 
             if (init->token == NJS_TOKEN_IN) {
-                return njs_parser_for_in_statement(vm, parser, &name, token);
+                token = njs_parser_for_in_statement(vm, parser, &name, token);
+                if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+                    return token;
+                }
+
+                goto done;
             }
         }
     }
@@ -1475,6 +1493,8 @@ njs_parser_for_statement(njs_vm_t *vm, n
 
     parser->node = node;
 
+done:
+
     return token;
 }
 
diff -r fca54d073408 -r ec44a042cf15 njs/njs_variable.c
--- a/njs/njs_variable.c	Thu Apr 18 19:17:22 2019 +0300
+++ b/njs/njs_variable.c	Fri Apr 19 00:28:31 2019 +0800
@@ -360,7 +360,6 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
     nxt_uint_t                scope_index;
     njs_index_t               index;
     njs_variable_t            *var;
-    const njs_value_t         *default_value;
     njs_variable_reference_t  *vr;
 
     vr = &node->u.reference;
@@ -398,14 +397,7 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
         var->argument = index;
     }
 
-    if (vr->type != NJS_DECLARATION && var->type <= NJS_VARIABLE_LET) {
-        goto not_found;
-    }
-
-    default_value = njs_is_object(&var->value) ? &var->value :
-                                                 &njs_value_undefined;
-
-    index = njs_scope_next_index(vm, vr->scope, scope_index, default_value);
+    index = njs_scope_next_index(vm, vr->scope, scope_index, &var->value);
 
     if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
         return NULL;


More information about the nginx-devel mailing list