From b161e0934ea9397cacde4626d0afd622d2443515 Mon Sep 17 00:00:00 2001 From: Emanuele Torre Date: Fri, 4 Aug 2023 02:50:05 +0200 Subject: [PATCH] Add ERRORK opcode to trigger constant errors --- src/compile.c | 7 +++++++ src/compile.h | 1 + src/execute.c | 6 ++++++ src/opcode_list.h | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/compile.c b/src/compile.c index 492340b172..4fa1ee6a48 100644 --- a/src/compile.c +++ b/src/compile.c @@ -144,6 +144,13 @@ block gen_op_simple(opcode op) { } +block gen_error(jv constant) { + assert(opcode_describe(ERRORK)->flags & OP_HAS_CONSTANT); + inst *i = inst_new(ERRORK); + i->imm.constant = constant; + return inst_block(i); +} + block gen_const(jv constant) { assert(opcode_describe(LOADK)->flags & OP_HAS_CONSTANT); inst* i = inst_new(LOADK); diff --git a/src/compile.h b/src/compile.h index 03cb54f568..c1512e6b87 100644 --- a/src/compile.h +++ b/src/compile.h @@ -19,6 +19,7 @@ block gen_location(location, struct locfile*, block); block gen_noop(); int block_is_noop(block b); block gen_op_simple(opcode op); +block gen_error(jv constant); block gen_const(jv constant); block gen_const_global(jv constant, const char *name); int block_is_const(block b); diff --git a/src/execute.c b/src/execute.c index 86def927a9..f1dcd105f5 100644 --- a/src/execute.c +++ b/src/execute.c @@ -405,6 +405,12 @@ jv jq_next(jq_state *jq) { case TOP: break; + case ERRORK: { + jv v = jv_array_get(jv_copy(frame_current(jq)->bc->constants), *pc++); + set_error(jq, jv_invalid_with_msg(v)); + goto do_backtrack; + } + case LOADK: { jv v = jv_array_get(jv_copy(frame_current(jq)->bc->constants), *pc++); assert(jv_is_valid(v)); diff --git a/src/opcode_list.h b/src/opcode_list.h index 06147f0ff7..85a8a5805f 100644 --- a/src/opcode_list.h +++ b/src/opcode_list.h @@ -47,3 +47,5 @@ OP(GENLABEL, NONE, 0, 1) OP(DESTRUCTURE_ALT, BRANCH, 0, 0) OP(STOREVN, VARIABLE, 1, 0) + +OP(ERRORK, CONSTANT, 1, 0)