Skip to content
Myr

jq_compile_args(): allow object args to be object

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nicolas Williams · committed by Nicolas Williams · parent 7b1bd99 · Browse files · 1 file+26 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+26 added−6 removed

  • other:+26 added−6 removed

1 file+26 added−6 removed

modified:src/execute.c+26 added-6 removed
1066 unmodified lines
1067}10681069int jq_compile_args(jq_state *jq, const char* str, jv args) {1070 jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);1071 assert(jv_get_kind(args) == JV_KIND_ARRAY);1071 assert(jv_get_kind(args) == JV_KIND_ARRAY || jv_get_kind(args) == JV_KIND_OBJECT);1072 struct locfile* locations;1073 locations = locfile_init(jq, "<top-level>", str, strlen(str));1074 block program;1075 jq_reset(jq);
2 unmodified lines
1078 jq->bc = 0;1079 }1080 int nerrors = load_program(jq, locations, &program);1081 if (nerrors == 0) {1082 /*1083 * jq_compile_args() is a public function. It should always have1084 * been the case that args was an object, but originally it was an1085 * array.1086 *1087 * We have to do an O(N) binding operation for each argument given.1088 *1089 * The best way to address this might be to have a bind function1090 * that takes an object instead of a const block and a varname.1091 *1092 * However, with `--args` this can be avoided, as one can have a1093 * single such binding then: $ARGS.1094 */1095 if (jv_get_kind(args) == JV_KIND_ARRAY) {1082 jv_array_foreach(args, i, arg) {1083 jv name = jv_object_get(jv_copy(arg), jv_string("name"));1084 jv value = jv_object_get(arg, jv_string("value"));1085 program = gen_var_binding(gen_const(value), jv_string_value(name), program);1086 jv_free(name);1096 jv_array_foreach(args, i, arg) {1097 jv name = jv_object_get(jv_copy(arg), jv_string("name"));1098 jv value = jv_object_get(arg, jv_string("value"));1099 program = gen_var_binding(gen_const(value), jv_string_value(name), program);1100 jv_free(name);1101 }1102 } else if (jv_get_kind(args) == JV_KIND_OBJECT) {1103 jv_object_foreach(args, name, value) {1104 program = gen_var_binding(gen_const(value), jv_string_value(name), program);1105 jv_free(name);1106 }1107 }11081109 nerrors = builtins_bind(jq, &program);1110 if (nerrors == 0) {
65 unmodified lines