Skip to content
Myr

jq_next: simplify CALL_BUILTIN implementation

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

authored by Emanuele Torre · committed by Emanuele Torre · parent 0b82b38 · Browse files · 1 file+11 added−19 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+11 added−19 removed

  • other:+11 added−19 removed

1 file+11 added−19 removed

modified:src/execute.c+11 added-19 removed
339 unmodified lines
340341#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)342343jv jq_next(jq_state *jq) {344 jv cfunc_input[MAX_CFUNCTION_ARGS];345344 jv_nomem_handler(jq->nomem_handler, jq->nomem_handler_data);345346 uint16_t* pc = stack_restore(jq);347 assert(pc);
558 unmodified lines
906 }907908 case CALL_BUILTIN: {909 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 }910 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);914915 jv top;916 switch (function->nargs) {917 case 1: top = function->fptr.a1(jq, in[0]); break;918 case 2: top = function->fptr.a2(jq, in[0], in[1]); break;919 case 3: top = function->fptr.a3(jq, in[0], in[1], in[2]); break;920 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 assert925 // 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");922 }923929 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);924 if (!jv_is_valid(top)) {925 if (jv_invalid_has_msg(jv_copy(top)))926 set_error(jq, top);927 goto do_backtrack;934 } else {935 // C-coded function returns invalid w/o msg? -> backtrack, as if936 // it had returned `empty`937 goto do_backtrack;928 }929930 stack_push(jq, top);931 break;932 }933934 case TAIL_CALL_JQ:
406 unmodified lines