]> git.kaiwu.me - njs.git/commitdiff
Object.getPrototypeOf() method.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 9 Jun 2017 14:55:08 +0000 (17:55 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 9 Jun 2017 14:55:08 +0000 (17:55 +0300)
njs/njs_object.c
njs/test/njs_unit_test.c

index 1f65e7305027673ca878f03b6ae8946af7e11c3c..60fe0424360524a8a7a7cf8cabd04c91e2f148ce 100644 (file)
@@ -503,6 +503,20 @@ njs_object_define_property(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 }
 
 
+static njs_ret_t
+njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    if (nargs > 1 && njs_is_object(&args[1])) {
+        njs_object_prototype_get_proto(vm, &args[1]);
+        return NXT_OK;
+    }
+
+    vm->exception = &njs_exception_type_error;
+    return NXT_ERROR;
+}
+
+
 /*
  * The __proto__ property of booleans, numbers and strings primitives,
  * of objects created by Boolean(), Number(), and String() constructors,
@@ -658,6 +672,14 @@ static const njs_object_prop_t  njs_object_constructor_properties[] =
                                      NJS_SKIP_ARG, NJS_OBJECT_ARG,
                                      NJS_STRING_ARG, NJS_OBJECT_ARG),
     },
+
+    /* Object.getPrototypeOf(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("getPrototypeOf"),
+        .value = njs_native_function(njs_object_get_prototype_of, 0,
+                                     NJS_SKIP_ARG, NJS_OBJECT_ARG),
+    },
 };
 
 
index ffcedc27f2a642b3a322e65b39c734a03c543f7f..7ab0e5c8f1f8c1600aa72e495b4d07ab432a0dd4 100644 (file)
@@ -5974,6 +5974,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'s'.hasOwnProperty('b')"),
       nxt_string("false") },
 
+    { nxt_string("var p = { a:5 }; var o = Object.create(p);"
+                 "Object.getPrototypeOf(o) === p"),
+      nxt_string("true") },
+
+    { nxt_string("var p = { a:5 }; var o = Object.create(p);"
+                 "Object.getPrototypeOf(o) === o.__proto__"),
+      nxt_string("true") },
+
+    { nxt_string("var o = Object.create(Object.prototype);"
+                 "Object.getPrototypeOf(o) === Object.prototype"),
+      nxt_string("true") },
+
+    { nxt_string("Object.getPrototypeOf(1)"),
+      nxt_string("TypeError") },
+
+    { nxt_string("Object.getPrototypeOf('a')"),
+      nxt_string("TypeError") },
+
     { nxt_string("var d = new Date(''); d +' '+ d.getTime()"),
       nxt_string("Invalid Date NaN") },