Skip to content
Myr

Fix destructuring alternation

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

Attempting to use the existing FORK_OPT opcode resulted in difficulty knowing when to pop an error message off the stack and when not to. This commit makes DESTRUCTURE_ALT a real opcode that is identical to FORK_OPT, except for never pushing the error message onto the stack when continuing from an error backtrack. Some small changes were necessary to the DUP/POP behavior surrounding destructuring to accomodate this.

authored by William Langford · committed by William Langford · parent 3dc5f4e · Browse files · 4 files+122 added−21 removed
This commit

State

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

Diff stats

4 files+122 added−21 removed

  • tests:+101 added−0 removed
  • other:+21 added−21 removed

4 files+122 added−21 removed

modified:src/compile.c+11 added-17 removed
757 unmodified lines
758}759760/* Build wrappers around destructuring matchers so that we can chain them761 * when we have errors. The approach is as follows:762 * FORK_OPT NEXT_MATCHER (unless last matcher)762 * DESTRUCTURE_ALT NEXT_MATCHER (unless last matcher)763 * existing_matcher_block764 * JUMP BODY765 */766static block bind_alternation_matchers(block matchers, block body) {767 block preamble = {0};768 block altmatchers = {0};769 block mb = {0};770 block final_matcher = matchers;771772 // Pass through the matchers to find all destructured names.772 while (matchers.first && matchers.first->op == DESTRUCTURE_ALT) {773 block_append(&altmatchers, inst_block(block_take(&matchers)));773 while (final_matcher.first && final_matcher.first->op == DESTRUCTURE_ALT) {774 block_append(&altmatchers, inst_block(block_take(&final_matcher)));775 }776777 // We don't have any alternations here, so we can use the simplest case.778 if (altmatchers.first == NULL) {778 return bind_matcher(matchers, body);779 return bind_matcher(final_matcher, body);780 }781781 // The final matcher needs to strip the error from the previous FORK_OPT782 block final_matcher = BLOCK(gen_op_simple(POP), gen_op_simple(DUP), matchers);783782 // Collect var names783 jv all_vars = jv_object();784 block_get_unbound_vars(altmatchers, &all_vars);785 block_get_unbound_vars(final_matcher, &all_vars);
11 unmodified lines
797 // Now we build each matcher in turn798 for (inst *i = altmatchers.first; i; i = i->next) {799 block submatcher = i->subfn;800803 // Get rid of the error from the previous matcher804 if (mb.first != NULL) {805 submatcher = BLOCK(gen_op_simple(POP), gen_op_simple(DUP), submatcher);806 }807801 // If we're successful, jump to the end of the matchers802 submatcher = BLOCK(submatcher, gen_op_target(JUMP, final_matcher));803811 // FORK_OPT to the end of this submatcher so we can skip to the next one on error812 mb = BLOCK(mb, gen_op_target(FORK_OPT, submatcher), submatcher);804 // DESTRUCTURE_ALT to the end of this submatcher so we can skip to the next one on error805 mb = BLOCK(mb, gen_op_target(DESTRUCTURE_ALT, submatcher), submatcher);806807 // We're done with this inst and we don't want it anymore808 // But we can't let it free the submatcher block.809 i->subfn.first = i->subfn.last = NULL;
185 unmodified lines
995 block top = gen_noop();996 if (body.first && body.first->op == TOP)997 top = inst_block(block_take(&body));9981006 if (matchers.first && matchers.first->op == DESTRUCTURE_ALT && !block_is_noop(var)) {999 if (matchers.first && matchers.first->op == DESTRUCTURE_ALT) {1000 block_append(&var, gen_op_simple(DUP));1008 block_append(&matchers, gen_op_simple(POP));1001 } else {1002 top = BLOCK(top, gen_op_simple(DUP));1003 }10041011 return BLOCK(top, gen_op_simple(DUP), gen_subexp(var), gen_op_simple(POP), bind_alternation_matchers(matchers, body));1005 return BLOCK(top, gen_subexp(var), gen_op_simple(POP), bind_alternation_matchers(matchers, body));1006}10071008// Like gen_var_binding(), but bind `break`'s wildcard unbound variable1009static block gen_wildvar_binding(block var, const char* name, block body) {
377 unmodified lines
modified:src/execute.c+9 added-3 removed
797 unmodified lines
798 break;799 }800801 case FORK_OPT:802 case DESTRUCTURE_ALT:803 case FORK: {804 stack_save(jq, pc - 1, stack_get_pos(jq));805 pc++; // skip offset this time806 break;807 }808808 case ON_BACKTRACK(FORK_OPT): {809 case ON_BACKTRACK(FORK_OPT):810 case ON_BACKTRACK(DESTRUCTURE_ALT): {811 if (jv_is_valid(jq->error)) {812 // `try EXP ...` backtracked here (no value, `empty`), so we backtrack more813 jv_free(stack_pop(jq));814 goto do_backtrack;815 }816 // `try EXP ...` exception caught in EXP817 // DESTRUCTURE_ALT doesn't want the error message on the stack,818 // as we would just want to throw it away anyway.819 if (opcode != ON_BACKTRACK(DESTRUCTURE_ALT)) {815 jv_free(stack_pop(jq)); // free the input816 stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message820 jv_free(stack_pop(jq)); // free the input821 stack_push(jq, jv_invalid_get_msg(jq->error)); // push the error's message822 }823 jq->error = jv_null();824 uint16_t offset = *pc++;825 pc += offset;826 break;
430 unmodified lines
modified:src/opcode_list.h+1 added-1 removed
42 unmodified lines
43OP(DEPS, CONSTANT, 0, 0)44OP(MODULEMETA, CONSTANT, 0, 0)45OP(GENLABEL, NONE, 0, 1)4647OP(DESTRUCTURE_ALT, NONE, 0, 0)47OP(DESTRUCTURE_ALT, BRANCH, 0, 0)48OP(STOREVN, VARIABLE, 1, 0)
modified:tests/jq.test+101 added
724 unmodified lines
725[1, null, 2, 3, null, null]726[4, 5, null, null, 7, null]727[null, null, null, null, null, "foo"]728729# Destructuring DUP/POP issues730.[] | . as {a:$a} ?// {a:$a} ?// {a:$a} | $a731[[3],[4],[5],6]732# Runtime error: "jq: Cannot index array with string \"c\""733734.[] as {a:$a} ?// {a:$a} ?// {a:$a} | $a735[[3],[4],[5],6]736# Runtime error: "jq: Cannot index array with string \"c\""737738[[3],[4],[5],6][] | . as {a:$a} ?// {a:$a} ?// {a:$a} | $a739null740# Runtime error: "jq: Cannot index array with string \"c\""741742[[3],[4],[5],6] | .[] as {a:$a} ?// {a:$a} ?// {a:$a} | $a743null744# Runtime error: "jq: Cannot index array with string \"c\""745746.[] | . as {a:$a} ?// {a:$a} ?// $a | $a747[[3],[4],[5],6]748[3]749[4]750[5]7516752753.[] as {a:$a} ?// {a:$a} ?// $a | $a754[[3],[4],[5],6]755[3]756[4]757[5]7586759760[[3],[4],[5],6][] | . as {a:$a} ?// {a:$a} ?// $a | $a761null762[3]763[4]764[5]7656766767[[3],[4],[5],6] | .[] as {a:$a} ?// {a:$a} ?// $a | $a768null769[3]770[4]771[5]7726773774.[] | . as {a:$a} ?// $a ?// {a:$a} | $a775[[3],[4],[5],6]776[3]777[4]778[5]7796780781.[] as {a:$a} ?// $a ?// {a:$a} | $a782[[3],[4],[5],6]783[3]784[4]785[5]7866787788[[3],[4],[5],6][] | . as {a:$a} ?// $a ?// {a:$a} | $a789null790[3]791[4]792[5]7936794795[[3],[4],[5],6] | .[] as {a:$a} ?// $a ?// {a:$a} | $a796null797[3]798[4]799[5]8006801802.[] | . as $a ?// {a:$a} ?// {a:$a} | $a803[[3],[4],[5],6]804[3]805[4]806[5]8076808809.[] as $a ?// {a:$a} ?// {a:$a} | $a810[[3],[4],[5],6]811[3]812[4]813[5]8146815816[[3],[4],[5],6][] | . as $a ?// {a:$a} ?// {a:$a} | $a817null818[3]819[4]820[5]8216822823[[3],[4],[5],6] | .[] as $a ?// {a:$a} ?// {a:$a} | $a824null825[3]826[4]827[5]8286829830. as $dot|any($dot[];not)831[1,2,3,4,true,false,1,2,3,4,5]832true833
686 unmodified lines