From: Artem S. Povalyukhin Date: Thu, 28 Nov 2019 10:25:00 +0000 (+0300) Subject: Improved Object.create(). X-Git-Tag: 0.3.8~40 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=7bac23947b324a2ee3d42016d252fd97427fcb8a;p=njs.git Improved Object.create(). This closes #261 issue on Github. --- diff --git a/src/njs_object.c b/src/njs_object.c index 592f6c90..38bcd8e0 100644 --- a/src/njs_object.c +++ b/src/njs_object.c @@ -28,6 +28,8 @@ static njs_int_t njs_object_enumerate_object(njs_vm_t *vm, 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_value_t *args, njs_uint_t nargs, } -/* 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_value_t *args, njs_uint_t nargs, 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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 873c5882..95bfdbb5 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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") },