[njs] Object.seal() method.
Dmitry Volyntsev
xeioex at nginx.com
Mon Jun 19 11:49:35 UTC 2017
details: http://hg.nginx.org/njs/rev/cc5ab912d455
branches:
changeset: 370:cc5ab912d455
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon Jun 19 14:46:39 2017 +0300
description:
Object.seal() method.
diffstat:
njs/njs_object.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
njs/test/njs_unit_test.c | 21 +++++++++++++++++++++
2 files changed, 66 insertions(+), 0 deletions(-)
diffs (93 lines):
diff -r bd6c05f66ea9 -r cc5ab912d455 njs/njs_object.c
--- a/njs/njs_object.c Mon Jun 19 14:46:34 2017 +0300
+++ b/njs/njs_object.c Mon Jun 19 14:46:39 2017 +0300
@@ -803,6 +803,43 @@ done:
static njs_ret_t
+njs_object_seal(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;
+
+ if (nargs < 2 || !njs_is_object(&args[1])) {
+ vm->exception = &njs_exception_type_error;
+ return NXT_ERROR;
+ }
+
+ object = args[1].data.u.object;
+ object->extensible = 0;
+
+ nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+
+ hash = &object->hash;
+
+ for ( ;; ) {
+ prop = nxt_lvlhsh_each(hash, &lhe);
+
+ if (prop == NULL) {
+ break;
+ }
+
+ prop->configurable = 0;
+ }
+
+ vm->retval = args[1];
+
+ 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)
{
@@ -1033,6 +1070,14 @@ static const njs_object_prop_t njs_obje
NJS_SKIP_ARG, NJS_OBJECT_ARG),
},
+ /* Object.seal(). */
+ {
+ .type = NJS_METHOD,
+ .name = njs_string("seal"),
+ .value = njs_native_function(njs_object_seal, 0,
+ NJS_SKIP_ARG, NJS_OBJECT_ARG),
+ },
+
/* Object.preventExtensions(). */
{
.type = NJS_METHOD,
diff -r bd6c05f66ea9 -r cc5ab912d455 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Jun 19 14:46:34 2017 +0300
+++ b/njs/test/njs_unit_test.c Mon Jun 19 14:46:39 2017 +0300
@@ -6215,6 +6215,27 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("var o = Object.freeze({a:1}); Object.isFrozen(o)"),
nxt_string("true") },
+ { nxt_string("var o = Object.seal({a:1}); o.a = 2; o.a"),
+ nxt_string("2") },
+
+ { nxt_string("var o = Object.seal({a:1}); delete o.a; o.a"),
+ nxt_string("1") },
+
+ { nxt_string("var o = Object.seal({a:1}); o.b = 1; o.b"),
+ nxt_string("undefined") },
+
+ { nxt_string("var o = Object.seal(Object.create({a:1})); o.a = 2; o.a"),
+ nxt_string("1") },
+
+ { nxt_string("var o = Object.seal({a:{b:1}}); o.a.b = 2; o.a.b"),
+ nxt_string("2") },
+
+ { nxt_string("Object.seal(1)"),
+ nxt_string("TypeError") },
+
+ { nxt_string("Object.seal('')"),
+ nxt_string("TypeError") },
+
{ 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