njs_array_t *array;
njs_object_t *proto;
- proto = value->data.u.object;
+ proto = njs_object(value);
if (setval == NULL) {
do {
} 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;
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,
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;
}
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;
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",
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",
value = &prop->value;
}
- return njs_object_completions(vm, value->data.u.object);
+ return njs_object_completions(vm, njs_object(value));
}
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;
}
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");
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");
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");
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;
-
- 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");
}
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;
}
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");
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");
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");
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;
-
- 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");
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);
}
}
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;
}
*/
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);
}
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;
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;
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;
}
}
- 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;
}
{
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:
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)) {
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;
switch (value->type) {
case NJS_OBJECT_NUMBER:
- value = &value->data.u.object_value->value;
+ value = njs_object_value(value);
/* Fall through. */
case NJS_NUMBER:
break;
case NJS_OBJECT_STRING:
- value = &value->data.u.object_value->value;
+ value = njs_object_value(value);
break;
case NJS_STRING:
{
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:
}
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;
}
switch (value->type) {
case NJS_OBJECT_STRING:
- value = &value->data.u.object_value->value;
+ value = njs_object_value(value);
njs_string_get(value, &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))))
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]");
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);
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));
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",
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",
{
njs_object_t *object;
- object = value->data.u.object;
+ object = njs_object(value);
if (!object->shared) {
return object;
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;
} else {
if (njs_is_object(value)) {
- object = value->data.u.object;
+ object = njs_object(value);
} else if (njs_is_primitive(value)) {
}
}
- 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;
}
if (!njs_is_null(value)) {
/* GC */
- object->__proto__ = value->data.u.object;
+ object->__proto__ = njs_object(value);
} else {
object->__proto__ = NULL;
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;
}
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;
}
return NXT_OK;
}
- object = value->data.u.object;
+ object = njs_object(value);
object->extensible = 0;
nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
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;
return NXT_OK;
}
- object = value->data.u.object;
+ object = njs_object(value);
object->extensible = 0;
nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
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;
return NXT_OK;
}
- args[1].data.u.object->extensible = 0;
+ njs_object(&args[1])->extensible = 0;
vm->retval = *value;
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;
* 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;
}
/* 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;
{
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)) {
} while (proto != NULL);
- object->__proto__ = value->data.u.object;
+ object->__proto__ = njs_object(value);
return 1;
}
return NJS_OK;
}
- object = value->data.u.object;
+ object = njs_object(value);
if (setval != NULL) {
if (njs_is_object(setval) || njs_is_null(setval)) {
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;
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;
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__;
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:
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;
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;
}
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:
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;
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)) {
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",
return (memcmp(start1, start2, size) == 0);
}
- return (val1->data.u.object == val2->data.u.object);
+ return (njs_object(val1) == njs_object(val2));
}
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)) {
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) {
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) {
((value)->data.u.date)
+#define njs_object_value(_value) \
+ (&(_value)->data.u.object_value->value)
+
+
#define njs_set_undefined(value) \
*(value) = njs_value_undefined
}
+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)
{
}
+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
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) {
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__;
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. */
}
if (nxt_fast_path(proto != NULL)) {
- object->__proto__ = proto->data.u.object;
+ object->__proto__ = njs_object(proto);
return object;
}
}
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;
}