summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-03-04 15:51:07 +0100
committerJo-Philipp Wich <jo@mein.io>2021-03-08 17:11:49 +0100
commitc6081c912b2f0f656c90237c9439bf1366f1d18b (patch)
tree3328b089b9651883476e65c79713daee7a0a0789
parente88ed690d38beaf2b9919e2fc03223d33f24459a (diff)
value: support traversing the prototype chain of array values
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--value.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/value.c b/value.c
index cea8220..6c62c57 100644
--- a/value.c
+++ b/value.c
@@ -202,38 +202,37 @@ json_object *
uc_getval(json_object *scope, json_object *key)
{
json_object *o, *v;
+ const char *k;
int64_t idx;
double d;
- if (!key)
- return NULL;
-
if (json_object_is_type(scope, json_type_array)) {
/* only consider doubles with integer values as array keys */
if (json_object_is_type(key, json_type_double)) {
d = json_object_get_double(key);
- if ((double)(int64_t)(d) != d)
- return NULL;
-
- idx = (int64_t)d;
+ if ((double)(int64_t)(d) == d)
+ idx = (int64_t)d;
+ else
+ idx = -1;
}
else {
errno = 0;
idx = json_object_get_int64(key);
if (errno != 0)
- return NULL;
+ idx = -1;
}
- return json_object_get(json_object_array_get_idx(scope, idx));
+ if (idx >= 0 && idx < json_object_array_length(scope))
+ return json_object_get(json_object_array_get_idx(scope, idx));
}
- for (o = scope; o; o = uc_getproto(o)) {
+ for (o = scope, k = key ? json_object_get_string(key) : "null"; o; o = uc_getproto(o)) {
if (!json_object_is_type(o, json_type_object))
continue;
- if (json_object_object_get_ex(o, key ? json_object_get_string(key) : "null", &v))
+ if (json_object_object_get_ex(o, k, &v))
return json_object_get(v);
}