[njs] Unified creation of objects with custom value slots.
Dmitry Volyntsev
xeioex at nginx.com
Tue Nov 2 12:40:59 UTC 2021
details: https://hg.nginx.org/njs/rev/0c5745fc200d
branches:
changeset: 1737:0c5745fc200d
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Nov 02 12:38:02 2021 +0000
description:
Unified creation of objects with custom value slots.
diffstat:
src/njs_crypto.c | 31 ++-----------------------------
src/njs_encoding.c | 52 ++++++++++++----------------------------------------
src/njs_extern.c | 19 ++-----------------
src/njs_iterator.c | 22 +++++-----------------
4 files changed, 21 insertions(+), 103 deletions(-)
diffs (247 lines):
diff -r 3257b9892274 -r 0c5745fc200d src/njs_crypto.c
--- a/src/njs_crypto.c Tue Nov 02 12:37:19 2021 +0000
+++ b/src/njs_crypto.c Tue Nov 02 12:38:02 2021 +0000
@@ -129,33 +129,6 @@ static njs_crypto_enc_t njs_encodings[]
};
-static njs_object_value_t *
-njs_crypto_object_value_alloc(njs_vm_t *vm, njs_object_type_t type)
-{
- njs_object_value_t *ov;
-
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
-
- if (njs_fast_path(ov != NULL)) {
- njs_lvlhsh_init(&ov->object.hash);
- njs_lvlhsh_init(&ov->object.shared_hash);
- ov->object.type = NJS_OBJECT_VALUE;
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
-
- ov->object.__proto__ = &vm->prototypes[type].object;
- ov->object.slots = NULL;
- return ov;
- }
-
- njs_memory_error(vm);
-
- return NULL;
-}
-
-
static njs_int_t
njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
@@ -169,7 +142,7 @@ njs_crypto_create_hash(njs_vm_t *vm, njs
return NJS_ERROR;
}
- hash = njs_crypto_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HASH);
+ hash = njs_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HASH, 0, NULL);
if (njs_slow_path(hash == NULL)) {
return NJS_ERROR;
}
@@ -510,7 +483,7 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs
alg->init(&ctx->u);
alg->update(&ctx->u, key_buf, 64);
- hmac = njs_crypto_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HMAC);
+ hmac = njs_object_value_alloc(vm, NJS_OBJ_TYPE_CRYPTO_HMAC, 0, NULL);
if (njs_slow_path(hmac == NULL)) {
return NJS_ERROR;
}
diff -r 3257b9892274 -r 0c5745fc200d src/njs_encoding.c
--- a/src/njs_encoding.c Tue Nov 02 12:37:19 2021 +0000
+++ b/src/njs_encoding.c Tue Nov 02 12:38:02 2021 +0000
@@ -48,34 +48,20 @@ static njs_int_t
njs_text_encoder_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- njs_object_t *proto;
- njs_object_value_t *ov;
+ njs_object_value_t *encoder;
if (!vm->top_frame->ctor) {
njs_type_error(vm, "Constructor of TextEncoder requires 'new'");
return NJS_ERROR;
}
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
- if (njs_slow_path(ov == NULL)) {
- njs_memory_error(vm);
+ encoder = njs_object_value_alloc(vm, NJS_OBJ_TYPE_TEXT_ENCODER, 0, NULL);
+ if (njs_slow_path(encoder == NULL)) {
return NJS_ERROR;
}
- proto = &vm->prototypes[NJS_OBJ_TYPE_TEXT_ENCODER].object;
-
- njs_lvlhsh_init(&ov->object.hash);
- njs_lvlhsh_init(&ov->object.shared_hash);
- ov->object.type = NJS_OBJECT_VALUE;
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
- ov->object.__proto__ = proto;
- ov->object.slots = NULL;
-
- njs_set_data(&ov->value, NULL, NJS_DATA_TAG_TEXT_ENCODER);
- njs_set_object_value(&vm->retval, ov);
+ njs_set_data(&encoder->value, NULL, NJS_DATA_TAG_TEXT_ENCODER);
+ njs_set_object_value(&vm->retval, encoder);
return NJS_OK;
}
@@ -330,8 +316,7 @@ njs_text_decoder_constructor(njs_vm_t *v
njs_index_t unused)
{
njs_int_t ret;
- njs_object_t *proto;
- njs_object_value_t *ov;
+ njs_object_value_t *decoder;
njs_encoding_decode_t *data;
if (!vm->top_frame->ctor) {
@@ -339,26 +324,13 @@ njs_text_decoder_constructor(njs_vm_t *v
return NJS_ERROR;
}
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t)
- + sizeof(njs_encoding_decode_t));
- if (njs_slow_path(ov == NULL)) {
- njs_memory_error(vm);
+ decoder = njs_object_value_alloc(vm, NJS_OBJ_TYPE_TEXT_DECODER,
+ sizeof(njs_encoding_decode_t), NULL);
+ if (njs_slow_path(decoder == NULL)) {
return NJS_ERROR;
}
- proto = &vm->prototypes[NJS_OBJ_TYPE_TEXT_DECODER].object;
-
- njs_lvlhsh_init(&ov->object.hash);
- njs_lvlhsh_init(&ov->object.shared_hash);
- ov->object.type = NJS_OBJECT_VALUE;
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
- ov->object.__proto__ = proto;
- ov->object.slots = NULL;
-
- data = (njs_encoding_decode_t *) ((uint8_t *) ov
+ data = (njs_encoding_decode_t *) ((uint8_t *) decoder
+ sizeof(njs_object_value_t));
ret = njs_text_decoder_arg_encoding(vm, args, nargs, data);
@@ -373,8 +345,8 @@ njs_text_decoder_constructor(njs_vm_t *v
njs_utf8_decode_init(&data->ctx);
- njs_set_data(&ov->value, data, NJS_DATA_TAG_TEXT_DECODER);
- njs_set_object_value(&vm->retval, ov);
+ njs_set_data(&decoder->value, data, NJS_DATA_TAG_TEXT_DECODER);
+ njs_set_object_value(&vm->retval, decoder);
return NJS_OK;
}
diff -r 3257b9892274 -r 0c5745fc200d src/njs_extern.c
--- a/src/njs_extern.c Tue Nov 02 12:37:19 2021 +0000
+++ b/src/njs_extern.c Tue Nov 02 12:38:02 2021 +0000
@@ -179,22 +179,14 @@ njs_external_prop_handler(njs_vm_t *vm,
*retval = *setval;
} else {
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
+ ov = njs_object_value_alloc(vm, NJS_OBJ_TYPE_OBJECT, 0, NULL);
if (njs_slow_path(ov == NULL)) {
- njs_memory_error(vm);
return NJS_ERROR;
}
slots = njs_object(value)->slots + self->value.data.magic16;
- njs_lvlhsh_init(&ov->object.hash);
ov->object.shared_hash = slots->external_shared_hash;
- ov->object.type = NJS_OBJECT;
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
- ov->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
ov->object.slots = slots;
external = njs_vm_external(vm, NJS_PROTO_ID_ANY, value);
@@ -308,23 +300,16 @@ njs_vm_external_create(njs_vm_t *vm, njs
proto = ((uintptr_t *) vm->protos->start)[proto_id];
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
+ ov = njs_object_value_alloc(vm, NJS_OBJ_TYPE_OBJECT, 0, NULL);
if (njs_slow_path(ov == NULL)) {
- njs_memory_error(vm);
return NJS_ERROR;
}
protos = (njs_arr_t *) proto;
slots = protos->start;
- njs_lvlhsh_init(&ov->object.hash);
ov->object.shared_hash = slots->external_shared_hash;
- ov->object.type = NJS_OBJECT;
ov->object.shared = shared;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
- ov->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
ov->object.slots = slots;
njs_set_object_value(value, ov);
diff -r 3257b9892274 -r 0c5745fc200d src/njs_iterator.c
--- a/src/njs_iterator.c Tue Nov 02 12:37:19 2021 +0000
+++ b/src/njs_iterator.c Tue Nov 02 12:38:02 2021 +0000
@@ -34,27 +34,15 @@ njs_int_t
njs_array_iterator_create(njs_vm_t *vm, const njs_value_t *target,
njs_value_t *retval, njs_object_enum_t kind)
{
- njs_object_value_t *ov;
+ njs_object_value_t *iterator;
njs_array_iterator_t *it;
- ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
- if (njs_slow_path(ov == NULL)) {
+ iterator = njs_object_value_alloc(vm, NJS_OBJ_TYPE_ARRAY_ITERATOR, 0, NULL);
+ if (njs_slow_path(iterator == NULL)) {
njs_memory_error(vm);
return NJS_ERROR;
}
- njs_lvlhsh_init(&ov->object.hash);
- njs_lvlhsh_init(&ov->object.shared_hash);
- ov->object.type = NJS_OBJECT_VALUE;
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
-
- ov->object.__proto__ =
- &vm->prototypes[NJS_OBJ_TYPE_ARRAY_ITERATOR].object;
- ov->object.slots = NULL;
-
it = njs_mp_alloc(vm->mem_pool, sizeof(njs_array_iterator_t));
if (njs_slow_path(it == NULL)) {
njs_memory_error(vm);
@@ -66,8 +54,8 @@ njs_array_iterator_create(njs_vm_t *vm,
it->next = 0;
it->kind = kind;
- njs_set_data(&ov->value, it, NJS_DATA_TAG_ARRAY_ITERATOR);
- njs_set_object_value(retval, ov);
+ njs_set_data(&iterator->value, it, NJS_DATA_TAG_ARRAY_ITERATOR);
+ njs_set_object_value(retval, iterator);
return NJS_OK;
}
More information about the nginx-devel
mailing list