From ac76b7b8370775b66f1f7f78d68888afa6bc059a Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 2 Sep 2020 22:18:04 +0200 Subject: eval: traverse prototype chain when resolving object properties Signed-off-by: Jo-Philipp Wich --- eval.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/eval.c b/eval.c index e47b395..283c790 100644 --- a/eval.c +++ b/eval.c @@ -344,9 +344,21 @@ ut_getref_required(struct ut_state *state, struct ut_opcode *op, struct json_obj return scope; } +static struct json_object * +ut_getproto(struct json_object *obj) +{ + struct ut_tagvalue *tag = json_object_get_userdata(obj); + + if (!tag || (tag->type != T_LBRACE && tag->type <= __T_MAX) || !tag->val) + return NULL; + + return tag->proto; +} + static struct json_object * ut_getval(struct json_object *scope, struct json_object *key) { + struct json_object *o, *v; int64_t idx; double d; @@ -374,7 +386,15 @@ ut_getval(struct json_object *scope, struct json_object *key) return json_object_get(json_object_array_get_idx(scope, idx)); } - return json_object_get(json_object_object_get(scope, key ? json_object_get_string(key) : "null")); + for (o = scope; o; o = ut_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)) + return json_object_get(v); + } + + return NULL; } static struct json_object * -- cgit v1.2.3