[njs] Simplified typeof operation.

Dmitry Volyntsev xeioex at nginx.com
Fri Apr 19 13:05:00 UTC 2019


details:   https://hg.nginx.org/njs/rev/a6c82ddff460
branches:  
changeset: 911:a6c82ddff460
user:      hongzhidao <hongzhidao at gmail.com>
date:      Fri Apr 19 20:03:49 2019 +0800
description:
Simplified typeof operation.

diffstat:

 njs/njs_generator.c |  23 +++++++++++++++++------
 njs/njs_variable.c  |   2 +-
 njs/njs_vm.c        |   7 +------
 3 files changed, 19 insertions(+), 13 deletions(-)

diffs (111 lines):

diff -r 6d7a4fb82b25 -r a6c82ddff460 njs/njs_generator.c
--- a/njs/njs_generator.c	Thu Apr 18 20:51:53 2019 +0300
+++ b/njs/njs_generator.c	Fri Apr 19 20:03:49 2019 +0800
@@ -575,7 +575,7 @@ njs_generate_builtin_object(njs_vm_t *vm
     njs_vmcode_object_copy_t  *copy;
 
     index = njs_variable_index(vm, node);
-    if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+    if (nxt_slow_path(index == NJS_INDEX_NONE)) {
         return NXT_ERROR;
     }
 
@@ -600,7 +600,7 @@ njs_generate_variable(njs_vm_t *vm, njs_
     njs_index_t  index;
 
     index = njs_variable_index(vm, node);
-    if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+    if (nxt_slow_path(index == NJS_INDEX_NONE)) {
         return NXT_ERROR;
     }
 
@@ -622,7 +622,7 @@ njs_generate_var_statement(njs_vm_t *vm,
     lvalue = node->left;
 
     index = njs_variable_index(vm, lvalue);
-    if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+    if (nxt_slow_path(index == NJS_INDEX_NONE)) {
         return NXT_ERROR;
     }
 
@@ -2128,6 +2128,17 @@ njs_generate_typeof_operation(njs_vm_t *
     if (expr->token == NJS_TOKEN_NAME) {
         expr->index = njs_variable_typeof(vm, expr);
 
+        if (expr->u.reference.variable) {
+            ret = njs_generate_variable(vm, generator, expr);
+            if (nxt_slow_path(ret != NXT_OK)) {
+                return NXT_ERROR;
+            }
+
+        } else {
+            expr->index = njs_value_index(vm, &njs_value_undefined,
+                                          generator->runtime);
+        }
+
     } else {
         ret = njs_generator(vm, generator, node->left);
         if (nxt_slow_path(ret != NXT_OK)) {
@@ -2794,7 +2805,7 @@ njs_generate_try_statement(njs_vm_t *vm,
         /* A "try/catch" case. */
 
         catch_index = njs_variable_index(vm, node->left);
-        if (nxt_slow_path(catch_index == NJS_INDEX_ERROR)) {
+        if (nxt_slow_path(catch_index == NJS_INDEX_NONE)) {
             return NXT_ERROR;
         }
 
@@ -2853,7 +2864,7 @@ njs_generate_try_statement(njs_vm_t *vm,
             /* A try/catch/finally case. */
 
             catch_index = njs_variable_index(vm, node->left->left);
-            if (nxt_slow_path(catch_index == NJS_INDEX_ERROR)) {
+            if (nxt_slow_path(catch_index == NJS_INDEX_NONE)) {
                 return NXT_ERROR;
             }
 
@@ -3039,7 +3050,7 @@ njs_generate_import_statement(njs_vm_t *
     expr = node->right;
 
     index = njs_variable_index(vm, lvalue);
-    if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+    if (nxt_slow_path(index == NJS_INDEX_NONE)) {
         return NXT_ERROR;
     }
 
diff -r 6d7a4fb82b25 -r a6c82ddff460 njs/njs_variable.c
--- a/njs/njs_variable.c	Thu Apr 18 20:51:53 2019 +0300
+++ b/njs/njs_variable.c	Fri Apr 19 20:03:49 2019 +0800
@@ -349,7 +349,7 @@ njs_variable_index(njs_vm_t *vm, njs_par
         return var->index;
     }
 
-    return NJS_INDEX_ERROR;
+    return NJS_INDEX_NONE;
 }
 
 
diff -r 6d7a4fb82b25 -r a6c82ddff460 njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Apr 18 20:51:53 2019 +0300
+++ b/njs/njs_vm.c	Fri Apr 19 20:03:49 2019 +0800
@@ -993,8 +993,6 @@ 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[NJS_TYPE_MAX] = {
@@ -1034,10 +1032,7 @@ njs_vmcode_typeof(njs_vm_t *vm, njs_valu
         &njs_string_object,
     };
 
-    /* A zero index means non-declared variable. */
-    type = (value != NULL) ? value->type : NJS_UNDEFINED;
-
-    vm->retval = *types[type];
+    vm->retval = *types[value->type];
 
     return sizeof(njs_vmcode_2addr_t);
 }


More information about the nginx-devel mailing list