[njs] Shell: improved njs_vm_value_dump().

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 11 18:24:46 UTC 2019


details:   https://hg.nginx.org/njs/rev/ff7760036c67
branches:  
changeset: 884:ff7760036c67
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Apr 11 21:24:16 2019 +0300
description:
Shell: improved njs_vm_value_dump().

njs_vm_value_dump() is used in two modes:
1) printing value of the previous expression in the shell.
2) console.log().

The behavior is different. Strings are printed with quotes
in the first case, but without in the second. Also special
ASCII symbols should not be escaped in console.log().

diffstat:

 njs/njs.c                    |   2 +-
 njs/njs.h                    |   2 +-
 njs/njs_builtin.c            |   2 +-
 njs/njs_json.c               |  27 ++++++++++++---------------
 njs/njs_shell.c              |   4 ++--
 njs/test/njs_expect_test.exp |  30 ++++++++++++++++--------------
 6 files changed, 33 insertions(+), 34 deletions(-)

diffs (272 lines):

diff -r 442f18a804b0 -r ff7760036c67 njs/njs.c
--- a/njs/njs.c	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/njs.c	Thu Apr 11 21:24:16 2019 +0300
@@ -706,7 +706,7 @@ njs_vm_retval_dump(njs_vm_t *vm, nxt_str
         njs_vm_init(vm);
     }
 
-    return njs_vm_value_dump(vm, dst, &vm->retval, 1);
+    return njs_vm_value_dump(vm, dst, &vm->retval, 0, 1);
 }
 
 
diff -r 442f18a804b0 -r ff7760036c67 njs/njs.h
--- a/njs/njs.h	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/njs.h	Thu Apr 11 21:24:16 2019 +0300
@@ -249,7 +249,7 @@ NXT_EXPORT njs_ret_t njs_vm_value_to_ext
 NXT_EXPORT njs_ret_t njs_vm_retval_to_ext_string(njs_vm_t *vm, nxt_str_t *dst);
 
 NXT_EXPORT njs_ret_t njs_vm_value_dump(njs_vm_t *vm, nxt_str_t *dst,
-    const njs_value_t *value, nxt_uint_t indent);
+    const njs_value_t *value, nxt_uint_t console, nxt_uint_t indent);
 NXT_EXPORT njs_ret_t njs_vm_retval_dump(njs_vm_t *vm, nxt_str_t *dst,
     nxt_uint_t indent);
 
