[njs] The "typeof" operation changes.

Igor Sysoev igor at sysoev.ru
Tue Mar 28 16:24:58 UTC 2017


details:   http://hg.nginx.org/njs/rev/0bde7f156477
branches:  
changeset: 320:0bde7f156477
user:      Igor Sysoev <igor at sysoev.ru>
date:      Sat Mar 25 13:42:40 2017 +0300
description:
The "typeof" operation changes.

diffstat:

 njs/njs_variable.c |  19 +++----------------
 njs/njs_vm.c       |   7 ++++++-
 2 files changed, 9 insertions(+), 17 deletions(-)

diffs (86 lines):

diff -r 8e1030bce13c -r 0bde7f156477 njs/njs_variable.c
--- a/njs/njs_variable.c	Tue Mar 21 16:35:36 2017 +0300
+++ b/njs/njs_variable.c	Sat Mar 25 13:42:40 2017 +0300
@@ -176,14 +176,12 @@ njs_variable_t *
 njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node,
     njs_name_reference_t reference)
 {
-    nxt_int_t           ret;
     nxt_array_t         *values;
     njs_index_t         index;
     njs_value_t         *value;
     njs_variable_t      *var;
     njs_parser_scope_t  *scope, *parent, *inclusive;
     nxt_lvlhsh_query_t  lhq;
-    const njs_value_t   *initial;
 
     lhq.key_hash = node->variable_name_hash;
     lhq.key = node->u.variable_name;
@@ -209,8 +207,6 @@ njs_variable_get(njs_vm_t *vm, njs_parse
                 }
             }
 
-            initial = &njs_value_void;
-
             goto found;
         }
 
@@ -229,23 +225,14 @@ njs_variable_get(njs_vm_t *vm, njs_parse
         goto not_found;
     }
 
-    /* Add variable referenced by typeof to the global scope. */
-
     var = njs_variable_alloc(vm, &lhq.key, NJS_VARIABLE_TYPEOF);
     if (nxt_slow_path(var == NULL)) {
         return NULL;
     }
 
-    lhq.replace = 0;
-    lhq.value = var;
-    lhq.pool = vm->mem_cache_pool;
+    var->index = NJS_INDEX_NONE;
 
-    ret = nxt_lvlhsh_insert(&scope->variables, &lhq);
-    if (nxt_slow_path(ret != NXT_OK)) {
-        return NULL;
-    }
-
-    initial = &njs_value_invalid;
+    return var;
 
 found:
 
@@ -285,7 +272,7 @@ found:
         *value = var->value;
 
     } else {
-        *value = *initial;
+        *value = njs_value_void;
     }
 
     index = scope->next_index;
diff -r 8e1030bce13c -r 0bde7f156477 njs/njs_vm.c
--- a/njs/njs_vm.c	Tue Mar 21 16:35:36 2017 +0300
+++ b/njs/njs_vm.c	Sat Mar 25 13:42:40 2017 +0300
@@ -1444,6 +1444,8 @@ njs_vmcode_post_decrement(njs_vm_t *vm, 
 njs_ret_t
 njs_vmcode_typeof(njs_vm_t *vm, njs_value_t *value, njs_value_t *invld)
 {
+    nxt_uint_t  type;
+
     /* ECMAScript 5.1: null, array and regexp are objects. */
 
     static const njs_value_t  *types[] = {
@@ -1466,7 +1468,10 @@ njs_vmcode_typeof(njs_vm_t *vm, njs_valu
         &njs_string_object,
     };
 
-    vm->retval = *types[value->type];
+    /* A zero index means non-declared variable. */
+    type = (value != NULL) ? value->type : NJS_VOID;
+
+    vm->retval = *types[type];
 
     return sizeof(njs_vmcode_2addr_t);
 }


More information about the nginx-devel mailing list