[njs] Refactored functions related to variables.

Dmitry Volyntsev xeioex at nginx.com
Wed Dec 26 17:18:34 UTC 2018


details:   https://hg.nginx.org/njs/rev/1cac2060faae
branches:  
changeset: 698:1cac2060faae
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Dec 26 19:57:41 2018 +0300
description:
Refactored functions related to variables.

    1) njs_builtin_add() is refactored out as a minor variant of
       njs_variable_add().
    2) parser is refactored out from arguments of the functions.

diffstat:

 njs/njs_parser.c   |  87 ++++++++++++++++++++++++++++++++++------------
 njs/njs_parser.h   |   7 +--
 njs/njs_variable.c |  99 +++++++++++++++--------------------------------------
 njs/njs_variable.h |   8 ++-
 njs/njs_vm.h       |   1 +
 5 files changed, 101 insertions(+), 101 deletions(-)

diffs (442 lines):

diff -r 36989f329e16 -r 1cac2060faae njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Dec 26 16:05:23 2018 +0300
+++ b/njs/njs_parser.c	Wed Dec 26 19:57:41 2018 +0300
@@ -430,6 +430,23 @@ njs_parser_match(njs_vm_t *vm, njs_parse
 }
 
 
+nxt_inline njs_variable_t *
+njs_parser_variable_add(njs_vm_t *vm, njs_parser_t *parser,
+    njs_variable_type_t type)
+{
+    return njs_variable_add(vm, parser->scope, &parser->lexer->text,
+                            parser->lexer->key_hash, type);
+}
+
+nxt_inline njs_ret_t
+njs_parser_variable_reference(njs_vm_t *vm, njs_parser_t *parser,
+    njs_parser_node_t *node, njs_variable_reference_t type)
+{
+    return njs_variable_reference(vm, parser->scope, node, &parser->lexer->text,
+                                  parser->lexer->key_hash, type);
+}
+
+
 static njs_token_t
 njs_parser_function_declaration(njs_vm_t *vm, njs_parser_t *parser)
 {
@@ -463,12 +480,12 @@ njs_parser_function_declaration(njs_vm_t
         return NJS_TOKEN_ILLEGAL;
     }
 
-    var = njs_variable_add(vm, parser, NJS_VARIABLE_FUNCTION);
+    var = njs_parser_variable_add(vm, parser, NJS_VARIABLE_FUNCTION);
     if (nxt_slow_path(var == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    ret = njs_variable_reference(vm, parser, node, NJS_DECLARATION);
+    ret = njs_parser_variable_reference(vm, parser, node, NJS_DECLARATION);
     if (nxt_slow_path(ret != NXT_OK)) {
         return NJS_TOKEN_ERROR;
     }
@@ -542,7 +559,7 @@ njs_parser_function_expression(njs_vm_t 
     }
 
     if (token == NJS_TOKEN_NAME) {
-        var = njs_variable_add(vm, parser, NJS_VARIABLE_SHIM);
+        var = njs_parser_variable_add(vm, parser, NJS_VARIABLE_SHIM);
         if (nxt_slow_path(var == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -646,7 +663,7 @@ njs_parser_function_lambda(njs_vm_t *vm,
             return NJS_TOKEN_ILLEGAL;
         }
 
-        arg = njs_variable_add(vm, parser, NJS_VARIABLE_VAR);
+        arg = njs_parser_variable_add(vm, parser, NJS_VARIABLE_VAR);
         if (nxt_slow_path(arg == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -836,7 +853,7 @@ njs_parser_var_statement(njs_vm_t *vm, n
             return NJS_TOKEN_ILLEGAL;
         }
 
-        var = njs_variable_add(vm, parser, NJS_VARIABLE_VAR);
+        var = njs_parser_variable_add(vm, parser, NJS_VARIABLE_VAR);
         if (nxt_slow_path(var == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -848,7 +865,7 @@ njs_parser_var_statement(njs_vm_t *vm, n
 
         name->token = NJS_TOKEN_NAME;
 
-        ret = njs_variable_reference(vm, parser, name, NJS_DECLARATION);
+        ret = njs_parser_variable_reference(vm, parser, name, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1299,7 +1316,7 @@ njs_parser_for_var_statement(njs_vm_t *v
             return NJS_TOKEN_ILLEGAL;
         }
 
-        var = njs_variable_add(vm, parser, NJS_VARIABLE_VAR);
+        var = njs_parser_variable_add(vm, parser, NJS_VARIABLE_VAR);
         if (nxt_slow_path(var == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1311,7 +1328,7 @@ njs_parser_for_var_statement(njs_vm_t *v
 
         name->token = NJS_TOKEN_NAME;
 
-        ret = njs_variable_reference(vm, parser, name, NJS_DECLARATION);
+        ret = njs_parser_variable_reference(vm, parser, name, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1584,7 +1601,7 @@ njs_parser_try_statement(njs_vm_t *vm, n
             return NJS_TOKEN_ERROR;
         }
 
-        var = njs_variable_add(vm, parser, NJS_VARIABLE_CATCH);
+        var = njs_parser_variable_add(vm, parser, NJS_VARIABLE_CATCH);
         if (nxt_slow_path(var == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1596,7 +1613,7 @@ njs_parser_try_statement(njs_vm_t *vm, n
 
         node->token = NJS_TOKEN_NAME;
 
-        ret = njs_variable_reference(vm, parser, node, NJS_DECLARATION);
+        ret = njs_parser_variable_reference(vm, parser, node, NJS_DECLARATION);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1816,7 +1833,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
             break;
         }
 
-        ret = njs_variable_reference(vm, parser, node, NJS_REFERENCE);
+        ret = njs_parser_variable_reference(vm, parser, node, NJS_REFERENCE);
         if (nxt_slow_path(ret != NXT_OK)) {
             return NJS_TOKEN_ERROR;
         }
@@ -2075,11 +2092,23 @@ static njs_token_t
 njs_parser_builtin_object(njs_vm_t *vm, njs_parser_t *parser,
     njs_parser_node_t *node)
 {
-    njs_ret_t       ret;
-    nxt_uint_t      index;
-    njs_variable_t  *var;
-
-    var = njs_builtin_add(vm, parser);
+    uint32_t            hash;
+    nxt_str_t           *name;
+    njs_ret_t           ret;
+    nxt_uint_t          index;
+    njs_variable_t      *var;
+    njs_parser_scope_t  *scope;
+
+    scope = parser->scope;
+
+    while (scope->type != NJS_SCOPE_GLOBAL) {
+        scope = scope->parent;
+    }
+
+    hash = parser->lexer->key_hash;
+    name = &parser->lexer->text;
+
+    var = njs_variable_add(vm, scope, name, hash, NJS_VARIABLE_VAR);
     if (nxt_slow_path(var == NULL)) {
         return NJS_TOKEN_ERROR;
     }
@@ -2090,7 +2119,7 @@ njs_parser_builtin_object(njs_vm_t *vm, 
     var->value.type = NJS_OBJECT;
     var->value.data.truth = 1;
 
-    ret = njs_variable_reference(vm, parser, node, NJS_REFERENCE);
+    ret = njs_variable_reference(vm, scope, node, name, hash, NJS_REFERENCE);
     if (nxt_slow_path(ret != NXT_OK)) {
         return NJS_TOKEN_ERROR;
     }
@@ -2107,11 +2136,23 @@ static njs_token_t
 njs_parser_builtin_function(njs_vm_t *vm, njs_parser_t *parser,
     njs_parser_node_t *node)
 {
-    njs_ret_t       ret;
-    nxt_uint_t      index;
-    njs_variable_t  *var;
-
-    var = njs_builtin_add(vm, parser);
+    uint32_t            hash;
+    nxt_str_t           *name;
+    njs_ret_t           ret;
+    nxt_uint_t          index;
+    njs_variable_t      *var;
+    njs_parser_scope_t  *scope;
+
+    scope = parser->scope;
+
+    while (scope->type != NJS_SCOPE_GLOBAL) {
+        scope = scope->parent;
+    }
+
+    hash = parser->lexer->key_hash;
+    name = &parser->lexer->text;
+
+    var = njs_variable_add(vm, scope, name, hash, NJS_VARIABLE_VAR);
     if (nxt_slow_path(var == NULL)) {
         return NJS_TOKEN_ERROR;
     }
@@ -2122,7 +2163,7 @@ njs_parser_builtin_function(njs_vm_t *vm
     var->value.type = NJS_FUNCTION;
     var->value.data.truth = 1;
 
-    ret = njs_variable_reference(vm, parser, node, NJS_REFERENCE);
+    ret = njs_variable_reference(vm, scope, node, name, hash, NJS_REFERENCE);
     if (nxt_slow_path(ret != NXT_OK)) {
         return NJS_TOKEN_ERROR;
     }
diff -r 36989f329e16 -r 1cac2060faae njs/njs_parser.h
--- a/njs/njs_parser.h	Wed Dec 26 16:05:23 2018 +0300
+++ b/njs/njs_parser.h	Wed Dec 26 19:57:41 2018 +0300
@@ -250,8 +250,6 @@ struct njs_parser_scope_s {
 };
 
 
-typedef struct njs_parser_node_s    njs_parser_node_t;
-
 struct njs_parser_node_s {
     njs_token_t                     token:16;
     uint8_t                         ctor:1;
@@ -328,8 +326,9 @@ njs_token_t njs_parser_property_name(njs
 njs_token_t njs_parser_property_token(njs_parser_t *parser);
 njs_token_t njs_parser_token(njs_parser_t *parser);
 nxt_int_t njs_parser_string_create(njs_vm_t *vm, njs_value_t *value);
-njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_t *parser,
-    njs_parser_node_t *node, njs_variable_reference_t reference);
+njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
+    njs_variable_reference_t reference);
 njs_variable_t *njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node);
 njs_index_t njs_variable_typeof(njs_vm_t *vm, njs_parser_node_t *node);
 njs_index_t njs_variable_index(njs_vm_t *vm, njs_parser_node_t *node);
diff -r 36989f329e16 -r 1cac2060faae njs/njs_variable.c
--- a/njs/njs_variable.c	Wed Dec 26 16:05:23 2018 +0300
+++ b/njs/njs_variable.c	Wed Dec 26 19:57:41 2018 +0300
@@ -1,6 +1,7 @@
 
 /*
  * Copyright (C) Igor Sysoev
+ * Copyright (C) Dmitry Volyntsev
  * Copyright (C) NGINX, Inc.
  */
 
@@ -15,8 +16,8 @@ typedef struct {
 } njs_variable_scope_t;
 
 
-static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_node_t *node,
-    njs_variable_scope_t *vs);
+static njs_ret_t  njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_variable_scope_t *vs, nxt_str_t *name, uint32_t hash);
 static njs_variable_t *njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name,
     njs_variable_type_t type);
 
@@ -48,67 +49,17 @@ const nxt_lvlhsh_proto_t  njs_variables_
 
 
 njs_variable_t *
-njs_builtin_add(njs_vm_t *vm, njs_parser_t *parser)
+njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope, nxt_str_t *name,
+    uint32_t hash, njs_variable_type_t type)
 {
     nxt_int_t           ret;
     njs_variable_t      *var;
-    njs_parser_scope_t  *scope;
     nxt_lvlhsh_query_t  lhq;
 
-    lhq.key_hash = parser->lexer->key_hash;
-    lhq.key = parser->lexer->text;
+    lhq.key_hash = hash;
+    lhq.key = *name;
     lhq.proto = &njs_variables_hash_proto;
 
-    scope = parser->scope;
-
-    while (scope->type != NJS_SCOPE_GLOBAL) {
-        scope = scope->parent;
-    }
-
-    if (nxt_lvlhsh_find(&scope->variables, &lhq) == NXT_OK) {
-        var = lhq.value;
-
-        return var;
-    }
-
-    var = njs_variable_alloc(vm, &lhq.key, NJS_VARIABLE_VAR);
-    if (nxt_slow_path(var == NULL)) {
-        return var;
-    }
-
-    lhq.replace = 0;
-    lhq.value = var;
-    lhq.pool = vm->mem_cache_pool;
-
-    ret = nxt_lvlhsh_insert(&scope->variables, &lhq);
-
-    if (nxt_fast_path(ret == NXT_OK)) {
-        return var;
-    }
-
-    njs_internal_error(vm, "lvlhsh insert failed");
-
-    nxt_mem_cache_free(vm->mem_cache_pool, var->name.start);
-    nxt_mem_cache_free(vm->mem_cache_pool, var);
-
-    return NULL;
-}
-
-
-njs_variable_t *
-njs_variable_add(njs_vm_t *vm, njs_parser_t *parser, njs_variable_type_t type)
-{
-    nxt_int_t           ret;
-    njs_variable_t      *var;
-    njs_parser_scope_t  *scope;
-    nxt_lvlhsh_query_t  lhq;
-
-    lhq.key_hash = parser->lexer->key_hash;
-    lhq.key = parser->lexer->text;
-    lhq.proto = &njs_variables_hash_proto;
-
-    scope = parser->scope;
-
     if (type >= NJS_VARIABLE_VAR) {
         /*
          * A "var" and "function" declarations are
@@ -175,17 +126,18 @@ const nxt_lvlhsh_proto_t  njs_reference_
 
 
 njs_ret_t
-njs_variable_reference(njs_vm_t *vm, njs_parser_t *parser,
-    njs_parser_node_t *node, njs_variable_reference_t reference)
+njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
+    njs_variable_reference_t reference)
 {
     njs_ret_t           ret;
     nxt_lvlhsh_query_t  lhq;
 
-    ret = njs_name_copy(vm, &node->u.variable_name, &parser->lexer->text);
+    ret = njs_name_copy(vm, &node->u.variable_name, name);
 
     if (nxt_fast_path(ret == NXT_OK)) {
-        node->variable_name_hash = parser->lexer->key_hash;
-        node->scope = parser->scope;
+        node->variable_name_hash = hash;
+        node->scope = scope;
         node->reference = reference;
 
         lhq.key_hash = node->variable_name_hash;
@@ -195,7 +147,7 @@ njs_variable_reference(njs_vm_t *vm, njs
         lhq.value = node;
         lhq.pool = vm->mem_cache_pool;
 
-        ret = nxt_lvlhsh_insert(&parser->scope->references, &lhq);
+        ret = nxt_lvlhsh_insert(&scope->references, &lhq);
 
         if (nxt_slow_path(ret != NXT_ERROR)) {
             ret = NXT_OK;
@@ -241,7 +193,9 @@ njs_variables_scope_resolve(njs_vm_t *vm
             }
 
             if (!local_scope) {
-                ret = njs_variable_find(vm, node, &vs);
+                ret = njs_variable_find(vm, node->scope, &vs,
+                                        &node->u.variable_name,
+                                        node->variable_name_hash);
                 if (nxt_slow_path(ret != NXT_OK)) {
                     continue;
                 }
@@ -308,7 +262,8 @@ njs_variable_typeof(njs_vm_t *vm, njs_pa
         return node->index;
     }
 
-    ret = njs_variable_find(vm, node, &vs);
+    ret = njs_variable_find(vm, node->scope, &vs, &node->u.variable_name,
+                            node->variable_name_hash);
 
     if (nxt_fast_path(ret == NXT_OK)) {
         return vs.variable->index;
@@ -348,7 +303,8 @@ njs_variable_get(njs_vm_t *vm, njs_parse
     njs_variable_t        *var;
     njs_variable_scope_t  vs;
 
-    ret = njs_variable_find(vm, node, &vs);
+    ret = njs_variable_find(vm, node->scope, &vs, &node->u.variable_name,
+                            node->variable_name_hash);
 
     if (nxt_slow_path(ret != NXT_OK)) {
         goto not_found;
@@ -446,17 +402,16 @@ not_found:
 
 
 static njs_ret_t
-njs_variable_find(njs_vm_t *vm, njs_parser_node_t *node,
-    njs_variable_scope_t *vs)
+njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_variable_scope_t *vs, nxt_str_t *name, uint32_t hash)
 {
-    njs_parser_scope_t  *scope, *parent, *previous;
+    njs_parser_scope_t  *parent, *previous;
 
-    vs->lhq.key_hash = node->variable_name_hash;
-    vs->lhq.key = node->u.variable_name;
+    vs->lhq.key_hash = hash;
+    vs->lhq.key = *name;
     vs->lhq.proto = &njs_variables_hash_proto;
 
     previous = NULL;
-    scope = node->scope;
 
     for ( ;; ) {
         if (nxt_lvlhsh_find(&scope->variables, &vs->lhq) == NXT_OK) {
@@ -517,6 +472,8 @@ njs_variable_alloc(njs_vm_t *vm, nxt_str
 
     nxt_mem_cache_free(vm->mem_cache_pool, var);
 
+    njs_memory_error(vm);
+
     return NULL;
 }
 
diff -r 36989f329e16 -r 1cac2060faae njs/njs_variable.h
--- a/njs/njs_variable.h	Wed Dec 26 16:05:23 2018 +0300
+++ b/njs/njs_variable.h	Wed Dec 26 19:57:41 2018 +0300
@@ -42,9 +42,11 @@ typedef struct {
          + njs_scope_offset((var)->index) - NJS_INDEX_GLOBAL_OFFSET)
 
 
-njs_variable_t *njs_builtin_add(njs_vm_t *vm, njs_parser_t *parser);
-njs_variable_t *njs_variable_add(njs_vm_t *vm, njs_parser_t *parser,
-    njs_variable_type_t type);
+njs_variable_t *njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope,
+    nxt_str_t *name, uint32_t hash, njs_variable_type_t type);
+njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_parser_node_t *node, nxt_str_t *name, uint32_t hash,
+    njs_variable_reference_t reference);
 njs_ret_t njs_variables_scope_reference(njs_vm_t *vm,
     njs_parser_scope_t *scope);
 njs_ret_t njs_name_copy(njs_vm_t *vm, nxt_str_t *dst, nxt_str_t *src);
diff -r 36989f329e16 -r 1cac2060faae njs/njs_vm.h
--- a/njs/njs_vm.h	Wed Dec 26 16:05:23 2018 +0300
+++ b/njs/njs_vm.h	Wed Dec 26 19:57:41 2018 +0300
@@ -165,6 +165,7 @@ typedef struct njs_frame_s            nj
 typedef struct njs_native_frame_s     njs_native_frame_t;
 typedef struct njs_property_next_s    njs_property_next_t;
 typedef struct njs_parser_scope_s     njs_parser_scope_t;
+typedef struct njs_parser_node_s      njs_parser_node_t;
 
 
 union njs_value_s {


More information about the nginx-devel mailing list