From: Dmitry Volyntsev Date: Wed, 29 Aug 2018 17:31:43 +0000 (+0300) Subject: Fixed Object.prototype.toString for different value types. X-Git-Tag: 0.2.4~15 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=d49311c60d8031459f2a6cc9f62ebb63c49532c8;p=njs.git Fixed Object.prototype.toString for different value types. --- diff --git a/njs/njs_object.c b/njs/njs_object.c index 12cc6c17..86e7b543 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -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; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 4af8df96..8d98e950 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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") },