[njs] Object.preventExtensions() method.

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


details:   http://hg.nginx.org/njs/rev/4d5a5d618fca
branches:  
changeset: 367:4d5a5d618fca
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jun 19 14:40:14 2017 +0300
description:
Object.preventExtensions() method.

diffstat:

 njs/njs_object.c         |  25 +++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  17 +++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)

diffs (69 lines):

diff -r 824fbb7fcd35 -r 4d5a5d618fca njs/njs_object.c
--- a/njs/njs_object.c	Mon Jun 19 14:39:56 2017 +0300
+++ b/njs/njs_object.c	Mon Jun 19 14:40:14 2017 +0300
@@ -754,6 +754,23 @@ njs_object_freeze(njs_vm_t *vm, njs_valu
 }
 
 
+static njs_ret_t
+njs_object_prevent_extensions(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    if (nargs < 2 || !njs_is_object(&args[1])) {
+        vm->exception = &njs_exception_type_error;
+        return NXT_ERROR;
+    }
+
+    args[1].data.u.object->extensible = 0;
+
+    vm->retval = args[1];
+
+    return NXT_OK;
+}
+
+
 /*
  * The __proto__ property of booleans, numbers and strings primitives,
  * of objects created by Boolean(), Number(), and String() constructors,
@@ -939,6 +956,14 @@ static const njs_object_prop_t  njs_obje
         .value = njs_native_function(njs_object_freeze, 0,
                                      NJS_SKIP_ARG, NJS_OBJECT_ARG),
     },
+
+    /* Object.preventExtensions(). */
+    {
+        .type = NJS_METHOD,
+        .name = njs_long_string("preventExtensions"),
+        .value = njs_native_function(njs_object_prevent_extensions, 0,
+                                     NJS_SKIP_ARG, NJS_OBJECT_ARG),
+    },
 };
 
 
diff -r 824fbb7fcd35 -r 4d5a5d618fca njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Jun 19 14:39:56 2017 +0300
+++ b/njs/test/njs_unit_test.c	Mon Jun 19 14:40:14 2017 +0300
@@ -6164,6 +6164,23 @@ 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("var o = Object.preventExtensions({a:1});"
+                 "Object.defineProperty(o, 'b', {value:1})"),
+      nxt_string("TypeError") },
+
+    { nxt_string("var o = Object.preventExtensions({a:1});"
+                 "Object.defineProperties(o, {b:{value:1}})"),
+      nxt_string("TypeError") },
+
+    { nxt_string("var o = Object.preventExtensions({a:1}); o.a = 2; o.a"),
+      nxt_string("2") },
+
+    { nxt_string("var o = Object.preventExtensions({a:1}); delete o.a; o.a"),
+      nxt_string("undefined") },
+
+    { nxt_string("var o = Object.preventExtensions({a:1}); o.b = 1; o.b"),
+      nxt_string("undefined") },
+
     { nxt_string("var d = new Date(''); d +' '+ d.getTime()"),
       nxt_string("Invalid Date NaN") },
 


More information about the nginx-devel mailing list