From: Igor Sysoev Date: Wed, 3 Feb 2016 13:53:15 +0000 (+0300) Subject: Properties and prototypes of objects created by Boolean(), X-Git-Tag: 0.1.0~76 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=fce2b5b99e778bcf536988725bdd805f4fc08430;p=njs.git Properties and prototypes of objects created by Boolean(), Number(), and String() constructors have been fixed. --- diff --git a/njs/njs_object.c b/njs/njs_object.c index 14f183d7..289567d3 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -303,24 +303,28 @@ njs_object_create(njs_vm_t *vm, njs_param_t *param) /* - * The __proto__ property of booleans, numbers and strings primitives - * and Boolean.prototype, Number.prototype, and String.prototype objects. + * The __proto__ property of booleans, numbers and strings primitives, + * of objects created by Boolean(), Number(), and String() constructors, + * and of Boolean.prototype, Number.prototype, and String.prototype objects. */ njs_ret_t njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value) { - nxt_uint_t index; + njs_object_t *proto; /* * The __proto__ getters reside in object prototypes of primitive types - * and have to return different results for primitive type and for object - * prototype. + * and have to return different results for primitive type and for objects. */ - index = njs_is_object(value) ? NJS_PROTOTYPE_OBJECT: - njs_primitive_prototype_index(value->type); + if (njs_is_object(value)) { + proto = value->data.u.object->__proto__; + + } else { + proto = &vm->prototypes[njs_primitive_prototype_index(value->type)]; + } - vm->retval.data.u.object = &vm->prototypes[index]; + vm->retval.data.u.object = proto; vm->retval.type = NJS_OBJECT; vm->retval.data.truth = 1; diff --git a/njs/njs_vm.c b/njs/njs_vm.c index da9793af..488439ea 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -899,6 +899,9 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *object, /* Fall through. */ case NJS_OBJECT: + case NJS_OBJECT_BOOLEAN: + case NJS_OBJECT_NUMBER: + case NJS_OBJECT_STRING: case NJS_FUNCTION: case NJS_REGEXP: obj = object->data.u.object; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 1232ee44..b55c11e8 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -3302,6 +3302,12 @@ static njs_unit_test_t njs_test[] = { nxt_string("false.__proto__ === Boolean.prototype"), nxt_string("true") }, + { nxt_string("var b = Boolean(1); b.__proto__ === Boolean.prototype"), + nxt_string("true") }, + + { nxt_string("var b = new Boolean(1); b.__proto__ === Boolean.prototype"), + nxt_string("true") }, + { nxt_string("Number()"), nxt_string("0") }, @@ -3332,6 +3338,12 @@ static njs_unit_test_t njs_test[] = { nxt_string("0..__proto__ === Number.prototype"), nxt_string("true") }, + { nxt_string("var n = Number(1); n.__proto__ === Number.prototype"), + nxt_string("true") }, + + { nxt_string("var n = new Number(1); n.__proto__ === Number.prototype"), + nxt_string("true") }, + { nxt_string("String()"), nxt_string("") }, @@ -3368,6 +3380,12 @@ static njs_unit_test_t njs_test[] = { nxt_string("'test'.__proto__ === String.prototype"), nxt_string("true") }, + { nxt_string("var s = String('abc'); s.__proto__ === String.prototype"), + nxt_string("true") }, + + { nxt_string("var s = new String('abc'); s.__proto__ === String.prototype"), + nxt_string("true") }, + { nxt_string("'test'.constructor === String"), nxt_string("true") },