Skip to content

Commit 8bcdc93

Browse files
committed
jq_next: simplify CALL_BUILTIN implementation
1 parent 0b82b38 commit 8bcdc93

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/execute.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ static void set_error(jq_state *jq, jv value) {
341341
#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)
342342

343343
jv jq_next(jq_state *jq) {
344-
jv cfunc_input[MAX_CFUNCTION_ARGS];
345-
346344
jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);
347345

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

910908
case CALL_BUILTIN: {
911909
int nargs = *pc++;
912-
jv top = stack_pop(jq);
913-
jv* in = cfunc_input;
914-
in[0] = top;
915-
for (int i = 1; i < nargs; i++) {
916-
in[i] = stack_pop(jq);
917-
}
918910
struct cfunction* function = &frame_current(jq)->bc->globals->cfunctions[*pc++];
911+
jv in[MAX_CFUNCTION_ARGS];
912+
for (int i = 0; i < nargs; ++i)
913+
in[i] = stack_pop(jq);
914+
915+
jv top;
919916
switch (function->nargs) {
920917
case 1: top = function->fptr.a1(jq, in[0]); break;
921918
case 2: top = function->fptr.a2(jq, in[0], in[1]); break;
922919
case 3: top = function->fptr.a3(jq, in[0], in[1], in[2]); break;
923920
case 4: top = function->fptr.a4(jq, in[0], in[1], in[2], in[3]); break;
924-
// FIXME: a) up to 7 arguments (input + 6), b) should assert
925-
// because the compiler should not generate this error.
926-
default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"))
921+
default: assert(0 && "Invalid number of arguments");
927922
}
928923

929-
if (jv_is_valid(top)) {
930-
stack_push(jq, top);
931-
} else if (jv_invalid_has_msg(jv_copy(top))) {
932-
set_error(jq, top);
933-
goto do_backtrack;
934-
} else {
935-
// C-coded function returns invalid w/o msg? -> backtrack, as if
936-
// it had returned `empty`
924+
if (!jv_is_valid(top)) {
925+
if (jv_invalid_has_msg(jv_copy(top)))
926+
set_error(jq, top);
937927
goto do_backtrack;
938928
}
929+
930+
stack_push(jq, top);
939931
break;
940932
}
941933

0 commit comments

Comments
 (0)