[njs] Object.prototype.isPrototypeOf() method.

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 9 14:55:42 UTC 2017


details:   http://hg.nginx.org/njs/rev/740823b9f444
branches:  
changeset: 360:740823b9f444
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jun 09 17:55:21 2017 +0300
description:
Object.prototype.isPrototypeOf() method.

diffstat:

 njs/njs_object.c         |  37 +++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  25 +++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 0 deletions(-)

diffs (89 lines):

diff -r e49777448f84 -r 740823b9f444 njs/njs_object.c
--- a/njs/njs_object.c	Fri Jun 09 17:55:08 2017 +0300
+++ b/njs/njs_object.c	Fri Jun 09 17:55:21 2017 +0300
@@ -935,6 +935,36 @@ done:
 }
 
 
+static njs_ret_t
+njs_object_prototype_is_prototype_of(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    njs_object_t       *object, *proto;
+    const njs_value_t  *retval;
+
+    retval = &njs_string_false;
+
+    if (njs_is_object(&args[0]) && njs_is_object(&args[1])) {
+        proto = args[0].data.u.object;
+        object = args[1].data.u.object;
+
+        do {
+            object = object->__proto__;
+
+            if (object == proto) {
+                retval = &njs_string_true;
+                break;
+            }
+
+        } while (object != NULL);
+    }
+
+    vm->retval = *retval;
+
+    return NXT_OK;
+}
+
+
 static const njs_object_prop_t  njs_object_prototype_properties[] =
 {
     {
@@ -967,6 +997,13 @@ static const njs_object_prop_t  njs_obje
         .value = njs_native_function(njs_object_prototype_has_own_property, 0,
                                      NJS_OBJECT_ARG, NJS_STRING_ARG),
     },
+
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("isPrototypeOf"),
+        .value = njs_native_function(njs_object_prototype_is_prototype_of, 0,
+                                     NJS_OBJECT_ARG, NJS_OBJECT_ARG),
+    },
 };
 
 
diff -r e49777448f84 -r 740823b9f444 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Jun 09 17:55:08 2017 +0300
+++ b/njs/test/njs_unit_test.c	Fri Jun 09 17:55:21 2017 +0300
@@ -5992,6 +5992,31 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Object.getPrototypeOf('a')"),
       nxt_string("TypeError") },
 
+    { nxt_string("var p = {}; var o = Object.create(p);"
+                 "p.isPrototypeOf(o)"),
+      nxt_string("true") },
+
+    { nxt_string("var pp = {}; var p = Object.create(pp);"
+                 "var o = Object.create(p);"
+                 "pp.isPrototypeOf(o)"),
+      nxt_string("true") },
+
+    { nxt_string("var p = {}; var o = Object.create(p);"
+                 "o.isPrototypeOf(p)"),
+      nxt_string("false") },
+
+    { nxt_string("var p = {}; var o = Object.create(p);"
+                 "o.isPrototypeOf()"),
+      nxt_string("false") },
+
+    { nxt_string("var p = {}; var o = Object.create(p);"
+                 "o.isPrototypeOf(1)"),
+      nxt_string("false") },
+
+    { nxt_string("var p = {}; var o = Object.create(p);"
+                 "1..isPrototypeOf(p)"),
+      nxt_string("false") },
+
     { nxt_string("var d = new Date(''); d +' '+ d.getTime()"),
       nxt_string("Invalid Date NaN") },
 


More information about the nginx-devel mailing list