From: Dmitry Volyntsev Date: Fri, 9 Jun 2017 14:55:08 +0000 (+0300) Subject: Object.getPrototypeOf() method. X-Git-Tag: 0.1.11~20 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=fbe2574dc8c1a56db5c81c43707150da95c07545;p=njs.git Object.getPrototypeOf() method. --- diff --git a/njs/njs_object.c b/njs/njs_object.c index 1f65e730..60fe0424 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -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), + }, }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index ffcedc27..7ab0e5c8 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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") },