[njs] Adding Symbol support for backtraces.

Dmitry Volyntsev xeioex at nginx.com
Tue Jun 30 15:36:51 UTC 2020


details:   https://hg.nginx.org/njs/rev/85c1b6ba326b
branches:  
changeset: 1442:85c1b6ba326b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jun 30 15:33:58 2020 +0000
description:
Adding Symbol support for backtraces.

diffstat:

 src/njs_builtin.c |  21 ++++++++++++++++-----
 src/njs_json.c    |   4 ++--
 src/njs_object.h  |   2 +-
 src/njs_string.c  |   2 +-
 src/njs_symbol.c  |  36 ++++++++++++++++++++++++++----------
 src/njs_symbol.h  |   2 +-
 6 files changed, 47 insertions(+), 20 deletions(-)

diffs (165 lines):

diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_builtin.c
--- a/src/njs_builtin.c	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_builtin.c	Tue Jun 30 15:33:58 2020 +0000
@@ -396,6 +396,7 @@ njs_builtin_traverse(njs_vm_t *vm, njs_t
     u_char                  *p, *start, *end;
     njs_int_t               ret, n;
     njs_str_t               name;
+    njs_value_t             key;
     njs_function_t          *func;
     njs_object_prop_t       *prop;
     njs_lvlhsh_query_t      lhq;
@@ -430,7 +431,21 @@ njs_builtin_traverse(njs_vm_t *vm, njs_t
     end = buf + sizeof(buf);
 
     do {
-        njs_string_get(&path[n]->prop->name, &name);
+        key = path[n]->prop->name;
+
+        if (njs_slow_path(njs_is_symbol(&key))) {
+            ret = njs_symbol_to_string(vm, &key, &key, 1);
+            if (njs_slow_path(ret != NJS_OK)) {
+                name = njs_str_value("#BROKEN_KEY");
+            }
+
+        } else {
+            if (p != buf) {
+                *p++ = '.';
+            }
+        }
+
+        njs_string_get(&key, &name);
 
         if (njs_slow_path((p + name.length + 1) > end)) {
             njs_type_error(vm, "njs_builtin_traverse() key is too long");
@@ -439,10 +454,6 @@ njs_builtin_traverse(njs_vm_t *vm, njs_t
 
         p = njs_cpymem(p, name.start, name.length);
 
-        if (n != 0) {
-            *p++ = '.';
-        }
-
     } while (n-- > 0);
 
     if (ctx->type == NJS_BUILTIN_TRAVERSE_MATCH) {
diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_json.c
--- a/src/njs_json.c	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_json.c	Tue Jun 30 15:33:58 2020 +0000
@@ -1856,7 +1856,7 @@ njs_dump_terminal(njs_json_stringify_t *
     case NJS_OBJECT_SYMBOL:
         value = njs_object_value(value);
 
-        ret = njs_symbol_to_string(stringify->vm, &str_val, value);
+        ret = njs_symbol_to_string(stringify->vm, &str_val, value, 0);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -1867,7 +1867,7 @@ njs_dump_terminal(njs_json_stringify_t *
         break;
 
     case NJS_SYMBOL:
-        ret = njs_symbol_to_string(stringify->vm, &str_val, value);
+        ret = njs_symbol_to_string(stringify->vm, &str_val, value, 0);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_object.h
--- a/src/njs_object.h	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_object.h	Tue Jun 30 15:33:58 2020 +0000
@@ -213,7 +213,7 @@ njs_key_string_get(njs_vm_t *vm, njs_val
     njs_int_t  ret;
 
     if (njs_slow_path(njs_is_symbol(key))) {
-        ret = njs_symbol_to_string(vm, key, key);
+        ret = njs_symbol_to_string(vm, key, key, 0);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
         }
diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_string.c
--- a/src/njs_string.c	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_string.c	Tue Jun 30 15:33:58 2020 +0000
@@ -543,7 +543,7 @@ njs_string_constructor(njs_vm_t *vm, njs
 
         if (njs_slow_path(!njs_is_string(value))) {
             if (!vm->top_frame->ctor && njs_is_symbol(value)) {
-                return njs_symbol_to_string(vm, &vm->retval, value);
+                return njs_symbol_to_string(vm, &vm->retval, value, 0);
             }
 
             ret = njs_value_to_string(vm, value, value);
diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_symbol.c
--- a/src/njs_symbol.c	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_symbol.c	Tue Jun 30 15:33:58 2020 +0000
@@ -55,7 +55,8 @@ static const njs_value_t  *njs_symbol_na
 
 
 njs_int_t
-njs_symbol_to_string(njs_vm_t *vm, njs_value_t *dst, const njs_value_t *value)
+njs_symbol_to_string(njs_vm_t *vm, njs_value_t *dst, const njs_value_t *value,
+    njs_bool_t as_name)
 {
     u_char             *start;
     const njs_value_t  *name;
@@ -78,17 +79,32 @@ njs_symbol_to_string(njs_vm_t *vm, njs_v
     }
 
     (void) njs_string_prop(&string, name);
-    string.length += njs_length("Symbol()");
+
+    if (as_name) {
+        string.length += njs_length("[]");
+
+        start = njs_string_alloc(vm, dst, string.size + 2, string.length);
+        if (njs_slow_path(start == NULL)) {
+            return NJS_ERROR;
+        }
+
+        start = njs_cpymem(start, "[", 1);
+        start = njs_cpymem(start, string.start, string.size);
+        *start = ']';
 
-    start = njs_string_alloc(vm, dst, string.size + 8, string.length);
-    if (njs_slow_path(start == NULL)) {
-        return NJS_ERROR;
+    } else {
+        string.length += njs_length("Symbol()");
+
+        start = njs_string_alloc(vm, dst, string.size + 8, string.length);
+        if (njs_slow_path(start == NULL)) {
+            return NJS_ERROR;
+        }
+
+        start = njs_cpymem(start, "Symbol(", 7);
+        start = njs_cpymem(start, string.start, string.size);
+        *start = ')';
     }
 
-    start = njs_cpymem(start, "Symbol(", 7);
-    start = njs_cpymem(start, string.start, string.size);
-    *start = ')';
-
     return NJS_OK;
 }
 
@@ -328,7 +344,7 @@ njs_symbol_prototype_to_string(njs_vm_t 
         return ret;
     }
 
-    return njs_symbol_to_string(vm, &vm->retval, &vm->retval);
+    return njs_symbol_to_string(vm, &vm->retval, &vm->retval, 0);
 }
 
 
diff -r 52dd845c0a36 -r 85c1b6ba326b src/njs_symbol.h
--- a/src/njs_symbol.h	Tue Jun 30 18:22:18 2020 +0300
+++ b/src/njs_symbol.h	Tue Jun 30 15:33:58 2020 +0000
@@ -8,7 +8,7 @@
 #define _NJS_SYMBOL_H_INCLUDED_
 
 njs_int_t njs_symbol_to_string(njs_vm_t *vm, njs_value_t *dst,
-    const njs_value_t *value);
+    const njs_value_t *value, njs_bool_t as_name);
 
 
 extern const njs_object_type_init_t  njs_symbol_type_init;


More information about the nginx-devel mailing list