[njs] Refactored njs_object_iterate() API.
Dmitry Volyntsev
xeioex at nginx.com
Thu Apr 27 04:27:29 UTC 2023
details: https://hg.nginx.org/njs/rev/a868f772ef16
branches:
changeset: 2094:a868f772ef16
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Wed Apr 26 21:19:48 2023 -0700
description:
Refactored njs_object_iterate() API.
As a side-effect it fixes dangling-pointer compilation error
found by GCC 13.1.
diffstat:
external/njs_webcrypto_module.c | 2 +-
src/njs_array.c | 68 ++++++++++++++++++----------------------
src/njs_iterator.c | 22 +++++-------
src/njs_iterator.h | 12 +++---
src/njs_promise.c | 4 +-
5 files changed, 49 insertions(+), 59 deletions(-)
diffs (325 lines):
diff -r ebb1eb0d4e43 -r a868f772ef16 external/njs_webcrypto_module.c
--- a/external/njs_webcrypto_module.c Wed Apr 26 19:38:27 2023 -0700
+++ b/external/njs_webcrypto_module.c Wed Apr 26 21:19:48 2023 -0700
@@ -4192,7 +4192,7 @@ njs_key_usage(njs_vm_t *vm, njs_value_t
*mask = 0;
- args.value = value;
+ njs_value_assign(&args.value, value);
args.from = 0;
args.to = length;
args.data = mask;
diff -r ebb1eb0d4e43 -r a868f772ef16 src/njs_array.c
--- a/src/njs_array.c Wed Apr 26 19:38:27 2023 -0700
+++ b/src/njs_array.c Wed Apr 26 21:19:48 2023 -0700
@@ -1862,10 +1862,10 @@ njs_array_iterator_call(njs_vm_t *vm, nj
arguments[0] = *entry;
njs_set_number(&arguments[1], n);
- arguments[2] = *args->value;
-
- return njs_function_call(vm, args->function, args->argument, arguments, 3,
- retval);
+ njs_value_assign(&arguments[2], &args->value);
+
+ return njs_function_call(vm, args->function, njs_value_arg(&args->argument),
+ arguments, 3, retval);
}
@@ -1921,7 +1921,7 @@ njs_array_handler_includes(njs_vm_t *vm,
entry = njs_value_arg(&njs_value_undefined);
}
- if (njs_values_same_zero(args->argument, entry)) {
+ if (njs_values_same_zero(njs_value_arg(&args->argument), entry)) {
njs_set_true(retval);
return NJS_DONE;
@@ -1935,7 +1935,7 @@ static njs_int_t
njs_array_handler_index_of(njs_vm_t *vm, njs_iterator_args_t *args,
njs_value_t *entry, int64_t n, njs_value_t *retval)
{
- if (njs_values_strict_equal(args->argument, entry)) {
+ if (njs_values_strict_equal(njs_value_arg(&args->argument), entry)) {
njs_set_number(retval, n);
return NJS_DONE;
@@ -2023,21 +2023,21 @@ njs_array_handler_reduce(njs_vm_t *vm, n
njs_value_t arguments[5];
if (njs_is_valid(entry)) {
- if (!njs_is_valid(args->argument)) {
- *(args->argument) = *entry;
+ if (!njs_value_is_valid(njs_value_arg(&args->argument))) {
+ njs_value_assign(&args->argument, entry);
return NJS_OK;
}
/* GC: array elt, array */
njs_set_undefined(&arguments[0]);
- arguments[1] = *args->argument;
+ njs_value_assign(&arguments[1], &args->argument);
arguments[2] = *entry;
njs_set_number(&arguments[3], n);
- arguments[4] = *args->value;
+ njs_value_assign(&arguments[4], &args->value);
ret = njs_function_apply(vm, args->function, arguments, 5,
- args->argument);
+ njs_value_arg(&args->argument));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
@@ -2120,18 +2120,17 @@ njs_array_prototype_iterator(njs_vm_t *v
int64_t i, length;
njs_int_t ret;
njs_array_t *array;
- njs_value_t accumulator;
njs_iterator_args_t iargs;
njs_iterator_handler_t handler;
- iargs.value = njs_argument(args, 0);
-
- ret = njs_value_to_object(vm, iargs.value);
+ njs_value_assign(&iargs.value, njs_argument(args, 0));
+
+ ret = njs_value_to_object(vm, njs_value_arg(&iargs.value));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
- ret = njs_value_length(vm, iargs.value, &iargs.to);
+ ret = njs_value_length(vm, njs_value_arg(&iargs.value), &iargs.to);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
@@ -2145,10 +2144,10 @@ njs_array_prototype_iterator(njs_vm_t *v
}
iargs.function = njs_function(njs_argument(args, 1));
- iargs.argument = njs_arg(args, nargs, 2);
+ njs_value_assign(&iargs.argument, njs_arg(args, nargs, 2));
} else {
- iargs.argument = njs_arg(args, nargs, 1);
+ njs_value_assign(&iargs.argument, njs_arg(args, nargs, 1));
}
switch (njs_array_type(magic)) {
@@ -2206,13 +2205,10 @@ njs_array_prototype_iterator(njs_vm_t *v
case NJS_ARRAY_REDUCE:
handler = njs_array_handler_reduce;
- njs_set_invalid(&accumulator);
-
- if (nargs > 2) {
- accumulator = *iargs.argument;
+ if (nargs <= 2) {
+ njs_value_invalid_set(njs_value_arg(&iargs.argument));
}
- iargs.argument = &accumulator;
break;
case NJS_ARRAY_FILTER:
@@ -2277,12 +2273,12 @@ done:
break;
case NJS_ARRAY_REDUCE:
- if (!njs_is_valid(&accumulator)) {
+ if (!njs_value_is_valid(njs_value_arg(&iargs.argument))) {
njs_type_error(vm, "Reduce of empty object with no initial value");
return NJS_ERROR;
}
- njs_value_assign(retval, &accumulator);
+ njs_value_assign(retval, njs_value_arg(&iargs.argument));
break;
case NJS_ARRAY_FILTER:
@@ -2301,20 +2297,19 @@ njs_array_prototype_reverse_iterator(njs
{
int64_t from, length;
njs_int_t ret;
- njs_value_t accumulator;
njs_iterator_args_t iargs;
njs_iterator_handler_t handler;
- iargs.value = njs_argument(args, 0);
-
- ret = njs_value_to_object(vm, iargs.value);
+ njs_value_assign(&iargs.value, njs_argument(args, 0));
+
+ ret = njs_value_to_object(vm, njs_value_arg(&iargs.value));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
- iargs.argument = njs_arg(args, nargs, 1);
-
- ret = njs_value_length(vm, iargs.value, &length);
+ njs_value_assign(&iargs.argument, njs_arg(args, nargs, 1));
+
+ ret = njs_value_length(vm, njs_value_arg(&iargs.value), &length);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
@@ -2355,13 +2350,12 @@ njs_array_prototype_reverse_iterator(njs
return NJS_ERROR;
}
- njs_set_invalid(&accumulator);
iargs.function = njs_function(njs_argument(args, 1));
- iargs.argument = &accumulator;
+ njs_value_invalid_set(njs_value_arg(&iargs.argument));
if (nargs > 2) {
- accumulator = *njs_argument(args, 2);
+ njs_value_assign(&iargs.argument, njs_argument(args, 2));
} else if (length == 0) {
goto done;
@@ -2392,12 +2386,12 @@ done:
case NJS_ARRAY_REDUCE_RIGHT:
default:
- if (!njs_is_valid(&accumulator)) {
+ if (!njs_value_is_valid(njs_value_arg(&iargs.argument))) {
njs_type_error(vm, "Reduce of empty object with no initial value");
return NJS_ERROR;
}
- njs_value_assign(retval, &accumulator);
+ njs_value_assign(retval, njs_value_arg(&iargs.argument));
break;
}
diff -r ebb1eb0d4e43 -r a868f772ef16 src/njs_iterator.c
--- a/src/njs_iterator.c Wed Apr 26 19:38:27 2023 -0700
+++ b/src/njs_iterator.c Wed Apr 26 21:19:48 2023 -0700
@@ -298,12 +298,12 @@ njs_object_iterate(njs_vm_t *vm, njs_ite
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_value_t *value, *entry, prop, character;
const u_char *p, *end, *pos;
njs_string_prop_t string_prop;
njs_object_value_t *object;
- value = args->value;
+ value = njs_value_arg(&args->value);
from = args->from;
to = args->to;
@@ -354,9 +354,7 @@ njs_object_iterate(njs_vm_t *vm, njs_ite
return NJS_ERROR;
}
- njs_set_object_value(&string_obj, object);
-
- args->value = &string_obj;
+ njs_set_object_value(njs_value_arg(&args->value), object);
}
else {
value = njs_object_value(value);
@@ -460,12 +458,12 @@ njs_object_iterate_reverse(njs_vm_t *vm,
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_value_t *entry, *value, prop, character;
const u_char *p, *end, *pos;
njs_string_prop_t string_prop;
njs_object_value_t *object;
- value = args->value;
+ value = njs_value_arg(&args->value);
from = args->from;
to = args->to;
@@ -518,9 +516,7 @@ njs_object_iterate_reverse(njs_vm_t *vm,
return NJS_ERROR;
}
- njs_set_object_value(&string_obj, object);
-
- args->value = &string_obj;
+ njs_set_object_value(njs_value_arg(&args->value), object);
}
else {
value = njs_object_value(value);
@@ -640,13 +636,13 @@ njs_iterator_object_handler(njs_vm_t *vm
njs_value_t prop, *entry;
if (key != NULL) {
- ret = njs_value_property(vm, args->value, key, &prop);
+ ret = njs_value_property(vm, njs_value_arg(&args->value), key, &prop);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
}
} else {
- ret = njs_value_property_i64(vm, args->value, i, &prop);
+ ret = njs_value_property_i64(vm, njs_value_arg(&args->value), i, &prop);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
}
@@ -687,7 +683,7 @@ njs_iterator_to_array(njs_vm_t *vm, njs_
return NULL;
}
- args.value = iterator;
+ njs_value_assign(&args.value, iterator);
args.to = length;
ret = njs_object_iterate(vm, &args, njs_iterator_to_array_handler, retval);
diff -r ebb1eb0d4e43 -r a868f772ef16 src/njs_iterator.h
--- a/src/njs_iterator.h Wed Apr 26 19:38:27 2023 -0700
+++ b/src/njs_iterator.h Wed Apr 26 21:19:48 2023 -0700
@@ -9,14 +9,14 @@
typedef struct {
- njs_function_t *function;
- njs_value_t *argument;
- njs_value_t *value;
+ njs_function_t *function;
+ njs_opaque_value_t argument;
+ njs_opaque_value_t value;
- void *data;
+ void *data;
- int64_t from;
- int64_t to;
+ int64_t from;
+ int64_t to;
} njs_iterator_args_t;
diff -r ebb1eb0d4e43 -r a868f772ef16 src/njs_promise.c
--- a/src/njs_promise.c Wed Apr 26 19:38:27 2023 -0700
+++ b/src/njs_promise.c Wed Apr 26 21:19:48 2023 -0700
@@ -1337,7 +1337,7 @@ njs_promise_perform_all(njs_vm_t *vm, nj
(*pargs->remaining) = 1;
- pargs->args.value = iterator;
+ njs_value_assign(&pargs->args.value, iterator);
pargs->args.to = length;
ret = njs_object_iterate(vm, &pargs->args, handler, retval);
@@ -1785,7 +1785,7 @@ njs_promise_race(njs_vm_t *vm, njs_value
pargs.function = njs_function(&resolve);
pargs.constructor = promise_ctor;
- pargs.args.value = iterator;
+ njs_value_assign(&pargs.args.value, iterator);
pargs.args.to = length;
ret = njs_object_iterate(vm, &pargs.args, njs_promise_perform_race_handler,
More information about the nginx-devel
mailing list