[njs] Object.getPrototypeOf() method.
Dmitry Volyntsev
xeioex at nginx.com
Fri Jun 9 14:55:39 UTC 2017
details: http://hg.nginx.org/njs/rev/e49777448f84
branches:
changeset: 359:e49777448f84
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Fri Jun 09 17:55:08 2017 +0300
description:
Object.getPrototypeOf() method.
diffstat:
njs/njs_object.c | 22 ++++++++++++++++++++++
njs/test/njs_unit_test.c | 18 ++++++++++++++++++
2 files changed, 40 insertions(+), 0 deletions(-)
diffs (67 lines):
diff -r 065bf91227a1 -r e49777448f84 njs/njs_object.c
--- a/njs/njs_object.c Fri Jun 09 17:22:27 2017 +0300
+++ b/njs/njs_object.c Fri Jun 09 17:55:08 2017 +0300
@@ -503,6 +503,20 @@ njs_object_define_property(njs_vm_t *vm,
}
+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_obje
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),
+ },
};
diff -r 065bf91227a1 -r e49777448f84 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Fri Jun 09 17:22:27 2017 +0300
+++ b/njs/test/njs_unit_test.c Fri Jun 09 17:55:08 2017 +0300
@@ -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") },
More information about the nginx-devel
mailing list