changeset 884:ff7760036c67

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().
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 11 Apr 2019 21:24:16 +0300
parents 442f18a804b0
children 1213e0a2b485
files njs/njs.c njs/njs.h njs/njs_builtin.c njs/njs_json.c njs/njs_shell.c njs/test/njs_expect_test.exp
diffstat 6 files changed, 33 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- 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_init(vm);
     }
 
-    return njs_vm_value_dump(vm, dst, &vm->retval, 1);
+    return njs_vm_value_dump(vm, dst, &vm->retval, 0, 1);
 }
 
 
--- 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_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);
 
--- 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 @@
     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;
     }
 
--- 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 @@
 
     dst_end = dst + 64;
 
-    if (quote) {
-        *dst++ = quote;
-    }
+    *dst++ = quote;
 
     while (p < end) {
 
@@ -1877,9 +1875,7 @@
 
     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 @@
 
 
 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 @@
     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 @@
 
 #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 @@
 
 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 @@
     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;
         }
--- 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 @@
     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 @@
     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;
--- 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 @@
      "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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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 @@
     {"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