[njs] Object.isSealed() method.

Dmitry Volyntsev xeioex at nginx.com
Mon Jun 19 11:49:36 UTC 2017


details:   http://hg.nginx.org/njs/rev/bcd7a7256805
branches:  
changeset: 371:bcd7a7256805
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jun 19 14:46:46 2017 +0300
description:
Object.isSealed() method.

diffstat:

 njs/njs_object.c         |  56 ++++++++++++++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  51 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+), 0 deletions(-)

diffs (134 lines):

diff -r cc5ab912d455 -r bcd7a7256805 njs/njs_object.c
--- a/njs/njs_object.c	Mon Jun 19 14:46:39 2017 +0300
+++ b/njs/njs_object.c	Mon Jun 19 14:46:46 2017 +0300
@@ -840,6 +840,54 @@ njs_object_seal(njs_vm_t *vm, njs_value_
 
 
 static njs_ret_t
+njs_object_is_sealed(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    nxt_lvlhsh_t       *hash;
+    njs_object_t       *object;
+    njs_object_prop_t  *prop;
+    nxt_lvlhsh_each_t  lhe;
+    const njs_value_t  *retval;
+
+    if (nargs < 2 || !njs_is_object(&args[1])) {
+        vm->exception = &njs_exception_type_error;
+        return NXT_ERROR;
+    }
+
+    retval = &njs_string_false;
+
+    object = args[1].data.u.object;
+    nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+
+    hash = &object->hash;
+
+    if (object->extensible) {
+        goto done;
+    }
+
+    for ( ;; ) {
+        prop = nxt_lvlhsh_each(hash, &lhe);
+
+        if (prop == NULL) {
+            break;
+        }
+
+        if (prop->writable) {
+            goto done;
+        }
+    }
+
+    retval = &njs_string_true;
+
+done:
+
+    vm->retval = *retval;
+
+    return NXT_OK;
+}
+
+
+static njs_ret_t
 njs_object_prevent_extensions(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
@@ -1078,6 +1126,14 @@ static const njs_object_prop_t  njs_obje
                                      NJS_SKIP_ARG, NJS_OBJECT_ARG),
     },
 
+    /* Object.isSealed(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("isSealed"),
+        .value = njs_native_function(njs_object_is_sealed, 0,
+                                     NJS_SKIP_ARG, NJS_OBJECT_ARG),
+    },
+
     /* Object.preventExtensions(). */
     {
         .type = NJS_METHOD,
diff -r cc5ab912d455 -r bcd7a7256805 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Jun 19 14:46:39 2017 +0300
+++ b/njs/test/njs_unit_test.c	Mon Jun 19 14:46:46 2017 +0300
@@ -6236,6 +6236,57 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Object.seal('')"),
       nxt_string("TypeError") },
 
+    { nxt_string("Object.isSealed({a:1})"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isSealed([1,2])"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isSealed(function() {})"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isSealed(new Date(''))"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isSealed(new RegExp(''))"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isSealed(1)"),
+      nxt_string("TypeError") },
+
+    { nxt_string("Object.isSealed('')"),
+      nxt_string("TypeError") },
+
+    { nxt_string("Object.isSealed(Object.defineProperties({}, {a:{value:1}}))"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{}, b:{}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isSealed(o)"),
+      nxt_string("true") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{}, b:{writable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isSealed(o)"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{writable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isSealed(o)"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{configurable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isSealed(o)"),
+      nxt_string("true") },
+
+    { nxt_string("var o = Object.preventExtensions({a:1});"
+                 "Object.isFrozen(o)"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.freeze({a:1}); Object.isFrozen(o)"),
+      nxt_string("true") },
+
     { nxt_string("var o = Object.preventExtensions({a:1});"
                  "Object.defineProperty(o, 'b', {value:1})"),
       nxt_string("TypeError") },


More information about the nginx-devel mailing list