]> git.kaiwu.me - njs.git/commitdiff
Object.isExtensible() method.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 19 Jun 2017 11:46:34 +0000 (14:46 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 19 Jun 2017 11:46:34 +0000 (14:46 +0300)
njs/njs_object.c
njs/test/njs_unit_test.c

index 6f200035c3827bff68330a5090f90d47a6f4201d..6e2e02af8c6703cb75442f1e0942bc1fdea11811 100644 (file)
@@ -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),
+    },
 };
 
 
index 9c22ef6377678fd6e304ad1fb5380d98a1e3a98c..420c1334310e2895668f186c9b898b5e70ecdc47 100644 (file)
@@ -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") },