[njs] Improved njs.dump() with built-in functions.

Dmitry Volyntsev xeioex at nginx.com
Fri Aug 21 13:08:37 UTC 2020


details:   https://hg.nginx.org/njs/rev/775c81dcbe61
branches:  
changeset: 1500:775c81dcbe61
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Aug 21 13:07:54 2020 +0000
description:
Improved njs.dump() with built-in functions.

diffstat:

 src/njs_json.c           |  22 ++++++++++++++++++++--
 src/test/njs_unit_test.c |  14 ++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diffs (63 lines):

diff -r 95ec4ee01853 -r 775c81dcbe61 src/njs_json.c
--- a/src/njs_json.c	Fri Aug 21 13:07:48 2020 +0000
+++ b/src/njs_json.c	Fri Aug 21 13:07:54 2020 +0000
@@ -1830,6 +1830,8 @@ njs_dump_terminal(njs_json_stringify_t *
     njs_typed_array_t  *array;
     njs_string_prop_t  string;
 
+    static const njs_value_t  name_string = njs_string("name");
+
     njs_int_t   (*to_string)(njs_vm_t *, njs_value_t *, const njs_value_t *);
 
     switch (value->type) {
@@ -1933,8 +1935,24 @@ njs_dump_terminal(njs_json_stringify_t *
         break;
 
     case NJS_FUNCTION:
-        if (njs_function(value)->native) {
-            njs_chb_append_literal(chain, "[Function: native]");
+        ret = njs_value_property(stringify->vm, value,
+                                 njs_value_arg(&name_string), &tag);
+        if (njs_slow_path(ret == NJS_ERROR)) {
+            return ret;
+        }
+
+        if (njs_is_string(&tag)) {
+            njs_string_get(&tag, &str);
+
+        } else if (njs_function(value)->native) {
+            str = njs_str_value("native");
+
+        } else {
+            str = njs_str_value("");
+        }
+
+        if (str.length != 0) {
+            njs_chb_sprintf(chain, 32 + str.length, "[Function: %V]", &str);
 
         } else {
             njs_chb_append_literal(chain, "[Function]");
diff -r 95ec4ee01853 -r 775c81dcbe61 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Aug 21 13:07:48 2020 +0000
+++ b/src/test/njs_unit_test.c	Fri Aug 21 13:07:54 2020 +0000
@@ -16747,6 +16747,20 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("njs.dump(Object(Symbol.iterator))"),
       njs_str("[Symbol: Symbol(Symbol.iterator)]") },
 
+    { njs_str("njs.dump(decodeURI)"),
+      njs_str("[Function: decodeURI]") },
+
+    { njs_str("delete decodeURI.name; njs.dump(decodeURI)"),
+      njs_str("[Function]") },
+
+    { njs_str("delete decodeURI.name; delete Function.prototype.name; "
+              "decodeURI.name = 1; njs.dump(decodeURI)"),
+      njs_str("[Function: native]") },
+
+    { njs_str("delete decodeURI.name; delete Function.prototype.name; "
+              "decodeURI.name = 'XXX'; njs.dump(decodeURI)"),
+      njs_str("[Function: XXX]") },
+
     /* Built-in methods name. */
 
     { njs_str(


More information about the nginx-devel mailing list