]> git.kaiwu.me - njs.git/commitdiff
Properties and prototypes of objects created by Boolean(),
authorIgor Sysoev <igor@sysoev.ru>
Wed, 3 Feb 2016 13:53:15 +0000 (16:53 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Wed, 3 Feb 2016 13:53:15 +0000 (16:53 +0300)
Number(), and String() constructors have been fixed.

njs/njs_object.c
njs/njs_vm.c
njs/test/njs_unit_test.c

index 14f183d71a610bdce891e45a1673a6cc0c11d53f..289567d3e2bbff55e8be2d28973a1a06a435e644 100644 (file)
@@ -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;
 
index da9793afd05707a4a758878159c502fe46275a10..488439ea170b22a3b98764a1cf641330071e93a6 100644 (file)
@@ -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;
index 1232ee44e8d7b663b6347a026b54502091b6df45..b55c11e899b854c8a028da6e4f338b3b208236e4 100644 (file)
@@ -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") },