From ecb607956189ecb6b06f8bae09530c4da48f11fb Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 11 Apr 2019 21:24:16 +0300 Subject: [PATCH] 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(). --- 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(-) diff --git a/njs/njs.c b/njs/njs.c index cdfb36a6..950ee708 100644 --- a/njs/njs.c +++ b/njs/njs.c @@ -706,7 +706,7 @@ njs_vm_retval_dump(njs_vm_t *vm, nxt_str_t *dst, nxt_uint_t indent) 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 --git a/njs/njs.h b/njs/njs.h index f3d317fd..51cc6ae1 100644 --- a/njs/njs.h +++ b/njs/njs.h @@ -249,7 +249,7 @@ NXT_EXPORT njs_ret_t njs_vm_value_to_ext_string(njs_vm_t *vm, nxt_str_t *dst, 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 --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 62241c94..82ecd6ee 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -1057,7 +1057,7 @@ njs_dump_value(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, 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 --git a/njs/njs_json.c b/njs/njs_json.c index bea2f244..0573bd81 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -1798,9 +1798,7 @@ njs_json_append_string(njs_json_stringify_t *stringify, dst_end = dst + 64; - if (quote) { - *dst++ = quote; - } + *dst++ = quote; while (p < end) { @@ -1877,9 +1875,7 @@ njs_json_append_string(njs_json_stringify_t *stringify, njs_json_buf_written(stringify, dst - stringify->last->pos); - if (quote) { - njs_json_buf_append(stringify, "e, 1); - } + njs_json_buf_append(stringify, "e, 1); return NXT_OK; } @@ -2119,9 +2115,9 @@ const njs_object_init_t njs_json_object_init = { 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 *stringify, const njs_value_t *value) 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_t *retval, const njs_value_t *value, 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 --git a/njs/njs_shell.c b/njs/njs_shell.c index 583473ec..76100046 100644 --- a/njs/njs_shell.c +++ b/njs/njs_shell.c @@ -930,7 +930,7 @@ njs_ext_console_log(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, 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_value_t *args, nxt_uint_t nargs, 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 --git a/njs/test/njs_expect_test.exp b/njs/test/njs_expect_test.exp index 6b3f0d86..1be27429 100644 --- a/njs/test/njs_expect_test.exp +++ b/njs/test/njs_expect_test.exp @@ -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 -- 2.47.3