[njs] Object.isFrozen() method.

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


details:   http://hg.nginx.org/njs/rev/b2ccd7673a5e
branches:  
changeset: 368:b2ccd7673a5e
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jun 19 14:41:03 2017 +0300
description:
Object.isFrozen() 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 4d5a5d618fca -r b2ccd7673a5e njs/njs_object.c
--- a/njs/njs_object.c	Mon Jun 19 14:40:14 2017 +0300
+++ b/njs/njs_object.c	Mon Jun 19 14:41:03 2017 +0300
@@ -755,6 +755,54 @@ njs_object_freeze(njs_vm_t *vm, njs_valu
 
 
 static njs_ret_t
+njs_object_is_frozen(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 || prop->configurable) {
+            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)
 {
@@ -957,6 +1005,14 @@ static const njs_object_prop_t  njs_obje
                                      NJS_SKIP_ARG, NJS_OBJECT_ARG),
     },
 
+    /* Object.isFrozen(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("isFrozen"),
+        .value = njs_native_function(njs_object_is_frozen, 0,
+                                     NJS_SKIP_ARG, NJS_OBJECT_ARG),
+    },
+
     /* Object.preventExtensions(). */
     {
         .type = NJS_METHOD,
diff -r 4d5a5d618fca -r b2ccd7673a5e njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Jun 19 14:40:14 2017 +0300
+++ b/njs/test/njs_unit_test.c	Mon Jun 19 14:41:03 2017 +0300
@@ -6164,6 +6164,57 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var r = Object.freeze(new RegExp('')); r.a = 1; r.a"),
       nxt_string("undefined") },
 
+    { nxt_string("Object.isFrozen({a:1})"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isFrozen([1,2])"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isFrozen(function() {})"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isFrozen(new Date(''))"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isFrozen(new RegExp(''))"),
+      nxt_string("false") },
+
+    { nxt_string("Object.isFrozen(1)"),
+      nxt_string("TypeError") },
+
+    { nxt_string("Object.isFrozen('')"),
+      nxt_string("TypeError") },
+
+    { nxt_string("Object.isFrozen(Object.defineProperties({}, {a:{value:1}}))"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{}, b:{}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isFrozen(o)"),
+      nxt_string("true") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{}, b:{writable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isFrozen(o)"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{writable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isFrozen(o)"),
+      nxt_string("false") },
+
+    { nxt_string("var o = Object.defineProperties({}, {a:{configurable:1}});"
+                 "o = Object.preventExtensions(o);"
+                 "Object.isFrozen(o)"),
+      nxt_string("false") },
+
+    { 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