Skip to content

Commit a403c89

Browse files
committed
chore: handle some expected errors to happen during function calls
1 parent 03ec04f commit a403c89

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/debug.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ int disassemble_instruction(Chunk* chunk, int offset)
7878
return jump_instruction("OP_JUMP_IF_FALSE", 1, chunk, offset);
7979
case OP_LOOP:
8080
return jump_instruction("OP_JUMP_IF_FALSE", -1, chunk, offset);
81+
case OP_CALL:
82+
return byte_instruction("OP_CALL", chunk, offset);
8183
case OP_RETURN:
8284
return simple_instruction("OP_RETURN", offset);
8385
default:

src/vm.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ static Value peek(int distance)
7171

7272
static bool call(ObjFunction* function, int arg_count)
7373
{
74+
if (function->arity != arg_count)
75+
{
76+
runtime_error("Expected %d arguments but got %d.", function->arity,
77+
arg_count);
78+
}
79+
if (vm.frame_count == FRAMES_MAX)
80+
{
81+
// as Java developer i hate this exception,
82+
// but sorry there no way to increase stack size here like jvm does :Joy
83+
runtime_error("Stack Overflow");
84+
return false;
85+
}
7486
CallFrame* frame = &vm.frames[vm.frame_count++];
7587
frame->function = function;
7688
frame->ip = function->chunk.code;
@@ -321,6 +333,7 @@ InterpretResult interpret(char* source)
321333
return INTERPRET_COMPILE_ERROR;
322334

323335
push(OBJ_VAL(function));
336+
call_value(OBJ_VAL(function), 0);
324337
CallFrame* frame = &vm.frames[vm.frame_count++];
325338
frame->function = function;
326339
frame->ip = function->chunk.code;

0 commit comments

Comments
 (0)