From: Valentin Bartenev Date: Mon, 6 May 2019 08:33:23 +0000 (+0300) Subject: Fixed own properties quering. X-Git-Tag: 0.3.2~24 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=b1ef106b9f728e34d1d294ad01c4f410ae9c8955;p=njs.git Fixed own properties quering. njs_property_query() and njs_object_property_query() were simplified. --- diff --git a/njs/njs_object.c b/njs/njs_object.c index 87c50d7f..144b2ca4 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -322,39 +322,11 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *object, obj = &vm->string_object; break; - case NJS_OBJECT_STRING: - if (nxt_fast_path(!njs_is_null_or_undefined_or_boolean(property))) { - index = njs_value_to_index(property); - - if (nxt_fast_path(index < NJS_STRING_MAX_LENGTH)) { - ret = njs_string_property_query(vm, pq, - &object->data.u.object_value->value, - index); - - if (nxt_fast_path(ret != NXT_DECLINED)) { - return ret; - } - } - } - - obj = object->data.u.object; - break; - - case NJS_ARRAY: - if (nxt_fast_path(!njs_is_null_or_undefined_or_boolean(property))) { - index = njs_value_to_index(property); - - if (nxt_fast_path(index < NJS_ARRAY_MAX_INDEX)) { - return njs_array_property_query(vm, pq, object->data.u.array, - index); - } - } - - /* Fall through. */ - case NJS_OBJECT: + case NJS_ARRAY: case NJS_OBJECT_BOOLEAN: case NJS_OBJECT_NUMBER: + case NJS_OBJECT_STRING: case NJS_REGEXP: case NJS_DATE: case NJS_OBJECT_ERROR: @@ -442,62 +414,58 @@ njs_object_property_query(njs_vm_t *vm, njs_property_query_t *pq, do { pq->prototype = proto; - if (nxt_fast_path(!pq->own || proto == object)) { - ret = nxt_lvlhsh_find(&proto->hash, &pq->lhq); + if (!njs_is_null_or_undefined_or_boolean(property)) { + switch (proto->type) { + case NJS_ARRAY: + index = njs_value_to_index(property); + if (nxt_fast_path(index < NJS_ARRAY_MAX_INDEX)) { + array = (njs_array_t *) proto; + return njs_array_property_query(vm, pq, array, index); + } - if (ret == NXT_OK) { - prop = pq->lhq.value; + break; - if (prop->type != NJS_WHITEOUT) { - pq->shared = 0; + case NJS_OBJECT_STRING: + index = njs_value_to_index(property); + if (nxt_fast_path(index < NJS_STRING_MAX_LENGTH)) { + ov = (njs_object_value_t *) proto; + ret = njs_string_property_query(vm, pq, &ov->value, index); - return ret; + if (nxt_fast_path(ret != NXT_DECLINED)) { + return ret; + } } - goto next; + default: + break; } + } - if (proto != object - && !njs_is_null_or_undefined_or_boolean(property)) - { - switch (proto->type) { - case NJS_ARRAY: - index = njs_value_to_index(property); - if (nxt_fast_path(index < NJS_ARRAY_MAX_INDEX)) { - array = (njs_array_t *) proto; - return njs_array_property_query(vm, pq, array, index); - } + ret = nxt_lvlhsh_find(&proto->hash, &pq->lhq); - break; + if (ret == NXT_OK) { + prop = pq->lhq.value; - case NJS_OBJECT_STRING: - index = njs_value_to_index(property); - if (nxt_fast_path(index < NJS_STRING_MAX_LENGTH)) { - ov = (njs_object_value_t *) proto; - return njs_string_property_query(vm, pq, &ov->value, - index); - } + if (prop->type != NJS_WHITEOUT) { + pq->shared = 0; - default: - break; - } + return ret; } - } - ret = nxt_lvlhsh_find(&proto->shared_hash, &pq->lhq); + } else { + ret = nxt_lvlhsh_find(&proto->shared_hash, &pq->lhq); - if (ret == NXT_OK) { - pq->shared = 1; + if (ret == NXT_OK) { + pq->shared = 1; - return ret; + return ret; + } } - if (pq->query > NJS_PROPERTY_QUERY_GET) { + if (pq->own || pq->query > NJS_PROPERTY_QUERY_GET) { return NXT_DECLINED; } -next: - proto = proto->__proto__; } while (proto != NULL); diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index e9d46eba..c1548626 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -9102,6 +9102,12 @@ static njs_unit_test_t njs_test[] = { nxt_string("'s'.hasOwnProperty('1')"), nxt_string("false") }, + { nxt_string("Object.hasOwnProperty('hasOwnProperty')"), + nxt_string("false") }, + + { nxt_string("Object.prototype.hasOwnProperty('hasOwnProperty')"), + nxt_string("true") }, + { nxt_string("var p = { a:5 }; var o = Object.create(p);" "Object.getPrototypeOf(o) === p"), nxt_string("true") }, @@ -9160,6 +9166,17 @@ static njs_unit_test_t njs_test[] = { nxt_string("Object.create(function(a,b,c){}).length"), nxt_string("3") }, + { nxt_string("Object.create(Math).hasOwnProperty('abs')"), + nxt_string("false") }, + + { nxt_string("var m = Object.create(Math); m.abs = 3;" + "[m.hasOwnProperty('abs'), m.abs]"), + nxt_string("true,3") }, + + { nxt_string("var m = Object.create(Math); m.abs = Math.floor;" + "[m.hasOwnProperty('abs'), delete m.abs, m.abs(-1)]"), + nxt_string("true,true,1") }, + { nxt_string("Object.getOwnPropertyDescriptor({a:1}, 'a').value"), nxt_string("1") },