Welcome! Log In Create A New Profile

Advanced

[njs] Improved console.log() for string values.

Dmitry Volyntsev
April 11, 2019 01:16PM
details: https://hg.nginx.org/njs/rev/bcf988ed8c08
branches:
changeset: 880:bcf988ed8c08
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Thu Apr 11 18:53:19 2019 +0300
description:
Improved console.log() for string values.

Outputting string values without quotes.

diffstat:

njs/njs_json.c | 19 ++++++++++-
njs/test/njs_expect_test.exp | 68 ++++++++++++++++++++++----------------------
2 files changed, 50 insertions(+), 37 deletions(-)

diffs (332 lines):

diff -r 4df50ca085e7 -r bcf988ed8c08 njs/njs_json.c
--- a/njs/njs_json.c Thu Apr 11 21:13:03 2019 +0800
+++ b/njs/njs_json.c Thu Apr 11 18:53:19 2019 +0300
@@ -1798,7 +1798,9 @@ njs_json_append_string(njs_json_stringif

dst_end = dst + 64;

- *dst++ = quote;
+ if (quote) {
+ *dst++ = quote;
+ }

while (p < end) {

@@ -1874,7 +1876,10 @@ njs_json_append_string(njs_json_stringif
}

njs_json_buf_written(stringify, dst - stringify->last->pos);
- njs_json_buf_append(stringify, &quote, 1);
+
+ if (quote) {
+ njs_json_buf_append(stringify, &quote, 1);
+ }

return NXT_OK;
}
@@ -2116,6 +2121,7 @@ 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)
{
+ char quote;
njs_ret_t ret;
nxt_str_t str;
nxt_uint_t written;
@@ -2139,7 +2145,13 @@ njs_dump_value(njs_json_stringify_t *str

case NJS_STRING:
njs_string_get(value, &str);
- return njs_json_append_string(stringify, value, '\'');
+
+ quote = '\0';
+ if (stringify->stack.items != 0) {
+ quote = '\'';
+ }
+
+ return njs_json_append_string(stringify, value, quote);

case NJS_OBJECT_NUMBER:
value = &value->data.u.object_value->value;
@@ -2351,6 +2363,7 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_
stringify->pool = vm->mem_pool;
stringify->nodes = NULL;
stringify->last = NULL;
+ stringify->stack.items = 0;

if (!njs_dump_is_object(value)) {
ret = njs_dump_value(stringify, value);
diff -r 4df50ca085e7 -r bcf988ed8c08 njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp Thu Apr 11 21:13:03 2019 +0800
+++ b/njs/test/njs_expect_test.exp Thu Apr 11 18:53:19 2019 +0300
@@ -191,13 +191,13 @@ njs_test {
{"console.log(1)\r\n"
"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(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
{"console.dump()\r\n"
"console.dump()\r\nundefined\r\n>> "}
{"console.dump(1)\r\n"
"console.dump(1)\r\n1\r\nundefined\r\n>> "}
{"console.dump(1, 'a')\r\n"
- "console.dump(1, 'a')\r\n1 'a'\r\nundefined\r\n>> "}
+ "console.dump(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
{"console.help()\r\n"
"console.help()\r\nVM built-in objects:"}
}
@@ -230,9 +230,9 @@ njs_test {

njs_test {
{"var print = console.log.bind(console); print(1, 'a', [1, 2])\r\n"
- "1 'a' \\\[1,2]\r\nundefined\r\n>> "}
+ "1 a \\\[1,2]\r\nundefined\r\n>> "}
{"var print = console.dump.bind(console); print(1, 'a', [1, 2])\r\n"
- "1 'a' \\\[\r\n 1,\r\n 2\r\n]\r\nundefined\r\n>> "}
+ "1 a \\\[\r\n 1,\r\n 2\r\n]\r\nundefined\r\n>> "}
}

# Backtraces for external objects
@@ -268,7 +268,7 @@ njs_test {
# except '\"'
njs_test {
{"\"\\r\\0\\\"\"\r\n"
- "'\\\\r\\\\u0000\"'"}
+ "\\\\r\\\\u0000\""}
}

njs_test {
@@ -299,23 +299,23 @@ njs_test {
# Non-ASCII characters
njs_test {
{"'絵文字'\r\n"
- "'絵文字'"}
+ "絵文字"}
{"var v = 'абвгдеёжзийкл';v[10]\r\n"
- "'й'"}
+ "й"}
}

# Immediate events

njs_test {
{"var t = setImmediate(console.log, 'a', 'aa')\r\n"
- "undefined\r\n'a' 'aa'"}
+ "undefined\r\na aa"}
}

njs_test {
{"var a = 1 + 1; setTimeout(function (x) {a = x}, 0, 'a'); a\r\n"
"2"}
{"a\r\n"
- "a\r\n'a'"}
+ "a\r\na"}
}

njs_test {
@@ -327,14 +327,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\n'a'"}
+ "a\r\na"}
}

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\n'a'"}
+ "a\r\na"}
}