diff -r 442f18a804b0 -r ff7760036c67 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/njs_builtin.c	Thu Apr 11 21:24:16 2019 +0300
@@ -1057,7 +1057,7 @@ njs_dump_value(njs_vm_t *vm, njs_value_t
     n = njs_primitive_value_to_integer(indent);
     n = nxt_min(n, 5);
 
-    if (njs_vm_value_dump(vm, &str, value, n) != NXT_OK) {
+    if (njs_vm_value_dump(vm, &str, value, 1, n) != NXT_OK) {
         return NXT_ERROR;
     }
 
diff -r 442f18a804b0 -r ff7760036c67 njs/njs_json.c
--- a/njs/njs_json.c	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/njs_json.c	Thu Apr 11 21:24:16 2019 +0300
@@ -1798,9 +1798,7 @@ njs_json_append_string(njs_json_stringif
 
     dst_end = dst + 64;
 
-    if (quote) {
-        *dst++ = quote;
-    }
+    *dst++ = quote;
 
     while (p < end) {
 
@@ -1877,9 +1875,7 @@ njs_json_append_string(njs_json_stringif
 
     njs_json_buf_written(stringify, dst - stringify->last->pos);
 
-    if (quote) {
-        njs_json_buf_append(stringify, &quote, 1);
-    }
+    njs_json_buf_append(stringify, &quote, 1);
 
     return NXT_OK;
 }
@@ -2119,9 +2115,9 @@ const njs_object_init_t  njs_json_object
 
 
 static nxt_int_t
-njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value)
+njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value,
+    nxt_uint_t console)
 {
-    char                quote;
     njs_ret_t           ret;
     nxt_str_t           str;
     nxt_uint_t          written;
@@ -2146,12 +2142,13 @@ njs_dump_value(njs_json_stringify_t *str
     case NJS_STRING:
         njs_string_get(value, &str);
 
-        quote = '\0';
-        if (stringify->stack.items != 0) {
-            quote = '\'';
+        if (!console || stringify->stack.items != 0) {
+            return njs_json_append_string(stringify, value, '\'');
         }
 
-        return njs_json_append_string(stringify, value, quote);
+        return njs_json_buf_append(stringify, (char *) str.start, str.length);
+
+        break;
 
     case NJS_OBJECT_NUMBER:
         value = &value->data.u.object_value->value;
@@ -2326,7 +2323,7 @@ memory_error:
 
 #define njs_dump_append_value(value)                                          \
     state->written = 1;                                                       \
-    ret = njs_dump_value(stringify, value);                                   \
+    ret = njs_dump_value(stringify, value, console);                          \
     if (nxt_slow_path(ret != NXT_OK)) {                                       \
         if (ret == NXT_DECLINED) {                                            \
             goto exception;                                                   \
@@ -2338,7 +2335,7 @@ memory_error:
 
 njs_ret_t
 njs_vm_value_dump(njs_vm_t *vm, nxt_str_t *retval, const njs_value_t *value,
-    nxt_uint_t indent)
+    nxt_uint_t console, nxt_uint_t indent)
 {
     nxt_int_t             i;
     njs_ret_t             ret;
@@ -2366,7 +2363,7 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_
     stringify->stack.items = 0;
 
     if (!njs_dump_is_object(value)) {
-        ret = njs_dump_value(stringify, value);
+        ret = njs_dump_value(stringify, value, console);
         if (nxt_slow_path(ret != NXT_OK)) {
             goto memory_error;
         }
diff -r 442f18a804b0 -r ff7760036c67 njs/njs_shell.c
--- a/njs/njs_shell.c	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/njs_shell.c	Thu Apr 11 21:24:16 2019 +0300
@@ -930,7 +930,7 @@ njs_ext_console_log(njs_vm_t *vm, njs_va
     n = 1;
 
     while (n < nargs) {
-        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 0)
+        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1, 0)
             == NJS_ERROR)
         {
             return NJS_ERROR;
@@ -962,7 +962,7 @@ njs_ext_console_dump(njs_vm_t *vm, njs_v
     n = 1;
 
     while (n < nargs) {
-        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1)
+        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1, 1)
             == NJS_ERROR)
         {
             return NJS_ERROR;
diff -r 442f18a804b0 -r ff7760036c67 njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Thu Apr 11 20:09:42 2019 +0300
+++ b/njs/test/njs_expect_test.exp	Thu Apr 11 21:24:16 2019 +0300
@@ -192,6 +192,8 @@ njs_test {
      "console.log(1)\r\n1\r\nundefined\r\n>> "}
     {"console.log(1, 'a')\r\n"
      "console.log(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
+    {"console.log('\\tабв\\nгд')\r\n"
+     "console.log('\\\\tабв\\\\nгд')\r\n\tабв\r\nгд\r\nundefined\r\n>> "}
     {"console.dump()\r\n"
      "console.dump()\r\nundefined\r\n>> "}
     {"console.dump(1)\r\n"
@@ -315,7 +317,7 @@ njs_test {
     {"var a = 1 + 1; setTimeout(function (x) {a = x}, 0, 'a'); a\r\n"
      "2"}
     {"a\r\n"
-     "a\r\na"}
+     "a\r\n'a'"}
 }
 
 njs_test {
@@ -327,14 +329,14 @@ njs_test {
     {"var a = 1 + 1; setTimeout(function (x) { setTimeout(function (y) {a = y}, 0, x)}, 0, 'a'); a\r\n"
      "2"}
     {"a\r\n"
-     "a\r\na"}
+     "a\r\n'a'"}
 }
 
 njs_test {
     {"var a = 1 + 1; setImmediate(function (x) { setImmediate(function (y) {a = y}, x)}, 'a'); a\r\n"
      "2"}
     {"a\r\n"
-     "a\r\na"}
+     "a\r\n'a'"}
 }
 
 njs_test {
@@ -362,7 +364,7 @@ njs_test {
     {"var i = 0, queue = []; (function x() { if (i < 5) setImmediate(x); queue.push(i++); })()\r\n"
      "undefined"}
     {"queue.toString()\r\n"
-     "queue.toString()\r\n0,1,2,3,4,5"}
+     "queue.toString()\r\n'0,1,2,3,4,5'"}
 }
 
 # require('fs')
@@ -426,35 +428,35 @@ njs_test {
     {"var fs = require('fs')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs/test/fs/utf8').toString('base64')\r\n"
-     "zrHOslrOsw==\r\n>> "}
+     "'zrHOslrOsw=='\r\n>> "}
 }
 
 njs_test {
     {"var fs = require('fs')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs/test/fs/utf8', 'utf8')[2]\r\n"
-     "Z\r\n>> "}
+     "'Z'\r\n>> "}
 }
 
 njs_test {
     {"var fs = require('fs')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs/test/fs/utf8')[4]\r\n"
-     "Z\r\n>> "}
+     "'Z'\r\n>> "}
 }
 
 njs_test {
     {"var fs = require('fs')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs/test/fs/utf8', {encoding:'utf8',flag:'r+'})\r\n"
-     "αβZγ\r\n>> "}
+     "'αβZγ'\r\n>> "}
 }
 
 njs_test {
     {"var fs = require('fs'), fn = 'njs/test/fs/ascii'\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync(fn)[599] + fs.readFileSync(fn, 'utf8')[599]\r\n"
-     "xx\r\n>> "}
+     "'xx'\r\n>> "}
 }
 
 njs_test {
@@ -532,7 +534,7 @@ njs_test {
     {"fs.writeFileSync('njs_test_file2', 'ABC')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs_test_file2')\r\n"
-     "ABC\r\n>> "}
+     "'ABC'\r\n>> "}
 }
 
 njs_test {
@@ -541,7 +543,7 @@ njs_test {
     {"fs.writeFileSync('njs_test_file2', 'ABC', 'utf8')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs_test_file2')\r\n"
-     "ABC\r\n>> "}
+     "'ABC'\r\n>> "}
 }
 
 njs_test {
@@ -552,7 +554,7 @@ njs_test {
     {"fs.writeFileSync('njs_test_file2', 'ABC')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs_test_file2')\r\n"
-     "ABC\r\n>> "}
+     "'ABC'\r\n>> "}
 }
 
 njs_test {
@@ -561,7 +563,7 @@ njs_test {
     {"fs.writeFileSync('njs_test_file2', 'ABC', {encoding:'utf8', mode:0o666})\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs_test_file2')\r\n"
-     "ABC\r\n>> "}
+     "'ABC'\r\n>> "}
 }
 
 exec rm -fr njs_wo_file
@@ -600,7 +602,7 @@ njs_test {
     {"fs.appendFileSync('njs_test_file2', 'ABC')\r\n"
      "undefined\r\n>> "}
     {"fs.readFileSync('njs_test_file2')\r\n"
-     "ABCABC\r\n>> "}
+     "'ABCABC'\r\n>> "}
 }
 
 # Modules


More information about the nginx-devel mailing list