From 3376843bbc33bbcbaf9063b68efd988e0370ef00 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Mon, 8 Jul 2019 17:49:43 +0300 Subject: [PATCH] Using njs_object() macro where applicable. --- njs/njs_array.c | 10 +++--- njs/njs_boolean.c | 8 ++--- njs/njs_builtin.c | 2 +- njs/njs_crypto.c | 70 +++++++++++++++------------------------ njs/njs_error.c | 18 ++++------ njs/njs_fs.c | 8 ++--- njs/njs_function.c | 2 +- njs/njs_json.c | 22 ++++++------ njs/njs_number.c | 8 ++--- njs/njs_object.c | 56 ++++++++++++++----------------- njs/njs_object_property.c | 6 ++-- njs/njs_parser_terminal.c | 2 +- njs/njs_string.c | 8 ++--- njs/njs_value.c | 8 ++--- njs/njs_value.h | 23 +++++++++++++ njs/njs_vm.c | 12 +++---- 16 files changed, 122 insertions(+), 141 deletions(-) diff --git a/njs/njs_array.c b/njs/njs_array.c index f5dc17b2..7edd538e 100644 --- a/njs/njs_array.c +++ b/njs/njs_array.c @@ -443,7 +443,7 @@ njs_array_length(njs_vm_t *vm, njs_value_t *value, njs_value_t *setval, njs_array_t *array; njs_object_t *proto; - proto = value->data.u.object; + proto = njs_object(value); if (setval == NULL) { do { @@ -633,7 +633,7 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this, } else if (njs_is_string(this) || this->type == NJS_OBJECT_STRING) { if (this->type == NJS_OBJECT_STRING) { - this = &this->data.u.object_value->value; + this = njs_object_value(this); } string_slice.start = start; @@ -967,7 +967,7 @@ njs_array_prototype_to_string(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, lhq.key_hash = NJS_JOIN_HASH; lhq.key = nxt_string_value("join"); - prop = njs_object_property(vm, args[0].data.u.object, &lhq); + prop = njs_object_property(vm, njs_object(&args[0]), &lhq); if (nxt_fast_path(prop != NULL && njs_is_function(&prop->value))) { return njs_function_replace(vm, prop->value.data.u.function, @@ -1470,9 +1470,7 @@ njs_array_prototype_fill_continuation(njs_vm_t *vm, njs_value_t *args, return NXT_ERROR; } - vm->retval.data.u.object = object; - vm->retval.type = object->type; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, object, object->type); return NXT_OK; } diff --git a/njs/njs_boolean.c b/njs/njs_boolean.c index 4fbc47fc..4c194a58 100644 --- a/njs/njs_boolean.c +++ b/njs/njs_boolean.c @@ -27,9 +27,7 @@ njs_boolean_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_ERROR; } - vm->retval.data.u.object = object; - vm->retval.type = NJS_OBJECT_BOOLEAN; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, object, NJS_OBJECT_BOOLEAN); } else { vm->retval = *value; @@ -84,7 +82,7 @@ njs_boolean_prototype_value_of(njs_vm_t *vm, njs_value_t *args, if (value->type != NJS_BOOLEAN) { if (value->type == NJS_OBJECT_BOOLEAN) { - value = &value->data.u.object_value->value; + value = njs_object_value(value); } else { njs_type_error(vm, "unexpected value type:%s", @@ -110,7 +108,7 @@ njs_boolean_prototype_to_string(njs_vm_t *vm, njs_value_t *args, if (value->type != NJS_BOOLEAN) { if (value->type == NJS_OBJECT_BOOLEAN) { - value = &value->data.u.object_value->value; + value = njs_object_value(value); } else { njs_type_error(vm, "unexpected value type:%s", diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 48f5878b..8ab2c3bd 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -854,7 +854,7 @@ njs_vm_expression_completions(njs_vm_t *vm, nxt_str_t *expression) value = &prop->value; } - return njs_object_completions(vm, value->data.u.object); + return njs_object_completions(vm, njs_object(value)); } diff --git a/njs/njs_crypto.c b/njs/njs_crypto.c index 92825df1..d4ca5f67 100644 --- a/njs/njs_crypto.c +++ b/njs/njs_crypto.c @@ -187,10 +187,7 @@ njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, alg->init(&dgst->u); njs_set_data(&hash->value, dgst); - - vm->retval.data.u.object_value = hash; - vm->retval.type = NJS_OBJECT_VALUE; - vm->retval.data.truth = 1; + njs_set_object_value(&vm->retval, hash); return NJS_OK; } @@ -200,9 +197,8 @@ static njs_ret_t njs_hash_prototype_update(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { - nxt_str_t data; - njs_digest_t *dgst; - njs_object_value_t *hash; + nxt_str_t data; + njs_digest_t *dgst; if (nxt_slow_path(nargs < 2 || !njs_is_string(&args[1]))) { njs_type_error(vm, "data must be a string"); @@ -214,16 +210,14 @@ njs_hash_prototype_update(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NJS_ERROR; } - if (nxt_slow_path(!njs_is_data(&args[0].data.u.object_value->value))) { + if (nxt_slow_path(!njs_is_data(njs_object_value(&args[0])))) { njs_type_error(vm, "value of \"this\" is not a data type"); return NJS_ERROR; } - hash = args[0].data.u.object_value; - njs_string_get(&args[1], &data); - dgst = njs_value_data(&hash->value); + dgst = njs_value_data(njs_object_value(&args[0])); if (nxt_slow_path(dgst->alg == NULL)) { njs_error(vm, "Digest already called"); @@ -242,13 +236,12 @@ static njs_ret_t njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { - u_char digest[32], *p; - njs_ret_t ret; - nxt_str_t enc_name, str; - njs_digest_t *dgst; - njs_hash_alg_t *alg; - njs_crypto_enc_t *enc; - njs_object_value_t *hash; + u_char digest[32], *p; + njs_ret_t ret; + nxt_str_t enc_name, str; + njs_digest_t *dgst; + njs_hash_alg_t *alg; + njs_crypto_enc_t *enc; if (nxt_slow_path(nargs > 1 && !njs_is_string(&args[1]))) { njs_type_error(vm, "encoding must be a string"); @@ -260,7 +253,7 @@ njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NJS_ERROR; } - if (nxt_slow_path(!njs_is_data(&args[0].data.u.object_value->value))) { + if (nxt_slow_path(!njs_is_data(njs_object_value(&args[0])))) { njs_type_error(vm, "value of \"this\" is not a data type"); return NJS_ERROR; } @@ -276,9 +269,7 @@ njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } } - hash = args[0].data.u.object_value; - - dgst = njs_value_data(&hash->value); + dgst = njs_value_data(njs_object_value(&args[0])); if (nxt_slow_path(dgst->alg == NULL)) { njs_error(vm, "Digest already called"); @@ -453,10 +444,7 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } njs_set_data(&hmac->value, ctx); - - vm->retval.data.u.object_value = hmac; - vm->retval.type = NJS_OBJECT_VALUE; - vm->retval.data.truth = 1; + njs_set_object_value(&vm->retval, hmac); return NJS_OK; } @@ -466,9 +454,8 @@ static njs_ret_t njs_hmac_prototype_update(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { - nxt_str_t data; - njs_hmac_t *ctx; - njs_object_value_t *hmac; + nxt_str_t data; + njs_hmac_t *ctx; if (nxt_slow_path(nargs < 2 || !njs_is_string(&args[1]))) { njs_type_error(vm, "data must be a string"); @@ -480,16 +467,14 @@ njs_hmac_prototype_update(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NJS_ERROR; } - if (nxt_slow_path(!njs_is_data(&args[0].data.u.object_value->value))) { + if (nxt_slow_path(!njs_is_data(njs_object_value(&args[0])))) { njs_type_error(vm, "value of \"this\" is not a data type"); return NJS_ERROR; } - hmac = args[0].data.u.object_value; - njs_string_get(&args[1], &data); - ctx = njs_value_data(&hmac->value); + ctx = njs_value_data(njs_object_value(&args[0])); if (nxt_slow_path(ctx->alg == NULL)) { njs_error(vm, "Digest already called"); @@ -508,13 +493,12 @@ static njs_ret_t njs_hmac_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { - u_char hash1[32], digest[32], *p; - nxt_str_t enc_name, str; - njs_ret_t ret; - njs_hmac_t *ctx; - njs_hash_alg_t *alg; - njs_crypto_enc_t *enc; - njs_object_value_t *hmac; + u_char hash1[32], digest[32], *p; + nxt_str_t enc_name, str; + njs_ret_t ret; + njs_hmac_t *ctx; + njs_hash_alg_t *alg; + njs_crypto_enc_t *enc; if (nxt_slow_path(nargs > 1 && !njs_is_string(&args[1]))) { njs_type_error(vm, "encoding must be a string"); @@ -526,7 +510,7 @@ njs_hmac_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NJS_ERROR; } - if (nxt_slow_path(!njs_is_data(&args[0].data.u.object_value->value))) { + if (nxt_slow_path(!njs_is_data(njs_object_value(&args[0])))) { njs_type_error(vm, "value of \"this\" is not a data type"); return NJS_ERROR; } @@ -542,9 +526,7 @@ njs_hmac_prototype_digest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } } - hmac = args[0].data.u.object_value; - - ctx = njs_value_data(&hmac->value); + ctx = njs_value_data(njs_object_value(&args[0])); if (nxt_slow_path(ctx->alg == NULL)) { njs_error(vm, "Digest already called"); diff --git a/njs/njs_error.c b/njs/njs_error.c index 228d542e..b5f89773 100644 --- a/njs/njs_error.c +++ b/njs/njs_error.c @@ -29,9 +29,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_value_type_t type, error = njs_error_alloc(vm, type, NULL, &string); if (nxt_fast_path(error != NULL)) { - dst->data.u.object = error; - dst->type = type; - dst->data.truth = 1; + njs_set_type_object(dst, error, type); } } @@ -148,9 +146,7 @@ njs_error_create(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_ERROR; } - vm->retval.data.u.object = error; - vm->retval.type = type; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, error, type); return NXT_OK; } @@ -513,9 +509,7 @@ njs_memory_error_set(njs_vm_t *vm, njs_value_t *value) */ object->extensible = 0; - value->data.type = NJS_OBJECT_INTERNAL_ERROR; - value->data.truth = 1; - value->data.u.object = object; + njs_set_type_object(value, object, NJS_OBJECT_INTERNAL_ERROR); } @@ -634,7 +628,7 @@ njs_error_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *error) lhq.key = nxt_string_value("name"); lhq.proto = &njs_object_hash_proto; - prop = njs_object_property(vm, error->data.u.object, &lhq); + prop = njs_object_property(vm, njs_object(error), &lhq); if (prop != NULL) { name_value = &prop->value; @@ -648,7 +642,7 @@ njs_error_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *error) lhq.key_hash = NJS_MESSAGE_HASH; lhq.key = nxt_string_value("message"); - prop = njs_object_property(vm, error->data.u.object, &lhq); + prop = njs_object_property(vm, njs_object(error), &lhq); if (prop != NULL) { message_value = &prop->value; @@ -773,7 +767,7 @@ njs_internal_error_prototype_to_string(njs_vm_t *vm, njs_value_t *args, if (nargs >= 1 && njs_is_object(&args[0])) { /* MemoryError is a nonextensible internal error. */ - if (!args[0].data.u.object->extensible) { + if (!njs_object(&args[0])->extensible) { static const njs_value_t name = njs_string("MemoryError"); vm->retval = name; diff --git a/njs/njs_fs.c b/njs/njs_fs.c index 983aad06..e2bdd6fe 100644 --- a/njs/njs_fs.c +++ b/njs/njs_fs.c @@ -979,9 +979,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall, } } - retval->data.u.object = error; - retval->type = NJS_OBJECT_ERROR; - retval->data.truth = 1; + njs_set_type_object(retval, error, NJS_OBJECT_ERROR); return NJS_OK; } @@ -1007,14 +1005,14 @@ njs_fs_mode(njs_value_t *value) { switch (value->type) { case NJS_OBJECT_NUMBER: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_NUMBER: return (mode_t) njs_number(value); case NJS_OBJECT_STRING: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_STRING: diff --git a/njs/njs_function.c b/njs/njs_function.c index 213f2243..6f3c7751 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -910,7 +910,7 @@ njs_function_instance_length(njs_vm_t *vm, njs_value_t *value, njs_function_t *function; njs_function_lambda_t *lambda; - proto = value->data.u.object; + proto = njs_object(value); do { if (nxt_fast_path(proto->type == NJS_FUNCTION)) { diff --git a/njs/njs_json.c b/njs/njs_json.c index 2cbe7825..2ea449b5 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -1487,7 +1487,7 @@ njs_object_to_json_function(njs_vm_t *vm, njs_value_t *value) lhq.key_hash = NJS_TO_JSON_HASH; lhq.key = nxt_string_value("toJSON"); - prop = njs_object_property(vm, value->data.u.object, &lhq); + prop = njs_object_property(vm, njs_object(value), &lhq); if (prop != NULL && njs_is_function(&prop->value)) { return prop->value.data.u.function; @@ -1622,7 +1622,7 @@ njs_json_stringify_array(njs_vm_t *vm, njs_json_stringify_t *stringify) switch (value->type) { case NJS_OBJECT_NUMBER: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_NUMBER: @@ -1635,7 +1635,7 @@ njs_json_stringify_array(njs_vm_t *vm, njs_json_stringify_t *stringify) break; case NJS_OBJECT_STRING: - value = &value->data.u.object_value->value; + value = njs_object_value(value); break; case NJS_STRING: @@ -1736,21 +1736,21 @@ njs_json_append_value(njs_json_stringify_t *stringify, const njs_value_t *value) { switch (value->type) { case NJS_OBJECT_STRING: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_STRING: return njs_json_append_string(stringify, value, '\"'); case NJS_OBJECT_NUMBER: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_NUMBER: return njs_json_append_number(stringify, value); case NJS_OBJECT_BOOLEAN: - value = &value->data.u.object_value->value; + value = njs_object_value(value); /* Fall through. */ case NJS_BOOLEAN: @@ -1924,7 +1924,7 @@ njs_json_wrap_value(njs_vm_t *vm, const njs_value_t *value) } wrapper->data.u.object = njs_object_alloc(vm); - if (nxt_slow_path(wrapper->data.u.object == NULL)) { + if (nxt_slow_path(njs_object(wrapper) == NULL)) { return NULL; } @@ -2134,7 +2134,7 @@ njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value, switch (value->type) { case NJS_OBJECT_STRING: - value = &value->data.u.object_value->value; + value = njs_object_value(value); njs_string_get(value, &str); @@ -2155,7 +2155,7 @@ njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value, break; case NJS_OBJECT_NUMBER: - value = &value->data.u.object_value->value; + value = njs_object_value(value); if (nxt_slow_path(njs_number(value) == 0.0 && signbit(njs_number(value)))) @@ -2178,7 +2178,7 @@ njs_dump_value(njs_json_stringify_t *stringify, const njs_value_t *value, break; case NJS_OBJECT_BOOLEAN: - value = &value->data.u.object_value->value; + value = njs_object_value(value); if (njs_is_true(value)) { njs_dump("[Boolean: true]"); @@ -2457,7 +2457,7 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_t *retval, const njs_value_t *value, val = &ext_val; } else { - object = state->value.data.u.object; + object = njs_object(&state->value); lhq.proto = &njs_object_hash_proto; ret = nxt_lvlhsh_find(&object->hash, &lhq); diff --git a/njs/njs_number.c b/njs/njs_number.c index d3f2abc5..2d298519 100644 --- a/njs/njs_number.c +++ b/njs/njs_number.c @@ -258,9 +258,7 @@ njs_number_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_ERROR; } - vm->retval.data.u.object = object; - vm->retval.type = NJS_OBJECT_NUMBER; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, object, NJS_OBJECT_NUMBER); } else { njs_set_number(&vm->retval, njs_number(value)); @@ -490,7 +488,7 @@ njs_number_prototype_value_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, if (value->type != NJS_NUMBER) { if (value->type == NJS_OBJECT_NUMBER) { - value = &value->data.u.object_value->value; + value = njs_object_value(value); } else { njs_type_error(vm, "unexpected value type:%s", @@ -517,7 +515,7 @@ njs_number_prototype_to_string(njs_vm_t *vm, njs_value_t *args, if (value->type != NJS_NUMBER) { if (value->type == NJS_OBJECT_NUMBER) { - value = &value->data.u.object_value->value; + value = njs_object_value(value); } else { njs_type_error(vm, "unexpected value type:%s", diff --git a/njs/njs_object.c b/njs/njs_object.c index a5fd7f48..eafc52cf 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -57,7 +57,7 @@ njs_object_value_copy(njs_vm_t *vm, njs_value_t *value) { njs_object_t *object; - object = value->data.u.object; + object = njs_object(value); if (!object->shared) { return object; @@ -66,7 +66,7 @@ njs_object_value_copy(njs_vm_t *vm, njs_value_t *value) object = nxt_mp_alloc(vm->mem_pool, sizeof(njs_object_t)); if (nxt_fast_path(object != NULL)) { - *object = *value->data.u.object; + *object = *njs_object(value); object->__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object; object->shared = 0; value->data.u.object = object; @@ -212,7 +212,7 @@ njs_object_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } else { if (njs_is_object(value)) { - object = value->data.u.object; + object = njs_object(value); } else if (njs_is_primitive(value)) { @@ -232,9 +232,7 @@ njs_object_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } } - vm->retval.data.u.object = object; - vm->retval.type = type; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, object, type); return NXT_OK; } @@ -260,7 +258,7 @@ njs_object_create(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, if (!njs_is_null(value)) { /* GC */ - object->__proto__ = value->data.u.object; + object->__proto__ = njs_object(value); } else { object->__proto__ = NULL; @@ -1105,7 +1103,7 @@ njs_object_define_property(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, value = njs_argument(args, 1); - if (!value->data.u.object->extensible) { + if (!njs_object(value)->extensible) { njs_type_error(vm, "object is not extensible"); return NXT_ERROR; } @@ -1149,7 +1147,7 @@ njs_object_define_properties(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, value = njs_argument(args, 1); - if (!value->data.u.object->extensible) { + if (!njs_object(value)->extensible) { njs_type_error(vm, "object is not extensible"); return NXT_ERROR; } @@ -1339,7 +1337,7 @@ njs_object_freeze(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_OK; } - object = value->data.u.object; + object = njs_object(value); object->extensible = 0; nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); @@ -1382,7 +1380,7 @@ njs_object_is_frozen(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, retval = &njs_value_false; - object = value->data.u.object; + object = njs_object(value); nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); hash = &object->hash; @@ -1434,7 +1432,7 @@ njs_object_seal(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_OK; } - object = value->data.u.object; + object = njs_object(value); object->extensible = 0; nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); @@ -1476,7 +1474,7 @@ njs_object_is_sealed(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, retval = &njs_value_false; - object = value->data.u.object; + object = njs_object(value); nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto); hash = &object->hash; @@ -1520,7 +1518,7 @@ njs_object_prevent_extensions(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_OK; } - args[1].data.u.object->extensible = 0; + njs_object(&args[1])->extensible = 0; vm->retval = *value; @@ -1541,8 +1539,8 @@ njs_object_is_extensible(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_OK; } - retval = value->data.u.object->extensible ? &njs_value_true - : &njs_value_false; + retval = njs_object(value)->extensible ? &njs_value_true + : &njs_value_false; vm->retval = *retval; @@ -1568,16 +1566,14 @@ njs_primitive_prototype_get_proto(njs_vm_t *vm, njs_value_t *value, * and have to return different results for primitive type and for objects. */ if (njs_is_object(value)) { - proto = value->data.u.object->__proto__; + proto = njs_object(value)->__proto__; } else { index = njs_primitive_prototype_index(value->type); proto = &vm->prototypes[index].object; } - retval->data.u.object = proto; - retval->type = proto->type; - retval->data.truth = 1; + njs_set_type_object(retval, proto, proto->type); return NXT_OK; } @@ -1633,9 +1629,7 @@ njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash, /* GC */ - prop->value.data.u.object = prototype; - prop->value.type = prototype->type; - prop->value.data.truth = 1; + njs_set_type_object(&prop->value, prototype, prototype->type); lhq.value = prop; lhq.key_hash = NJS_PROTOTYPE_HASH; @@ -1861,7 +1855,7 @@ njs_object_set_prototype_of(njs_vm_t *vm, njs_object_t *object, { const njs_object_t *proto; - proto = njs_is_object(value) ? value->data.u.object->__proto__ + proto = njs_is_object(value) ? njs_object(value)->__proto__ : NULL; if (nxt_slow_path(object->__proto__ == proto)) { @@ -1882,7 +1876,7 @@ njs_object_set_prototype_of(njs_vm_t *vm, njs_object_t *object, } while (proto != NULL); - object->__proto__ = value->data.u.object; + object->__proto__ = njs_object(value); return 1; } @@ -1900,7 +1894,7 @@ njs_object_prototype_proto(njs_vm_t *vm, njs_value_t *value, return NJS_OK; } - object = value->data.u.object; + object = njs_object(value); if (setval != NULL) { if (njs_is_object(setval) || njs_is_null(setval)) { @@ -1919,9 +1913,7 @@ njs_object_prototype_proto(njs_vm_t *vm, njs_value_t *value, proto = object->__proto__; if (nxt_fast_path(proto != NULL)) { - retval->data.u.object = proto; - retval->type = proto->type; - retval->data.truth = 1; + njs_set_type_object(retval, proto, proto->type); } else { *retval = njs_value_null; @@ -1947,7 +1939,7 @@ njs_object_prototype_create_constructor(njs_vm_t *vm, njs_value_t *value, njs_object_prototype_t *prototype; if (njs_is_object(value)) { - object = value->data.u.object; + object = njs_object(value); do { prototype = (njs_object_prototype_t *) object; @@ -2217,8 +2209,8 @@ njs_object_prototype_is_prototype_of(njs_vm_t *vm, njs_value_t *args, value = njs_arg(args, nargs, 1); if (njs_is_object(prototype) && njs_is_object(value)) { - proto = prototype->data.u.object; - object = value->data.u.object; + proto = njs_object(prototype); + object = njs_object(value); do { object = object->__proto__; diff --git a/njs/njs_object_property.c b/njs/njs_object_property.c index 1fd1b16b..79f009ae 100644 --- a/njs/njs_object_property.c +++ b/njs/njs_object_property.c @@ -97,7 +97,7 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *object, case NJS_OBJECT_TYPE_ERROR: case NJS_OBJECT_URI_ERROR: case NJS_OBJECT_VALUE: - obj = object->data.u.object; + obj = njs_object(object); break; case NJS_FUNCTION: @@ -678,7 +678,7 @@ njs_value_property_set(njs_vm_t *vm, njs_value_t *object, return ret; } - if (nxt_slow_path(!object->data.u.object->extensible)) { + if (nxt_slow_path(!njs_object(object)->extensible)) { njs_type_error(vm, "Cannot add property \"%V\", " "object is not extensible", &pq.lhq.key); return NXT_ERROR; @@ -799,7 +799,7 @@ njs_object_prop_define(njs_vm_t *vm, njs_value_t *object, return ret; } - prop = njs_descriptor_prop(vm, name, value->data.u.object); + prop = njs_descriptor_prop(vm, name, njs_object(value)); if (nxt_slow_path(prop == NULL)) { return NXT_ERROR; } diff --git a/njs/njs_parser_terminal.c b/njs/njs_parser_terminal.c index b1c89443..6d9a47d4 100644 --- a/njs/njs_parser_terminal.c +++ b/njs/njs_parser_terminal.c @@ -447,7 +447,7 @@ njs_parser_builtin(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node, switch (type) { case NJS_OBJECT: index = node->token - NJS_TOKEN_FIRST_OBJECT; - var->value.data.u.object = &vm->shared->objects[index]; + njs_set_object(&var->value, &vm->shared->objects[index]); break; case NJS_FUNCTION: diff --git a/njs/njs_string.c b/njs/njs_string.c index 120f5838..2ce4c586 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -562,9 +562,7 @@ njs_string_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, return NXT_ERROR; } - vm->retval.data.u.object = object; - vm->retval.type = NJS_OBJECT_STRING; - vm->retval.data.truth = 1; + njs_set_type_object(&vm->retval, object, NJS_OBJECT_STRING); } else { vm->retval = *value; @@ -652,7 +650,7 @@ njs_string_instance_length(njs_vm_t *vm, njs_value_t *value, length = 0; if (nxt_slow_path(njs_is_object(value))) { - proto = value->data.u.object; + proto = njs_object(value); do { if (nxt_fast_path(proto->type == NJS_OBJECT_STRING)) { @@ -769,7 +767,7 @@ njs_string_prototype_value_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, if (value->type != NJS_STRING) { if (value->type == NJS_OBJECT_STRING) { - value = &value->data.u.object_value->value; + value = njs_object_value(value); } else { njs_type_error(vm, "unexpected value type:%s", diff --git a/njs/njs_value.c b/njs/njs_value.c index 5e7d77b3..fb3e10c8 100644 --- a/njs/njs_value.c +++ b/njs/njs_value.c @@ -158,7 +158,7 @@ njs_values_strict_equal(const njs_value_t *val1, const njs_value_t *val2) return (memcmp(start1, start2, size) == 0); } - return (val1->data.u.object == val2->data.u.object); + return (njs_object(val1) == njs_object(val2)); } @@ -201,7 +201,7 @@ njs_value_to_primitive(njs_vm_t *vm, njs_value_t *value, nxt_uint_t hint) lhq.key_hash = hashes[hint]; lhq.key = names[hint]; - prop = njs_object_property(vm, value->data.u.object, &lhq); + prop = njs_object_property(vm, njs_object(value), &lhq); if (nxt_fast_path(prop != NULL)) { @@ -267,7 +267,7 @@ njs_value_enumerate(njs_vm_t *vm, const njs_value_t *value, njs_object_value_t obj_val; if (njs_is_object(value)) { - return njs_object_enumerate(vm, value->data.u.object, kind, all); + return njs_object_enumerate(vm, njs_object(value), kind, all); } if (value->type != NJS_STRING) { @@ -288,7 +288,7 @@ njs_value_own_enumerate(njs_vm_t *vm, const njs_value_t *value, njs_object_value_t obj_val; if (njs_is_object(value)) { - return njs_object_own_enumerate(vm, value->data.u.object, kind, all); + return njs_object_own_enumerate(vm, njs_object(value), kind, all); } if (value->type != NJS_STRING) { diff --git a/njs/njs_value.h b/njs/njs_value.h index 0ac4e574..564d2d9f 100644 --- a/njs/njs_value.h +++ b/njs/njs_value.h @@ -540,6 +540,10 @@ typedef enum { ((value)->data.u.date) +#define njs_object_value(_value) \ + (&(_value)->data.u.object_value->value) + + #define njs_set_undefined(value) \ *(value) = njs_value_undefined @@ -583,6 +587,16 @@ njs_set_object(njs_value_t *value, njs_object_t *object) } +nxt_inline void +njs_set_type_object(njs_value_t *value, njs_object_t *object, + nxt_uint_t type) +{ + value->data.u.object = object; + value->type = type; + value->data.truth = 1; +} + + nxt_inline void njs_set_array(njs_value_t *value, njs_array_t *array) { @@ -601,6 +615,15 @@ njs_set_date(njs_value_t *value, njs_date_t *date) } +nxt_inline void +njs_set_object_value(njs_value_t *value, njs_object_value_t *object_value) +{ + value->data.u.object_value = object_value; + value->type = NJS_OBJECT_VALUE; + value->data.truth = 1; +} + + #define njs_set_invalid(value) \ (value)->type = NJS_INVALID diff --git a/njs/njs_vm.c b/njs/njs_vm.c index 19297a98..8a372290 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -523,7 +523,7 @@ njs_vmcode_property_init(njs_vm_t *vm, njs_value_t *object, lhq.proto = &njs_object_hash_proto; lhq.pool = vm->mem_pool; - obj = object->data.u.object; + obj = njs_object(object); ret = nxt_lvlhsh_find(&obj->__proto__->shared_hash, &lhq); if (ret == NXT_OK) { @@ -865,8 +865,8 @@ njs_vmcode_instance_of(njs_vm_t *vm, njs_value_t *object, return NXT_ERROR; } - prototype = value.data.u.object; - proto = object->data.u.object; + prototype = njs_object(&value); + proto = njs_object(object); do { proto = proto->__proto__; @@ -1494,7 +1494,7 @@ njs_values_equal(njs_vm_t *vm, const njs_value_t *val1, const njs_value_t *val2) return njs_string_eq(val1, val2); } - return (val1->data.u.object == val2->data.u.object); + return (njs_object(val1) == njs_object(val2)); } /* Sort values as: numeric < string < objects. */ @@ -1805,7 +1805,7 @@ njs_function_new_object(njs_vm_t *vm, njs_value_t *value) } if (nxt_fast_path(proto != NULL)) { - object->__proto__ = proto->data.u.object; + object->__proto__ = njs_object(proto); return object; } } @@ -2685,7 +2685,7 @@ njs_vm_value_to_string(njs_vm_t *vm, nxt_str_t *dst, const njs_value_t *src) if (nxt_slow_path(src->type == NJS_OBJECT_INTERNAL_ERROR)) { /* MemoryError is a nonextensible internal error. */ - if (!src->data.u.object->extensible) { + if (!njs_object(src)->extensible) { njs_string_get(&njs_string_memory_error, dst); return NXT_OK; } -- 2.47.3