summaryrefslogtreecommitdiffstats
path: root/src/execute.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/execute.c')
-rw-r--r--src/execute.c49
1 files changed, 19 insertions, 30 deletions
diff --git a/src/execute.c b/src/execute.c
index e364da46..bc39c0c9 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -1066,6 +1066,23 @@ static struct bytecode *optimize(struct bytecode *bc) {
return optimize_code(bc);
}
+static jv
+args2obj(jv args)
+{
+ if (jv_get_kind(args) == JV_KIND_OBJECT)
+ return args;
+ assert(jv_get_kind(args) == JV_KIND_ARRAY);
+ jv r = jv_object();
+ jv kk = jv_string("name");
+ jv vk = jv_string("value");
+ jv_array_foreach(args, i, v)
+ r = jv_object_set(r, jv_object_get(jv_copy(v), kk), jv_object_get(v, vk));
+ jv_free(args);
+ jv_free(kk);
+ jv_free(vk);
+ return r;
+}
+
int jq_compile_args(jq_state *jq, const char* str, jv args) {
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
assert(jv_get_kind(args) == JV_KIND_ARRAY || jv_get_kind(args) == JV_KIND_OBJECT);
@@ -1079,49 +1096,21 @@ int jq_compile_args(jq_state *jq, const char* str, jv args) {
}
int nerrors = load_program(jq, locations, &program);
if (nerrors == 0) {
- /*
- * jq_compile_args() is a public function. It should always have
- * been the case that args was an object, but originally it was an
- * array.
- *
- * We have to do an O(N) binding operation for each argument given.
- *
- * The best way to address this might be to have a bind function
- * that takes an object instead of a const block and a varname.
- *
- * However, with `--args` this can be avoided, as one can have a
- * single such binding then: $ARGS.
- */
- if (jv_get_kind(args) == JV_KIND_ARRAY) {
- jv_array_foreach(args, i, arg) {
- jv name = jv_object_get(jv_copy(arg), jv_string("name"));
- jv value = jv_object_get(arg, jv_string("value"));
- program = gen_var_binding(gen_const(value), jv_string_value(name), program);
- jv_free(name);
- }
- } else if (jv_get_kind(args) == JV_KIND_OBJECT) {
- jv_object_foreach(args, name, value) {
- program = gen_var_binding(gen_const(value), jv_string_value(name), program);
- jv_free(name);
- }
- }
-
nerrors = builtins_bind(jq, &program);
if (nerrors == 0) {
- nerrors = block_compile(program, &jq->bc, locations);
+ nerrors = block_compile(program, &jq->bc, locations, args = args2obj(args));
}
}
if (nerrors)
jq_report_error(jq, jv_string_fmt("jq: %d compile %s", nerrors, nerrors > 1 ? "errors" : "error"));
if (jq->bc)
jq->bc = optimize(jq->bc);
- jv_free(args);
locfile_free(locations);
return jq->bc != NULL;
}
int jq_compile(jq_state *jq, const char* str) {
- return jq_compile_args(jq, str, jv_array());
+ return jq_compile_args(jq, str, jv_object());
}
jv jq_get_jq_origin(jq_state *jq) {