]> git.kaiwu.me - njs.git/commitdiff
Fixed Object.prototype.toString for different value types.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 29 Aug 2018 17:31:43 +0000 (20:31 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 29 Aug 2018 17:31:43 +0000 (20:31 +0300)
njs/njs_object.c
njs/test/njs_unit_test.c

index 12cc6c17d8f649865f91d4c868d4b425b665eae8..86e7b543df8937c4d26043f2c2e8466d39194880 100644 (file)
@@ -1768,33 +1768,14 @@ static const njs_value_t  njs_object_regexp_string =
 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 *vm, njs_value_t *args,
         &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;
index 4af8df966efc73312dcdb779fd6f30cb215bf308..8d98e950be6267b4b3872aefa73042e223bb33bb 100644 (file)
@@ -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") },