]> git.kaiwu.me - njs.git/commitdiff
The "typeof" operation changes.
authorIgor Sysoev <igor@sysoev.ru>
Sat, 25 Mar 2017 10:42:40 +0000 (13:42 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Sat, 25 Mar 2017 10:42:40 +0000 (13:42 +0300)
njs/njs_variable.c
njs/njs_vm.c

index 9a5e59475526173336f6e36f8bfbae20b047fbbb..7cc48bfc06ec3ccd8cde978a8e4ccd347ecdf8a8 100644 (file)
@@ -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_parser_node_t *node,
                 }
             }
 
-            initial = &njs_value_void;
-
             goto found;
         }
 
@@ -229,23 +225,14 @@ njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node,
         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;
index c926d5b14c0c54b3fb7d630eb47f1f72c1cd95d3..7b15e8f54a78a91e704bbe1844a77ad1805c9e7d 100644 (file)
@@ -1444,6 +1444,8 @@ njs_vmcode_post_decrement(njs_vm_t *vm, njs_value_t *reference,
 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_value_t *value, njs_value_t *invld)
         &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);
 }