Skip to content

Commit

Permalink
jq_next: simplify CALL_BUILTIN implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele6 committed Dec 1, 2024
1 parent 0b82b38 commit 8bcdc93
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ static void set_error(jq_state *jq, jv value) {
#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)

jv jq_next(jq_state *jq) {
jv cfunc_input[MAX_CFUNCTION_ARGS];

jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);

uint16_t* pc = stack_restore(jq);
Expand Down Expand Up @@ -909,33 +907,27 @@ jv jq_next(jq_state *jq) {

case CALL_BUILTIN: {
int nargs = *pc++;
jv top = stack_pop(jq);
jv* in = cfunc_input;
in[0] = top;
for (int i = 1; i < nargs; i++) {
in[i] = stack_pop(jq);
}
struct cfunction* function = &frame_current(jq)->bc->globals->cfunctions[*pc++];
jv in[MAX_CFUNCTION_ARGS];
for (int i = 0; i < nargs; ++i)
in[i] = stack_pop(jq);

jv top;
switch (function->nargs) {
case 1: top = function->fptr.a1(jq, in[0]); break;
case 2: top = function->fptr.a2(jq, in[0], in[1]); break;
case 3: top = function->fptr.a3(jq, in[0], in[1], in[2]); break;
case 4: top = function->fptr.a4(jq, in[0], in[1], in[2], in[3]); break;
// FIXME: a) up to 7 arguments (input + 6), b) should assert
// because the compiler should not generate this error.
default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"))
default: assert(0 && "Invalid number of arguments");
}

if (jv_is_valid(top)) {
stack_push(jq, top);
} else if (jv_invalid_has_msg(jv_copy(top))) {
set_error(jq, top);
goto do_backtrack;
} else {
// C-coded function returns invalid w/o msg? -> backtrack, as if
// it had returned `empty`
if (!jv_is_valid(top)) {
if (jv_invalid_has_msg(jv_copy(top)))
set_error(jq, top);
goto do_backtrack;
}

stack_push(jq, top);
break;
}

Expand Down

0 comments on commit 8bcdc93

Please sign in to comment.