Welcome! Log In Create A New Profile

Advanced

[njs] Improved Object.create().

Dmitry Volyntsev
November 28, 2019 07:12AM
details: https://hg.nginx.org/njs/rev/5bbbc4361799
branches:
changeset: 1269:5bbbc4361799
user: Artem S. Povalyukhin <artem.povaluhin@gmail.com>
date: Thu Nov 28 13:25:00 2019 +0300
description:
Improved Object.create().

This closes #261 issue on Github.

diffstat:

src/njs_object.c | 16 +++++++++++++---
src/test/njs_unit_test.c | 11 +++++++++++
2 files changed, 24 insertions(+), 3 deletions(-)

diffs (72 lines):

diff -r 9dfc67399ef5 -r 5bbbc4361799 src/njs_object.c
--- a/src/njs_object.c Thu Nov 28 09:59:10 2019 +0300
+++ b/src/njs_object.c Thu Nov 28 13:25:00 2019 +0300
@@ -28,6 +28,8 @@ static njs_int_t njs_object_enumerate_ob
static njs_int_t njs_object_own_enumerate_object(njs_vm_t *vm,
const njs_object_t *object, const njs_object_t *parent, njs_array_t *items,
njs_object_enum_t kind, njs_object_enum_type_t type, njs_bool_t all);
+static njs_int_t njs_object_define_properties(njs_vm_t *vm, njs_value_t *args,
+ njs_uint_t nargs, njs_index_t unused);


njs_object_t *
@@ -250,13 +252,11 @@ njs_object_constructor(njs_vm_t *vm, njs
}


-/* TODO: properties with attributes. */
-
static njs_int_t
njs_object_create(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- njs_value_t *value;
+ njs_value_t *value, *descs, arguments[3];
njs_object_t *object;

value = njs_arg(args, nargs, 1);
@@ -278,6 +278,16 @@ njs_object_create(njs_vm_t *vm, njs_valu

njs_set_object(&vm->retval, object);

+ descs = njs_arg(args, nargs, 2);
+
+ if (njs_slow_path(!njs_is_undefined(descs))) {
+ arguments[0] = args[0];
+ arguments[1] = vm->retval;
+ arguments[2] = *descs;
+
+ return njs_object_define_properties(vm, arguments, 3, unused);
+ }
+
return NJS_OK;
}

diff -r 9dfc67399ef5 -r 5bbbc4361799 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Thu Nov 28 09:59:10 2019 +0300
+++ b/src/test/njs_unit_test.c Thu Nov 28 13:25:00 2019 +0300
@@ -10762,6 +10762,8 @@ static njs_unit_test_t njs_test[] =
"o.__proto__ === p"),
njs_str("true") },

+ /* Object.create() */
+
{ njs_str("var o = Object.create(Object.prototype);"
"o.__proto__ === Object.prototype"),
njs_str("true") },
@@ -10775,6 +10777,15 @@ static njs_unit_test_t njs_test[] =
{ njs_str("Object.create(1)"),
njs_str("TypeError: prototype may only be an object or null: number") },

+ { njs_str("var o = Object.create(null, { a: { value: 1 } }); o.a"),
+ njs_str("1") },
+
+ { njs_str("var o = Object.create({ a: 0 }, { a: { value: 1 } }); o.a"),
+ njs_str("1") },
+
+ { njs_str("var o = Object.create({ get a() { return this.b; } }, { b: { value: 1 } }); o.a"),
+ njs_str("1") },
+
{ njs_str("var o = {a:1, b:2, c:3};"
"Object.keys(o)"),
njs_str("a,b,c") },
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Improved Object.create().

Dmitry Volyntsev 64 November 28, 2019 07:12AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 70
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready