From: Dmitry Volyntsev Date: Mon, 19 Jun 2017 11:46:34 +0000 (+0300) Subject: Object.isExtensible() method. X-Git-Tag: 0.1.11~10 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=263f6fea836835864ac9964f6ea762ac40afd1f8;p=njs.git Object.isExtensible() method. --- diff --git a/njs/njs_object.c b/njs/njs_object.c index 6f200035..6e2e02af 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -819,6 +819,26 @@ njs_object_prevent_extensions(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } +static njs_ret_t +njs_object_is_extensible(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + const njs_value_t *retval; + + if (nargs < 2 || !njs_is_object(&args[1])) { + vm->exception = &njs_exception_type_error; + return NXT_ERROR; + } + + retval = args[1].data.u.object->extensible ? &njs_string_true : + &njs_string_false; + + vm->retval = *retval; + + return NXT_OK; +} + + /* * The __proto__ property of booleans, numbers and strings primitives, * of objects created by Boolean(), Number(), and String() constructors, @@ -1020,6 +1040,14 @@ static const njs_object_prop_t njs_object_constructor_properties[] = .value = njs_native_function(njs_object_prevent_extensions, 0, NJS_SKIP_ARG, NJS_OBJECT_ARG), }, + + /* Object.isExtensible(). */ + { + .type = NJS_METHOD, + .name = njs_string("isExtensible"), + .value = njs_native_function(njs_object_is_extensible, 0, + NJS_SKIP_ARG, NJS_OBJECT_ARG), + }, }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 9c22ef63..420c1334 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -6232,6 +6232,39 @@ static njs_unit_test_t njs_test[] = { nxt_string("var o = Object.preventExtensions({a:1}); o.b = 1; o.b"), nxt_string("undefined") }, + { nxt_string("Object.isExtensible({})"), + nxt_string("true") }, + + { nxt_string("Object.isExtensible([])"), + nxt_string("true") }, + + { nxt_string("Object.isExtensible(function() {})"), + nxt_string("true") }, + + { nxt_string("Object.isExtensible(new Date(''))"), + nxt_string("true") }, + + { nxt_string("Object.isExtensible(new RegExp(''))"), + nxt_string("true") }, + + { nxt_string("Object.isExtensible(1)"), + nxt_string("TypeError") }, + + { nxt_string("Object.isExtensible('')"), + nxt_string("TypeError") }, + + { nxt_string("Object.isExtensible(Object.preventExtensions({}))"), + nxt_string("false") }, + + { nxt_string("Object.isExtensible(Object.preventExtensions([]))"), + nxt_string("false") }, + + { nxt_string("Object.isExtensible(Object.freeze({}))"), + nxt_string("false") }, + + { nxt_string("Object.isExtensible(Object.freeze([]))"), + nxt_string("false") }, + { nxt_string("var d = new Date(''); d +' '+ d.getTime()"), nxt_string("Invalid Date NaN") },