[njs] njs_vm_run() is simplified.

Dmitry Volyntsev xeioex at nginx.com
Wed Nov 7 15:41:48 UTC 2018


details:   http://hg.nginx.org/njs/rev/6ebf35347eee
branches:  
changeset: 642:6ebf35347eee
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Nov 07 18:41:29 2018 +0300
description:
njs_vm_run() is simplified.

diffstat:

 njs/njs.c       |  59 +++++++++++++++++---------------------------------------
 njs/njs.h       |   1 +
 njs/njs_shell.c |   1 +
 3 files changed, 20 insertions(+), 41 deletions(-)

diffs (131 lines):

diff -r f5ba2d1d3752 -r 6ebf35347eee njs/njs.c
--- a/njs/njs.c	Wed Nov 07 18:41:28 2018 +0300
+++ b/njs/njs.c	Wed Nov 07 18:41:29 2018 +0300
@@ -183,8 +183,6 @@ njs_vm_create(njs_vm_opt_t *options)
             if (nxt_slow_path(ret != NXT_OK)) {
                 return NULL;
             }
-
-            vm->retval = njs_value_void;
         }
     }
 
@@ -283,6 +281,13 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
     vm->scope_size = parser->scope_size;
     vm->variables_hash = parser->scope->variables;
 
+    if (vm->options.init) {
+        ret = njs_vm_init(vm);
+        if (nxt_slow_path(ret != NXT_OK)) {
+            return ret;
+        }
+    }
+
     return NJS_OK;
 
 fail:
@@ -360,8 +365,6 @@ njs_vm_clone(njs_vm_t *vm, njs_external_
             goto fail;
         }
 
-        nvm->retval = njs_value_void;
-
         return nvm;
     }
 
@@ -436,6 +439,10 @@ njs_vm_init(njs_vm_t *vm)
     vm->trace.handler = njs_parser_trace_handler;
     vm->trace.data = vm;
 
+    if (njs_is_null(&vm->retval)) {
+        vm->retval = njs_value_void;
+    }
+
     return NXT_OK;
 }
 
@@ -558,12 +565,9 @@ njs_vm_post_event(njs_vm_t *vm, njs_vm_e
 nxt_int_t
 njs_vm_run(njs_vm_t *vm)
 {
-    nxt_str_t  s;
     nxt_int_t  ret;
 
-    nxt_thread_log_debug("RUN:");
-
-    if (vm->backtrace != NULL) {
+    if (nxt_slow_path(vm->backtrace != NULL)) {
         nxt_array_reset(vm->backtrace);
     }
 
@@ -573,42 +577,15 @@ njs_vm_run(njs_vm_t *vm)
         ret = njs_vm_handle_events(vm);
     }
 
-    if (nxt_slow_path(ret == NXT_AGAIN)) {
-        nxt_thread_log_debug("VM: AGAIN");
-        return ret;
-    }
+    switch (ret) {
+    case NJS_STOP:
+        return NJS_OK;
 
-    if (nxt_slow_path(ret != NJS_STOP)) {
-        nxt_thread_log_debug("VM: ERROR");
+    case NXT_AGAIN:
+    case NXT_ERROR:
+    default:
         return ret;
     }
-
-    if (vm->retval.type == NJS_NUMBER) {
-        nxt_thread_log_debug("VM: %f", vm->retval.data.u.number);
-
-    } else if (vm->retval.type == NJS_BOOLEAN) {
-        nxt_thread_log_debug("VM: boolean: %d", vm->retval.data.truth);
-
-    } else if (vm->retval.type == NJS_STRING) {
-
-        if (njs_vm_value_to_ext_string(vm, &s, &vm->retval, 0) == NJS_OK) {
-            nxt_thread_log_debug("VM: '%V'", &s);
-        }
-
-    } else if (vm->retval.type == NJS_FUNCTION) {
-        nxt_thread_log_debug("VM: function");
-
-    } else if (vm->retval.type == NJS_NULL) {
-        nxt_thread_log_debug("VM: null");
-
-    } else if (vm->retval.type == NJS_VOID) {
-        nxt_thread_log_debug("VM: void");
-
-    } else {
-        nxt_thread_log_debug("VM: unknown: %d", vm->retval.type);
-    }
-
-    return NJS_OK;
 }
 
 
diff -r f5ba2d1d3752 -r 6ebf35347eee njs/njs.h
--- a/njs/njs.h	Wed Nov 07 18:41:28 2018 +0300
+++ b/njs/njs.h	Wed Nov 07 18:41:29 2018 +0300
@@ -143,6 +143,7 @@ typedef struct {
     njs_vm_ops_t                    *ops;
 
     uint8_t                         trailer;         /* 1 bit */
+    uint8_t                         init;            /* 1 bit */
     uint8_t                         accumulative;    /* 1 bit */
     uint8_t                         backtrace;       /* 1 bit */
     uint8_t                         sandbox;         /* 1 bit */
diff -r f5ba2d1d3752 -r 6ebf35347eee njs/njs_shell.c
--- a/njs/njs_shell.c	Wed Nov 07 18:41:28 2018 +0300
+++ b/njs/njs_shell.c	Wed Nov 07 18:41:29 2018 +0300
@@ -145,6 +145,7 @@ main(int argc, char **argv)
 
     nxt_memzero(&vm_options, sizeof(njs_vm_opt_t));
 
+    vm_options.init = !opts.interactive;
     vm_options.accumulative = opts.interactive;
     vm_options.backtrace = 1;
     vm_options.sandbox = opts.sandbox;


More information about the nginx-devel mailing list