njs_test {
@@ -362,7 +362,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\n'0,1,2,3,4,5'"}
+ "queue.toString()\r\n0,1,2,3,4,5"}
}

# require('fs')
@@ -380,37 +380,37 @@ njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.readFile('njs/test/fs/utf8', 'utf8', function (e, data) {console.log(data[2]+data.length)})\r\n"
- "'Z4'\r\nundefined\r\n>> "}
+ "Z4\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.readFile('njs/test/fs/utf8', function (e, data) {console.log(data[4]+data.length)})\r\n"
- "'Z7'\r\nundefined\r\n>> "}
+ "Z7\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.readFile('njs/test/fs/utf8', {encoding:'utf8',flag:'r+'}, function (e, data) {console.log(data)})\r\n"
- "'αβZγ'\r\nundefined\r\n>> "}
+ "αβZγ\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.readFile('njs/test/fs/ascii', function (e, data) {console.log(data[599])})\r\n"
- "'x'\r\nundefined\r\n>> "}
+ "x\r\nundefined\r\n>> "}
{"fs.readFile('njs/test/fs/ascii', {encoding:'utf8',flag:'r+'}, function (e, data) {console.log(data[599])})\r\n"
- "'x'\r\nundefined\r\n>> "}
+ "x\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs'); \r\n"
"undefined\r\n>> "}
{"fs.readFile('njs/test/fs/nonexistent', 'utf8', function (e) {console.log(JSON.stringify(e))})\r\n"
- "'{\"errno\":2,\"path\":\"njs/test/fs/nonexistent\",\"syscall\":\"open\"}'\r\nundefined\r\n>> "}
+ "{\"errno\":2,\"path\":\"njs/test/fs/nonexistent\",\"syscall\":\"open\"}\r\nundefined\r\n>> "}
}

njs_test {
@@ -426,42 +426,42 @@ 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 {
{"var fs = require('fs'); \r\n"
"undefined\r\n>> "}
{"try { fs.readFileSync('njs/test/fs/nonexistent')} catch (e) {console.log(JSON.stringify(e))}\r\n"
- "'{\"errno\":2,\"path\":\"njs/test/fs/nonexistent\",\"syscall\":\"open\"}'\r\nundefined\r\n>> "}
+ "{\"errno\":2,\"path\":\"njs/test/fs/nonexistent\",\"syscall\":\"open\"}\r\nundefined\r\n>> "}
}

njs_test {
@@ -489,21 +489,21 @@ njs_test {
{"function h1(e) {if (e) {throw e}; console.log(fs.readFileSync('njs_test_file2'))}\r\n"
"undefined\r\n>> "}
{"fs.writeFile('njs_test_file2', 'ABC', h1)\r\n"
- "'ABC'\r\nundefined\r\n>> "}
+ "ABC\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.writeFile('njs_test_file2', 'ABC', 'utf8', function (e) { if (e) {throw e}; console.log(fs.readFileSync('njs_test_file2'))})\r\n"
- "'ABC'\r\nundefined\r\n>> "}
+ "ABC\r\nundefined\r\n>> "}
}

njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.writeFile('njs_test_file2', 'ABC', {encoding:'utf8', mode:0o666}, function (e) { if (e) {throw e}; console.log(fs.readFileSync('njs_test_file2'))})\r\n"
- "'ABC'\r\nundefined\r\n>> "}
+ "ABC\r\nundefined\r\n>> "}
}

exec rm -fr njs_wo_file
@@ -519,7 +519,7 @@ njs_test {
{"var fs = require('fs')\r\n"
"undefined\r\n>> "}
{"fs.writeFile('/invalid_path', 'ABC', function (e) { console.log(JSON.stringify(e))})\r\n"
- "'{\"errno\":13,\"path\":\"/invalid_path\",\"syscall\":\"open\"}'\r\nundefined\r\n>> "}
+ "{\"errno\":13,\"path\":\"/invalid_path\",\"syscall\":\"open\"}\r\nundefined\r\n>> "}
}

# require('fs').writeFileSync()
@@ -532,7 +532,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 +541,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 +552,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 +561,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
@@ -585,7 +585,7 @@ njs_test {
{"function h2(e) {fs.appendFile('njs_test_file2', 'ABC', h1)}\r\n"
"undefined\r\n>> "}
{"fs.appendFile('njs_test_file2', 'ABC', h2)\r\n"
- "'ABCABC'\r\nundefined\r\n>> "}
+ "ABCABC\r\nundefined\r\n>> "}
}

# require('fs').appendFileSync()
@@ -600,7 +600,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
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Improved console.log() for string values.

Dmitry Volyntsev 128 April 11, 2019 01:16PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 108
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready