[njs] Introduced njs_object_to_string().

Dmitry Volyntsev xeioex at nginx.com
Fri Sep 16 06:46:13 UTC 2022


details:   https://hg.nginx.org/njs/rev/75922905bd9c
branches:  
changeset: 1954:75922905bd9c
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Sep 15 17:56:35 2022 -0700
description:
Introduced njs_object_to_string().

diffstat:

 src/njs_object.c |  20 +++++++++++++-------
 src/njs_object.h |  20 ++++++++++++++++++--
 2 files changed, 31 insertions(+), 9 deletions(-)

diffs (100 lines):

diff -r 46d505a902bb -r 75922905bd9c src/njs_object.c
--- a/src/njs_object.c	Wed Sep 14 22:14:50 2022 -0700
+++ b/src/njs_object.c	Thu Sep 15 17:56:35 2022 -0700
@@ -2343,17 +2343,23 @@ njs_int_t
 njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused)
 {
+    return njs_object_to_string(vm, &args[0], &vm->retval);
+}
+
+
+njs_int_t
+njs_object_to_string(njs_vm_t *vm, njs_value_t *this, njs_value_t *retval)
+{
     u_char             *p;
     njs_int_t          ret;
-    njs_value_t        tag, *this;
+    njs_value_t        tag;
     njs_string_prop_t  string;
     const njs_value_t  *name;
 
-    this = njs_argument(args, 0);
-
     if (njs_is_null_or_undefined(this)) {
-        vm->retval = njs_is_null(this) ? njs_object_null_string
-                                       : njs_object_undefined_string;
+        njs_value_assign(retval,
+                         njs_is_null(this) ? &njs_object_null_string
+                                           : &njs_object_undefined_string);
 
         return NJS_OK;
     }
@@ -2418,14 +2424,14 @@ njs_object_prototype_to_string(njs_vm_t 
             return NJS_ERROR;
         }
 
-        vm->retval = *name;
+        njs_value_assign(retval, name);
 
         return NJS_OK;
     }
 
     (void) njs_string_prop(&string, &tag);
 
-    p = njs_string_alloc(vm, &vm->retval, string.size + njs_length("[object ]"),
+    p = njs_string_alloc(vm, retval, string.size + njs_length("[object ]"),
                          string.length + njs_length("[object ]"));
     if (njs_slow_path(p == NULL)) {
         return NJS_ERROR;
diff -r 46d505a902bb -r 75922905bd9c src/njs_object.h
--- a/src/njs_object.h	Wed Sep 14 22:14:50 2022 -0700
+++ b/src/njs_object.h	Thu Sep 15 17:56:35 2022 -0700
@@ -65,6 +65,8 @@ njs_int_t njs_object_prototype_create_co
     njs_value_t *retval);
 njs_value_t *njs_property_constructor_set(njs_vm_t *vm, njs_lvlhsh_t *hash,
     njs_value_t *constructor);
+njs_int_t njs_object_to_string(njs_vm_t *vm, njs_value_t *value,
+    njs_value_t *retval);
 njs_int_t njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused);
 njs_int_t njs_object_length(njs_vm_t *vm, njs_value_t *value, int64_t *dst);
@@ -186,7 +188,8 @@ njs_primitive_value_to_key(njs_vm_t *vm,
 
 
 njs_inline njs_int_t
-njs_value_to_key(njs_vm_t *vm, njs_value_t *dst, njs_value_t *value)
+njs_value_to_key2(njs_vm_t *vm, njs_value_t *dst, njs_value_t *value,
+    njs_bool_t convert)
 {
     njs_int_t    ret;
     njs_value_t  primitive;
@@ -197,7 +200,13 @@ njs_value_to_key(njs_vm_t *vm, njs_value
             value = njs_object_value(value);
 
         } else {
-            ret = njs_value_to_primitive(vm, &primitive, value, 1);
+            if (convert) {
+                ret = njs_value_to_primitive(vm, &primitive, value, 1);
+
+            } else {
+                ret = njs_object_to_string(vm, value, &primitive);
+            }
+
             if (njs_slow_path(ret != NJS_OK)) {
                 return ret;
             }
@@ -211,6 +220,13 @@ njs_value_to_key(njs_vm_t *vm, njs_value
 
 
 njs_inline njs_int_t
+njs_value_to_key(njs_vm_t *vm, njs_value_t *dst, njs_value_t *value)
+{
+    return njs_value_to_key2(vm, dst, value, 1);
+}
+
+
+njs_inline njs_int_t
 njs_key_string_get(njs_vm_t *vm, njs_value_t *key, njs_str_t *str)
 {
     njs_int_t  ret;



More information about the nginx-devel mailing list