Ensure the correct value is being compared for OP_BREAK
@@ -1681,6 +1681,99 @@ static void expressionStatement(Compiler *compiler) {
}
}
+static int getArgCount(uint8_t code, const ValueArray constants, int ip) {
+ switch (code) {
+ case OP_NIL:
+ case OP_TRUE:
+ case OP_FALSE:
+ case OP_NEW_LIST:
+ case OP_ADD_LIST:
+ case OP_NEW_DICT:
+ case OP_ADD_DICT:
+ case OP_SUBSCRIPT:
+ case OP_SUBSCRIPT_ASSIGN:
+ case OP_SLICE:
+ case OP_PUSH:
+ case OP_POP:
+ case OP_EQUAL:
+ case OP_GREATER:
+ case OP_LESS:
+ case OP_ADD:
+ case OP_INCREMENT:
+ case OP_DECREMENT:
+ case OP_MULTIPLY:
+ case OP_DIVIDE:
+ case OP_POW:
+ case OP_MOD:
+ case OP_NOT:
+ case OP_NEGATE:
+ case OP_CLOSE_UPVALUE:
+ case OP_RETURN:
+ case OP_EMPTY:
+ case OP_END_CLASS:
+ case OP_IMPORT_VARIABLE:
+ case OP_IMPORT_END:
+ case OP_USE:
+ case OP_OPEN_FILE:
+ case OP_CLOSE_FILE:
+ case OP_BREAK:
+ case OP_BITWISE_AND:
+ case OP_BITWISE_XOR:
+ case OP_BITWISE_OR:
+ case OP_POP_REPL:
+ return 0;
+
+ case OP_CONSTANT:
+ case OP_UNPACK_LIST:
+ case OP_GET_LOCAL:
+ case OP_SET_LOCAL:
+ case OP_GET_GLOBAL:
+ case OP_GET_MODULE:
+ case OP_DEFINE_MODULE:
+ case OP_SET_MODULE:
+ case OP_GET_UPVALUE:
+ case OP_SET_UPVALUE:
+ case OP_GET_PROPERTY:
+ case OP_GET_PROPERTY_NO_POP:
+ case OP_SET_PROPERTY:
+ case OP_SET_INIT_PROPERTIES:
+ case OP_GET_SUPER:
+ case OP_CALL:
+ case OP_METHOD:
+ case OP_IMPORT:
+ return 1;
+
+ case OP_DEFINE_OPTIONAL:
+ case OP_JUMP:
+ case OP_JUMP_IF_NIL:
+ case OP_JUMP_IF_FALSE:
+ case OP_LOOP:
+ case OP_INVOKE:
+ case OP_SUPER:
+ case OP_CLASS:
+ case OP_SUBCLASS:
+ case OP_IMPORT_BUILTIN:
+ return 2;
+
+ case OP_IMPORT_BUILTIN_VARIABLE:
+ return 3;
+
+ case OP_CLOSURE: {
+ ObjFunction* loadedFn = AS_FUNCTION(constants.values[ip + 1]);
+
+ // There is one byte for the constant, then two for each upvalue.
+ return 1 + (loadedFn->upvalueCount * 2);
+ }
+
+ case OP_IMPORT_FROM: {
+ int count = constants.values[ip + 1];
+ return 1 + count;
+ }
+ }
+
+ return 0;
+}
+
static void endLoop(Compiler *compiler) {
if (compiler->loop->end != -1) {
patchJump(compiler, compiler->loop->end);
@@ -1694,7 +1787,7 @@ static void endLoop(Compiler *compiler) {
patchJump(compiler, i + 1);
i += 3;
} else {
- i++;
+ i += 1 + getArgCount(compiler->function->chunk.code[i], compiler->function->chunk.constants, i);
}
}
