[njs] Getting rid of special types for primitive objects.
Dmitry Volyntsev
xeioex at nginx.com
Tue Nov 2 12:40:55 UTC 2021
details: https://hg.nginx.org/njs/rev/adb4ac09b86d
branches:
changeset: 1735:adb4ac09b86d
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Nov 02 12:37:00 2021 +0000
description:
Getting rid of special types for primitive objects.
The following types were removed: NJS_OBJECT_BOOLEAN,
NJS_OBJECT_NUMBER, NJS_OBJECT_SYMBOL, NJS_OBJECT_STRING.
Instead a generic NJS_OBJECT_VALUE type is used for objects
with custom slots.
diffstat:
src/njs_array.c | 6 +-
src/njs_boolean.c | 14 +-
src/njs_builtin.c | 2 +-
src/njs_iterator.c | 40 ++--
src/njs_json.c | 319 ++++++++++++++++++++++++--------------------
src/njs_number.c | 22 +-
src/njs_object.c | 247 +++++++++++++++++----------------
src/njs_object.h | 6 +-
src/njs_string.c | 35 ++--
src/njs_symbol.c | 2 +-
src/njs_value.c | 33 +--
src/njs_value.h | 46 +++--
src/njs_value_conversion.h | 4 +-
src/njs_vmcode.c | 4 -
src/test/njs_unit_test.c | 7 +-
15 files changed, 411 insertions(+), 376 deletions(-)
diffs (truncated from 1356 to 1000 lines):
diff -r 685adce92af1 -r adb4ac09b86d src/njs_array.c
--- a/src/njs_array.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_array.c Tue Nov 02 12:37:00 2021 +0000
@@ -775,9 +775,9 @@ njs_array_prototype_slice_copy(njs_vm_t
length--;
} while (length != 0);
- } else if (njs_is_string(this) || this->type == NJS_OBJECT_STRING) {
-
- if (this->type == NJS_OBJECT_STRING) {
+ } else if (njs_is_string(this) || njs_is_object_string(this)) {
+
+ if (njs_is_object_string(this)) {
this = njs_object_value(this);
}
diff -r 685adce92af1 -r adb4ac09b86d src/njs_boolean.c
--- a/src/njs_boolean.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_boolean.c Tue Nov 02 12:37:00 2021 +0000
@@ -12,8 +12,8 @@ static njs_int_t
njs_boolean_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- njs_object_t *object;
- const njs_value_t *value;
+ const njs_value_t *value;
+ njs_object_value_t *object;
if (nargs == 1) {
value = &njs_value_false;
@@ -23,12 +23,12 @@ njs_boolean_constructor(njs_vm_t *vm, nj
}
if (vm->top_frame->ctor) {
- object = njs_object_value_alloc(vm, value, value->type);
+ object = njs_object_value_alloc(vm, NJS_OBJ_TYPE_BOOLEAN, 0, value);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
- njs_set_type_object(&vm->retval, object, NJS_OBJECT_BOOLEAN);
+ njs_set_object_value(&vm->retval, object);
} else {
vm->retval = *value;
@@ -78,7 +78,7 @@ njs_boolean_prototype_value_of(njs_vm_t
if (value->type != NJS_BOOLEAN) {
- if (value->type == NJS_OBJECT_BOOLEAN) {
+ if (njs_is_object_boolean(value)) {
value = njs_object_value(value);
} else {
@@ -104,7 +104,7 @@ njs_boolean_prototype_to_string(njs_vm_t
if (value->type != NJS_BOOLEAN) {
- if (value->type == NJS_OBJECT_BOOLEAN) {
+ if (njs_is_object_boolean(value)) {
value = njs_object_value(value);
} else {
@@ -168,6 +168,6 @@ const njs_object_type_init_t njs_boolea
.prototype_props = &njs_boolean_prototype_init,
.prototype_value = { .object_value = {
.value = njs_value(NJS_BOOLEAN, 0, 0.0),
- .object = { .type = NJS_OBJECT_BOOLEAN } }
+ .object = { .type = NJS_OBJECT_VALUE } }
},
};
diff -r 685adce92af1 -r adb4ac09b86d src/njs_builtin.c
--- a/src/njs_builtin.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_builtin.c Tue Nov 02 12:37:00 2021 +0000
@@ -333,7 +333,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
string_object = &shared->string_object;
njs_lvlhsh_init(&string_object->hash);
string_object->shared_hash = shared->string_instance_hash;
- string_object->type = NJS_OBJECT_STRING;
+ string_object->type = NJS_OBJECT_VALUE;
string_object->shared = 1;
string_object->extensible = 0;
diff -r 685adce92af1 -r adb4ac09b86d src/njs_iterator.c
--- a/src/njs_iterator.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_iterator.c Tue Nov 02 12:37:00 2021 +0000
@@ -311,14 +311,14 @@ njs_int_t
njs_object_iterate(njs_vm_t *vm, njs_iterator_args_t *args,
njs_iterator_handler_t handler)
{
- double idx;
- int64_t length, i, from, to;
- njs_int_t ret;
- njs_array_t *array, *keys;
- njs_value_t *value, *entry, prop, character, string_obj;
- njs_object_t *object;
- const u_char *p, *end, *pos;
- njs_string_prop_t string_prop;
+ double idx;
+ int64_t length, i, from, to;
+ njs_int_t ret;
+ njs_array_t *array, *keys;
+ njs_value_t *value, *entry, prop, character, string_obj;
+ const u_char *p, *end, *pos;
+ njs_string_prop_t string_prop;
+ njs_object_value_t *object;
value = args->value;
from = args->from;
@@ -366,12 +366,12 @@ njs_object_iterate(njs_vm_t *vm, njs_ite
if (njs_is_string(value) || njs_is_object_string(value)) {
if (njs_is_string(value)) {
- object = njs_object_value_alloc(vm, value, NJS_STRING);
+ object = njs_object_value_alloc(vm, NJS_OBJ_TYPE_STRING, 0, value);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
- njs_set_type_object(&string_obj, object, NJS_OBJECT_STRING);
+ njs_set_object_value(&string_obj, object);
args->value = &string_obj;
}
@@ -473,14 +473,14 @@ njs_int_t
njs_object_iterate_reverse(njs_vm_t *vm, njs_iterator_args_t *args,
njs_iterator_handler_t handler)
{
- double idx;
- int64_t i, from, to, length;
- njs_int_t ret;
- njs_array_t *array, *keys;
- njs_value_t *entry, *value, prop, character, string_obj;
- njs_object_t *object;
- const u_char *p, *end, *pos;
- njs_string_prop_t string_prop;
+ double idx;
+ int64_t i, from, to, length;
+ njs_int_t ret;
+ njs_array_t *array, *keys;
+ njs_value_t *entry, *value, prop, character, string_obj;
+ const u_char *p, *end, *pos;
+ njs_string_prop_t string_prop;
+ njs_object_value_t *object;
value = args->value;
from = args->from;
@@ -530,12 +530,12 @@ njs_object_iterate_reverse(njs_vm_t *vm,
if (njs_is_string(value) || njs_is_object_string(value)) {
if (njs_is_string(value)) {
- object = njs_object_value_alloc(vm, value, NJS_STRING);
+ object = njs_object_value_alloc(vm, NJS_OBJ_TYPE_STRING, 0, value);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
- njs_set_type_object(&string_obj, object, NJS_OBJECT_STRING);
+ njs_set_object_value(&string_obj, object);
args->value = &string_obj;
}
diff -r 685adce92af1 -r adb4ac09b86d src/njs_json.c
--- a/src/njs_json.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_json.c Tue Nov 02 12:37:00 2021 +0000
@@ -220,15 +220,22 @@ njs_json_stringify(njs_vm_t *vm, njs_val
space = njs_arg(args, nargs, 3);
+ if (njs_is_object(space)) {
+ if (njs_is_object_number(space)) {
+ ret = njs_value_to_numeric(vm, space, space);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
+
+ } else if (njs_is_object_string(space)) {
+ ret = njs_value_to_string(vm, space, space);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
+ }
+ }
+
switch (space->type) {
- case NJS_OBJECT_STRING:
- ret = njs_value_to_string(vm, space, space);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
- }
-
- /* Fall through. */
-
case NJS_STRING:
length = njs_string_prop(&prop, space);
@@ -250,14 +257,6 @@ njs_json_stringify(njs_vm_t *vm, njs_val
break;
- case NJS_OBJECT_NUMBER:
- ret = njs_value_to_numeric(vm, space, space);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
- }
-
- /* Fall through. */
-
case NJS_NUMBER:
i64 = njs_min(njs_number_to_integer(njs_number(space)), 10);
@@ -1138,10 +1137,27 @@ njs_json_pop_stringify_state(njs_json_st
njs_inline njs_bool_t
njs_json_is_object(const njs_value_t *value)
{
- return (((value)->type == NJS_OBJECT)
- || ((value)->type == NJS_ARRAY)
- || ((value)->type == NJS_OBJECT_SYMBOL)
- || ((value)->type >= NJS_REGEXP));
+ if (!njs_is_object(value)) {
+ return 0;
+ }
+
+ if (njs_is_function(value)) {
+ return 0;
+ }
+
+ if (njs_is_object_value(value)) {
+ switch (njs_object_value(value)->type) {
+ case NJS_BOOLEAN:
+ case NJS_NUMBER:
+ case NJS_STRING:
+ return 0;
+
+ default:
+ break;
+ }
+ }
+
+ return 1;
}
@@ -1487,74 +1503,82 @@ static njs_int_t
njs_json_stringify_array(njs_vm_t *vm, njs_json_stringify_t *stringify)
{
njs_int_t ret;
- uint32_t i, n, k, properties_length, array_length;
- njs_value_t *value, num_value;
- njs_array_t *properties, *array;
-
- properties_length = 1;
- array = njs_array(&stringify->replacer);
- array_length = array->length;
-
- for (i = 0; i < array_length; i++) {
- if (njs_is_valid(&array->start[i])) {
- properties_length++;
- }
+ int64_t i, k, length;
+ njs_value_t *value, *item;
+ njs_array_t *properties;
+
+ ret = njs_object_length(vm, &stringify->replacer, &length);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
}
- properties = njs_array_alloc(vm, 1, properties_length, NJS_ARRAY_SPARE);
+ properties = njs_array_alloc(vm, 1, 0, NJS_ARRAY_SPARE);
if (njs_slow_path(properties == NULL)) {
return NJS_ERROR;
}
- n = 0;
- properties->start[n++] = njs_string_empty;
-
- for (i = 0; i < array_length; i++) {
- value = &array->start[i];
-
- if (!njs_is_valid(&array->start[i])) {
- continue;
+ item = njs_array_push(vm, properties);
+ njs_value_assign(item, &njs_string_empty);
+
+ for (i = 0; i < length; i++) {
+ ret = njs_value_property_i64(vm, &stringify->replacer, i,
+ &stringify->retval);
+ if (njs_slow_path(ret == NJS_ERROR)) {
+ return ret;
}
+ value = &stringify->retval;
+
switch (value->type) {
+ case NJS_STRING:
+ break;
+
case NJS_NUMBER:
- ret = njs_number_to_string(vm, &num_value, value);
+ ret = njs_number_to_string(vm, value, value);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
- value = &num_value;
break;
- case NJS_OBJECT_NUMBER:
- case NJS_OBJECT_STRING:
- ret = njs_value_to_string(vm, value, value);
- if (njs_slow_path(ret != NJS_OK)) {
- return NJS_ERROR;
+ case NJS_OBJECT_VALUE:
+ switch (njs_object_value(value)->type) {
+ case NJS_NUMBER:
+ case NJS_STRING:
+ ret = njs_value_to_string(vm, value, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NJS_ERROR;
+ }
+
+ break;
+
+ default:
+ continue;
}
- /* Fall through. */
-
- case NJS_STRING:
break;
default:
continue;
}
- for (k = 0; k < n; k ++) {
+ for (k = 0; k < properties->length; k++) {
if (njs_values_strict_equal(value, &properties->start[k]) == 1) {
break;
}
}
- if (k == n) {
- properties->start[n++] = *value;
+ if (k == properties->length) {
+ item = njs_array_push(vm, properties);
+ if (njs_slow_path(item == NULL)) {
+ return NJS_ERROR;
+ }
+
+ njs_value_assign(item, value);
}
}
- properties->length = n;
- stringify->replacer.data.u.array = properties;
+ njs_set_array(&stringify->replacer, properties);
return NJS_OK;
}
@@ -1565,35 +1589,42 @@ njs_json_append_value(njs_vm_t *vm, njs_
{
njs_int_t ret;
+ if (njs_is_object_value(value)) {
+ switch (njs_object_value(value)->type) {
+ case NJS_NUMBER:
+ ret = njs_value_to_numeric(vm, value, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
+
+ break;
+
+ case NJS_BOOLEAN:
+ njs_value_assign(value, njs_object_value(value));
+ break;
+
+ case NJS_STRING:
+ ret = njs_value_to_string(vm, value, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
+
+ break;
+
+ default:
+ break;
+ }
+ }
+
switch (value->type) {
- case NJS_OBJECT_STRING:
- ret = njs_value_to_string(vm, value, value);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
- }
-
- /* Fall through. */
-
case NJS_STRING:
njs_json_append_string(chain, value, '\"');
break;
- case NJS_OBJECT_NUMBER:
- ret = njs_value_to_numeric(vm, value, value);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
- }
-
- /* Fall through. */
-
case NJS_NUMBER:
njs_json_append_number(chain, value);
break;
- case NJS_OBJECT_BOOLEAN:
- value = njs_object_value(value);
- /* Fall through. */
-
case NJS_BOOLEAN:
if (njs_is_true(value)) {
njs_chb_append_literal(chain, "true");
@@ -1829,12 +1860,22 @@ njs_dump_terminal(njs_json_stringify_t *
njs_int_t (*to_string)(njs_vm_t *, njs_value_t *, const njs_value_t *);
switch (value->type) {
- case NJS_OBJECT_STRING:
- value = njs_object_value(value);
-
- njs_chb_append_literal(chain, "[String: ");
- njs_json_append_string(chain, value, '\'');
- njs_chb_append_literal(chain, "]");
+ case NJS_NULL:
+ njs_chb_append_literal(chain, "null");
+ break;
+
+ case NJS_UNDEFINED:
+ njs_chb_append_literal(chain, "undefined");
+ break;
+
+ case NJS_BOOLEAN:
+ if (njs_is_true(value)) {
+ njs_chb_append_literal(chain, "true");
+
+ } else {
+ njs_chb_append_literal(chain, "false");
+ }
+
break;
case NJS_STRING:
@@ -1849,19 +1890,6 @@ njs_dump_terminal(njs_json_stringify_t *
break;
- case NJS_OBJECT_SYMBOL:
- value = njs_object_value(value);
-
- ret = njs_symbol_descriptive_string(stringify->vm, &str_val, value);
- if (njs_slow_path(ret != NJS_OK)) {
- return NJS_ERROR;
- }
-
- njs_string_get(&str_val, &str);
- njs_chb_sprintf(chain, 16 + str.length, "[Symbol: %V]", &str);
-
- break;
-
case NJS_SYMBOL:
ret = njs_symbol_descriptive_string(stringify->vm, &str_val, value);
if (njs_slow_path(ret != NJS_OK)) {
@@ -1873,59 +1901,61 @@ njs_dump_terminal(njs_json_stringify_t *
break;
- case NJS_OBJECT_NUMBER:
+ case NJS_INVALID:
+ njs_chb_append_literal(chain, "<empty>");
+ break;
+
+ case NJS_OBJECT_VALUE:
value = njs_object_value(value);
- if (njs_slow_path(njs_number(value) == 0.0
- && signbit(njs_number(value))))
- {
-
- njs_chb_append_literal(chain, "[Number: -0]");
+ switch (value->type) {
+ case NJS_BOOLEAN:
+ if (njs_is_true(value)) {
+ njs_chb_append_literal(chain, "[Boolean: true]");
+
+ } else {
+ njs_chb_append_literal(chain, "[Boolean: false]");
+ }
+
+ break;
+
+ case NJS_NUMBER:
+ if (njs_slow_path(njs_number(value) == 0.0
+ && signbit(njs_number(value))))
+ {
+
+ njs_chb_append_literal(chain, "[Number: -0]");
+ break;
+ }
+
+ ret = njs_number_to_string(stringify->vm, &str_val, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NJS_ERROR;
+ }
+
+ njs_string_get(&str_val, &str);
+ njs_chb_sprintf(chain, 16 + str.length, "[Number: %V]", &str);
+ break;
+
+ case NJS_SYMBOL:
+ ret = njs_symbol_descriptive_string(stringify->vm, &str_val, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NJS_ERROR;
+ }
+
+ njs_string_get(&str_val, &str);
+ njs_chb_sprintf(chain, 16 + str.length, "[Symbol: %V]", &str);
+
+ break;
+
+ case NJS_STRING:
+ default:
+ njs_chb_append_literal(chain, "[String: ");
+ njs_json_append_string(chain, value, '\'');
+ njs_chb_append_literal(chain, "]");
break;
}
- ret = njs_number_to_string(stringify->vm, &str_val, value);
- if (njs_slow_path(ret != NJS_OK)) {
- return NJS_ERROR;
- }
-
- njs_string_get(&str_val, &str);
- njs_chb_sprintf(chain, 16 + str.length, "[Number: %V]", &str);
-
- break;
-
- case NJS_OBJECT_BOOLEAN:
- value = njs_object_value(value);
-
- if (njs_is_true(value)) {
- njs_chb_append_literal(chain, "[Boolean: true]");
-
- } else {
- njs_chb_append_literal(chain, "[Boolean: false]");
- }
-
- break;
-
- case NJS_BOOLEAN:
- if (njs_is_true(value)) {
- njs_chb_append_literal(chain, "true");
-
- } else {
- njs_chb_append_literal(chain, "false");
- }
-
- break;
-
- case NJS_UNDEFINED:
- njs_chb_append_literal(chain, "undefined");
- break;
-
- case NJS_NULL:
- njs_chb_append_literal(chain, "null");
- break;
-
- case NJS_INVALID:
- njs_chb_append_literal(chain, "<empty>");
break;
case NJS_FUNCTION:
@@ -2031,7 +2061,8 @@ njs_dump_is_recursive(const njs_value_t
{
return (value->type == NJS_OBJECT && !njs_object(value)->error_data)
|| (value->type == NJS_ARRAY)
- || (value->type >= NJS_OBJECT_SPECIAL_MAX);
+ || (value->type >= NJS_OBJECT_SPECIAL_MAX
+ && !njs_is_object_primitive(value));
}
diff -r 685adce92af1 -r adb4ac09b86d src/njs_number.c
--- a/src/njs_number.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_number.c Tue Nov 02 12:37:00 2021 +0000
@@ -311,9 +311,9 @@ static njs_int_t
njs_number_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- njs_int_t ret;
- njs_value_t *value;
- njs_object_t *object;
+ njs_int_t ret;
+ njs_value_t *value;
+ njs_object_value_t *object;
if (nargs == 1) {
value = njs_value_arg(&njs_value_zero);
@@ -330,12 +330,12 @@ njs_number_constructor(njs_vm_t *vm, njs
}
if (vm->top_frame->ctor) {
- object = njs_object_value_alloc(vm, value, NJS_NUMBER);
+ object = njs_object_value_alloc(vm, NJS_OBJ_TYPE_NUMBER, 0, value);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
- njs_set_type_object(&vm->retval, object, NJS_OBJECT_NUMBER);
+ njs_set_object_value(&vm->retval, object);
} else {
njs_set_number(&vm->retval, njs_number(value));
@@ -572,7 +572,7 @@ njs_number_prototype_value_of(njs_vm_t *
if (value->type != NJS_NUMBER) {
- if (value->type == NJS_OBJECT_NUMBER) {
+ if (njs_is_object_number(value)) {
value = njs_object_value(value);
} else {
@@ -601,7 +601,7 @@ njs_number_prototype_to_string(njs_vm_t
if (value->type != NJS_NUMBER) {
- if (value->type == NJS_OBJECT_NUMBER) {
+ if (njs_is_object_number(value)) {
value = njs_object_value(value);
} else {
@@ -650,7 +650,7 @@ njs_number_prototype_to_fixed(njs_vm_t *
value = &args[0];
if (value->type != NJS_NUMBER) {
- if (value->type == NJS_OBJECT_NUMBER) {
+ if (njs_is_object_number(value)) {
value = njs_object_value(value);
} else {
@@ -748,7 +748,7 @@ njs_number_prototype_to_precision(njs_vm
value = &args[0];
if (value->type != NJS_NUMBER) {
- if (value->type == NJS_OBJECT_NUMBER) {
+ if (njs_is_object_number(value)) {
value = njs_object_value(value);
} else {
@@ -798,7 +798,7 @@ njs_number_prototype_to_exponential(njs_
value = &args[0];
if (value->type != NJS_NUMBER) {
- if (value->type == NJS_OBJECT_NUMBER) {
+ if (njs_is_object_number(value)) {
value = njs_object_value(value);
} else {
@@ -1187,6 +1187,6 @@ const njs_object_type_init_t njs_number
.prototype_props = &njs_number_prototype_init,
.prototype_value = { .object_value = {
.value = njs_value(NJS_NUMBER, 0, 0.0),
- .object = { .type = NJS_OBJECT_NUMBER } }
+ .object = { .type = NJS_OBJECT_VALUE } }
},
};
diff -r 685adce92af1 -r adb4ac09b86d src/njs_object.c
--- a/src/njs_object.c Tue Nov 02 12:35:34 2021 +0000
+++ b/src/njs_object.c Tue Nov 02 12:37:00 2021 +0000
@@ -92,42 +92,41 @@ njs_object_value_copy(njs_vm_t *vm, njs_
}
-njs_object_t *
-njs_object_value_alloc(njs_vm_t *vm, const njs_value_t *value, njs_uint_t type)
+njs_object_value_t *
+njs_object_value_alloc(njs_vm_t *vm, njs_uint_t prototype_index, size_t extra,
+ const njs_value_t *value)
{
- njs_uint_t index;
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);
-
- if (type == NJS_STRING) {
- ov->object.shared_hash = vm->shared->string_instance_hash;
-
- } else {
- njs_lvlhsh_init(&ov->object.shared_hash);
- }
-
- ov->object.type = njs_object_value_type(type);
- ov->object.shared = 0;
- ov->object.extensible = 1;
- ov->object.error_data = 0;
- ov->object.fast_array = 0;
-
- index = njs_primitive_prototype_index(type);
- ov->object.__proto__ = &vm->prototypes[index].object;
- ov->object.slots = NULL;
-
+ ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t) + extra);
+ if (njs_slow_path(ov == NULL)) {
+ njs_memory_error(vm);
+ return NULL;
+ }
+
+ njs_lvlhsh_init(&ov->object.hash);
+
+ if (prototype_index == NJS_OBJ_TYPE_STRING) {
+ ov->object.shared_hash = vm->shared->string_instance_hash;
+
+ } else {
+ 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[prototype_index].object;
+ ov->object.slots = NULL;
+
+ if (value != NULL) {
ov->value = *value;
-
- return &ov->object;
}
- njs_memory_error(vm);
-
- return NULL;
+ return ov;
}
@@ -219,46 +218,45 @@ static njs_int_t
njs_object_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- njs_uint_t type;
- njs_value_t *value;
- njs_object_t *object;
+ njs_uint_t type, index;
+ njs_value_t *value;
+ njs_object_t *object;
+ njs_object_value_t *obj_val;
value = njs_arg(args, nargs, 1);
type = value->type;
if (njs_is_null_or_undefined(value)) {
-
object = njs_object_alloc(vm);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
- type = NJS_OBJECT;
-
- } else {
-
- if (njs_is_object(value)) {
- object = njs_object(value);
-
- } else if (njs_is_primitive(value)) {
-
- /* value->type is the same as prototype offset. */
- object = njs_object_value_alloc(vm, value, type);
- if (njs_slow_path(object == NULL)) {
- return NJS_ERROR;
- }
-
- type = njs_object_value_type(type);
-
- } else {
- njs_type_error(vm, "unexpected constructor argument:%s",
- njs_type_string(type));
-
+ njs_set_object(&vm->retval, object);
+
+ return NJS_OK;
+ }
+
+ if (njs_is_primitive(value)) {
+ index = njs_primitive_prototype_index(type);
+ obj_val = njs_object_value_alloc(vm, index, 0, value);
+ if (njs_slow_path(obj_val == NULL)) {
return NJS_ERROR;
}
+
+ njs_set_object_value(&vm->retval, obj_val);
+
+ return NJS_OK;
}
- njs_set_type_object(&vm->retval, object, type);
+ if (njs_slow_path(!njs_is_object(value))) {
+ njs_type_error(vm, "unexpected constructor argument:%s",
+ njs_type_string(type));
+
+ return NJS_ERROR;
+ }
+
+ njs_value_assign(&vm->retval, value);
return NJS_OK;
}
@@ -450,11 +448,16 @@ njs_object_enumerate_value(njs_vm_t *vm,
items, kind);
break;
- case NJS_OBJECT_STRING:
+ case NJS_OBJECT_VALUE:
obj_val = (njs_object_value_t *) object;
- ret = njs_object_enumerate_string(vm, &obj_val->value, items, kind);
- break;
+ if (njs_is_string(&obj_val->value)) {
+ ret = njs_object_enumerate_string(vm, &obj_val->value, items,
+ kind);
+ break;
+ }
+
+ /* Fall through. */
default:
goto object;
@@ -497,11 +500,16 @@ njs_object_own_enumerate_value(njs_vm_t
items, kind);
break;
- case NJS_OBJECT_STRING:
+ case NJS_OBJECT_VALUE:
obj_val = (njs_object_value_t *) object;
- ret = njs_object_enumerate_string(vm, &obj_val->value, items, kind);
- break;
+ if (njs_is_string(&obj_val->value)) {
+ ret = njs_object_enumerate_string(vm, &obj_val->value, items,
+ kind);
+ break;
+ }
+
+ /* Fall through. */
default:
goto object;
@@ -1462,7 +1470,7 @@ static njs_int_t
njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- uint32_t index, type;
+ uint32_t index;
njs_value_t *value;
value = njs_arg(args, nargs, 1);
@@ -1474,10 +1482,14 @@ njs_object_get_prototype_of(njs_vm_t *vm
if (!njs_is_null_or_undefined(value)) {
index = njs_primitive_prototype_index(value->type);
- type = njs_is_symbol(value) ? NJS_OBJECT
- : njs_object_value_type(value->type);
-
- njs_set_type_object(&vm->retval, &vm->prototypes[index].object, type);
+
+ if (njs_is_symbol(value)) {
+ njs_set_object(&vm->retval, &vm->prototypes[index].object);
+
+ } else {
+ njs_set_object_value(&vm->retval,
+ &vm->prototypes[index].object_value);
+ }
return NJS_OK;
}
@@ -2300,14 +2312,8 @@ static const njs_value_t njs_object_boo
njs_long_string("[object Boolean]");
static const njs_value_t njs_object_number_string =
njs_long_string("[object Number]");
-static const njs_value_t njs_object_symbol_string =
- njs_long_string("[object Symbol]");
static const njs_value_t njs_object_string_string =
njs_long_string("[object String]");
-static const njs_value_t njs_object_data_string =
- njs_string("[object Data]");
-static const njs_value_t njs_object_exernal_string =
- njs_long_string("[object External]");
static const njs_value_t njs_object_object_string =
njs_long_string("[object Object]");
static const njs_value_t njs_object_array_string =
@@ -2329,67 +2335,68 @@ njs_object_prototype_to_string(njs_vm_t
{
u_char *p;
njs_int_t ret;
- njs_value_t tag, *value;
+ njs_value_t tag, *this;
njs_string_prop_t string;
const njs_value_t *name;
- static const njs_value_t *class_name[NJS_VALUE_TYPE_MAX] = {
- /* Primitives. */
- &njs_object_null_string,
- &njs_object_undefined_string,
- &njs_object_boolean_string,
- &njs_object_number_string,
- &njs_object_symbol_string,
- &njs_object_string_string,
-
- &njs_object_data_string,
- &njs_object_exernal_string,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
-
- /* Objects. */
- &njs_object_object_string,
- &njs_object_array_string,
- &njs_object_boolean_string,
- &njs_object_number_string,
- &njs_object_symbol_string,
- &njs_object_string_string,
- &njs_object_function_string,
- &njs_object_regexp_string,
- &njs_object_date_string,
- &njs_object_object_string,
- &njs_object_object_string,
- &njs_object_object_string,
- &njs_object_object_string,
- };
-
- value = njs_argument(args, 0);
- name = class_name[value->type];
-
- if (njs_is_null_or_undefined(value)) {
- vm->retval = *name;
+ this = njs_argument(args, 0);
+
+ if (njs_is_null_or_undefined(this)) {
+ vm->retval = njs_is_null(this) ? njs_object_null_string
+ : njs_object_undefined_string;
return NJS_OK;
}
- if (njs_is_error(value)) {
- name = &njs_object_error_string;
+ ret = njs_value_to_object(vm, this);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
}
- if (njs_is_object(value)
- && njs_lvlhsh_eq(&njs_object(value)->shared_hash,
+ name = &njs_object_object_string;
+
+ if (njs_is_array(this)) {
+ name = &njs_object_array_string;
+
+ } else if (njs_is_object(this)
+ && njs_lvlhsh_eq(&njs_object(this)->shared_hash,
&vm->shared->arguments_object_instance_hash))
{
name = &njs_object_arguments_string;
+
+ } else if (njs_is_function(this)) {
+ name = &njs_object_function_string;
+
+ } else if (njs_is_error(this)) {
+ name = &njs_object_error_string;
+
+ } else if (njs_is_object_value(this)) {
+
+ switch (njs_object_value(this)->type) {
+ case NJS_BOOLEAN:
+ name = &njs_object_boolean_string;
+ break;
+
More information about the nginx-devel
mailing list