[njs] Fixed Object.prototype.toString for different value types.

Dmitry Volyntsev xeioex at nginx.com
Wed Aug 29 17:48:48 UTC 2018


details:   http://hg.nginx.org/njs/rev/3d4c542a162b
branches:  
changeset: 592:3d4c542a162b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Aug 29 20:31:43 2018 +0300
description:
Fixed Object.prototype.toString for different value types.

diffstat:

 njs/njs_object.c         |  61 +++++++++--------------------------------------
 njs/test/njs_unit_test.c |   9 +++++++
 2 files changed, 21 insertions(+), 49 deletions(-)

diffs (118 lines):

diff -r 0191fae31b6d -r 3d4c542a162b njs/njs_object.c
--- a/njs/njs_object.c	Tue Aug 28 18:42:43 2018 +0300
+++ b/njs/njs_object.c	Wed Aug 29 20:31:43 2018 +0300
@@ -1768,33 +1768,14 @@ static const njs_value_t  njs_object_reg
 static const njs_value_t  njs_object_date_string = njs_string("[object Date]");
 static const njs_value_t  njs_object_error_string =
                                      njs_string("[object Error]");
-static const njs_value_t  njs_object_eval_error_string =
-                                     njs_long_string("[object EvalError]");
-static const njs_value_t  njs_object_internal_error_string =
-                                     njs_long_string("[object InternalError]");
-static const njs_value_t  njs_object_range_error_string =
-                                     njs_long_string("[object RangeError]");
-static const njs_value_t  njs_object_ref_error_string =
-                                     njs_long_string("[object RefError]");
-static const njs_value_t  njs_object_syntax_error_string =
-                                     njs_long_string("[object SyntaxError]");
-static const njs_value_t  njs_object_type_error_string =
-                                     njs_long_string("[object TypeError]");
-static const njs_value_t  njs_object_uri_error_string =
-                                     njs_long_string("[object URIError]");
-static const njs_value_t  njs_object_object_value_string =
-                                     njs_long_string("[object ObjectValue]");
-
 
 
 njs_ret_t
 njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused)
 {
-    int32_t                 index;
-    njs_object_t            *object;
-    const njs_value_t       *value;
-    njs_object_prototype_t  *prototype;
+    int32_t            index;
+    const njs_value_t  *value;
 
     static const njs_value_t  *class_name[] = {
         /* Primitives. */
@@ -1826,42 +1807,24 @@ njs_object_prototype_to_string(njs_vm_t 
         &njs_object_regexp_string,
         &njs_object_date_string,
         &njs_object_error_string,
-        &njs_object_eval_error_string,
-        &njs_object_internal_error_string,
-        &njs_object_range_error_string,
-        &njs_object_ref_error_string,
-        &njs_object_syntax_error_string,
-        &njs_object_type_error_string,
-        &njs_object_uri_error_string,
-        &njs_object_object_value_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_error_string,
+        &njs_object_object_string,
     };
 
     value = &args[0];
     index = value->type;
 
-    if (njs_is_object(value)) {
-        object = value->data.u.object;
-
-        do {
-            prototype = (njs_object_prototype_t *) object;
-            index = prototype - vm->prototypes;
-
-            if (index >= 0 && index < NJS_PROTOTYPE_MAX) {
-                index += NJS_OBJECT;
-                goto found;
-            }
-
-            object = object->__proto__;
-
-        } while (object != NULL);
-
-        nxt_thread_log_alert("prototype not found");
-
+    if (nxt_slow_path(class_name[index] == &njs_string_empty)) {
+        njs_internal_error(vm, "Unknown value type");
         return NXT_ERROR;
     }
 
-found:
-
     vm->retval = *class_name[index];
 
     return NXT_OK;
diff -r 0191fae31b6d -r 3d4c542a162b njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Aug 28 18:42:43 2018 +0300
+++ b/njs/test/njs_unit_test.c	Wed Aug 29 20:31:43 2018 +0300
@@ -6081,6 +6081,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Object.prototype.toString.call(Object.prototype)"),
       nxt_string("[object Object]") },
 
+    { nxt_string("Object.prototype.toString.call(new Array)"),
+      nxt_string("[object Array]") },
+
+    { nxt_string("Object.prototype.toString.call(new URIError)"),
+      nxt_string("[object Error]") },
+
     { nxt_string("Object.prototype"),
       nxt_string("[object Object]") },
 
@@ -9525,6 +9531,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("require('crypto').createHash('sha1')"),
       nxt_string("[object Hash]") },
 
+    { nxt_string("Object.prototype.toString.call(require('crypto').createHash('sha1'))"),
+      nxt_string("[object Object]") },
+
     { nxt_string("var h = require('crypto').createHash('md5');"
                  "h.update('AB').digest('hex')"),
       nxt_string("b86fc6b051f63d73de262d4c34e3a0a9") },


More information about the nginx-devel mailing list