[njs] Using njs_object() macro where applicable.
Dmitry Volyntsev
xeioex at nginx.com
Mon Jul 8 14:52:34 UTC 2019
details: https://hg.nginx.org/njs/rev/5eee6645c7e2
branches:
changeset: 1034:5eee6645c7e2
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon Jul 08 17:49:43 2019 +0300
description:
Using njs_object() macro where applicable.
diffstat:
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(-)
diffs (914 lines):
diff -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_array.c
--- a/njs/njs_array.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_array.c Mon Jul 08 17:49:43 2019 +0300
@@ -443,7 +443,7 @@ njs_array_length(njs_vm_t *vm, njs_value
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
} 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 *
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(nj
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_boolean.c
--- a/njs/njs_boolean.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_boolean.c Mon Jul 08 17:49:43 2019 +0300
@@ -27,9 +27,7 @@ njs_boolean_constructor(njs_vm_t *vm, nj
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
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
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_builtin.c
--- a/njs/njs_builtin.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_builtin.c Mon Jul 08 17:49:43 2019 +0300
@@ -854,7 +854,7 @@ njs_vm_expression_completions(njs_vm_t *
value = &prop->value;
}
- return njs_object_completions(vm, value->data.u.object);
+ return njs_object_completions(vm, njs_object(value));
}
diff -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_crypto.c
--- a/njs/njs_crypto.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_crypto.c Mon Jul 08 17:49:43 2019 +0300
@@ -187,10 +187,7 @@ njs_crypto_create_hash(njs_vm_t *vm, njs
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,
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,
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,
}
}
- 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
}
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,
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,
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,
}
}
- 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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_error.c
--- a/njs/njs_error.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_error.c Mon Jul 08 17:49:43 2019 +0300
@@ -29,9 +29,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t
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
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_v
*/
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_va
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_va
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(n
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_fs.c
--- a/njs/njs_fs.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_fs.c Mon Jul 08 17:49:43 2019 +0300
@@ -979,9 +979,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *
}
}
- 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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_function.c
--- a/njs/njs_function.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_function.c Mon Jul 08 17:49:43 2019 +0300
@@ -910,7 +910,7 @@ njs_function_instance_length(njs_vm_t *v
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_json.c
--- a/njs/njs_json.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_json.c Mon Jul 08 17:49:43 2019 +0300
@@ -1487,7 +1487,7 @@ njs_object_to_json_function(njs_vm_t *vm
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, n
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, n
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
{
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
}
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 *str
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 *str
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 *str
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_
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_number.c
--- a/njs/njs_number.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_number.c Mon Jul 08 17:49:43 2019 +0300
@@ -258,9 +258,7 @@ njs_number_constructor(njs_vm_t *vm, njs
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 *
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
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_object.c
--- a/njs/njs_object.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_object.c Mon Jul 08 17:49:43 2019 +0300
@@ -57,7 +57,7 @@ njs_object_value_copy(njs_vm_t *vm, njs_
{
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_
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
} 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
}
}
- 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_valu
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,
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 *v
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_valu
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_v
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_
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_v
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 *
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, n
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
* 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 *
/* 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
{
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
} 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,
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,
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_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
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_object_property.c
--- a/njs/njs_object_property.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_object_property.c Mon Jul 08 17:49:43 2019 +0300
@@ -97,7 +97,7 @@ njs_property_query(njs_vm_t *vm, njs_pro
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
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
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_parser_terminal.c Mon Jul 08 17:49:43 2019 +0300
@@ -447,7 +447,7 @@ njs_parser_builtin(njs_vm_t *vm, njs_par
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_string.c
--- a/njs/njs_string.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_string.c Mon Jul 08 17:49:43 2019 +0300
@@ -562,9 +562,7 @@ njs_string_constructor(njs_vm_t *vm, njs
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,
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 *
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_value.c
--- a/njs/njs_value.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_value.c Mon Jul 08 17:49:43 2019 +0300
@@ -158,7 +158,7 @@ njs_values_strict_equal(const njs_value_
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
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_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, co
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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_value.h
--- a/njs/njs_value.h Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_value.h Mon Jul 08 17:49:43 2019 +0300
@@ -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
@@ -584,6 +588,16 @@ njs_set_object(njs_value_t *value, njs_o
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)
{
value->data.u.array = array;
@@ -601,6 +615,15 @@ njs_set_date(njs_value_t *value, njs_dat
}
+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 -r 254c9b04b0d2 -r 5eee6645c7e2 njs/njs_vm.c
--- a/njs/njs_vm.c Mon Jul 08 17:49:14 2019 +0300
+++ b/njs/njs_vm.c Mon Jul 08 17:49:43 2019 +0300
@@ -523,7 +523,7 @@ njs_vmcode_property_init(njs_vm_t *vm, n
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
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
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, nj
}
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
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;
}
More information about the nginx-devel
mailing list